Debian Codegraph @

receiver/receiver.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package receiver // import "go.opentelemetry.io/collector/receiver"
5
6 import (
7 "context"
8
9 "go.opentelemetry.io/collector/component"
10 "go.opentelemetry.io/collector/consumer"
11 "go.opentelemetry.io/collector/pipeline"
12 "go.opentelemetry.io/collector/receiver/internal"
13 )
14
15 // Traces receiver receives traces.
16 // Its purpose is to translate data from any format to the collector's internal trace format.
17 // Traces receiver feeds a consumer.Traces with data.
18 //
19 // For example, it could be Zipkin data source which translates Zipkin spans into ptrace.Traces.
20 type Traces interface {
21 component.Component
22 }
23
24 // Metrics receiver receives metrics.
25 // Its purpose is to translate data from any format to the collector's internal metrics format.
26 // Metrics receiver feeds a consumer.Metrics with data.
27 //
28 // For example, it could be Prometheus data source which translates Prometheus metrics into pmetric.Metrics.
29 type Metrics interface {
30 component.Component
31 }
32
33 // Logs receiver receives logs.
34 // Its purpose is to translate data from any format to the collector's internal logs data format.
35 // Logs receiver feeds a consumer.Logs with data.
36 //
37 // For example, it could be a receiver that reads syslogs and convert them into plog.Logs.
38 type Logs interface {
39 component.Component
40 }
41
42 // Settings configures receiver creators.
43 type Settings struct {
44 // ID returns the ID of the component that will be created.
45 ID component.ID
46
47 component.TelemetrySettings
48
49 // BuildInfo can be used by components for informational purposes.
50 BuildInfo component.BuildInfo
51
52 // prevent unkeyed literal initialization
53 _ struct{}
54 }
55
56 // Factory is a factory interface for receivers.
57 //
58 // This interface cannot be directly implemented. Implementations must
59 // use the NewFactory to implement it.
60 type Factory interface {
61 component.Factory
62
63 // CreateTraces creates a Traces based on this config.
64 // If the receiver type does not support traces,
65 // this function returns the error [pipeline.ErrSignalNotSupported].
66 // Implementers can assume `next` is never nil.
67 CreateTraces(ctx context.Context, set Settings, cfg component.Config, next consumer.Traces) (Traces, error)
68
69 // TracesStability gets the stability level of the Traces receiver.
70 TracesStability() component.StabilityLevel
71
72 // CreateMetrics creates a Metrics based on this config.
73 // If the receiver type does not support metrics,
74 // this function returns the error [pipeline.ErrSignalNotSupported].
75 // Implementers can assume `next` is never nil.
76 CreateMetrics(ctx context.Context, set Settings, cfg component.Config, next consumer.Metrics) (Metrics, error)
77
78 // MetricsStability gets the stability level of the Metrics receiver.
79 MetricsStability() component.StabilityLevel
80
81 // CreateLogs creates a Logs based on this config.
82 // If the receiver type does not support logs,
83 // this function returns the error [pipeline.ErrSignalNotSupported].
84 // Implementers can assume `next` is never nil.
85 CreateLogs(ctx context.Context, set Settings, cfg component.Config, next consumer.Logs) (Logs, error)
86
87 // LogsStability gets the stability level of the Logs receiver.
88 LogsStability() component.StabilityLevel
89
90 unexportedFactoryFunc()
91 }
92
93 // FactoryOption apply changes to Factory.
94 type FactoryOption interface {
95 // applyOption applies the option.
96 applyOption(o *factory)
97 }
98
99 // factoryOptionFunc is an FactoryOption created through a function.
100 type factoryOptionFunc func(*factory)
101
102 func (f factoryOptionFunc) applyOption(o *factory) {
103 f(o)
104 }
105
106 // CreateTracesFunc is the equivalent of Factory.CreateTraces.
107 type CreateTracesFunc func(context.Context, Settings, component.Config, consumer.Traces) (Traces, error)
108
109 // CreateMetricsFunc is the equivalent of Factory.CreateMetrics.
110 type CreateMetricsFunc func(context.Context, Settings, component.Config, consumer.Metrics) (Metrics, error)
111
112 // CreateLogsFunc is the equivalent of Factory.CreateLogs.
113 type CreateLogsFunc func(context.Context, Settings, component.Config, consumer.Logs) (Logs, error)
114
115 type factory struct {
116 cfgType component.Type
117 component.CreateDefaultConfigFunc
118 createTracesFunc CreateTracesFunc
119 tracesStabilityLevel component.StabilityLevel
120 createMetricsFunc CreateMetricsFunc
121 metricsStabilityLevel component.StabilityLevel
122 createLogsFunc CreateLogsFunc
123 logsStabilityLevel component.StabilityLevel
124 }
125
126 func (f *factory) Type() component.Type {
127 return f.cfgType
128 }
129
130 func (f *factory) unexportedFactoryFunc() {}
131
132 func (f *factory) TracesStability() component.StabilityLevel {
133 return f.tracesStabilityLevel
134 }
135
136 func (f *factory) MetricsStability() component.StabilityLevel {
137 return f.metricsStabilityLevel
138 }
139
140 func (f *factory) LogsStability() component.StabilityLevel {
141 return f.logsStabilityLevel
142 }
143
144 func (f *factory) CreateTraces(ctx context.Context, set Settings, cfg component.Config, next consumer.Traces) (Traces, error) {
145 if f.createTracesFunc == nil {
146 return nil, pipeline.ErrSignalNotSupported
147 }
148
149 if set.ID.Type() != f.Type() {
150 return nil, internal.ErrIDMismatch(set.ID, f.Type())
151 }
152
153 return f.createTracesFunc(ctx, set, cfg, next)
154 }
155
156 func (f *factory) CreateMetrics(ctx context.Context, set Settings, cfg component.Config, next consumer.Metrics) (Metrics, error) {
157 if f.createMetricsFunc == nil {
158 return nil, pipeline.ErrSignalNotSupported
159 }
160
161 if set.ID.Type() != f.Type() {
162 return nil, internal.ErrIDMismatch(set.ID, f.Type())
163 }
164
165 return f.createMetricsFunc(ctx, set, cfg, next)
166 }
167
168 func (f *factory) CreateLogs(ctx context.Context, set Settings, cfg component.Config, next consumer.Logs) (Logs, error) {
169 if f.createLogsFunc == nil {
170 return nil, pipeline.ErrSignalNotSupported
171 }
172
173 if set.ID.Type() != f.Type() {
174 return nil, internal.ErrIDMismatch(set.ID, f.Type())
175 }
176
177 return f.createLogsFunc(ctx, set, cfg, next)
178 }
179
180 // WithTraces overrides the default "error not supported" implementation for Factory.CreateTraces and the default "undefined" stability level.
181 func WithTraces(createTraces CreateTracesFunc, sl component.StabilityLevel) FactoryOption {
182 return factoryOptionFunc(func(o *factory) {
183 o.tracesStabilityLevel = sl
184 o.createTracesFunc = createTraces
185 })
186 }
187
188 // WithMetrics overrides the default "error not supported" implementation for Factory.CreateMetrics and the default "undefined" stability level.
189 func WithMetrics(createMetrics CreateMetricsFunc, sl component.StabilityLevel) FactoryOption {
190 return factoryOptionFunc(func(o *factory) {
191 o.metricsStabilityLevel = sl
192 o.createMetricsFunc = createMetrics
193 })
194 }
195
196 // WithLogs overrides the default "error not supported" implementation for Factory.CreateLogs and the default "undefined" stability level.
197 func WithLogs(createLogs CreateLogsFunc, sl component.StabilityLevel) FactoryOption {
198 return factoryOptionFunc(func(o *factory) {
199 o.logsStabilityLevel = sl
200 o.createLogsFunc = createLogs
201 })
202 }
203
204 // NewFactory returns a Factory.
205 func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
206 f := &factory{
207 cfgType: cfgType,
208 CreateDefaultConfigFunc: createDefaultConfig,
209 }
210 for _, opt := range options {
211 opt.applyOption(f)
212 }
213 return f
214 }