|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package internal // import "go.opentelemetry.io/collector/service/telemetry/internal" |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "go.opentelemetry.io/otel/trace" |
| 10 | + tracenoop "go.opentelemetry.io/otel/trace/noop" |
| 11 | + "go.uber.org/zap" |
| 12 | + |
| 13 | + "go.opentelemetry.io/collector/component" |
| 14 | +) |
| 15 | + |
| 16 | +// CreateSettings holds configuration for building Telemetry. |
| 17 | +type CreateSettings struct { |
| 18 | + BuildInfo component.BuildInfo |
| 19 | + AsyncErrorChannel chan error |
| 20 | + ZapOptions []zap.Option |
| 21 | +} |
| 22 | + |
| 23 | +// Factory is factory interface for telemetry. |
| 24 | +// This interface cannot be directly implemented. Implementations must |
| 25 | +// use the NewFactory to implement it. |
| 26 | +type Factory interface { |
| 27 | + // CreateDefaultConfig creates the default configuration for the telemetry. |
| 28 | + // TODO: Should we just inherit from component.Factory? |
| 29 | + CreateDefaultConfig() component.Config |
| 30 | + |
| 31 | + // CreateLogger creates a logger. |
| 32 | + CreateLogger(ctx context.Context, set CreateSettings, cfg component.Config) (*zap.Logger, error) |
| 33 | + |
| 34 | + // CreateTracerProvider creates a TracerProvider. |
| 35 | + CreateTracerProvider(ctx context.Context, set CreateSettings, cfg component.Config) (trace.TracerProvider, error) |
| 36 | + |
| 37 | + // TODO: Add CreateMeterProvider. |
| 38 | + |
| 39 | + // unexportedFactoryFunc is used to prevent external implementations of Factory. |
| 40 | + unexportedFactoryFunc() |
| 41 | +} |
| 42 | + |
| 43 | +// FactoryOption apply changes to Factory. |
| 44 | +type FactoryOption interface { |
| 45 | + // applyTelemetryFactoryOption applies the option. |
| 46 | + applyTelemetryFactoryOption(o *factory) |
| 47 | +} |
| 48 | + |
| 49 | +var _ FactoryOption = (*factoryOptionFunc)(nil) |
| 50 | + |
| 51 | +// factoryOptionFunc is an FactoryOption created through a function. |
| 52 | +type factoryOptionFunc func(*factory) |
| 53 | + |
| 54 | +func (f factoryOptionFunc) applyTelemetryFactoryOption(o *factory) { |
| 55 | + f(o) |
| 56 | +} |
| 57 | + |
| 58 | +var _ Factory = (*factory)(nil) |
| 59 | + |
| 60 | +// factory is the implementation of Factory. |
| 61 | +type factory struct { |
| 62 | + createDefaultConfig component.CreateDefaultConfigFunc |
| 63 | + CreateLoggerFunc |
| 64 | + CreateTracerProviderFunc |
| 65 | +} |
| 66 | + |
| 67 | +func (f *factory) CreateDefaultConfig() component.Config { |
| 68 | + return f.createDefaultConfig() |
| 69 | +} |
| 70 | + |
| 71 | +// CreateLoggerFunc is the equivalent of Factory.CreateLogger. |
| 72 | +type CreateLoggerFunc func(context.Context, CreateSettings, component.Config) (*zap.Logger, error) |
| 73 | + |
| 74 | +// WithLogger overrides the default no-op logger. |
| 75 | +func WithLogger(createLogger CreateLoggerFunc) FactoryOption { |
| 76 | + return factoryOptionFunc(func(o *factory) { |
| 77 | + o.CreateLoggerFunc = createLogger |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +func (f *factory) CreateLogger(ctx context.Context, set CreateSettings, cfg component.Config) (*zap.Logger, error) { |
| 82 | + if f.CreateLoggerFunc == nil { |
| 83 | + return zap.NewNop(), nil |
| 84 | + } |
| 85 | + return f.CreateLoggerFunc(ctx, set, cfg) |
| 86 | +} |
| 87 | + |
| 88 | +// CreateTracerProviderFunc is the equivalent of Factory.CreateTracerProvider. |
| 89 | +type CreateTracerProviderFunc func(context.Context, CreateSettings, component.Config) (trace.TracerProvider, error) |
| 90 | + |
| 91 | +// WithTracerProvider overrides the default no-op tracer provider. |
| 92 | +func WithTracerProvider(createTracerProvider CreateTracerProviderFunc) FactoryOption { |
| 93 | + return factoryOptionFunc(func(o *factory) { |
| 94 | + o.CreateTracerProviderFunc = createTracerProvider |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +func (f *factory) CreateTracerProvider(ctx context.Context, set CreateSettings, cfg component.Config) (trace.TracerProvider, error) { |
| 99 | + if f.CreateTracerProviderFunc == nil { |
| 100 | + return tracenoop.NewTracerProvider(), nil |
| 101 | + } |
| 102 | + return f.CreateTracerProviderFunc(ctx, set, cfg) |
| 103 | +} |
| 104 | + |
| 105 | +func (f *factory) unexportedFactoryFunc() {} |
| 106 | + |
| 107 | +// NewFactory returns a new Factory. |
| 108 | +func NewFactory(createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory { |
| 109 | + f := &factory{ |
| 110 | + createDefaultConfig: createDefaultConfig, |
| 111 | + } |
| 112 | + for _, op := range options { |
| 113 | + op.applyTelemetryFactoryOption(f) |
| 114 | + } |
| 115 | + return f |
| 116 | +} |
0 commit comments