// Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 package internal // import "go.opentelemetry.io/collector/receiver/internal" import ( "context" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/consumer/consumerprofiles" "go.opentelemetry.io/collector/pipeline" ) // Factory is a factory interface for receivers. // // This interface cannot be directly implemented. Implementations must // use the NewReceiverFactory to implement it. type Factory interface { component.Factory // CreateTracesReceiver creates a TracesReceiver based on this config. // If the receiver type does not support traces, // this function returns the error [pipeline.ErrSignalNotSupported]. // Implementers can assume `nextConsumer` is never nil. CreateTracesReceiver(ctx context.Context, set Settings, cfg component.Config, nextConsumer consumer.Traces) (Traces, error) // TracesReceiverStability gets the stability level of the TracesReceiver. TracesReceiverStability() component.StabilityLevel // CreateMetricsReceiver creates a MetricsReceiver based on this config. // If the receiver type does not support metrics, // this function returns the error [pipeline.ErrSignalNotSupported]. // Implementers can assume `nextConsumer` is never nil. CreateMetricsReceiver(ctx context.Context, set Settings, cfg component.Config, nextConsumer consumer.Metrics) (Metrics, error) // MetricsReceiverStability gets the stability level of the MetricsReceiver. MetricsReceiverStability() component.StabilityLevel // CreateLogsReceiver creates a LogsReceiver based on this config. // If the receiver type does not support logs, // this function returns the error [pipeline.ErrSignalNotSupported]. // Implementers can assume `nextConsumer` is never nil. CreateLogsReceiver(ctx context.Context, set Settings, cfg component.Config, nextConsumer consumer.Logs) (Logs, error) // LogsReceiverStability gets the stability level of the LogsReceiver. LogsReceiverStability() component.StabilityLevel // CreateProfilesReceiver creates a ProfilesReceiver based on this config. // If the receiver type does not support tracing or if the config is not valid // an error will be returned instead. `nextConsumer` is never nil. CreateProfilesReceiver(ctx context.Context, set Settings, cfg component.Config, nextConsumer consumerprofiles.Profiles) (Profiles, error) // ProfilesReceiverStability gets the stability level of the ProfilesReceiver. ProfilesReceiverStability() component.StabilityLevel unexportedFactoryFunc() } // FactoryOption apply changes to ReceiverOptions. type FactoryOption interface { // applyOption applies the option. applyOption(o *factory) } // factoryOptionFunc is an ReceiverFactoryOption created through a function. type factoryOptionFunc func(*factory) func (f factoryOptionFunc) applyOption(o *factory) { f(o) } // CreateTracesFunc is the equivalent of Factory.CreateTraces. type CreateTracesFunc func(context.Context, Settings, component.Config, consumer.Traces) (Traces, error) // CreateTracesReceiver implements Factory.CreateTracesReceiver(). func (f CreateTracesFunc) CreateTracesReceiver( ctx context.Context, set Settings, cfg component.Config, nextConsumer consumer.Traces) (Traces, error) { if f == nil { return nil, pipeline.ErrSignalNotSupported } return f(ctx, set, cfg, nextConsumer) } // CreateMetricsFunc is the equivalent of Factory.CreateMetrics. type CreateMetricsFunc func(context.Context, Settings, component.Config, consumer.Metrics) (Metrics, error) // CreateMetricsReceiver implements Factory.CreateMetricsReceiver(). func (f CreateMetricsFunc) CreateMetricsReceiver( ctx context.Context, set Settings, cfg component.Config, nextConsumer consumer.Metrics, ) (Metrics, error) { if f == nil { return nil, pipeline.ErrSignalNotSupported } return f(ctx, set, cfg, nextConsumer) } // CreateLogsFunc is the equivalent of ReceiverFactory.CreateLogsReceiver(). type CreateLogsFunc func(context.Context, Settings, component.Config, consumer.Logs) (Logs, error) // CreateLogsReceiver implements Factory.CreateLogsReceiver(). func (f CreateLogsFunc) CreateLogsReceiver( ctx context.Context, set Settings, cfg component.Config, nextConsumer consumer.Logs, ) (Logs, error) { if f == nil { return nil, pipeline.ErrSignalNotSupported } return f(ctx, set, cfg, nextConsumer) } // CreateProfilesFunc is the equivalent of Factory.CreateProfiles. type CreateProfilesFunc func(context.Context, Settings, component.Config, consumerprofiles.Profiles) (Profiles, error) // CreateProfilesReceiver implements Factory.CreateProfilesReceiver(). func (f CreateProfilesFunc) CreateProfilesReceiver( ctx context.Context, set Settings, cfg component.Config, nextConsumer consumerprofiles.Profiles) (Profiles, error) { if f == nil { return nil, pipeline.ErrSignalNotSupported } return f(ctx, set, cfg, nextConsumer) } type factory struct { cfgType component.Type component.CreateDefaultConfigFunc CreateTracesFunc tracesStabilityLevel component.StabilityLevel CreateMetricsFunc metricsStabilityLevel component.StabilityLevel CreateLogsFunc logsStabilityLevel component.StabilityLevel CreateProfilesFunc profilesStabilityLevel component.StabilityLevel } func (f *factory) Type() component.Type { return f.cfgType } func (f *factory) unexportedFactoryFunc() {} func (f *factory) TracesReceiverStability() component.StabilityLevel { return f.tracesStabilityLevel } func (f *factory) MetricsReceiverStability() component.StabilityLevel { return f.metricsStabilityLevel } func (f *factory) LogsReceiverStability() component.StabilityLevel { return f.logsStabilityLevel } func (f *factory) ProfilesReceiverStability() component.StabilityLevel { return f.profilesStabilityLevel } // WithTraces overrides the default "error not supported" implementation for CreateTracesReceiver and the default "undefined" stability level. func WithTraces(createTracesReceiver CreateTracesFunc, sl component.StabilityLevel) FactoryOption { return factoryOptionFunc(func(o *factory) { o.tracesStabilityLevel = sl o.CreateTracesFunc = createTracesReceiver }) } // WithMetrics overrides the default "error not supported" implementation for CreateMetricsReceiver and the default "undefined" stability level. func WithMetrics(createMetricsReceiver CreateMetricsFunc, sl component.StabilityLevel) FactoryOption { return factoryOptionFunc(func(o *factory) { o.metricsStabilityLevel = sl o.CreateMetricsFunc = createMetricsReceiver }) } // WithLogs overrides the default "error not supported" implementation for CreateLogsReceiver and the default "undefined" stability level. func WithLogs(createLogsReceiver CreateLogsFunc, sl component.StabilityLevel) FactoryOption { return factoryOptionFunc(func(o *factory) { o.logsStabilityLevel = sl o.CreateLogsFunc = createLogsReceiver }) } // WithProfiles overrides the default "error not supported" implementation for CreateProfilesReceiver and the default "undefined" stability level. func WithProfiles(createProfilesReceiver CreateProfilesFunc, sl component.StabilityLevel) FactoryOption { return factoryOptionFunc(func(o *factory) { o.profilesStabilityLevel = sl o.CreateProfilesFunc = createProfilesReceiver }) } // NewFactory returns a Factory. func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory { f := &factory{ cfgType: cfgType, CreateDefaultConfigFunc: createDefaultConfig, } for _, opt := range options { opt.applyOption(f) } return f }