forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.go
155 lines (122 loc) · 4.51 KB
/
receiver.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package azureeventhubreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/azureeventhubreceiver"
import (
"context"
"errors"
"fmt"
eventhub "github.com/Azure/azure-event-hubs-go/v3"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/azureeventhubreceiver/internal/metadata"
)
type dataConsumer interface {
consume(ctx context.Context, event *eventhub.Event) error
setNextLogsConsumer(nextLogsConsumer consumer.Logs)
setNextMetricsConsumer(nextLogsConsumer consumer.Metrics)
}
type eventLogsUnmarshaler interface {
UnmarshalLogs(event *eventhub.Event) (plog.Logs, error)
}
type eventMetricsUnmarshaler interface {
UnmarshalMetrics(event *eventhub.Event) (pmetric.Metrics, error)
}
type eventhubReceiver struct {
eventHandler *eventhubHandler
dataType component.Type
logger *zap.Logger
logsUnmarshaler eventLogsUnmarshaler
metricsUnmarshaler eventMetricsUnmarshaler
nextLogsConsumer consumer.Logs
nextMetricsConsumer consumer.Metrics
obsrecv *receiverhelper.ObsReport
}
func (receiver *eventhubReceiver) Start(ctx context.Context, host component.Host) error {
return receiver.eventHandler.run(ctx, host)
}
func (receiver *eventhubReceiver) Shutdown(ctx context.Context) error {
return receiver.eventHandler.close(ctx)
}
func (receiver *eventhubReceiver) setNextLogsConsumer(nextLogsConsumer consumer.Logs) {
receiver.nextLogsConsumer = nextLogsConsumer
}
func (receiver *eventhubReceiver) setNextMetricsConsumer(nextMetricsConsumer consumer.Metrics) {
receiver.nextMetricsConsumer = nextMetricsConsumer
}
func (receiver *eventhubReceiver) consume(ctx context.Context, event *eventhub.Event) error {
switch receiver.dataType {
case component.DataTypeLogs:
return receiver.consumeLogs(ctx, event)
case component.DataTypeMetrics:
return receiver.consumeMetrics(ctx, event)
case component.DataTypeTraces:
fallthrough
default:
return fmt.Errorf("invalid data type: %v", receiver.dataType)
}
}
func (receiver *eventhubReceiver) consumeLogs(ctx context.Context, event *eventhub.Event) error {
if receiver.nextLogsConsumer == nil {
return nil
}
if receiver.logsUnmarshaler == nil {
return errors.New("unable to unmarshal logs with configured format")
}
logsContext := receiver.obsrecv.StartLogsOp(ctx)
logs, err := receiver.logsUnmarshaler.UnmarshalLogs(event)
if err != nil {
return fmt.Errorf("failed to unmarshal logs: %w", err)
}
receiver.logger.Debug("Log Records", zap.Any("logs", logs))
err = receiver.nextLogsConsumer.ConsumeLogs(logsContext, logs)
receiver.obsrecv.EndLogsOp(logsContext, metadata.Type.String(), 1, err)
return err
}
func (receiver *eventhubReceiver) consumeMetrics(ctx context.Context, event *eventhub.Event) error {
if receiver.nextMetricsConsumer == nil {
return nil
}
if receiver.metricsUnmarshaler == nil {
return errors.New("unable to unmarshal metrics with configured format")
}
metricsContext := receiver.obsrecv.StartMetricsOp(ctx)
metrics, err := receiver.metricsUnmarshaler.UnmarshalMetrics(event)
if err != nil {
return fmt.Errorf("failed to unmarshal metrics: %w", err)
}
receiver.logger.Debug("Metric Records", zap.Any("metrics", metrics))
err = receiver.nextMetricsConsumer.ConsumeMetrics(metricsContext, metrics)
receiver.obsrecv.EndMetricsOp(metricsContext, metadata.Type.String(), 1, err)
return err
}
func newReceiver(
receiverType component.Type,
logsUnmarshaler eventLogsUnmarshaler,
metricsUnmarshaler eventMetricsUnmarshaler,
eventHandler *eventhubHandler,
settings receiver.CreateSettings,
) (component.Component, error) {
obsrecv, err := receiverhelper.NewObsReport(receiverhelper.ObsReportSettings{
ReceiverID: settings.ID,
Transport: "event",
ReceiverCreateSettings: settings,
})
if err != nil {
return nil, err
}
eventhubReceiver := &eventhubReceiver{
dataType: receiverType,
eventHandler: eventHandler,
logger: settings.Logger,
logsUnmarshaler: logsUnmarshaler,
metricsUnmarshaler: metricsUnmarshaler,
obsrecv: obsrecv,
}
eventHandler.setDataConsumer(eventhubReceiver)
return eventhubReceiver, nil
}