2022-02-25 13:47:19 +01:00
|
|
|
package sinks
|
|
|
|
|
|
|
|
import (
|
2023-10-13 09:53:49 +02:00
|
|
|
"bytes"
|
2022-02-25 13:47:19 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2022-10-10 11:53:11 +02:00
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric"
|
2022-02-25 13:47:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type SampleSinkConfig struct {
|
2022-03-15 16:16:26 +01:00
|
|
|
// defines JSON tags for 'type' and 'meta_as_tags' (string list)
|
2022-02-28 09:39:59 +01:00
|
|
|
// See: metricSink.go
|
|
|
|
defaultSinkConfig
|
|
|
|
// Additional config options, for SampleSink
|
2022-02-25 13:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type SampleSink struct {
|
2022-03-15 16:16:26 +01:00
|
|
|
// declares elements 'name' and 'meta_as_tags' (string to bool map!)
|
2022-02-28 09:39:59 +01:00
|
|
|
sink
|
2022-02-25 13:47:19 +01:00
|
|
|
config SampleSinkConfig // entry point to the SampleSinkConfig
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:39:59 +01:00
|
|
|
// Implement functions required for Sink interface
|
|
|
|
// Write(...), Flush(), Close()
|
|
|
|
// See: metricSink.go
|
|
|
|
|
2022-02-25 13:47:19 +01:00
|
|
|
// Code to submit a single CCMetric to the sink
|
|
|
|
func (s *SampleSink) Write(point lp.CCMetric) error {
|
2022-03-15 16:16:26 +01:00
|
|
|
// based on s.meta_as_tags use meta infos as tags
|
2022-02-25 13:47:19 +01:00
|
|
|
log.Print(point)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the sink uses batched sends internally, you can tell to flush its buffers
|
|
|
|
func (s *SampleSink) Flush() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close sink: close network connection, close files, close libraries, ...
|
|
|
|
func (s *SampleSink) Close() {
|
|
|
|
cclog.ComponentDebug(s.name, "CLOSE")
|
|
|
|
}
|
|
|
|
|
|
|
|
// New function to create a new instance of the sink
|
|
|
|
// Initialize the sink by giving it a name and reading in the config JSON
|
|
|
|
func NewSampleSink(name string, config json.RawMessage) (Sink, error) {
|
|
|
|
s := new(SampleSink)
|
2022-02-28 12:16:48 +01:00
|
|
|
|
|
|
|
// Set name of sampleSink
|
|
|
|
// The name should be chosen in such a way that different instances of SampleSink can be distinguished
|
2022-02-25 13:47:19 +01:00
|
|
|
s.name = fmt.Sprintf("SampleSink(%s)", name) // Always specify a name here
|
|
|
|
|
|
|
|
// Set defaults in s.config
|
2022-02-28 09:39:59 +01:00
|
|
|
// Allow overwriting these defaults by reading config JSON
|
2022-02-25 13:47:19 +01:00
|
|
|
|
|
|
|
// Read in the config JSON
|
|
|
|
if len(config) > 0 {
|
2023-10-13 09:53:49 +02:00
|
|
|
d := json.NewDecoder(bytes.NewReader(config))
|
|
|
|
d.DisallowUnknownFields()
|
|
|
|
if err := d.Decode(&s.config); err != nil {
|
|
|
|
cclog.ComponentError(s.name, "Error reading config:", err.Error())
|
2022-02-25 13:47:19 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 16:16:26 +01:00
|
|
|
// Create lookup map to use meta infos as tags in the output metric
|
|
|
|
s.meta_as_tags = make(map[string]bool)
|
|
|
|
for _, k := range s.config.MetaAsTags {
|
|
|
|
s.meta_as_tags[k] = true
|
|
|
|
}
|
|
|
|
|
2022-02-25 13:47:19 +01:00
|
|
|
// Check if all required fields in the config are set
|
2022-02-28 09:39:59 +01:00
|
|
|
// E.g. use 'len(s.config.Option) > 0' for string settings
|
2022-02-25 13:47:19 +01:00
|
|
|
|
|
|
|
// Establish connection to the server, library, ...
|
|
|
|
// Check required files exist and lookup path(s) of executable(s)
|
|
|
|
|
|
|
|
// Return (nil, meaningful error message) in case of errors
|
|
|
|
return s, nil
|
|
|
|
}
|