Update ccMetric README and FromMetric copy

This commit is contained in:
Thomas Roehl 2022-07-27 18:06:41 +02:00
parent f5ad45e49f
commit 32bb9c5fc0
2 changed files with 50 additions and 21 deletions

View File

@ -6,27 +6,52 @@ It is basically a copy of the [InfluxDB line protocol](https://github.com/influx
```golang ```golang
type ccMetric struct { type ccMetric struct {
name string // same as name string // Measurement name
tags []*influx.Tag // original meta map[string]string // map of meta data tags
fields []*influx.Field // Influx tags map[string]string // map of of tags
tm time.Time // line-protocol fields map[string]interface{} // map of of fields
meta []*influx.Tag tm time.Time // timestamp
} }
type CCMetric interface { type CCMetric interface {
influx.MutableMetric // the same functions as defined by influx.MutableMetric ToPoint(metaAsTags map[string]bool) *write.Point // Generate influxDB point for data type ccMetric
RemoveTag(key string) // this is not published by the original influx.MutableMetric ToLineProtocol(metaAsTags map[string]bool) string // Generate influxDB line protocol for data type ccMetric
Meta() map[string]string String() string // Return line-protocol like string
MetaList() []*inlux.Tag
AddMeta(key, value string) Name() string // Get metric name
HasMeta(key string) bool SetName(name string) // Set metric name
GetMeta(key string) (string, bool)
RemoveMeta(key string) Time() time.Time // Get timestamp
SetTime(t time.Time) // Set timestamp
Tags() map[string]string // Map of tags
AddTag(key, value string) // Add a tag
GetTag(key string) (value string, ok bool) // Get a tag by its key
HasTag(key string) (ok bool) // Check if a tag key is present
RemoveTag(key string) // Remove a tag by its key
Meta() map[string]string // Map of meta data tags
AddMeta(key, value string) // Add a meta data tag
GetMeta(key string) (value string, ok bool) // Get a meta data tab addressed by its key
HasMeta(key string) (ok bool) // Check if a meta data key is present
RemoveMeta(key string) // Remove a meta data tag by its key
Fields() map[string]interface{} // Map of fields
AddField(key string, value interface{}) // Add a field
GetField(key string) (value interface{}, ok bool) // Get a field addressed by its key
HasField(key string) (ok bool) // Check if a field key is present
RemoveField(key string) // Remove a field addressed by its key
} }
func New(name string, tags map[string]string, meta map[string]string, fields map[string]interface{}, tm time.Time) (CCMetric, error)
func FromMetric(other CCMetric) CCMetric
func FromInfluxMetric(other lp.Metric) CCMetric
``` ```
The `CCMetric` interface provides the same functions as the `MutableMetric` like `{Add, Remove, Has}{Tag, Field}` and additionally provides `{Add, Remove, Has}Meta`. The `CCMetric` interface provides the same functions as the `MutableMetric` like `{Add, Get, Remove, Has}{Tag, Field}` and additionally provides `{Add, Get, Remove, Has}Meta`.
The InfluxDB protocol creates a new metric with `influx.New(name, tags, fields, time)` while CCMetric uses `ccMetric.New(name, tags, meta, fields, time)` where `tags` and `meta` are both of type `map[string]string`. The InfluxDB protocol creates a new metric with `influx.New(name, tags, fields, time)` while CCMetric uses `ccMetric.New(name, tags, meta, fields, time)` where `tags` and `meta` are both of type `map[string]string`.
You can copy a CCMetric with `FromMetric(other CCMetric) CCMetric`. If you get an `influx.Metric` from a function, like the line protocol parser, you can use `FromInfluxMetric(other influx.Metric) CCMetric` to get a CCMetric out of it (see `NatsReceiver` for an example). You can copy a CCMetric with `FromMetric(other CCMetric) CCMetric`. If you get an `influx.Metric` from a function, like the line protocol parser, you can use `FromInfluxMetric(other influx.Metric) CCMetric` to get a CCMetric out of it (see `NatsReceiver` for an example).
Although the [cc-specifications](https://github.com/ClusterCockpit/cc-specifications/blob/master/interfaces/lineprotocol/README.md) defines that there is only a `value` field for the metric value, the CCMetric still can have multiple values similar to the InfluxDB line protocol.

View File

@ -50,6 +50,7 @@ type CCMetric interface {
GetField(key string) (value interface{}, ok bool) // Get a field addressed by its key GetField(key string) (value interface{}, ok bool) // Get a field addressed by its key
HasField(key string) (ok bool) // Check if a field key is present HasField(key string) (ok bool) // Check if a field key is present
RemoveField(key string) // Remove a field addressed by its key RemoveField(key string) // Remove a field addressed by its key
String() string // Return line-protocol like string
} }
// String implements the stringer interface for data type ccMetric // String implements the stringer interface for data type ccMetric
@ -217,23 +218,26 @@ func New(
} }
// FromMetric copies the metric <other> // FromMetric copies the metric <other>
func FromMetric(other ccMetric) CCMetric { func FromMetric(other CCMetric) CCMetric {
otags := other.Tags()
ometa := other.Meta()
ofields := other.Fields()
m := &ccMetric{ m := &ccMetric{
name: other.Name(), name: other.Name(),
tags: make(map[string]string, len(other.tags)), tags: make(map[string]string, len(otags)),
meta: make(map[string]string, len(other.meta)), meta: make(map[string]string, len(ometa)),
fields: make(map[string]interface{}, len(other.fields)), fields: make(map[string]interface{}, len(ofields)),
tm: other.Time(), tm: other.Time(),
} }
// deep copy tags, meta data tags and fields // deep copy tags, meta data tags and fields
for key, value := range other.tags { for key, value := range otags {
m.tags[key] = value m.tags[key] = value
} }
for key, value := range other.meta { for key, value := range ometa {
m.meta[key] = value m.meta[key] = value
} }
for key, value := range other.fields { for key, value := range ofields {
m.fields[key] = value m.fields[key] = value
} }
return m return m