-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
event_hubs.go
154 lines (117 loc) · 3.25 KB
/
event_hubs.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
//go:generate ../../../tools/readme_config_includer/generator
package event_hubs
import (
"context"
_ "embed"
"time"
eventhub "github.com/Azure/azure-event-hubs-go/v3"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/influxdata/telegraf/plugins/serializers"
)
//go:embed sample.conf
var sampleConfig string
/*
** Wrapper interface for eventhub.Hub
*/
type EventHubInterface interface {
GetHub(s string) error
Close(ctx context.Context) error
SendBatch(ctx context.Context, iterator eventhub.BatchIterator, opts ...eventhub.BatchOption) error
}
type eventHub struct {
hub *eventhub.Hub
}
func (eh *eventHub) GetHub(s string) error {
hub, err := eventhub.NewHubFromConnectionString(s)
if err != nil {
return err
}
eh.hub = hub
return nil
}
func (eh *eventHub) Close(ctx context.Context) error {
return eh.hub.Close(ctx)
}
func (eh *eventHub) SendBatch(ctx context.Context, iterator eventhub.BatchIterator, opts ...eventhub.BatchOption) error {
return eh.hub.SendBatch(ctx, iterator, opts...)
}
/* End wrapper interface */
type EventHubs struct {
Log telegraf.Logger `toml:"-"`
ConnectionString string `toml:"connection_string"`
Timeout config.Duration `toml:"timeout"`
PartitionKey string `toml:"partition_key"`
MaxMessageSize int `toml:"max_message_size"`
Hub EventHubInterface
batchOptions []eventhub.BatchOption
serializer serializers.Serializer
}
const (
defaultRequestTimeout = time.Second * 30
)
func (*EventHubs) SampleConfig() string {
return sampleConfig
}
func (e *EventHubs) Init() error {
err := e.Hub.GetHub(e.ConnectionString)
if err != nil {
return err
}
if e.MaxMessageSize > 0 {
e.batchOptions = append(e.batchOptions, eventhub.BatchWithMaxSizeInBytes(e.MaxMessageSize))
}
return nil
}
func (e *EventHubs) Connect() error {
return nil
}
func (e *EventHubs) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(e.Timeout))
defer cancel()
err := e.Hub.Close(ctx)
if err != nil {
return err
}
return nil
}
func (e *EventHubs) SetSerializer(serializer serializers.Serializer) {
e.serializer = serializer
}
func (e *EventHubs) Write(metrics []telegraf.Metric) error {
events := make([]*eventhub.Event, 0, len(metrics))
for _, metric := range metrics {
payload, err := e.serializer.Serialize(metric)
if err != nil {
e.Log.Debugf("Could not serialize metric: %v", err)
continue
}
event := eventhub.NewEvent(payload)
if e.PartitionKey != "" {
if key, ok := metric.GetTag(e.PartitionKey); ok {
event.PartitionKey = &key
} else if key, ok := metric.GetField(e.PartitionKey); ok {
if strKey, ok := key.(string); ok {
event.PartitionKey = &strKey
}
}
}
events = append(events, event)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(e.Timeout))
defer cancel()
err := e.Hub.SendBatch(ctx, eventhub.NewEventBatchIterator(events...), e.batchOptions...)
if err != nil {
return err
}
return nil
}
func init() {
outputs.Add("event_hubs", func() telegraf.Output {
return &EventHubs{
Hub: &eventHub{},
Timeout: config.Duration(defaultRequestTimeout),
}
})
}