Skip to content

Commit

Permalink
[chore] Remove use of alias types in service/telemetry (#11182)
Browse files Browse the repository at this point in the history
This PR moves internal definitions that were aliased publicly to the
public package, and makes sure that hides any other type so that the API
surface is the same.

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
bogdandrutu and codeboten authored Sep 20, 2024
1 parent 07c3e17 commit 7253ab8
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 182 deletions.
3 changes: 1 addition & 2 deletions service/telemetry/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/service/telemetry/internal"
)

func TestAttributes(t *testing.T) {
Expand Down Expand Up @@ -50,7 +49,7 @@ func TestAttributes(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
attrs := attributes(internal.Settings{BuildInfo: tt.buildInfo}, tt.cfg)
attrs := attributes(Settings{BuildInfo: tt.buildInfo}, tt.cfg)
require.Equal(t, tt.wantAttributes, attrs)
})
}
Expand Down
84 changes: 54 additions & 30 deletions service/telemetry/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/service/internal/resource"
"go.opentelemetry.io/collector/service/telemetry/internal"
)

// disableHighCardinalityMetricsfeatureGate is the feature gate that controls whether the collector should enable
Expand All @@ -27,46 +26,46 @@ var disableHighCardinalityMetricsfeatureGate = featuregate.GlobalRegistry().Must
featuregate.WithRegisterDescription("controls whether the collector should enable potentially high"+
"cardinality metrics. The gate will be removed when the collector allows for view configuration."))

func createDefaultConfig() component.Config {
return &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Development: false,
Encoding: "console",
Sampling: &LogsSamplingConfig{
Enabled: true,
Tick: 10 * time.Second,
Initial: 10,
Thereafter: 100,
},
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
DisableCaller: false,
DisableStacktrace: false,
InitialFields: map[string]any(nil),
},
Metrics: MetricsConfig{
Level: configtelemetry.LevelNormal,
Address: ":8888",
},
}
// Settings holds configuration for building Telemetry.
type Settings struct {
BuildInfo component.BuildInfo
AsyncErrorChannel chan error
ZapOptions []zap.Option
}

// Factory is a telemetry factory.
type Factory = internal.Factory
// Factory is factory interface for telemetry.
// This interface cannot be directly implemented. Implementations must
// use the NewFactory to implement it.
type Factory interface {
// CreateDefaultConfig creates the default configuration for the telemetry.
// TODO: Should we just inherit from component.Factory?
CreateDefaultConfig() component.Config

// CreateLogger creates a logger.
CreateLogger(ctx context.Context, set Settings, cfg component.Config) (*zap.Logger, error)

// CreateTracerProvider creates a TracerProvider.
CreateTracerProvider(ctx context.Context, set Settings, cfg component.Config) (trace.TracerProvider, error)

// CreateMeterProvider creates a MeterProvider.
CreateMeterProvider(ctx context.Context, set Settings, cfg component.Config) (metric.MeterProvider, error)

// unexportedFactoryFunc is used to prevent external implementations of Factory.
unexportedFactoryFunc()
}

// NewFactory creates a new Factory.
func NewFactory() Factory {
return internal.NewFactory(createDefaultConfig,
internal.WithLogger(func(_ context.Context, set Settings, cfg component.Config) (*zap.Logger, error) {
return newFactory(createDefaultConfig,
withLogger(func(_ context.Context, set Settings, cfg component.Config) (*zap.Logger, error) {
c := *cfg.(*Config)
return newLogger(c.Logs, set.ZapOptions)
}),
internal.WithTracerProvider(func(ctx context.Context, set Settings, cfg component.Config) (trace.TracerProvider, error) {
withTracerProvider(func(ctx context.Context, set Settings, cfg component.Config) (trace.TracerProvider, error) {
c := *cfg.(*Config)
return newTracerProvider(ctx, set, c)
}),
internal.WithMeterProvider(func(_ context.Context, set Settings, cfg component.Config) (metric.MeterProvider, error) {
withMeterProvider(func(_ context.Context, set Settings, cfg component.Config) (metric.MeterProvider, error) {
c := *cfg.(*Config)
disableHighCard := disableHighCardinalityMetricsfeatureGate.IsEnabled()
return newMeterProvider(
Expand All @@ -80,3 +79,28 @@ func NewFactory() Factory {
}),
)
}

func createDefaultConfig() component.Config {
return &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Development: false,
Encoding: "console",
Sampling: &LogsSamplingConfig{
Enabled: true,
Tick: 10 * time.Second,
Initial: 10,
Thereafter: 100,
},
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
DisableCaller: false,
DisableStacktrace: false,
InitialFields: map[string]any(nil),
},
Metrics: MetricsConfig{
Level: configtelemetry.LevelNormal,
Address: ":8888",
},
}
}
109 changes: 109 additions & 0 deletions service/telemetry/factory_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package telemetry // import "go.opentelemetry.io/collector/service/telemetry"

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"

"go.opentelemetry.io/collector/component"
)

// factoryOption apply changes to Factory.
type factoryOption interface {
// applyTelemetryFactoryOption applies the option.
applyTelemetryFactoryOption(o *factory)
}

var _ factoryOption = (*factoryOptionFunc)(nil)

// factoryOptionFunc is an factoryOption created through a function.
type factoryOptionFunc func(*factory)

func (f factoryOptionFunc) applyTelemetryFactoryOption(o *factory) {
f(o)
}

var _ Factory = (*factory)(nil)

// Factory is the implementation of Factory.
type factory struct {
createDefaultConfig component.CreateDefaultConfigFunc
createLoggerFunc
createTracerProviderFunc
createMeterProviderFunc
}

func (f *factory) CreateDefaultConfig() component.Config {
return f.createDefaultConfig()
}

// createLoggerFunc is the equivalent of Factory.CreateLogger.
type createLoggerFunc func(context.Context, Settings, component.Config) (*zap.Logger, error)

// withLogger overrides the default no-op logger.
func withLogger(createLogger createLoggerFunc) factoryOption {
return factoryOptionFunc(func(o *factory) {
o.createLoggerFunc = createLogger
})
}

func (f *factory) CreateLogger(ctx context.Context, set Settings, cfg component.Config) (*zap.Logger, error) {
if f.createLoggerFunc == nil {
return zap.NewNop(), nil
}
return f.createLoggerFunc(ctx, set, cfg)
}

// createTracerProviderFunc is the equivalent of Factory.CreateTracerProvider.
type createTracerProviderFunc func(context.Context, Settings, component.Config) (trace.TracerProvider, error)

// withTracerProvider overrides the default no-op tracer provider.
func withTracerProvider(createTracerProvider createTracerProviderFunc) factoryOption {
return factoryOptionFunc(func(o *factory) {
o.createTracerProviderFunc = createTracerProvider
})
}

func (f *factory) CreateTracerProvider(ctx context.Context, set Settings, cfg component.Config) (trace.TracerProvider, error) {
if f.createTracerProviderFunc == nil {
return tracenoop.NewTracerProvider(), nil
}
return f.createTracerProviderFunc(ctx, set, cfg)
}

// createMeterProviderFunc is the equivalent of Factory.CreateMeterProvider.
type createMeterProviderFunc func(context.Context, Settings, component.Config) (metric.MeterProvider, error)

// withMeterProvider overrides the default no-op meter provider.
func withMeterProvider(createMeterProvider createMeterProviderFunc) factoryOption {
return factoryOptionFunc(func(o *factory) {
o.createMeterProviderFunc = createMeterProvider
})
}

func (f *factory) CreateMeterProvider(ctx context.Context, set Settings, 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.
func newFactory(createDefaultConfig component.CreateDefaultConfigFunc, options ...factoryOption) Factory {
f := &factory{
createDefaultConfig: createDefaultConfig,
}
for _, op := range options {
op.applyTelemetryFactoryOption(f)
}
return f
}
File renamed without changes.
137 changes: 0 additions & 137 deletions service/telemetry/internal/factory.go

This file was deleted.

11 changes: 0 additions & 11 deletions service/telemetry/telemetry.go

This file was deleted.

Loading

0 comments on commit 7253ab8

Please sign in to comment.