Skip to content

Commit

Permalink
move exporter into an internal package, in preparation for profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed Jul 3, 2024
1 parent 9405cfe commit 21cd388
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 218 deletions.
87 changes: 87 additions & 0 deletions exporter/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package exporter // import "go.opentelemetry.io/collector/exporter"

import (
"context"
"fmt"

"go.uber.org/zap"

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

// Builder exporter is a helper struct that given a set of Configs and Factories helps with creating exporters.
type Builder struct {
cfgs map[component.ID]component.Config
factories map[component.Type]Factory
}

// NewBuilder creates a new exporter.Builder to help with creating components form a set of configs and factories.
func NewBuilder(cfgs map[component.ID]component.Config, factories map[component.Type]Factory) *Builder {
return &Builder{cfgs: cfgs, factories: factories}
}

// CreateTraces creates a Traces exporter based on the settings and config.
func (b *Builder) CreateTraces(ctx context.Context, set Settings) (Traces, error) {
cfg, existsCfg := b.cfgs[set.ID]
if !existsCfg {
return nil, fmt.Errorf("exporter %q is not configured", set.ID)
}

f, existsFactory := b.factories[set.ID.Type()]
if !existsFactory {
return nil, fmt.Errorf("exporter factory not available for: %q", set.ID)
}

logStabilityLevel(set.Logger, f.TracesExporterStability())
return f.CreateTracesExporter(ctx, set, cfg)
}

// CreateMetrics creates a Metrics exporter based on the settings and config.
func (b *Builder) CreateMetrics(ctx context.Context, set Settings) (Metrics, error) {
cfg, existsCfg := b.cfgs[set.ID]
if !existsCfg {
return nil, fmt.Errorf("exporter %q is not configured", set.ID)
}

f, existsFactory := b.factories[set.ID.Type()]
if !existsFactory {
return nil, fmt.Errorf("exporter factory not available for: %q", set.ID)
}

logStabilityLevel(set.Logger, f.MetricsExporterStability())
return f.CreateMetricsExporter(ctx, set, cfg)
}

// CreateLogs creates a Logs exporter based on the settings and config.
func (b *Builder) CreateLogs(ctx context.Context, set Settings) (Logs, error) {
cfg, existsCfg := b.cfgs[set.ID]
if !existsCfg {
return nil, fmt.Errorf("exporter %q is not configured", set.ID)
}

f, existsFactory := b.factories[set.ID.Type()]
if !existsFactory {
return nil, fmt.Errorf("exporter factory not available for: %q", set.ID)
}

logStabilityLevel(set.Logger, f.LogsExporterStability())
return f.CreateLogsExporter(ctx, set, cfg)
}

func (b *Builder) Factory(componentType component.Type) component.Factory {
return b.factories[componentType]
}

// logStabilityLevel logs the stability level of a component. The log level is set to info for
// undefined, unmaintained, deprecated and development. The log level is set to debug
// for alpha, beta and stable.
func logStabilityLevel(logger *zap.Logger, sl component.StabilityLevel) {
if sl >= component.StabilityLevelAlpha {
logger.Debug(sl.LogMessage())
} else {
logger.Info(sl.LogMessage())
}
}
233 changes: 15 additions & 218 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,194 +4,65 @@
package exporter // import "go.opentelemetry.io/collector/exporter"

import (
"context"
"fmt"

"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter/internal"
)

// Traces is an exporter that can consume traces.
type Traces interface {
component.Component
consumer.Traces
}
type Traces = internal.Traces

// Metrics is an exporter that can consume metrics.
type Metrics interface {
component.Component
consumer.Metrics
}
type Metrics = internal.Metrics

// Logs is an exporter that can consume logs.
type Logs interface {
component.Component
consumer.Logs
}
type Logs = internal.Logs

// CreateSettings configures Exporter creators.
//
// Deprecated: [v0.103.0] Use exporter.Settings instead.
type CreateSettings = Settings
type CreateSettings = internal.Settings

// Settings configures exporter creators.
type Settings struct {
// ID returns the ID of the component that will be created.
ID component.ID

component.TelemetrySettings

// BuildInfo can be used by components for informational purposes
BuildInfo component.BuildInfo
}
type Settings = internal.Settings

// Factory is factory interface for exporters.
//
// This interface cannot be directly implemented. Implementations must
// use the NewFactory to implement it.
type Factory interface {
component.Factory

// CreateTracesExporter creates a TracesExporter based on this config.
// If the exporter type does not support tracing or if the config is not valid,
// an error will be returned instead.
CreateTracesExporter(ctx context.Context, set Settings, cfg component.Config) (Traces, error)

// TracesExporterStability gets the stability level of the TracesExporter.
TracesExporterStability() component.StabilityLevel

// CreateMetricsExporter creates a MetricsExporter based on this config.
// If the exporter type does not support metrics or if the config is not valid,
// an error will be returned instead.
CreateMetricsExporter(ctx context.Context, set Settings, cfg component.Config) (Metrics, error)

// MetricsExporterStability gets the stability level of the MetricsExporter.
MetricsExporterStability() component.StabilityLevel

// CreateLogsExporter creates a LogsExporter based on the config.
// If the exporter type does not support logs or if the config is not valid,
// an error will be returned instead.
CreateLogsExporter(ctx context.Context, set Settings, cfg component.Config) (Logs, error)

// LogsExporterStability gets the stability level of the LogsExporter.
LogsExporterStability() component.StabilityLevel

unexportedFactoryFunc()
}
type Factory = internal.Internal

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

var _ FactoryOption = (*factoryOptionFunc)(nil)

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

func (f factoryOptionFunc) applyExporterFactoryOption(o *factory) {
f(o)
}
type FactoryOption = internal.FactoryOption

// CreateTracesFunc is the equivalent of Factory.CreateTraces.
type CreateTracesFunc func(context.Context, Settings, component.Config) (Traces, error)

// CreateTracesExporter implements ExporterFactory.CreateTracesExporter().
func (f CreateTracesFunc) CreateTracesExporter(ctx context.Context, set Settings, cfg component.Config) (Traces, error) {
if f == nil {
return nil, component.ErrDataTypeIsNotSupported
}
return f(ctx, set, cfg)
}
type CreateTracesFunc = internal.CreateTracesFunc

// CreateMetricsFunc is the equivalent of Factory.CreateMetrics.
type CreateMetricsFunc func(context.Context, Settings, component.Config) (Metrics, error)

// CreateMetricsExporter implements ExporterFactory.CreateMetricsExporter().
func (f CreateMetricsFunc) CreateMetricsExporter(ctx context.Context, set Settings, cfg component.Config) (Metrics, error) {
if f == nil {
return nil, component.ErrDataTypeIsNotSupported
}
return f(ctx, set, cfg)
}
type CreateMetricsFunc = internal.CreateMetricsFunc

// CreateLogsFunc is the equivalent of Factory.CreateLogs.
type CreateLogsFunc func(context.Context, Settings, component.Config) (Logs, error)

// CreateLogsExporter implements Factory.CreateLogsExporter().
func (f CreateLogsFunc) CreateLogsExporter(ctx context.Context, set Settings, cfg component.Config) (Logs, error) {
if f == nil {
return nil, component.ErrDataTypeIsNotSupported
}
return f(ctx, set, cfg)
}

type factory struct {
cfgType component.Type
component.CreateDefaultConfigFunc
CreateTracesFunc
tracesStabilityLevel component.StabilityLevel
CreateMetricsFunc
metricsStabilityLevel component.StabilityLevel
CreateLogsFunc
logsStabilityLevel component.StabilityLevel
}

func (f *factory) Type() component.Type {
return f.cfgType
}

func (f *factory) unexportedFactoryFunc() {}

func (f *factory) TracesExporterStability() component.StabilityLevel {
return f.tracesStabilityLevel
}

func (f *factory) MetricsExporterStability() component.StabilityLevel {
return f.metricsStabilityLevel
}

func (f *factory) LogsExporterStability() component.StabilityLevel {
return f.logsStabilityLevel
}
type CreateLogsFunc = internal.CreateLogsFunc

// WithTraces overrides the default "error not supported" implementation for CreateTracesExporter and the default "undefined" stability level.
func WithTraces(createTraces CreateTracesFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factory) {
o.tracesStabilityLevel = sl
o.CreateTracesFunc = createTraces
})
return internal.WithTraces(createTraces, sl)
}

// WithMetrics overrides the default "error not supported" implementation for CreateMetricsExporter and the default "undefined" stability level.
func WithMetrics(createMetrics CreateMetricsFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factory) {
o.metricsStabilityLevel = sl
o.CreateMetricsFunc = createMetrics
})
return internal.WithMetrics(createMetrics, sl)
}

// WithLogs overrides the default "error not supported" implementation for CreateLogsExporter and the default "undefined" stability level.
func WithLogs(createLogs CreateLogsFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factory) {
o.logsStabilityLevel = sl
o.CreateLogsFunc = createLogs
})
return internal.WithLogs(createLogs, sl)
}

// 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.applyExporterFactoryOption(f)
}
return f
return internal.NewFactory(cfgType, createDefaultConfig, options...)
}

// MakeFactoryMap takes a list of factories and returns a map with Factory type as keys.
Expand All @@ -206,77 +77,3 @@ func MakeFactoryMap(factories ...Factory) (map[component.Type]Factory, error) {
}
return fMap, nil
}

// Builder exporter is a helper struct that given a set of Configs and Factories helps with creating exporters.
type Builder struct {
cfgs map[component.ID]component.Config
factories map[component.Type]Factory
}

// NewBuilder creates a new exporter.Builder to help with creating components form a set of configs and factories.
func NewBuilder(cfgs map[component.ID]component.Config, factories map[component.Type]Factory) *Builder {
return &Builder{cfgs: cfgs, factories: factories}
}

// CreateTraces creates a Traces exporter based on the settings and config.
func (b *Builder) CreateTraces(ctx context.Context, set Settings) (Traces, error) {
cfg, existsCfg := b.cfgs[set.ID]
if !existsCfg {
return nil, fmt.Errorf("exporter %q is not configured", set.ID)
}

f, existsFactory := b.factories[set.ID.Type()]
if !existsFactory {
return nil, fmt.Errorf("exporter factory not available for: %q", set.ID)
}

logStabilityLevel(set.Logger, f.TracesExporterStability())
return f.CreateTracesExporter(ctx, set, cfg)
}

// CreateMetrics creates a Metrics exporter based on the settings and config.
func (b *Builder) CreateMetrics(ctx context.Context, set Settings) (Metrics, error) {
cfg, existsCfg := b.cfgs[set.ID]
if !existsCfg {
return nil, fmt.Errorf("exporter %q is not configured", set.ID)
}

f, existsFactory := b.factories[set.ID.Type()]
if !existsFactory {
return nil, fmt.Errorf("exporter factory not available for: %q", set.ID)
}

logStabilityLevel(set.Logger, f.MetricsExporterStability())
return f.CreateMetricsExporter(ctx, set, cfg)
}

// CreateLogs creates a Logs exporter based on the settings and config.
func (b *Builder) CreateLogs(ctx context.Context, set Settings) (Logs, error) {
cfg, existsCfg := b.cfgs[set.ID]
if !existsCfg {
return nil, fmt.Errorf("exporter %q is not configured", set.ID)
}

f, existsFactory := b.factories[set.ID.Type()]
if !existsFactory {
return nil, fmt.Errorf("exporter factory not available for: %q", set.ID)
}

logStabilityLevel(set.Logger, f.LogsExporterStability())
return f.CreateLogsExporter(ctx, set, cfg)
}

func (b *Builder) Factory(componentType component.Type) component.Factory {
return b.factories[componentType]
}

// logStabilityLevel logs the stability level of a component. The log level is set to info for
// undefined, unmaintained, deprecated and development. The log level is set to debug
// for alpha, beta and stable.
func logStabilityLevel(logger *zap.Logger, sl component.StabilityLevel) {
if sl >= component.StabilityLevelAlpha {
logger.Debug(sl.LogMessage())
} else {
logger.Info(sl.LogMessage())
}
}
15 changes: 15 additions & 0 deletions exporter/internal/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// copyright the opentelemetry authors
// spdx-license-identifier: apache-2.0

package internal // import "go.opentelemetry.io/collector/exporter/internal"

import "go.opentelemetry.io/collector/component" // Settings configures exporter creators.
type Settings struct {
// ID returns the ID of the component that will be created.
ID component.ID

component.TelemetrySettings

// BuildInfo can be used by components for informational purposes
BuildInfo component.BuildInfo
}
Loading

0 comments on commit 21cd388

Please sign in to comment.