forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudevents.go
199 lines (175 loc) · 4.62 KB
/
cloudevents.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package cloudevents
import (
"encoding/json"
"errors"
"fmt"
"time"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/gofrs/uuid/v5"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/serializers"
)
const (
EventTypeSingle = "com.influxdata.telegraf.metric"
EventTypeBatch = "com.influxdata.telegraf.metrics"
)
type Serializer struct {
Version string `toml:"cloudevents_version"`
Source string `toml:"cloudevents_source"`
SourceTag string `toml:"cloudevents_source_tag"`
EventType string `toml:"cloudevents_event_type"`
EventTime string `toml:"cloudevents_event_time"`
BatchFormat string `toml:"cloudevents_batch_format"`
Log telegraf.Logger `toml:"-"`
idgen uuid.Generator
}
func (s *Serializer) Init() error {
switch s.Version {
case "":
s.Version = event.CloudEventsVersionV1
case event.CloudEventsVersionV03, event.CloudEventsVersionV1:
default:
return errors.New("invalid 'cloudevents_version'")
}
switch s.EventTime {
case "":
s.EventTime = "latest"
case "none", "earliest", "latest", "creation":
default:
return errors.New("invalid 'cloudevents_event_time'")
}
switch s.BatchFormat {
case "":
s.BatchFormat = "events"
case "metrics", "events":
default:
return errors.New("invalid 'cloudevents_batch_format'")
}
if s.Source == "" {
s.Source = "telegraf"
}
s.idgen = uuid.NewGen()
return nil
}
func (s *Serializer) Serialize(m telegraf.Metric) ([]byte, error) {
// Create the event that forms the envelop around the metric
evt, err := s.createEvent(m)
if err != nil {
return nil, err
}
return evt.MarshalJSON()
}
func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
switch s.BatchFormat {
case "metrics":
return s.batchMetrics(metrics)
case "events":
return s.batchEvents(metrics)
}
return nil, fmt.Errorf("unexpected batch-format %q", s.BatchFormat)
}
func (s *Serializer) batchMetrics(metrics []telegraf.Metric) ([]byte, error) {
// Determine the necessary information
eventType := EventTypeBatch
if s.EventType != "" {
eventType = s.EventType
}
id, err := s.idgen.NewV1()
if err != nil {
return nil, fmt.Errorf("generating ID failed: %w", err)
}
// Serialize the metrics
var earliest, latest time.Time
data := make([]map[string]interface{}, 0, len(metrics))
for _, m := range metrics {
ts := m.Time()
data = append(data, map[string]interface{}{
"name": m.Name(),
"tags": m.Tags(),
"fields": m.Fields(),
"timestamp": ts.UnixNano(),
})
if ts.Before(earliest) {
earliest = ts
}
if ts.After(latest) {
latest = ts
}
}
// Create the event that forms the envelop around the metric
evt := cloudevents.NewEvent(s.Version)
evt.SetSource(s.Source)
evt.SetID(id.String())
evt.SetType(eventType)
if err := evt.SetData(cloudevents.ApplicationJSON, data); err != nil {
return nil, fmt.Errorf("setting data failed: %w", err)
}
switch s.EventTime {
case "creation":
evt.SetTime(time.Now())
case "earliest":
evt.SetTime(earliest)
case "latest":
evt.SetTime(latest)
}
return json.Marshal(evt)
}
func (s *Serializer) batchEvents(metrics []telegraf.Metric) ([]byte, error) {
events := make([]*cloudevents.Event, 0, len(metrics))
for _, m := range metrics {
e, err := s.createEvent(m)
if err != nil {
s.Log.Errorf("Creating event for %v failed: %v", m, err)
continue
}
events = append(events, e)
}
return json.Marshal(events)
}
func (s *Serializer) createEvent(m telegraf.Metric) (*cloudevents.Event, error) {
// Determine the necessary information
source := s.Source
if s.SourceTag != "" {
if v, ok := m.GetTag(s.SourceTag); ok {
source = v
}
}
eventType := EventTypeSingle
if s.EventType != "" {
eventType = s.EventType
}
id, err := s.idgen.NewV1()
if err != nil {
return nil, fmt.Errorf("generating ID failed: %w", err)
}
// Serialize the metric
data := map[string]interface{}{
"name": m.Name(),
"tags": m.Tags(),
"fields": m.Fields(),
"timestamp": m.Time().UnixNano(),
}
// Create the event that forms the envelop around the metric
evt := cloudevents.NewEvent(s.Version)
evt.SetSource(source)
evt.SetID(id.String())
evt.SetType(eventType)
if err := evt.SetData(cloudevents.ApplicationJSON, data); err != nil {
return nil, fmt.Errorf("setting data failed: %w", err)
}
switch s.EventTime {
case "creation":
evt.SetTime(time.Now())
case "earliest", "latest":
evt.SetTime(m.Time())
}
return &evt, nil
}
func init() {
serializers.Add("cloudevents",
func() serializers.Serializer {
return &Serializer{}
},
)
}