Debian Codegraph @

exporter/exporter.go

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