From b0e100da77c21c054abfd126cd0f0aa4fb9c3d9e Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Mon, 17 Jun 2024 08:38:17 -0700 Subject: [PATCH] [service] add internal factory method for meter provider This will be used when moving the meter provider configuration to use the otel-go config package. Part of #10414 Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- service/telemetry/internal/factory.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/service/telemetry/internal/factory.go b/service/telemetry/internal/factory.go index f1368c9704d..9c6d82bcc15 100644 --- a/service/telemetry/internal/factory.go +++ b/service/telemetry/internal/factory.go @@ -6,6 +6,8 @@ package internal // import "go.opentelemetry.io/collector/service/telemetry/inte import ( "context" + "go.opentelemetry.io/otel/metric" + metricnoop "go.opentelemetry.io/otel/metric/noop" "go.opentelemetry.io/otel/trace" tracenoop "go.opentelemetry.io/otel/trace/noop" "go.uber.org/zap" @@ -34,7 +36,8 @@ type Factory interface { // CreateTracerProvider creates a TracerProvider. CreateTracerProvider(ctx context.Context, set CreateSettings, cfg component.Config) (trace.TracerProvider, error) - // TODO: Add CreateMeterProvider. + // CreateMeterProvider creates a MeterProvider. + CreateMeterProvider(ctx context.Context, set CreateSettings, cfg component.Config) (metric.MeterProvider, error) // unexportedFactoryFunc is used to prevent external implementations of Factory. unexportedFactoryFunc() @@ -62,6 +65,7 @@ type factory struct { createDefaultConfig component.CreateDefaultConfigFunc CreateLoggerFunc CreateTracerProviderFunc + CreateMeterProviderFunc } func (f *factory) CreateDefaultConfig() component.Config { @@ -102,6 +106,21 @@ func (f *factory) CreateTracerProvider(ctx context.Context, set CreateSettings, return f.CreateTracerProviderFunc(ctx, set, cfg) } +type CreateMeterProviderFunc func(context.Context, CreateSettings, component.Config) (metric.MeterProvider, error) + +func WithMeterProvier(createMeterProvider CreateMeterProviderFunc) FactoryOption { + return factoryOptionFunc(func(o *factory) { + o.CreateMeterProviderFunc = createMeterProvider + }) +} + +func (f *factory) CreateMeterProvider(ctx context.Context, set CreateSettings, cfg component.Config) (metric.MeterProvider, error) { + if f.CreateMeterProviderFunc == nil { + return metricnoop.NewMeterProvider(), nil + } + return f.CreateMeterProviderFunc(ctx, set, cfg) +} + func (f *factory) unexportedFactoryFunc() {} // NewFactory returns a new Factory.