Skip to content

Commit

Permalink
[chore] Move back exporter definitions, make profile embed exporter (o…
Browse files Browse the repository at this point in the history
…pen-telemetry#11290)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Sep 27, 2024
1 parent 40396d5 commit 99cf16e
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 311 deletions.
156 changes: 142 additions & 14 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,188 @@
package exporter // import "go.opentelemetry.io/collector/exporter"

import (
"context"
"fmt"

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

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

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

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

// Settings configures exporter creators.
type Settings = internal.Settings
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
}

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

// CreateTracesExporter creates a TracesExporter based on this config.
// If the exporter type does not support tracing,
// this function returns the error [pipeline.ErrSignalNotSupported].
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,
// this function returns the error [pipeline.ErrSignalNotSupported].
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,
// this function returns the error [pipeline.ErrSignalNotSupported].
CreateLogsExporter(ctx context.Context, set Settings, cfg component.Config) (Logs, error)

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

unexportedFactoryFunc()
}

// FactoryOption apply changes to Factory.
type FactoryOption = internal.FactoryOption
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)
}

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

// CreateTracesExporter implements Factory.CreateTraces.
func (f CreateTracesFunc) CreateTracesExporter(ctx context.Context, set Settings, cfg component.Config) (Traces, error) {
if f == nil {
return nil, pipeline.ErrSignalNotSupported
}
return f(ctx, set, cfg)
}

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

// CreateMetricsExporter implements Factory.CreateMetrics.
func (f CreateMetricsFunc) CreateMetricsExporter(ctx context.Context, set Settings, cfg component.Config) (Metrics, error) {
if f == nil {
return nil, pipeline.ErrSignalNotSupported
}
return f(ctx, set, cfg)
}

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

// CreateLogsExporter implements Factory.CreateLogs.
func (f CreateLogsFunc) CreateLogsExporter(ctx context.Context, set Settings, cfg component.Config) (Logs, error) {
if f == nil {
return nil, pipeline.ErrSignalNotSupported
}
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
}

// 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 internal.WithTraces(createTraces, sl)
return factoryOptionFunc(func(o *factory) {
o.tracesStabilityLevel = sl
o.CreateTracesFunc = createTraces
})
}

// 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 internal.WithMetrics(createMetrics, sl)
return factoryOptionFunc(func(o *factory) {
o.metricsStabilityLevel = sl
o.CreateMetricsFunc = createMetrics
})
}

// 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 internal.WithLogs(createLogs, sl)
return factoryOptionFunc(func(o *factory) {
o.logsStabilityLevel = sl
o.CreateLogsFunc = createLogs
})
}

// NewFactory returns a Factory.
func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
return internal.NewFactory(cfgType, createDefaultConfig, options...)
f := &factory{
cfgType: cfgType,
CreateDefaultConfigFunc: createDefaultConfig,
}
for _, opt := range options {
opt.applyExporterFactoryOption(f)
}
return f
}

// MakeFactoryMap takes a list of factories and returns a map with Factory type as keys.
Expand Down
107 changes: 102 additions & 5 deletions exporter/exporterprofiles/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,115 @@
package exporterprofiles // import "go.opentelemetry.io/collector/exporter/exporterprofiles"

import (
"context"

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

// Profiles is an exporter that can consume profiles.
type Profiles = internal.Profiles
type Profiles interface {
component.Component
consumerprofiles.Profiles
}

type Factory interface {
exporter.Factory

// CreateProfilesExporter creates a ProfilesExporter based on this config.
// If the exporter type does not support tracing,
// this function returns the error [pipeline.ErrSignalNotSupported].
CreateProfilesExporter(ctx context.Context, set exporter.Settings, cfg component.Config) (Profiles, error)

// ProfilesExporterStability gets the stability level of the ProfilesExporter.
ProfilesExporterStability() component.StabilityLevel
}

// FactoryOption apply changes to ReceiverOptions.
type FactoryOption interface {
// applyOption applies the option.
applyOption(o *factoryOpts)
}

// factoryOptionFunc is an ReceiverFactoryOption created through a function.
type factoryOptionFunc func(*factoryOpts)

func (f factoryOptionFunc) applyOption(o *factoryOpts) {
f(o)
}

type factoryOpts struct {
cfgType component.Type
component.CreateDefaultConfigFunc
opts []exporter.FactoryOption
CreateProfilesFunc
profilesStabilityLevel component.StabilityLevel
}

// CreateProfilesFunc is the equivalent of Factory.CreateProfiles.
type CreateProfilesFunc = internal.CreateProfilesFunc
type CreateProfilesFunc func(context.Context, exporter.Settings, component.Config) (Profiles, error)

// CreateProfilesExporter implements Factory.CreateProfiles.
func (f CreateProfilesFunc) CreateProfilesExporter(ctx context.Context, set exporter.Settings, cfg component.Config) (Profiles, error) {
if f == nil {
return nil, pipeline.ErrSignalNotSupported
}
return f(ctx, set, cfg)
}

// WithTraces overrides the default "error not supported" implementation for CreateTraces and the default "undefined" stability level.
func WithTraces(createTraces exporter.CreateTracesFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factoryOpts) {
o.opts = append(o.opts, exporter.WithTraces(createTraces, sl))
})
}

// WithMetrics overrides the default "error not supported" implementation for CreateMetrics and the default "undefined" stability level.
func WithMetrics(createMetrics exporter.CreateMetricsFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factoryOpts) {
o.opts = append(o.opts, exporter.WithMetrics(createMetrics, sl))
})
}

// WithLogs overrides the default "error not supported" implementation for CreateLogs and the default "undefined" stability level.
func WithLogs(createLogs exporter.CreateLogsFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factoryOpts) {
o.opts = append(o.opts, exporter.WithLogs(createLogs, sl))
})
}

// WithProfiles overrides the default "error not supported" implementation for CreateProfilesExporter and the default "undefined" stability level.
func WithProfiles(createProfiles CreateProfilesFunc, sl component.StabilityLevel) exporter.FactoryOption {
return internal.WithProfiles(createProfiles, sl)
func WithProfiles(createProfiles CreateProfilesFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factoryOpts) {
o.profilesStabilityLevel = sl
o.CreateProfilesFunc = createProfiles
})
}

type factory struct {
exporter.Factory
CreateProfilesFunc
profilesStabilityLevel component.StabilityLevel
}

func (f *factory) ProfilesExporterStability() component.StabilityLevel {
return f.profilesStabilityLevel
}

// NewFactory returns a Factory.
func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
opts := factoryOpts{
cfgType: cfgType,
CreateDefaultConfigFunc: createDefaultConfig,
}
for _, opt := range options {
opt.applyOption(&opts)
}
return &factory{
Factory: exporter.NewFactory(opts.cfgType, opts.CreateDefaultConfig, opts.opts...),
CreateProfilesFunc: opts.CreateProfilesFunc,
profilesStabilityLevel: opts.profilesStabilityLevel,
}
}
2 changes: 1 addition & 1 deletion exporter/exporterprofiles/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func TestNewFactoryWithProfiles(t *testing.T) {
var testType = component.MustNewType("test")
defaultCfg := struct{}{}
factory := exporter.NewFactory(
factory := NewFactory(
testType,
func() component.Config { return &defaultCfg },
WithProfiles(createProfiles, component.StabilityLevelDevelopment),
Expand Down
4 changes: 2 additions & 2 deletions exporter/exporterprofiles/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ go 1.22.0
require (
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/component v0.110.0
go.opentelemetry.io/collector/consumer/consumerprofiles v0.110.0
go.opentelemetry.io/collector/consumer/consumertest v0.110.0
go.opentelemetry.io/collector/exporter v0.110.0
go.opentelemetry.io/collector/pipeline v0.110.0
)

require (
Expand All @@ -18,11 +20,9 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.110.0 // indirect
go.opentelemetry.io/collector/consumer v0.110.0 // indirect
go.opentelemetry.io/collector/consumer/consumerprofiles v0.110.0 // indirect
go.opentelemetry.io/collector/internal/globalsignal v0.110.0 // indirect
go.opentelemetry.io/collector/pdata v1.16.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.110.0 // indirect
go.opentelemetry.io/collector/pipeline v0.110.0 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions exporter/exportertest/nop_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func NewNopSettings() exporter.Settings {

// NewNopFactory returns an exporter.Factory that constructs nop exporters.
func NewNopFactory() exporter.Factory {
return exporter.NewFactory(
return exporterprofiles.NewFactory(
nopType,
func() component.Config { return &nopConfig{} },
exporter.WithTraces(createTracesExporter, component.StabilityLevelStable),
exporter.WithMetrics(createMetricsExporter, component.StabilityLevelStable),
exporter.WithLogs(createLogsExporter, component.StabilityLevelStable),
exporterprofiles.WithTraces(createTracesExporter, component.StabilityLevelStable),
exporterprofiles.WithMetrics(createMetricsExporter, component.StabilityLevelStable),
exporterprofiles.WithLogs(createLogsExporter, component.StabilityLevelStable),
exporterprofiles.WithProfiles(createProfilesExporter, component.StabilityLevelAlpha),
)
}
Expand Down
3 changes: 2 additions & 1 deletion exporter/exportertest/nop_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/exporter/exporterprofiles"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pprofile"
Expand Down Expand Up @@ -43,7 +44,7 @@ func TestNewNopFactory(t *testing.T) {
assert.NoError(t, logs.ConsumeLogs(context.Background(), plog.NewLogs()))
assert.NoError(t, logs.Shutdown(context.Background()))

profiles, err := factory.CreateProfilesExporter(context.Background(), NewNopSettings(), cfg)
profiles, err := factory.(exporterprofiles.Factory).CreateProfilesExporter(context.Background(), NewNopSettings(), cfg)
require.NoError(t, err)
assert.NoError(t, profiles.Start(context.Background(), componenttest.NewNopHost()))
assert.NoError(t, profiles.ConsumeProfiles(context.Background(), pprofile.NewProfiles()))
Expand Down
Loading

0 comments on commit 99cf16e

Please sign in to comment.