Add method ToLineProtocol which generates influxDB line protocol for data type ccMetric

This commit is contained in:
Holger Obermaier 2022-02-07 21:13:57 +01:00
parent 9586e67f81
commit 627163d4df

View File

@ -5,6 +5,8 @@ import (
"sort" "sort"
"time" "time"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
write "github.com/influxdata/influxdb-client-go/v2/api/write"
lp "github.com/influxdata/line-protocol" // MIT license lp "github.com/influxdata/line-protocol" // MIT license
) )
@ -70,6 +72,21 @@ func (m *ccMetric) String() string {
return fmt.Sprintf("%s %v %v %v %d", m.name, m.tags, m.meta, m.Fields(), m.tm.UnixNano()) return fmt.Sprintf("%s %v %v %v %d", m.name, m.tags, m.meta, m.Fields(), m.tm.UnixNano())
} }
// ToLineProtocol generates influxDB line protocol for data type ccMetric
func (m *ccMetric) ToLineProtocol(metaAsTags bool) string {
tags := make(map[string]string)
for key, value := range m.tags {
tags[key] = value
}
if metaAsTags {
for key, value := range m.meta {
tags[key] = value
}
}
p := influxdb2.NewPoint(m.name, tags, m.Fields(), m.tm)
return write.PointToLineProtocol(p, time.Nanosecond)
}
// Name returns the measurement name // Name returns the measurement name
func (m *ccMetric) Name() string { func (m *ccMetric) Name() string {
return m.name return m.name