forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventhubhandler.go
191 lines (154 loc) · 4.91 KB
/
eventhubhandler.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package azureeventhubreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/azureeventhubreceiver"
import (
"context"
eventhub "github.com/Azure/azure-event-hubs-go/v3"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/receiver"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
)
type hubWrapper interface {
GetRuntimeInformation(ctx context.Context) (*eventhub.HubRuntimeInformation, error)
Receive(ctx context.Context, partitionID string, handler eventhub.Handler, opts ...eventhub.ReceiveOption) (listerHandleWrapper, error)
Close(ctx context.Context) error
}
type hubWrapperImpl struct {
hub *eventhub.Hub
}
func (h *hubWrapperImpl) GetRuntimeInformation(ctx context.Context) (*eventhub.HubRuntimeInformation, error) {
return h.hub.GetRuntimeInformation(ctx)
}
func (h *hubWrapperImpl) Receive(ctx context.Context, partitionID string, handler eventhub.Handler, opts ...eventhub.ReceiveOption) (listerHandleWrapper, error) {
l, err := h.hub.Receive(ctx, partitionID, handler, opts...)
return l, err
}
func (h *hubWrapperImpl) Close(ctx context.Context) error {
return h.hub.Close(ctx)
}
type listerHandleWrapper interface {
Done() <-chan struct{}
Err() error
}
type eventhubHandler struct {
hub hubWrapper
dataConsumer dataConsumer
config *Config
settings receiver.CreateSettings
cancel context.CancelFunc
}
func (h *eventhubHandler) run(ctx context.Context, host component.Host) error {
ctx, h.cancel = context.WithCancel(ctx)
storageClient, err := adapter.GetStorageClient(ctx, host, h.config.StorageID, h.settings.ID)
if err != nil {
h.settings.Logger.Debug("Error connecting to Storage", zap.Error(err))
return err
}
if h.hub == nil { // set manually for testing.
hub, newHubErr := eventhub.NewHubFromConnectionString(h.config.Connection, eventhub.HubWithOffsetPersistence(&storageCheckpointPersister{storageClient: storageClient}))
if newHubErr != nil {
h.settings.Logger.Debug("Error connecting to Event Hub", zap.Error(newHubErr))
return newHubErr
}
h.hub = &hubWrapperImpl{
hub: hub,
}
}
if h.config.Partition == "" {
// listen to each partition of the Event Hub
var runtimeInfo *eventhub.HubRuntimeInformation
runtimeInfo, err = h.hub.GetRuntimeInformation(ctx)
if err != nil {
h.settings.Logger.Debug("Error getting Runtime Information", zap.Error(err))
return err
}
for _, partitionID := range runtimeInfo.PartitionIDs {
err = h.setUpOnePartition(ctx, partitionID, false)
if err != nil {
h.settings.Logger.Debug("Error setting up partition", zap.Error(err))
return err
}
}
} else {
err = h.setUpOnePartition(ctx, h.config.Partition, true)
if err != nil {
h.settings.Logger.Debug("Error setting up partition", zap.Error(err))
return err
}
}
if h.hub != nil {
return nil
}
hub, err := eventhub.NewHubFromConnectionString(h.config.Connection)
if err != nil {
return err
}
h.hub = &hubWrapperImpl{
hub: hub,
}
runtimeInfo, err := hub.GetRuntimeInformation(ctx)
if err != nil {
return err
}
for _, partitionID := range runtimeInfo.PartitionIDs {
_, err := hub.Receive(ctx, partitionID, h.newMessageHandler, eventhub.ReceiveWithLatestOffset())
if err != nil {
return err
}
}
return nil
}
func (h *eventhubHandler) setUpOnePartition(ctx context.Context, partitionID string, applyOffset bool) error {
receiverOptions := []eventhub.ReceiveOption{}
if applyOffset && h.config.Offset != "" {
receiverOptions = append(receiverOptions, eventhub.ReceiveWithStartingOffset(h.config.Offset))
} else {
receiverOptions = append(receiverOptions, eventhub.ReceiveWithLatestOffset())
}
if h.config.ConsumerGroup != "" {
receiverOptions = append(receiverOptions, eventhub.ReceiveWithConsumerGroup(h.config.ConsumerGroup))
}
handle, err := h.hub.Receive(ctx, partitionID, h.newMessageHandler, receiverOptions...)
if err != nil {
return err
}
go func() {
<-handle.Done()
err := handle.Err()
if err != nil {
h.settings.Logger.Error("Error reported by event hub", zap.Error(err))
}
}()
return nil
}
func (h *eventhubHandler) newMessageHandler(ctx context.Context, event *eventhub.Event) error {
err := h.dataConsumer.consume(ctx, event)
if err != nil {
h.settings.Logger.Error("error decoding message", zap.Error(err))
return err
}
return nil
}
func (h *eventhubHandler) close(ctx context.Context) error {
if h.hub != nil {
err := h.hub.Close(ctx)
if err != nil {
return err
}
h.hub = nil
}
if h.cancel != nil {
h.cancel()
}
return nil
}
func (h *eventhubHandler) setDataConsumer(dataConsumer dataConsumer) {
h.dataConsumer = dataConsumer
}
func newEventhubHandler(config *Config, settings receiver.CreateSettings) *eventhubHandler {
return &eventhubHandler{
config: config,
settings: settings,
}
}