-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
example_receiver.go
140 lines (121 loc) · 4.38 KB
/
example_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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testcomponents
import (
"context"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver/receiverhelper"
)
// ExampleReceiver is for testing purposes. We are defining an example config and factory
// for "examplereceiver" receiver type.
type ExampleReceiver struct {
config.ReceiverSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
// Configures the receiver server protocol.
confignet.TCPAddr `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
ExtraSetting string `mapstructure:"extra"`
ExtraMapSetting map[string]string `mapstructure:"extra_map"`
ExtraListSetting []string `mapstructure:"extra_list"`
}
const recvType = "examplereceiver"
// ExampleReceiverFactory is factory for ExampleReceiver.
var ExampleReceiverFactory = receiverhelper.NewFactory(
recvType,
createReceiverDefaultConfig,
receiverhelper.WithTraces(createTracesReceiver),
receiverhelper.WithMetrics(createMetricsReceiver),
receiverhelper.WithLogs(createLogsReceiver))
func createReceiverDefaultConfig() config.Receiver {
return &ExampleReceiver{
ReceiverSettings: config.ReceiverSettings{
TypeVal: recvType,
NameVal: recvType,
},
TCPAddr: confignet.TCPAddr{
Endpoint: "localhost:1000",
},
ExtraSetting: "some string",
ExtraMapSetting: nil,
ExtraListSetting: nil,
}
}
// CreateTracesReceiver creates a trace receiver based on this config.
func createTracesReceiver(
_ context.Context,
_ component.ReceiverCreateParams,
cfg config.Receiver,
nextConsumer consumer.Traces,
) (component.TracesReceiver, error) {
receiver := createReceiver(cfg)
receiver.Traces = nextConsumer
return receiver, nil
}
// CreateMetricsReceiver creates a metrics receiver based on this config.
func createMetricsReceiver(
_ context.Context,
_ component.ReceiverCreateParams,
cfg config.Receiver,
nextConsumer consumer.Metrics,
) (component.MetricsReceiver, error) {
receiver := createReceiver(cfg)
receiver.Metrics = nextConsumer
return receiver, nil
}
func createLogsReceiver(
_ context.Context,
_ component.ReceiverCreateParams,
cfg config.Receiver,
nextConsumer consumer.Logs,
) (component.LogsReceiver, error) {
receiver := createReceiver(cfg)
receiver.Logs = nextConsumer
return receiver, nil
}
func createReceiver(cfg config.Receiver) *ExampleReceiverProducer {
// There must be one receiver for all data types. We maintain a map of
// receivers per config.
// Check to see if there is already a receiver for this config.
receiver, ok := exampleReceivers[cfg]
if !ok {
receiver = &ExampleReceiverProducer{}
// Remember the receiver in the map
exampleReceivers[cfg] = receiver
}
return receiver
}
// ExampleReceiverProducer allows producing traces and metrics for testing purposes.
type ExampleReceiverProducer struct {
Started bool
Stopped bool
consumer.Traces
consumer.Metrics
consumer.Logs
}
// Start tells the receiver to start its processing.
func (erp *ExampleReceiverProducer) Start(_ context.Context, _ component.Host) error {
erp.Started = true
return nil
}
// Shutdown tells the receiver that should stop reception,
func (erp *ExampleReceiverProducer) Shutdown(context.Context) error {
erp.Stopped = true
return nil
}
// This is the map of already created example receivers for particular configurations.
// We maintain this map because the ReceiverFactory is asked trace and metric receivers separately
// when it gets CreateTracesReceiver() and CreateMetricsReceiver() but they must not
// create separate objects, they must use one Receiver object per configuration.
var exampleReceivers = map[config.Receiver]*ExampleReceiverProducer{}