forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprometheus.go
70 lines (55 loc) · 1.34 KB
/
prometheus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package prometheus
import (
"bytes"
"time"
"github.com/influxdata/telegraf"
"github.com/prometheus/common/expfmt"
)
// TimestampExport controls if the output contains timestamps.
type TimestampExport int
const (
NoExportTimestamp TimestampExport = iota
ExportTimestamp
)
// MetricSortOrder controls if the output is sorted.
type MetricSortOrder int
const (
NoSortMetrics MetricSortOrder = iota
SortMetrics
)
// StringHandling defines how to process string fields.
type StringHandling int
const (
DiscardStrings StringHandling = iota
StringAsLabel
)
type FormatConfig struct {
TimestampExport TimestampExport
MetricSortOrder MetricSortOrder
StringHandling StringHandling
}
type Serializer struct {
config FormatConfig
}
func NewSerializer(config FormatConfig) (*Serializer, error) {
s := &Serializer{config: config}
return s, nil
}
func (s *Serializer) Serialize(metric telegraf.Metric) ([]byte, error) {
return s.SerializeBatch([]telegraf.Metric{metric})
}
func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
coll := NewCollection(s.config)
for _, metric := range metrics {
coll.Add(metric, time.Now())
}
var buf bytes.Buffer
for _, mf := range coll.GetProto() {
enc := expfmt.NewEncoder(&buf, expfmt.FmtText)
err := enc.Encode(mf)
if err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}