|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package internal // import "go.opentelemetry.io/collector/receiver/internal" |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "go.opentelemetry.io/collector/component" |
| 10 | + "go.opentelemetry.io/collector/consumer" |
| 11 | +) |
| 12 | + |
| 13 | +// Factory is a factory interface for receivers. |
| 14 | +// |
| 15 | +// This interface cannot be directly implemented. Implementations must |
| 16 | +// use the NewReceiverFactory to implement it. |
| 17 | +type Factory interface { |
| 18 | + component.Factory |
| 19 | + |
| 20 | + // CreateTracesReceiver creates a TracesReceiver based on this config. |
| 21 | + // If the receiver type does not support tracing or if the config is not valid |
| 22 | + // an error will be returned instead. `nextConsumer` is never nil. |
| 23 | + CreateTracesReceiver(ctx context.Context, set Settings, cfg component.Config, nextConsumer consumer.Traces) (Traces, error) |
| 24 | + |
| 25 | + // TracesReceiverStability gets the stability level of the TracesReceiver. |
| 26 | + TracesReceiverStability() component.StabilityLevel |
| 27 | + |
| 28 | + // CreateMetricsReceiver creates a MetricsReceiver based on this config. |
| 29 | + // If the receiver type does not support metrics or if the config is not valid |
| 30 | + // an error will be returned instead. `nextConsumer` is never nil. |
| 31 | + CreateMetricsReceiver(ctx context.Context, set Settings, cfg component.Config, nextConsumer consumer.Metrics) (Metrics, error) |
| 32 | + |
| 33 | + // MetricsReceiverStability gets the stability level of the MetricsReceiver. |
| 34 | + MetricsReceiverStability() component.StabilityLevel |
| 35 | + |
| 36 | + // CreateLogsReceiver creates a LogsReceiver based on this config. |
| 37 | + // If the receiver type does not support the data type or if the config is not valid |
| 38 | + // an error will be returned instead. `nextConsumer` is never nil. |
| 39 | + CreateLogsReceiver(ctx context.Context, set Settings, cfg component.Config, nextConsumer consumer.Logs) (Logs, error) |
| 40 | + |
| 41 | + // LogsReceiverStability gets the stability level of the LogsReceiver. |
| 42 | + LogsReceiverStability() component.StabilityLevel |
| 43 | + |
| 44 | + unexportedFactoryFunc() |
| 45 | +} |
| 46 | + |
| 47 | +// FactoryOption apply changes to ReceiverOptions. |
| 48 | +type FactoryOption interface { |
| 49 | + // applyOption applies the option. |
| 50 | + applyOption(o *factory) |
| 51 | +} |
| 52 | + |
| 53 | +// factoryOptionFunc is an ReceiverFactoryOption created through a function. |
| 54 | +type factoryOptionFunc func(*factory) |
| 55 | + |
| 56 | +func (f factoryOptionFunc) applyOption(o *factory) { |
| 57 | + f(o) |
| 58 | +} |
| 59 | + |
| 60 | +// CreateTracesFunc is the equivalent of Factory.CreateTraces. |
| 61 | +type CreateTracesFunc func(context.Context, Settings, component.Config, consumer.Traces) (Traces, error) |
| 62 | + |
| 63 | +// CreateTracesReceiver implements Factory.CreateTracesReceiver(). |
| 64 | +func (f CreateTracesFunc) CreateTracesReceiver( |
| 65 | + ctx context.Context, |
| 66 | + set Settings, |
| 67 | + cfg component.Config, |
| 68 | + nextConsumer consumer.Traces) (Traces, error) { |
| 69 | + if f == nil { |
| 70 | + return nil, component.ErrDataTypeIsNotSupported |
| 71 | + } |
| 72 | + return f(ctx, set, cfg, nextConsumer) |
| 73 | +} |
| 74 | + |
| 75 | +// CreateMetricsFunc is the equivalent of Factory.CreateMetrics. |
| 76 | +type CreateMetricsFunc func(context.Context, Settings, component.Config, consumer.Metrics) (Metrics, error) |
| 77 | + |
| 78 | +// CreateMetricsReceiver implements Factory.CreateMetricsReceiver(). |
| 79 | +func (f CreateMetricsFunc) CreateMetricsReceiver( |
| 80 | + ctx context.Context, |
| 81 | + set Settings, |
| 82 | + cfg component.Config, |
| 83 | + nextConsumer consumer.Metrics, |
| 84 | +) (Metrics, error) { |
| 85 | + if f == nil { |
| 86 | + return nil, component.ErrDataTypeIsNotSupported |
| 87 | + } |
| 88 | + return f(ctx, set, cfg, nextConsumer) |
| 89 | +} |
| 90 | + |
| 91 | +// CreateLogsFunc is the equivalent of ReceiverFactory.CreateLogsReceiver(). |
| 92 | +type CreateLogsFunc func(context.Context, Settings, component.Config, consumer.Logs) (Logs, error) |
| 93 | + |
| 94 | +// CreateLogsReceiver implements Factory.CreateLogsReceiver(). |
| 95 | +func (f CreateLogsFunc) CreateLogsReceiver( |
| 96 | + ctx context.Context, |
| 97 | + set Settings, |
| 98 | + cfg component.Config, |
| 99 | + nextConsumer consumer.Logs, |
| 100 | +) (Logs, error) { |
| 101 | + if f == nil { |
| 102 | + return nil, component.ErrDataTypeIsNotSupported |
| 103 | + } |
| 104 | + return f(ctx, set, cfg, nextConsumer) |
| 105 | +} |
| 106 | + |
| 107 | +type factory struct { |
| 108 | + cfgType component.Type |
| 109 | + component.CreateDefaultConfigFunc |
| 110 | + CreateTracesFunc |
| 111 | + tracesStabilityLevel component.StabilityLevel |
| 112 | + CreateMetricsFunc |
| 113 | + metricsStabilityLevel component.StabilityLevel |
| 114 | + CreateLogsFunc |
| 115 | + logsStabilityLevel component.StabilityLevel |
| 116 | +} |
| 117 | + |
| 118 | +func (f *factory) Type() component.Type { |
| 119 | + return f.cfgType |
| 120 | +} |
| 121 | + |
| 122 | +func (f *factory) unexportedFactoryFunc() {} |
| 123 | + |
| 124 | +func (f *factory) TracesReceiverStability() component.StabilityLevel { |
| 125 | + return f.tracesStabilityLevel |
| 126 | +} |
| 127 | + |
| 128 | +func (f *factory) MetricsReceiverStability() component.StabilityLevel { |
| 129 | + return f.metricsStabilityLevel |
| 130 | +} |
| 131 | + |
| 132 | +func (f *factory) LogsReceiverStability() component.StabilityLevel { |
| 133 | + return f.logsStabilityLevel |
| 134 | +} |
| 135 | + |
| 136 | +// WithTraces overrides the default "error not supported" implementation for CreateTracesReceiver and the default "undefined" stability level. |
| 137 | +func WithTraces(createTracesReceiver CreateTracesFunc, sl component.StabilityLevel) FactoryOption { |
| 138 | + return factoryOptionFunc(func(o *factory) { |
| 139 | + o.tracesStabilityLevel = sl |
| 140 | + o.CreateTracesFunc = createTracesReceiver |
| 141 | + }) |
| 142 | +} |
| 143 | + |
| 144 | +// WithMetrics overrides the default "error not supported" implementation for CreateMetricsReceiver and the default "undefined" stability level. |
| 145 | +func WithMetrics(createMetricsReceiver CreateMetricsFunc, sl component.StabilityLevel) FactoryOption { |
| 146 | + return factoryOptionFunc(func(o *factory) { |
| 147 | + o.metricsStabilityLevel = sl |
| 148 | + o.CreateMetricsFunc = createMetricsReceiver |
| 149 | + }) |
| 150 | +} |
| 151 | + |
| 152 | +// WithLogs overrides the default "error not supported" implementation for CreateLogsReceiver and the default "undefined" stability level. |
| 153 | +func WithLogs(createLogsReceiver CreateLogsFunc, sl component.StabilityLevel) FactoryOption { |
| 154 | + return factoryOptionFunc(func(o *factory) { |
| 155 | + o.logsStabilityLevel = sl |
| 156 | + o.CreateLogsFunc = createLogsReceiver |
| 157 | + }) |
| 158 | +} |
| 159 | + |
| 160 | +// NewFactory returns a Factory. |
| 161 | +func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory { |
| 162 | + f := &factory{ |
| 163 | + cfgType: cfgType, |
| 164 | + CreateDefaultConfigFunc: createDefaultConfig, |
| 165 | + } |
| 166 | + for _, opt := range options { |
| 167 | + opt.applyOption(f) |
| 168 | + } |
| 169 | + return f |
| 170 | +} |
0 commit comments