Debian Codegraph @

connector/connector.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package connector // import "go.opentelemetry.io/collector/connector"
5
6 import (
7 "context"
8
9 "go.opentelemetry.io/collector/component"
10 "go.opentelemetry.io/collector/connector/internal"
11 "go.opentelemetry.io/collector/consumer"
12 "go.opentelemetry.io/collector/pipeline"
13 )
14
15 // A Traces connector acts as an exporter from a traces pipeline and a receiver
16 // to one or more traces, metrics, or logs pipelines.
17 // Traces feeds a consumer.Traces, consumer.Metrics, or consumer.Logs with data.
18 //
19 // Examples:
20 // - Traces could be collected in one pipeline and routed to another traces pipeline
21 // based on criteria such as attributes or other content of the trace. The second
22 // pipeline can then process and export the trace to the appropriate backend.
23 // - Traces could be summarized by a metrics connector that emits statistics describing
24 // the number of traces observed.
25 // - Traces could be analyzed by a logs connector that emits events when particular
26 // criteria are met.
27 type Traces interface {
28 component.Component
29 consumer.Traces
30 }
31
32 // A Metrics connector acts as an exporter from a metrics pipeline and a receiver
33 // to one or more traces, metrics, or logs pipelines.
34 // Metrics feeds a consumer.Traces, consumer.Metrics, or consumer.Logs with data.
35 //
36 // Examples:
37 // - Latency between related data points could be modeled and emitted as traces.
38 // - Metrics could be collected in one pipeline and routed to another metrics pipeline
39 // based on criteria such as attributes or other content of the metric. The second
40 // pipeline can then process and export the metric to the appropriate backend.
41 // - Metrics could be analyzed by a logs connector that emits events when particular
42 // criteria are met.
43 type Metrics interface {
44 component.Component
45 consumer.Metrics
46 }
47
48 // A Logs connector acts as an exporter from a logs pipeline and a receiver
49 // to one or more traces, metrics, or logs pipelines.
50 // Logs feeds a consumer.Traces, consumer.Metrics, or consumer.Logs with data.
51 //
52 // Examples:
53 // - Structured logs containing span information could be consumed and emitted as traces.
54 // - Metrics could be extracted from structured logs that contain numeric data.
55 // - Logs could be collected in one pipeline and routed to another logs pipeline
56 // based on criteria such as attributes or other content of the log. The second
57 // pipeline can then process and export the log to the appropriate backend.
58 type Logs interface {
59 component.Component
60 consumer.Logs
61 }
62
63 // Settings configures Connector creators.
64 type Settings struct {
65 // ID returns the ID of the component that will be created.
66 ID component.ID
67
68 component.TelemetrySettings
69
70 // BuildInfo can be used by components for informational purposes
71 BuildInfo component.BuildInfo
72 // prevent unkeyed literal initialization
73 _ struct{}
74 }
75
76 // Factory is a factory interface for connectors.
77 //
78 // This interface cannot be directly implemented. Implementations must
79 // use the NewFactory to implement it.
80 type Factory interface {
81 component.Factory
82
83 // CreateDefaultConfig creates the default configuration for the Connector.
84 // This method can be called multiple times depending on the pipeline
85 // configuration and should not cause side-effects that prevent the creation
86 // of multiple instances of the Connector.
87 // The object returned by this method needs to pass the checks implemented by
88 // 'configtest.CheckConfigStruct'. It is recommended to have these checks in the
89 // tests of any implementation of the Factory interface.
90 CreateDefaultConfig() component.Config
91
92 CreateTracesToTraces(ctx context.Context, set Settings, cfg component.Config, next consumer.Traces) (Traces, error)
93 CreateTracesToMetrics(ctx context.Context, set Settings, cfg component.Config, next consumer.Metrics) (Traces, error)
94 CreateTracesToLogs(ctx context.Context, set Settings, cfg component.Config, next consumer.Logs) (Traces, error)
95
96 CreateMetricsToTraces(ctx context.Context, set Settings, cfg component.Config, next consumer.Traces) (Metrics, error)
97 CreateMetricsToMetrics(ctx context.Context, set Settings, cfg component.Config, next consumer.Metrics) (Metrics, error)
98 CreateMetricsToLogs(ctx context.Context, set Settings, cfg component.Config, next consumer.Logs) (Metrics, error)
99
100 CreateLogsToTraces(ctx context.Context, set Settings, cfg component.Config, next consumer.Traces) (Logs, error)
101 CreateLogsToMetrics(ctx context.Context, set Settings, cfg component.Config, next consumer.Metrics) (Logs, error)
102 CreateLogsToLogs(ctx context.Context, set Settings, cfg component.Config, next consumer.Logs) (Logs, error)
103
104 TracesToTracesStability() component.StabilityLevel
105 TracesToMetricsStability() component.StabilityLevel
106 TracesToLogsStability() component.StabilityLevel
107
108 MetricsToTracesStability() component.StabilityLevel
109 MetricsToMetricsStability() component.StabilityLevel
110 MetricsToLogsStability() component.StabilityLevel
111
112 LogsToTracesStability() component.StabilityLevel
113 LogsToMetricsStability() component.StabilityLevel
114 LogsToLogsStability() component.StabilityLevel
115
116 unexportedFactoryFunc()
117 }
118
119 // FactoryOption applies changes to Factory.
120 type FactoryOption interface {
121 // apply applies the option.
122 apply(o *factory)
123 }
124
125 var _ FactoryOption = (*factoryOptionFunc)(nil)
126
127 // factoryOptionFunc is an FactoryOption created through a function.
128 type factoryOptionFunc func(*factory)
129
130 func (f factoryOptionFunc) apply(o *factory) {
131 f(o)
132 }
133
134 // CreateTracesToTracesFunc is the equivalent of Factory.CreateTracesToTraces().
135 type CreateTracesToTracesFunc func(context.Context, Settings, component.Config, consumer.Traces) (Traces, error)
136
137 // CreateTracesToMetricsFunc is the equivalent of Factory.CreateTracesToMetrics().
138 type CreateTracesToMetricsFunc func(context.Context, Settings, component.Config, consumer.Metrics) (Traces, error)
139
140 // CreateTracesToLogsFunc is the equivalent of Factory.CreateTracesToLogs().
141 type CreateTracesToLogsFunc func(context.Context, Settings, component.Config, consumer.Logs) (Traces, error)
142
143 // CreateMetricsToTracesFunc is the equivalent of Factory.CreateMetricsToTraces().
144 type CreateMetricsToTracesFunc func(context.Context, Settings, component.Config, consumer.Traces) (Metrics, error)
145
146 // CreateMetricsToMetricsFunc is the equivalent of Factory.CreateMetricsToTraces().
147 type CreateMetricsToMetricsFunc func(context.Context, Settings, component.Config, consumer.Metrics) (Metrics, error)
148
149 // CreateMetricsToLogsFunc is the equivalent of Factory.CreateMetricsToLogs().
150 type CreateMetricsToLogsFunc func(context.Context, Settings, component.Config, consumer.Logs) (Metrics, error)
151
152 // CreateLogsToTracesFunc is the equivalent of Factory.CreateLogsToTraces().
153 type CreateLogsToTracesFunc func(context.Context, Settings, component.Config, consumer.Traces) (Logs, error)
154
155 // CreateLogsToMetricsFunc is the equivalent of Factory.CreateLogsToMetrics().
156 type CreateLogsToMetricsFunc func(context.Context, Settings, component.Config, consumer.Metrics) (Logs, error)
157
158 // CreateLogsToLogsFunc is the equivalent of Factory.CreateLogsToLogs().
159 type CreateLogsToLogsFunc func(context.Context, Settings, component.Config, consumer.Logs) (Logs, error)
160
161 // WithTracesToTraces overrides the default "error not supported" implementation for WithTracesToTraces and the default "undefined" stability level.
162 func WithTracesToTraces(createTracesToTraces CreateTracesToTracesFunc, sl component.StabilityLevel) FactoryOption {
163 return factoryOptionFunc(func(o *factory) {
164 o.tracesToTracesStabilityLevel = sl
165 o.createTracesToTracesFunc = createTracesToTraces
166 })
167 }
168
169 // WithTracesToMetrics overrides the default "error not supported" implementation for WithTracesToMetrics and the default "undefined" stability level.
170 func WithTracesToMetrics(createTracesToMetrics CreateTracesToMetricsFunc, sl component.StabilityLevel) FactoryOption {
171 return factoryOptionFunc(func(o *factory) {
172 o.tracesToMetricsStabilityLevel = sl
173 o.createTracesToMetricsFunc = createTracesToMetrics
174 })
175 }
176
177 // WithTracesToLogs overrides the default "error not supported" implementation for WithTracesToLogs and the default "undefined" stability level.
178 func WithTracesToLogs(createTracesToLogs CreateTracesToLogsFunc, sl component.StabilityLevel) FactoryOption {
179 return factoryOptionFunc(func(o *factory) {
180 o.tracesToLogsStabilityLevel = sl
181 o.createTracesToLogsFunc = createTracesToLogs
182 })
183 }
184
185 // WithMetricsToTraces overrides the default "error not supported" implementation for WithMetricsToTraces and the default "undefined" stability level.
186 func WithMetricsToTraces(createMetricsToTraces CreateMetricsToTracesFunc, sl component.StabilityLevel) FactoryOption {
187 return factoryOptionFunc(func(o *factory) {
188 o.metricsToTracesStabilityLevel = sl
189 o.createMetricsToTracesFunc = createMetricsToTraces
190 })
191 }
192
193 // WithMetricsToMetrics overrides the default "error not supported" implementation for WithMetricsToMetrics and the default "undefined" stability level.
194 func WithMetricsToMetrics(createMetricsToMetrics CreateMetricsToMetricsFunc, sl component.StabilityLevel) FactoryOption {
195 return factoryOptionFunc(func(o *factory) {
196 o.metricsToMetricsStabilityLevel = sl
197 o.createMetricsToMetricsFunc = createMetricsToMetrics
198 })
199 }
200
201 // WithMetricsToLogs overrides the default "error not supported" implementation for WithMetricsToLogs and the default "undefined" stability level.
202 func WithMetricsToLogs(createMetricsToLogs CreateMetricsToLogsFunc, sl component.StabilityLevel) FactoryOption {
203 return factoryOptionFunc(func(o *factory) {
204 o.metricsToLogsStabilityLevel = sl
205 o.createMetricsToLogsFunc = createMetricsToLogs
206 })
207 }
208
209 // WithLogsToTraces overrides the default "error not supported" implementation for WithLogsToTraces and the default "undefined" stability level.
210 func WithLogsToTraces(createLogsToTraces CreateLogsToTracesFunc, sl component.StabilityLevel) FactoryOption {
211 return factoryOptionFunc(func(o *factory) {
212 o.logsToTracesStabilityLevel = sl
213 o.createLogsToTracesFunc = createLogsToTraces
214 })
215 }
216
217 // WithLogsToMetrics overrides the default "error not supported" implementation for WithLogsToMetrics and the default "undefined" stability level.
218 func WithLogsToMetrics(createLogsToMetrics CreateLogsToMetricsFunc, sl component.StabilityLevel) FactoryOption {
219 return factoryOptionFunc(func(o *factory) {
220 o.logsToMetricsStabilityLevel = sl
221 o.createLogsToMetricsFunc = createLogsToMetrics
222 })
223 }
224
225 // WithLogsToLogs overrides the default "error not supported" implementation for WithLogsToLogs and the default "undefined" stability level.
226 func WithLogsToLogs(createLogsToLogs CreateLogsToLogsFunc, sl component.StabilityLevel) FactoryOption {
227 return factoryOptionFunc(func(o *factory) {
228 o.logsToLogsStabilityLevel = sl
229 o.createLogsToLogsFunc = createLogsToLogs
230 })
231 }
232
233 // factory implements the Factory interface.
234 type factory struct {
235 cfgType component.Type
236 component.CreateDefaultConfigFunc
237
238 createTracesToTracesFunc CreateTracesToTracesFunc
239 createTracesToMetricsFunc CreateTracesToMetricsFunc
240 createTracesToLogsFunc CreateTracesToLogsFunc
241
242 createMetricsToTracesFunc CreateMetricsToTracesFunc
243 createMetricsToMetricsFunc CreateMetricsToMetricsFunc
244 createMetricsToLogsFunc CreateMetricsToLogsFunc
245
246 createLogsToTracesFunc CreateLogsToTracesFunc
247 createLogsToMetricsFunc CreateLogsToMetricsFunc
248 createLogsToLogsFunc CreateLogsToLogsFunc
249
250 tracesToTracesStabilityLevel component.StabilityLevel
251 tracesToMetricsStabilityLevel component.StabilityLevel
252 tracesToLogsStabilityLevel component.StabilityLevel
253
254 metricsToTracesStabilityLevel component.StabilityLevel
255 metricsToMetricsStabilityLevel component.StabilityLevel
256 metricsToLogsStabilityLevel component.StabilityLevel
257
258 logsToTracesStabilityLevel component.StabilityLevel
259 logsToMetricsStabilityLevel component.StabilityLevel
260 logsToLogsStabilityLevel component.StabilityLevel
261 }
262
263 // Type returns the type of component.
264 func (f *factory) Type() component.Type {
265 return f.cfgType
266 }
267
268 func (f *factory) unexportedFactoryFunc() {}
269
270 func (f *factory) TracesToTracesStability() component.StabilityLevel {
271 return f.tracesToTracesStabilityLevel
272 }
273
274 func (f *factory) TracesToMetricsStability() component.StabilityLevel {
275 return f.tracesToMetricsStabilityLevel
276 }
277
278 func (f *factory) TracesToLogsStability() component.StabilityLevel {
279 return f.tracesToLogsStabilityLevel
280 }
281
282 func (f *factory) MetricsToTracesStability() component.StabilityLevel {
283 return f.metricsToTracesStabilityLevel
284 }
285
286 func (f *factory) MetricsToMetricsStability() component.StabilityLevel {
287 return f.metricsToMetricsStabilityLevel
288 }
289
290 func (f *factory) MetricsToLogsStability() component.StabilityLevel {
291 return f.metricsToLogsStabilityLevel
292 }
293
294 func (f *factory) LogsToTracesStability() component.StabilityLevel {
295 return f.logsToTracesStabilityLevel
296 }
297
298 func (f *factory) LogsToMetricsStability() component.StabilityLevel {
299 return f.logsToMetricsStabilityLevel
300 }
301
302 func (f *factory) LogsToLogsStability() component.StabilityLevel {
303 return f.logsToLogsStabilityLevel
304 }
305
306 func (f *factory) CreateTracesToTraces(ctx context.Context, set Settings, cfg component.Config, next consumer.Traces) (Traces, error) {
307 if f.createTracesToTracesFunc == nil {
308 return nil, internal.ErrDataTypes(set.ID, pipeline.SignalTraces, pipeline.SignalTraces)
309 }
310
311 if set.ID.Type() != f.Type() {
312 return nil, internal.ErrIDMismatch(set.ID, f.Type())
313 }
314
315 return f.createTracesToTracesFunc(ctx, set, cfg, next)
316 }
317
318 func (f *factory) CreateTracesToMetrics(ctx context.Context, set Settings, cfg component.Config, next consumer.Metrics) (Traces, error) {
319 if f.createTracesToMetricsFunc == nil {
320 return nil, internal.ErrDataTypes(set.ID, pipeline.SignalTraces, pipeline.SignalMetrics)
321 }
322
323 if set.ID.Type() != f.Type() {
324 return nil, internal.ErrIDMismatch(set.ID, f.Type())
325 }
326
327 return f.createTracesToMetricsFunc(ctx, set, cfg, next)
328 }
329
330 func (f *factory) CreateTracesToLogs(ctx context.Context, set Settings, cfg component.Config, next consumer.Logs) (Traces, error) {
331 if f.createTracesToLogsFunc == nil {
332 return nil, internal.ErrDataTypes(set.ID, pipeline.SignalTraces, pipeline.SignalLogs)
333 }
334
335 if set.ID.Type() != f.Type() {
336 return nil, internal.ErrIDMismatch(set.ID, f.Type())
337 }
338
339 return f.createTracesToLogsFunc(ctx, set, cfg, next)
340 }
341
342 func (f *factory) CreateMetricsToTraces(ctx context.Context, set Settings, cfg component.Config, next consumer.Traces) (Metrics, error) {
343 if f.createMetricsToTracesFunc == nil {
344 return nil, internal.ErrDataTypes(set.ID, pipeline.SignalMetrics, pipeline.SignalTraces)
345 }
346
347 if set.ID.Type() != f.Type() {
348 return nil, internal.ErrIDMismatch(set.ID, f.Type())
349 }
350
351 return f.createMetricsToTracesFunc(ctx, set, cfg, next)
352 }
353
354 func (f *factory) CreateMetricsToMetrics(ctx context.Context, set Settings, cfg component.Config, next consumer.Metrics) (Metrics, error) {
355 if f.createMetricsToMetricsFunc == nil {
356 return nil, internal.ErrDataTypes(set.ID, pipeline.SignalMetrics, pipeline.SignalMetrics)
357 }
358
359 if set.ID.Type() != f.Type() {
360 return nil, internal.ErrIDMismatch(set.ID, f.Type())
361 }
362
363 return f.createMetricsToMetricsFunc(ctx, set, cfg, next)
364 }
365
366 func (f *factory) CreateMetricsToLogs(ctx context.Context, set Settings, cfg component.Config, next consumer.Logs) (Metrics, error) {
367 if f.createMetricsToLogsFunc == nil {
368 return nil, internal.ErrDataTypes(set.ID, pipeline.SignalMetrics, pipeline.SignalLogs)
369 }
370
371 if set.ID.Type() != f.Type() {
372 return nil, internal.ErrIDMismatch(set.ID, f.Type())
373 }
374
375 return f.createMetricsToLogsFunc(ctx, set, cfg, next)
376 }
377
378 func (f *factory) CreateLogsToTraces(ctx context.Context, set Settings, cfg component.Config, next consumer.Traces) (Logs, error) {
379 if f.createLogsToTracesFunc == nil {
380 return nil, internal.ErrDataTypes(set.ID, pipeline.SignalLogs, pipeline.SignalTraces)
381 }
382
383 if set.ID.Type() != f.Type() {
384 return nil, internal.ErrIDMismatch(set.ID, f.Type())
385 }
386
387 return f.createLogsToTracesFunc(ctx, set, cfg, next)
388 }
389
390 func (f *factory) CreateLogsToMetrics(ctx context.Context, set Settings, cfg component.Config, next consumer.Metrics) (Logs, error) {
391 if f.createLogsToMetricsFunc == nil {
392 return nil, internal.ErrDataTypes(set.ID, pipeline.SignalLogs, pipeline.SignalMetrics)
393 }
394
395 if set.ID.Type() != f.Type() {
396 return nil, internal.ErrIDMismatch(set.ID, f.Type())
397 }
398
399 return f.createLogsToMetricsFunc(ctx, set, cfg, next)
400 }
401
402 func (f *factory) CreateLogsToLogs(ctx context.Context, set Settings, cfg component.Config, next consumer.Logs) (Logs, error) {
403 if f.createLogsToLogsFunc == nil {
404 return nil, internal.ErrDataTypes(set.ID, pipeline.SignalLogs, pipeline.SignalLogs)
405 }
406
407 if set.ID.Type() != f.Type() {
408 return nil, internal.ErrIDMismatch(set.ID, f.Type())
409 }
410
411 return f.createLogsToLogsFunc(ctx, set, cfg, next)
412 }
413
414 // NewFactory returns a Factory.
415 func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
416 f := &factory{
417 cfgType: cfgType,
418 CreateDefaultConfigFunc: createDefaultConfig,
419 }
420 for _, opt := range options {
421 opt.apply(f)
422 }
423 return f
424 }