@@ -20,6 +20,7 @@ import (
2020
2121 "go.uber.org/zap"
2222
23+ "github.com/open-telemetry/opentelemetry-collector/component"
2324 "github.com/open-telemetry/opentelemetry-collector/config/configerror"
2425 "github.com/open-telemetry/opentelemetry-collector/config/configmodels"
2526 "github.com/open-telemetry/opentelemetry-collector/consumer"
@@ -82,7 +83,20 @@ func (f *ExampleReceiverFactory) CreateTraceReceiver(
8283 if cfg .(* ExampleReceiver ).FailTraceCreation {
8384 return nil , configerror .ErrDataTypeIsNotSupported
8485 }
85- return & ExampleReceiverProducer {TraceConsumer : nextConsumer }, nil
86+
87+ // There must be one receiver for both metrics and traces. We maintain a map of
88+ // receivers per config.
89+
90+ // Check to see if there is already a receiver for this config.
91+ receiver , ok := exampleReceivers [cfg ]
92+ if ! ok {
93+ receiver = & ExampleReceiverProducer {}
94+ // Remember the receiver in the map
95+ exampleReceivers [cfg ] = receiver
96+ }
97+ receiver .TraceConsumer = nextConsumer
98+
99+ return receiver , nil
86100}
87101
88102// CreateMetricsReceiver creates a metrics receiver based on this config.
@@ -94,33 +108,44 @@ func (f *ExampleReceiverFactory) CreateMetricsReceiver(
94108 if cfg .(* ExampleReceiver ).FailMetricsCreation {
95109 return nil , configerror .ErrDataTypeIsNotSupported
96110 }
97- return & ExampleReceiverProducer {MetricsConsumer : nextConsumer }, nil
111+
112+ // There must be one receiver for both metrics and traces. We maintain a map of
113+ // receivers per config.
114+
115+ // Check to see if there is already a receiver for this config.
116+ receiver , ok := exampleReceivers [cfg ]
117+ if ! ok {
118+ receiver = & ExampleReceiverProducer {}
119+ // Remember the receiver in the map
120+ exampleReceivers [cfg ] = receiver
121+ }
122+ receiver .MetricsConsumer = nextConsumer
123+
124+ return receiver , nil
98125}
99126
100127// ExampleReceiverProducer allows producing traces and metrics for testing purposes.
101128type ExampleReceiverProducer struct {
102129 TraceConsumer consumer.TraceConsumer
103- TraceStarted bool
104- TraceStopped bool
130+ Started bool
131+ Stopped bool
105132 MetricsConsumer consumer.MetricsConsumer
106- MetricsStarted bool
107- MetricsStopped bool
108133}
109134
110135// TraceSource returns the name of the trace data source.
111136func (erp * ExampleReceiverProducer ) TraceSource () string {
112137 return ""
113138}
114139
115- // StartTraceReception tells the receiver to start its processing.
116- func (erp * ExampleReceiverProducer ) StartTraceReception (host receiver .Host ) error {
117- erp .TraceStarted = true
140+ // Start tells the receiver to start its processing.
141+ func (erp * ExampleReceiverProducer ) Start (host component .Host ) error {
142+ erp .Started = true
118143 return nil
119144}
120145
121- // StopTraceReception tells the receiver that should stop reception,
122- func (erp * ExampleReceiverProducer ) StopTraceReception () error {
123- erp .TraceStopped = true
146+ // Shutdown tells the receiver that should stop reception,
147+ func (erp * ExampleReceiverProducer ) Shutdown () error {
148+ erp .Stopped = true
124149 return nil
125150}
126151
@@ -129,17 +154,11 @@ func (erp *ExampleReceiverProducer) MetricsSource() string {
129154 return ""
130155}
131156
132- // StartMetricsReception tells the receiver to start its processing.
133- func (erp * ExampleReceiverProducer ) StartMetricsReception (host receiver.Host ) error {
134- erp .MetricsStarted = true
135- return nil
136- }
137-
138- // StopMetricsReception tells the receiver that should stop reception,
139- func (erp * ExampleReceiverProducer ) StopMetricsReception () error {
140- erp .MetricsStopped = true
141- return nil
142- }
157+ // This is the map of already created example receivers for particular configurations.
158+ // We maintain this map because the Factory is asked trace and metric receivers separately
159+ // when it gets CreateTraceReceiver() and CreateMetricsReceiver() but they must not
160+ // create separate objects, they must use one Receiver object per configuration.
161+ var exampleReceivers = map [configmodels.Receiver ]* ExampleReceiverProducer {}
143162
144163// MultiProtoReceiver is for testing purposes. We are defining an example multi protocol
145164// config and factory for "multireceiver" receiver type.
@@ -293,7 +312,7 @@ type ExampleExporterConsumer struct {
293312// Start tells the exporter to start. The exporter may prepare for exporting
294313// by connecting to the endpoint. Host parameter can be used for communicating
295314// with the host after Start() has already returned.
296- func (exp * ExampleExporterConsumer ) Start (host exporter .Host ) error {
315+ func (exp * ExampleExporterConsumer ) Start (host component .Host ) error {
297316 exp .ExporterStarted = true
298317 return nil
299318}
0 commit comments