-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathregistry.go
62 lines (52 loc) · 1.81 KB
/
registry.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
package main
import (
"fmt"
"time"
)
// SerializerOutput is an interface for output plugins that are able to
// serialize telegraf metrics into arbitrary data formats.
type SerializerOutput interface {
// SetSerializer sets the serializer function for the interface.
SetSerializer(serializer Serializer)
}
// Serializer is an interface defining functions that a serializer plugin must
// satisfy.
type Serializer interface {
// Serialize takes a single telegraf metric and turns it into a byte buffer.
// separate metrics should be separated by a newline, and there should be
// a newline at the end of the buffer.
Serialize(metric Metric) ([]byte, error)
}
// Config is a struct that covers the data types needed for all serializer types,
// and can be used to instantiate _any_ of the serializers.
type SerializerConfig struct {
// Dataformat can be one of: influx, graphite, or json
DataFormat string
// Prefix to add to all measurements, only supports Graphite
Prefix string
// Template for converting telegraf metrics into Graphite
// only supports Graphite
Template string
// Timestamp units to use for JSON formatted output
TimestampUnits time.Duration
}
// NewSerializer a Serializer interface based on the given config.
func NewSerializer(config *SerializerConfig) (Serializer, error) {
var err error
var serializer Serializer
switch config.DataFormat {
case "influx":
serializer, err = NewInfluxSerializer()
case "json":
serializer, err = NewJsonSerializer(config.TimestampUnits)
default:
err = fmt.Errorf("Invalid data format: %s", config.DataFormat)
}
return serializer, err
}
func NewJsonSerializer(timestampUnits time.Duration) (Serializer, error) {
return &JsonSerializer{TimestampUnits: timestampUnits}, nil
}
func NewInfluxSerializer() (Serializer, error) {
return &InfluxSerializer{}, nil
}