Additional comments

This commit is contained in:
Holger Obermaier 2022-02-28 12:16:48 +01:00
parent 2c08e53be4
commit 33fec95eac
5 changed files with 46 additions and 22 deletions

View File

@ -13,29 +13,30 @@ import (
)
type MetricCollector interface {
Name() string
Init(config json.RawMessage) error
Initialized() bool
Read(duration time.Duration, output chan lp.CCMetric)
Close()
Name() string // Name of the metric collector
Init(config json.RawMessage) error // Initialize metric collector
Initialized() bool // Is metric collector initialized?
Read(duration time.Duration, output chan lp.CCMetric) // Read metrics from metric collector
Close() // Close / finish metric collector
}
type metricCollector struct {
name string
init bool
meta map[string]string
name string // name of the metric
init bool // is metric collector initialized?
meta map[string]string // static meta data tags
}
// Name() returns the name of the metric collector
// Name returns the name of the metric collector
func (c *metricCollector) Name() string {
return c.name
}
// Setup is for future use
func (c *metricCollector) setup() error {
return nil
}
// Initialized() indicates whether the metric collector has been initialized.
// Initialized indicates whether the metric collector has been initialized
func (c *metricCollector) Initialized() bool {
return c.init
}
@ -64,6 +65,7 @@ func stringArrayContains(array []string, str string) (int, bool) {
return -1, false
}
// SocketList returns the list of physical sockets as read from /proc/cpuinfo
func SocketList() []int {
buffer, err := ioutil.ReadFile("/proc/cpuinfo")
if err != nil {
@ -89,6 +91,7 @@ func SocketList() []int {
return packs
}
// CpuList returns the list of physical CPUs (in contrast to logical CPUs) as read from /proc/cpuinfo
func CpuList() []int {
buffer, err := ioutil.ReadFile("/proc/cpuinfo")
if err != nil {
@ -117,8 +120,8 @@ func CpuList() []int {
// RemoveFromStringList removes the string r from the array of strings s
// If r is not contained in the array an error is returned
func RemoveFromStringList(s []string, r string) ([]string, error) {
for i, item := range s {
if r == item {
for i := range s {
if r == s[i] {
return append(s[:i], s[i+1:]...), nil
}
}

View File

@ -8,6 +8,7 @@ type defaultReceiverConfig struct {
Type string `json:"type"`
}
// Receiver configuration: Listen address, port
type ReceiverConfig struct {
Addr string `json:"address"`
Port string `json:"port"`
@ -23,15 +24,17 @@ type receiver struct {
type Receiver interface {
Start()
Close()
Name() string
SetSink(sink chan lp.CCMetric)
Close() // Close / finish metric receiver
Name() string // Name of the metric receiver
SetSink(sink chan lp.CCMetric) // Set sink channel
}
// Name returns the name of the metric receiver
func (r *receiver) Name() string {
return r.name
}
// SetSink set the sink channel
func (r *receiver) SetSink(sink chan lp.CCMetric) {
r.sink = sink
}

View File

@ -7,6 +7,7 @@ import (
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
)
// SampleReceiver configuration: receiver type, listen address, port
type SampleReceiverConfig struct {
Type string `json:"type"`
Addr string `json:"address"`
@ -24,6 +25,10 @@ type SampleReceiver struct {
// wg sync.WaitGroup
}
// Implement functions required for Receiver interface
// Start(), Close()
// See: metricReceiver.go
func (r *SampleReceiver) Start() {
cclog.ComponentDebug(r.name, "START")
@ -44,6 +49,7 @@ func (r *SampleReceiver) Start() {
// }()
}
// Close receiver: close network connection, close files, close libraries, ...
func (r *SampleReceiver) Close() {
cclog.ComponentDebug(r.name, "CLOSE")
@ -54,13 +60,21 @@ func (r *SampleReceiver) Close() {
// r.wg.Wait()
}
// New function to create a new instance of the receiver
// Initialize the receiver by giving it a name and reading in the config JSON
func NewSampleReceiver(name string, config json.RawMessage) (Receiver, error) {
r := new(SampleReceiver)
r.name = fmt.Sprintf("HttpReceiver(%s)", name)
// Set name of SampleReceiver
// The name should be chosen in such a way that different instances of SampleReceiver can be distinguished
r.name = fmt.Sprintf("SampleReceiver(%s)", name)
// Set static information
r.meta = map[string]string{"source": r.name}
// Set defaults in r.config
// Allow overwriting these defaults by reading config JSON
// Read the sample receiver specific JSON config
if len(config) > 0 {
err := json.Unmarshal(config, &r.config)

View File

@ -10,17 +10,18 @@ type defaultSinkConfig struct {
}
type sink struct {
meta_as_tags bool
name string
meta_as_tags bool // Use meta data tags as tags
name string // Name of the sink
}
type Sink interface {
Write(point lp.CCMetric) error
Flush() error
Close()
Name() string
Write(point lp.CCMetric) error // Write metric to the sink
Flush() error // Flush buffered metrics
Close() // Close / finish metric sink
Name() string // Name of the metric sink
}
// Name returns the name of the metric sink
func (s *sink) Name() string {
return s.name
}

View File

@ -46,6 +46,9 @@ func (s *SampleSink) Close() {
// 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)
// Set name of sampleSink
// The name should be chosen in such a way that different instances of SampleSink can be distinguished
s.name = fmt.Sprintf("SampleSink(%s)", name) // Always specify a name here
// Set defaults in s.config