Move everything from exporterhelper to component. (#4899)
Updates https://github.com/open-telemetry/opentelemetry-collector/issues/4681 Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
This commit is contained in:
parent
95262b5f0d
commit
83c6ed1366
|
@ -18,6 +18,12 @@
|
|||
- Deprecated `processorhelper.WithMetrics` in favour of `component.WithMetricsProcessor`
|
||||
- Deprecated `processorhelper.WithLogs` in favour of `component.WithLogsProcessor`
|
||||
- Deprecated `processorhelper.NewFactory` in favour of `component.NewProcessorFactory`
|
||||
- Move helpers from exporterhelper to component (#4899)
|
||||
- Deprecated `exporterhelper.CreateDefaultConfig` in favour of `component.ExporterDefaultConfigFunc`
|
||||
- Deprecated `exporterhelper.WithTraces` in favour of `component.WithTracesExporter`
|
||||
- Deprecated `exporterhelper.WithMetrics` in favour of `component.WithMetricsExporter`
|
||||
- Deprecated `exporterhelper.WithLogs` in favour of `component.WithLogsExporter`
|
||||
- Deprecated `exporterhelper.NewFactory` in favour of `component.NewExporterFactory`
|
||||
|
||||
### 💡 Enhancements 💡
|
||||
|
||||
|
|
|
@ -20,7 +20,6 @@ import (
|
|||
"go.opentelemetry.io/collector/component"
|
||||
"go.opentelemetry.io/collector/config"
|
||||
"go.opentelemetry.io/collector/consumer/consumertest"
|
||||
"go.opentelemetry.io/collector/internal/internalinterface"
|
||||
)
|
||||
|
||||
// NewNopExporterCreateSettings returns a new nop settings for Create*Exporter functions.
|
||||
|
@ -35,54 +34,31 @@ type nopExporterConfig struct {
|
|||
config.ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
|
||||
}
|
||||
|
||||
// nopExporterFactory is factory for nopExporter.
|
||||
type nopExporterFactory struct {
|
||||
internalinterface.BaseInternal
|
||||
}
|
||||
|
||||
var nopExporterFactoryInstance = &nopExporterFactory{}
|
||||
var nopExporterFactory = component.NewExporterFactory(
|
||||
"nop",
|
||||
func() config.Exporter {
|
||||
return &nopExporterConfig{
|
||||
ExporterSettings: config.NewExporterSettings(config.NewComponentID("nop")),
|
||||
}
|
||||
},
|
||||
component.WithTracesExporter(createTracesExporter),
|
||||
component.WithMetricsExporter(createMetricsExporter),
|
||||
component.WithLogsExporter(createLogsExporter))
|
||||
|
||||
// NewNopExporterFactory returns a component.ExporterFactory that constructs nop exporters.
|
||||
func NewNopExporterFactory() component.ExporterFactory {
|
||||
return nopExporterFactoryInstance
|
||||
return nopExporterFactory
|
||||
}
|
||||
|
||||
// Type gets the type of the Exporter config created by this factory.
|
||||
func (f *nopExporterFactory) Type() config.Type {
|
||||
return "nop"
|
||||
}
|
||||
|
||||
// CreateDefaultConfig creates the default configuration for the Exporter.
|
||||
func (f *nopExporterFactory) CreateDefaultConfig() config.Exporter {
|
||||
return &nopExporterConfig{
|
||||
ExporterSettings: config.NewExporterSettings(config.NewComponentID("nop")),
|
||||
}
|
||||
}
|
||||
|
||||
// CreateTracesExporter implements component.ExporterFactory interface.
|
||||
func (f *nopExporterFactory) CreateTracesExporter(
|
||||
_ context.Context,
|
||||
_ component.ExporterCreateSettings,
|
||||
_ config.Exporter,
|
||||
) (component.TracesExporter, error) {
|
||||
func createTracesExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.TracesExporter, error) {
|
||||
return nopExporterInstance, nil
|
||||
}
|
||||
|
||||
// CreateMetricsExporter implements component.ExporterFactory interface.
|
||||
func (f *nopExporterFactory) CreateMetricsExporter(
|
||||
_ context.Context,
|
||||
_ component.ExporterCreateSettings,
|
||||
_ config.Exporter,
|
||||
) (component.MetricsExporter, error) {
|
||||
func createMetricsExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.MetricsExporter, error) {
|
||||
return nopExporterInstance, nil
|
||||
}
|
||||
|
||||
// CreateLogsExporter implements component.ExporterFactory interface.
|
||||
func (f *nopExporterFactory) CreateLogsExporter(
|
||||
_ context.Context,
|
||||
_ component.ExporterCreateSettings,
|
||||
_ config.Exporter,
|
||||
) (component.LogsExporter, error) {
|
||||
func createLogsExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.LogsExporter, error) {
|
||||
return nopExporterInstance, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -17,8 +17,10 @@ package component // import "go.opentelemetry.io/collector/component"
|
|||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/collector/component/componenterror"
|
||||
"go.opentelemetry.io/collector/config"
|
||||
"go.opentelemetry.io/collector/consumer"
|
||||
"go.opentelemetry.io/collector/internal/internalinterface"
|
||||
)
|
||||
|
||||
// Exporter exports telemetry data from the collector to a destination.
|
||||
|
@ -52,11 +54,10 @@ type ExporterCreateSettings struct {
|
|||
BuildInfo BuildInfo
|
||||
}
|
||||
|
||||
// ExporterFactory can create MetricsExporter, TracesExporter and
|
||||
// LogsExporter. This is the new preferred factory type to create exporters.
|
||||
// ExporterFactory is factory interface for exporters.
|
||||
//
|
||||
// This interface cannot be directly implemented. Implementations must
|
||||
// use the exporterhelper.NewFactory to implement it.
|
||||
// use the NewExporterFactory to implement it.
|
||||
type ExporterFactory interface {
|
||||
Factory
|
||||
|
||||
|
@ -72,18 +73,106 @@ type ExporterFactory interface {
|
|||
// CreateTracesExporter creates a trace exporter 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 ExporterCreateSettings,
|
||||
cfg config.Exporter) (TracesExporter, error)
|
||||
CreateTracesExporter(ctx context.Context, set ExporterCreateSettings, cfg config.Exporter) (TracesExporter, error)
|
||||
|
||||
// CreateMetricsExporter creates a metrics exporter 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 ExporterCreateSettings,
|
||||
cfg config.Exporter) (MetricsExporter, error)
|
||||
CreateMetricsExporter(ctx context.Context, set ExporterCreateSettings, cfg config.Exporter) (MetricsExporter, error)
|
||||
|
||||
// CreateLogsExporter creates an exporter 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 ExporterCreateSettings,
|
||||
cfg config.Exporter) (LogsExporter, error)
|
||||
CreateLogsExporter(ctx context.Context, set ExporterCreateSettings, cfg config.Exporter) (LogsExporter, error)
|
||||
}
|
||||
|
||||
// ExporterFactoryOption apply changes to ExporterOptions.
|
||||
type ExporterFactoryOption func(o *exporterFactory)
|
||||
|
||||
// ExporterCreateDefaultConfigFunc is the equivalent of ExporterFactory.CreateDefaultConfig().
|
||||
type ExporterCreateDefaultConfigFunc func() config.Exporter
|
||||
|
||||
// CreateDefaultConfig implements ExporterFactory.CreateDefaultConfig().
|
||||
func (f ExporterCreateDefaultConfigFunc) CreateDefaultConfig() config.Exporter {
|
||||
return f()
|
||||
}
|
||||
|
||||
// CreateTracesExporterFunc is the equivalent of ExporterFactory.CreateTracesExporter().
|
||||
type CreateTracesExporterFunc func(context.Context, ExporterCreateSettings, config.Exporter) (TracesExporter, error)
|
||||
|
||||
// CreateTracesExporter implements ExporterFactory.CreateTracesExporter().
|
||||
func (f CreateTracesExporterFunc) CreateTracesExporter(ctx context.Context, set ExporterCreateSettings, cfg config.Exporter) (TracesExporter, error) {
|
||||
if f == nil {
|
||||
return nil, componenterror.ErrDataTypeIsNotSupported
|
||||
}
|
||||
return f(ctx, set, cfg)
|
||||
}
|
||||
|
||||
// CreateMetricsExporterFunc is the equivalent of ExporterFactory.CreateMetricsExporter().
|
||||
type CreateMetricsExporterFunc func(context.Context, ExporterCreateSettings, config.Exporter) (MetricsExporter, error)
|
||||
|
||||
// CreateMetricsExporter implements ExporterFactory.CreateMetricsExporter().
|
||||
func (f CreateMetricsExporterFunc) CreateMetricsExporter(ctx context.Context, set ExporterCreateSettings, cfg config.Exporter) (MetricsExporter, error) {
|
||||
if f == nil {
|
||||
return nil, componenterror.ErrDataTypeIsNotSupported
|
||||
}
|
||||
return f(ctx, set, cfg)
|
||||
}
|
||||
|
||||
// CreateLogsExporterFunc is the equivalent of ExporterFactory.CreateLogsExporter().
|
||||
type CreateLogsExporterFunc func(context.Context, ExporterCreateSettings, config.Exporter) (LogsExporter, error)
|
||||
|
||||
// CreateLogsExporter implements ExporterFactory.CreateLogsExporter().
|
||||
func (f CreateLogsExporterFunc) CreateLogsExporter(ctx context.Context, set ExporterCreateSettings, cfg config.Exporter) (LogsExporter, error) {
|
||||
if f == nil {
|
||||
return nil, componenterror.ErrDataTypeIsNotSupported
|
||||
}
|
||||
return f(ctx, set, cfg)
|
||||
}
|
||||
|
||||
type exporterFactory struct {
|
||||
internalinterface.BaseInternal
|
||||
cfgType config.Type
|
||||
ExporterCreateDefaultConfigFunc
|
||||
CreateTracesExporterFunc
|
||||
CreateMetricsExporterFunc
|
||||
CreateLogsExporterFunc
|
||||
}
|
||||
|
||||
// WithTracesExporter overrides the default "error not supported" implementation for CreateTracesExporter.
|
||||
func WithTracesExporter(createTracesExporter CreateTracesExporterFunc) ExporterFactoryOption {
|
||||
return func(o *exporterFactory) {
|
||||
o.CreateTracesExporterFunc = createTracesExporter
|
||||
}
|
||||
}
|
||||
|
||||
// WithMetricsExporter overrides the default "error not supported" implementation for CreateMetricsExporter.
|
||||
func WithMetricsExporter(createMetricsExporter CreateMetricsExporterFunc) ExporterFactoryOption {
|
||||
return func(o *exporterFactory) {
|
||||
o.CreateMetricsExporterFunc = createMetricsExporter
|
||||
}
|
||||
}
|
||||
|
||||
// WithLogsExporter overrides the default "error not supported" implementation for CreateLogsExporter.
|
||||
func WithLogsExporter(createLogsExporter CreateLogsExporterFunc) ExporterFactoryOption {
|
||||
return func(o *exporterFactory) {
|
||||
o.CreateLogsExporterFunc = createLogsExporter
|
||||
}
|
||||
}
|
||||
|
||||
// NewExporterFactory returns a ExporterFactory.
|
||||
func NewExporterFactory(cfgType config.Type, createDefaultConfig ExporterCreateDefaultConfigFunc, options ...ExporterFactoryOption) ExporterFactory {
|
||||
f := &exporterFactory{
|
||||
cfgType: cfgType,
|
||||
ExporterCreateDefaultConfigFunc: createDefaultConfig,
|
||||
}
|
||||
for _, opt := range options {
|
||||
opt(f)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// Type returns the type of the Exporter created by this ExporterFactory.
|
||||
func (f *exporterFactory) Type() config.Type {
|
||||
return f.cfgType
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package component
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -22,48 +23,52 @@ import (
|
|||
"go.opentelemetry.io/collector/config"
|
||||
)
|
||||
|
||||
type TestExporterFactory struct {
|
||||
ExporterFactory
|
||||
name string
|
||||
func TestNewExporterFactory(t *testing.T) {
|
||||
const typeStr = "test"
|
||||
defaultCfg := config.NewExporterSettings(config.NewComponentID(typeStr))
|
||||
factory := NewExporterFactory(
|
||||
typeStr,
|
||||
func() config.Exporter { return &defaultCfg })
|
||||
assert.EqualValues(t, typeStr, factory.Type())
|
||||
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
|
||||
_, err := factory.CreateTracesExporter(context.Background(), ExporterCreateSettings{}, &defaultCfg)
|
||||
assert.Error(t, err)
|
||||
_, err = factory.CreateMetricsExporter(context.Background(), ExporterCreateSettings{}, &defaultCfg)
|
||||
assert.Error(t, err)
|
||||
_, err = factory.CreateLogsExporter(context.Background(), ExporterCreateSettings{}, &defaultCfg)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
// Type gets the type of the Exporter config created by this factory.
|
||||
func (f *TestExporterFactory) Type() config.Type {
|
||||
return config.Type(f.name)
|
||||
func TestNewExporterFactory_WithOptions(t *testing.T) {
|
||||
const typeStr = "test"
|
||||
defaultCfg := config.NewExporterSettings(config.NewComponentID(typeStr))
|
||||
factory := NewExporterFactory(
|
||||
typeStr,
|
||||
func() config.Exporter { return &defaultCfg },
|
||||
WithTracesExporter(createTracesExporter),
|
||||
WithMetricsExporter(createMetricsExporter),
|
||||
WithLogsExporter(createLogsExporter))
|
||||
assert.EqualValues(t, typeStr, factory.Type())
|
||||
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
|
||||
|
||||
_, err := factory.CreateTracesExporter(context.Background(), ExporterCreateSettings{}, &defaultCfg)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = factory.CreateMetricsExporter(context.Background(), ExporterCreateSettings{}, &defaultCfg)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = factory.CreateLogsExporter(context.Background(), ExporterCreateSettings{}, &defaultCfg)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestBuildExporters(t *testing.T) {
|
||||
type testCase struct {
|
||||
in []ExporterFactory
|
||||
out map[config.Type]ExporterFactory
|
||||
}
|
||||
|
||||
testCases := []testCase{
|
||||
{
|
||||
in: []ExporterFactory{
|
||||
&TestExporterFactory{name: "exp1"},
|
||||
&TestExporterFactory{name: "exp2"},
|
||||
},
|
||||
out: map[config.Type]ExporterFactory{
|
||||
"exp1": &TestExporterFactory{name: "exp1"},
|
||||
"exp2": &TestExporterFactory{name: "exp2"},
|
||||
},
|
||||
},
|
||||
{
|
||||
in: []ExporterFactory{
|
||||
&TestExporterFactory{name: "exp1"},
|
||||
&TestExporterFactory{name: "exp1"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range testCases {
|
||||
out, err := MakeExporterFactoryMap(c.in...)
|
||||
if c.out == nil {
|
||||
assert.Error(t, err)
|
||||
continue
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, c.out, out)
|
||||
}
|
||||
func createTracesExporter(context.Context, ExporterCreateSettings, config.Exporter) (TracesExporter, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func createMetricsExporter(context.Context, ExporterCreateSettings, config.Exporter) (MetricsExporter, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func createLogsExporter(context.Context, ExporterCreateSettings, config.Exporter) (LogsExporter, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -96,3 +96,41 @@ func TestMakeProcessorFactoryMap(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakeExporterFactoryMap(t *testing.T) {
|
||||
type testCase struct {
|
||||
name string
|
||||
in []ExporterFactory
|
||||
out map[config.Type]ExporterFactory
|
||||
}
|
||||
|
||||
p1 := NewExporterFactory("p1", nil)
|
||||
p2 := NewExporterFactory("p2", nil)
|
||||
testCases := []testCase{
|
||||
{
|
||||
name: "different names",
|
||||
in: []ExporterFactory{p1, p2},
|
||||
out: map[config.Type]ExporterFactory{
|
||||
p1.Type(): p1,
|
||||
p2.Type(): p2,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "same name",
|
||||
in: []ExporterFactory{p1, p2, NewExporterFactory("p1", nil)},
|
||||
},
|
||||
}
|
||||
|
||||
for i := range testCases {
|
||||
tt := testCases[i]
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
out, err := MakeExporterFactoryMap(tt.in...)
|
||||
if tt.out == nil {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.out, out)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
defaultExporterCfg = config.NewExporterSettings(config.NewComponentID(typeStr))
|
||||
defaultExporterCfg = config.NewExporterSettings(config.NewComponentID("test"))
|
||||
exporterTag, _ = tag.NewKey("exporter")
|
||||
defaultExporterTags = []tag.Tag{
|
||||
{Key: exporterTag, Value: "test"},
|
||||
|
|
|
@ -15,114 +15,32 @@
|
|||
package exporterhelper // import "go.opentelemetry.io/collector/exporter/exporterhelper"
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/collector/component"
|
||||
"go.opentelemetry.io/collector/component/componenterror"
|
||||
"go.opentelemetry.io/collector/config"
|
||||
"go.opentelemetry.io/collector/internal/internalinterface"
|
||||
)
|
||||
|
||||
// FactoryOption apply changes to ExporterOptions.
|
||||
type FactoryOption func(o *factory)
|
||||
// Deprecated: [v0.46.0] use component.ExporterFactoryOption.
|
||||
type FactoryOption = component.ExporterFactoryOption
|
||||
|
||||
// CreateDefaultConfig is the equivalent of component.ExporterFactory.CreateDefaultConfig()
|
||||
type CreateDefaultConfig func() config.Exporter
|
||||
// Deprecated: [v0.46.0] use component.ExporterCreateDefaultConfigFunc.
|
||||
type CreateDefaultConfig = component.ExporterCreateDefaultConfigFunc
|
||||
|
||||
// CreateTracesExporter is the equivalent of component.ExporterFactory.CreateTracesExporter()
|
||||
type CreateTracesExporter func(context.Context, component.ExporterCreateSettings, config.Exporter) (component.TracesExporter, error)
|
||||
// Deprecated: [v0.46.0] use component.CreateTracesExporterFunc.
|
||||
type CreateTracesExporter = component.CreateTracesExporterFunc
|
||||
|
||||
// CreateMetricsExporter is the equivalent of component.ExporterFactory.CreateMetricsExporter()
|
||||
type CreateMetricsExporter func(context.Context, component.ExporterCreateSettings, config.Exporter) (component.MetricsExporter, error)
|
||||
// Deprecated: [v0.46.0] use component.CreateMetricsExporterFunc.
|
||||
type CreateMetricsExporter = component.CreateMetricsExporterFunc
|
||||
|
||||
// CreateLogsExporter is the equivalent of component.ExporterFactory.CreateLogsExporter()
|
||||
type CreateLogsExporter func(context.Context, component.ExporterCreateSettings, config.Exporter) (component.LogsExporter, error)
|
||||
// Deprecated: [v0.46.0] use component.CreateLogsExporterFunc.
|
||||
type CreateLogsExporter = component.CreateLogsExporterFunc
|
||||
|
||||
type factory struct {
|
||||
internalinterface.BaseInternal
|
||||
cfgType config.Type
|
||||
createDefaultConfig CreateDefaultConfig
|
||||
createTracesExporter CreateTracesExporter
|
||||
createMetricsExporter CreateMetricsExporter
|
||||
createLogsExporter CreateLogsExporter
|
||||
}
|
||||
// Deprecated: [v0.46.0] use component.WithTracesExporter.
|
||||
var WithTraces = component.WithTracesExporter
|
||||
|
||||
// WithTraces overrides the default "error not supported" implementation for CreateTracesReceiver.
|
||||
func WithTraces(createTracesExporter CreateTracesExporter) FactoryOption {
|
||||
return func(o *factory) {
|
||||
o.createTracesExporter = createTracesExporter
|
||||
}
|
||||
}
|
||||
// Deprecated: [v0.46.0] use component.WithMetricsExporter.
|
||||
var WithMetrics = component.WithMetricsExporter
|
||||
|
||||
// WithMetrics overrides the default "error not supported" implementation for CreateMetricsReceiver.
|
||||
func WithMetrics(createMetricsExporter CreateMetricsExporter) FactoryOption {
|
||||
return func(o *factory) {
|
||||
o.createMetricsExporter = createMetricsExporter
|
||||
}
|
||||
}
|
||||
// Deprecated: [v0.46.0] use component.WithLogsExporter.
|
||||
var WithLogs = component.WithLogsExporter
|
||||
|
||||
// WithLogs overrides the default "error not supported" implementation for CreateLogsReceiver.
|
||||
func WithLogs(createLogsExporter CreateLogsExporter) FactoryOption {
|
||||
return func(o *factory) {
|
||||
o.createLogsExporter = createLogsExporter
|
||||
}
|
||||
}
|
||||
|
||||
// NewFactory returns a component.ExporterFactory.
|
||||
func NewFactory(
|
||||
cfgType config.Type,
|
||||
createDefaultConfig CreateDefaultConfig,
|
||||
options ...FactoryOption) component.ExporterFactory {
|
||||
f := &factory{
|
||||
cfgType: cfgType,
|
||||
createDefaultConfig: createDefaultConfig,
|
||||
}
|
||||
for _, opt := range options {
|
||||
opt(f)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// Type gets the type of the Exporter config created by this factory.
|
||||
func (f *factory) Type() config.Type {
|
||||
return f.cfgType
|
||||
}
|
||||
|
||||
// CreateDefaultConfig creates the default configuration for processor.
|
||||
func (f *factory) CreateDefaultConfig() config.Exporter {
|
||||
return f.createDefaultConfig()
|
||||
}
|
||||
|
||||
// CreateTracesExporter creates a component.TracesExporter based on this config.
|
||||
func (f *factory) CreateTracesExporter(
|
||||
ctx context.Context,
|
||||
set component.ExporterCreateSettings,
|
||||
cfg config.Exporter) (component.TracesExporter, error) {
|
||||
if f.createTracesExporter != nil {
|
||||
return f.createTracesExporter(ctx, set, cfg)
|
||||
}
|
||||
return nil, componenterror.ErrDataTypeIsNotSupported
|
||||
}
|
||||
|
||||
// CreateMetricsExporter creates a component.MetricsExporter based on this config.
|
||||
func (f *factory) CreateMetricsExporter(
|
||||
ctx context.Context,
|
||||
set component.ExporterCreateSettings,
|
||||
cfg config.Exporter) (component.MetricsExporter, error) {
|
||||
if f.createMetricsExporter != nil {
|
||||
return f.createMetricsExporter(ctx, set, cfg)
|
||||
}
|
||||
return nil, componenterror.ErrDataTypeIsNotSupported
|
||||
}
|
||||
|
||||
// CreateLogsExporter creates a metrics processor based on this config.
|
||||
func (f *factory) CreateLogsExporter(
|
||||
ctx context.Context,
|
||||
set component.ExporterCreateSettings,
|
||||
cfg config.Exporter,
|
||||
) (component.LogsExporter, error) {
|
||||
if f.createLogsExporter != nil {
|
||||
return f.createLogsExporter(ctx, set, cfg)
|
||||
}
|
||||
return nil, componenterror.ErrDataTypeIsNotSupported
|
||||
}
|
||||
// Deprecated: [v0.46.0] use component.NewExporterFactory.
|
||||
var NewFactory = component.NewExporterFactory
|
||||
|
|
|
@ -1,96 +0,0 @@
|
|||
// Copyright The OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package exporterhelper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"go.opentelemetry.io/collector/component"
|
||||
"go.opentelemetry.io/collector/component/componenterror"
|
||||
"go.opentelemetry.io/collector/component/componenttest"
|
||||
"go.opentelemetry.io/collector/config"
|
||||
"go.opentelemetry.io/collector/model/pdata"
|
||||
)
|
||||
|
||||
const typeStr = "test"
|
||||
|
||||
var (
|
||||
defaultCfg = config.NewExporterSettings(config.NewComponentID(typeStr))
|
||||
nopTracesExporter, _ = NewTracesExporter(&defaultCfg, componenttest.NewNopExporterCreateSettings(), func(ctx context.Context, td pdata.Traces) error {
|
||||
return nil
|
||||
})
|
||||
nopMetricsExporter, _ = NewMetricsExporter(&defaultCfg, componenttest.NewNopExporterCreateSettings(), func(ctx context.Context, md pdata.Metrics) error {
|
||||
return nil
|
||||
})
|
||||
nopLogsExporter, _ = NewLogsExporter(&defaultCfg, componenttest.NewNopExporterCreateSettings(), func(ctx context.Context, md pdata.Logs) error {
|
||||
return nil
|
||||
})
|
||||
)
|
||||
|
||||
func TestNewFactory(t *testing.T) {
|
||||
factory := NewFactory(
|
||||
typeStr,
|
||||
defaultConfig)
|
||||
assert.EqualValues(t, typeStr, factory.Type())
|
||||
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
|
||||
_, err := factory.CreateTracesExporter(context.Background(), componenttest.NewNopExporterCreateSettings(), &defaultCfg)
|
||||
assert.Equal(t, componenterror.ErrDataTypeIsNotSupported, err)
|
||||
_, err = factory.CreateMetricsExporter(context.Background(), componenttest.NewNopExporterCreateSettings(), &defaultCfg)
|
||||
assert.Equal(t, componenterror.ErrDataTypeIsNotSupported, err)
|
||||
_, err = factory.CreateLogsExporter(context.Background(), componenttest.NewNopExporterCreateSettings(), &defaultCfg)
|
||||
assert.Equal(t, componenterror.ErrDataTypeIsNotSupported, err)
|
||||
}
|
||||
|
||||
func TestNewFactory_WithConstructors(t *testing.T) {
|
||||
factory := NewFactory(
|
||||
typeStr,
|
||||
defaultConfig,
|
||||
WithTraces(createTracesExporter),
|
||||
WithMetrics(createMetricsExporter),
|
||||
WithLogs(createLogsExporter))
|
||||
assert.EqualValues(t, typeStr, factory.Type())
|
||||
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
|
||||
|
||||
te, err := factory.CreateTracesExporter(context.Background(), componenttest.NewNopExporterCreateSettings(), &defaultCfg)
|
||||
assert.NoError(t, err)
|
||||
assert.Same(t, nopTracesExporter, te)
|
||||
|
||||
me, err := factory.CreateMetricsExporter(context.Background(), componenttest.NewNopExporterCreateSettings(), &defaultCfg)
|
||||
assert.NoError(t, err)
|
||||
assert.Same(t, nopMetricsExporter, me)
|
||||
|
||||
le, err := factory.CreateLogsExporter(context.Background(), componenttest.NewNopExporterCreateSettings(), &defaultCfg)
|
||||
assert.NoError(t, err)
|
||||
assert.Same(t, nopLogsExporter, le)
|
||||
}
|
||||
|
||||
func defaultConfig() config.Exporter {
|
||||
return &defaultCfg
|
||||
}
|
||||
|
||||
func createTracesExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.TracesExporter, error) {
|
||||
return nopTracesExporter, nil
|
||||
}
|
||||
|
||||
func createMetricsExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.MetricsExporter, error) {
|
||||
return nopMetricsExporter, nil
|
||||
}
|
||||
|
||||
func createLogsExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.LogsExporter, error) {
|
||||
return nopLogsExporter, nil
|
||||
}
|
|
@ -22,7 +22,6 @@ import (
|
|||
|
||||
"go.opentelemetry.io/collector/component"
|
||||
"go.opentelemetry.io/collector/config"
|
||||
"go.opentelemetry.io/collector/exporter/exporterhelper"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -34,12 +33,12 @@ const (
|
|||
|
||||
// NewFactory creates a factory for Logging exporter
|
||||
func NewFactory() component.ExporterFactory {
|
||||
return exporterhelper.NewFactory(
|
||||
return component.NewExporterFactory(
|
||||
typeStr,
|
||||
createDefaultConfig,
|
||||
exporterhelper.WithTraces(createTracesExporter),
|
||||
exporterhelper.WithMetrics(createMetricsExporter),
|
||||
exporterhelper.WithLogs(createLogsExporter))
|
||||
component.WithTracesExporter(createTracesExporter),
|
||||
component.WithMetricsExporter(createMetricsExporter),
|
||||
component.WithLogsExporter(createLogsExporter))
|
||||
}
|
||||
|
||||
func createDefaultConfig() config.Exporter {
|
||||
|
|
|
@ -32,12 +32,12 @@ const (
|
|||
|
||||
// NewFactory creates a factory for OTLP exporter.
|
||||
func NewFactory() component.ExporterFactory {
|
||||
return exporterhelper.NewFactory(
|
||||
return component.NewExporterFactory(
|
||||
typeStr,
|
||||
createDefaultConfig,
|
||||
exporterhelper.WithTraces(createTracesExporter),
|
||||
exporterhelper.WithMetrics(createMetricsExporter),
|
||||
exporterhelper.WithLogs(createLogsExporter))
|
||||
component.WithTracesExporter(createTracesExporter),
|
||||
component.WithMetricsExporter(createMetricsExporter),
|
||||
component.WithLogsExporter(createLogsExporter))
|
||||
}
|
||||
|
||||
func createDefaultConfig() config.Exporter {
|
||||
|
|
|
@ -35,12 +35,12 @@ const (
|
|||
|
||||
// NewFactory creates a factory for OTLP exporter.
|
||||
func NewFactory() component.ExporterFactory {
|
||||
return exporterhelper.NewFactory(
|
||||
return component.NewExporterFactory(
|
||||
typeStr,
|
||||
createDefaultConfig,
|
||||
exporterhelper.WithTraces(createTracesExporter),
|
||||
exporterhelper.WithMetrics(createMetricsExporter),
|
||||
exporterhelper.WithLogs(createLogsExporter))
|
||||
component.WithTracesExporter(createTracesExporter),
|
||||
component.WithMetricsExporter(createMetricsExporter),
|
||||
component.WithLogsExporter(createLogsExporter))
|
||||
}
|
||||
|
||||
func createDefaultConfig() config.Exporter {
|
||||
|
|
|
@ -20,7 +20,6 @@ import (
|
|||
"go.opentelemetry.io/collector/component"
|
||||
"go.opentelemetry.io/collector/config"
|
||||
"go.opentelemetry.io/collector/consumer"
|
||||
"go.opentelemetry.io/collector/exporter/exporterhelper"
|
||||
"go.opentelemetry.io/collector/model/pdata"
|
||||
)
|
||||
|
||||
|
@ -44,12 +43,12 @@ func (cfg *ExampleExporter) Unmarshal(componentParser *config.Map) error {
|
|||
const expType = "exampleexporter"
|
||||
|
||||
// ExampleExporterFactory is factory for ExampleExporter.
|
||||
var ExampleExporterFactory = exporterhelper.NewFactory(
|
||||
var ExampleExporterFactory = component.NewExporterFactory(
|
||||
expType,
|
||||
createExporterDefaultConfig,
|
||||
exporterhelper.WithTraces(createTracesExporter),
|
||||
exporterhelper.WithMetrics(createMetricsExporter),
|
||||
exporterhelper.WithLogs(createLogsExporter))
|
||||
component.WithTracesExporter(createTracesExporter),
|
||||
component.WithMetricsExporter(createMetricsExporter),
|
||||
component.WithLogsExporter(createLogsExporter))
|
||||
|
||||
// CreateDefaultConfig creates the default configuration for the Exporter.
|
||||
func createExporterDefaultConfig() config.Exporter {
|
||||
|
@ -61,27 +60,15 @@ func createExporterDefaultConfig() config.Exporter {
|
|||
}
|
||||
}
|
||||
|
||||
func createTracesExporter(
|
||||
_ context.Context,
|
||||
_ component.ExporterCreateSettings,
|
||||
_ config.Exporter,
|
||||
) (component.TracesExporter, error) {
|
||||
func createTracesExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.TracesExporter, error) {
|
||||
return &ExampleExporterConsumer{}, nil
|
||||
}
|
||||
|
||||
func createMetricsExporter(
|
||||
_ context.Context,
|
||||
_ component.ExporterCreateSettings,
|
||||
_ config.Exporter,
|
||||
) (component.MetricsExporter, error) {
|
||||
func createMetricsExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.MetricsExporter, error) {
|
||||
return &ExampleExporterConsumer{}, nil
|
||||
}
|
||||
|
||||
func createLogsExporter(
|
||||
_ context.Context,
|
||||
_ component.ExporterCreateSettings,
|
||||
_ config.Exporter,
|
||||
) (component.LogsExporter, error) {
|
||||
func createLogsExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.LogsExporter, error) {
|
||||
return &ExampleExporterConsumer{}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,6 @@ package builder
|
|||
import (
|
||||
"go.opentelemetry.io/collector/component"
|
||||
"go.opentelemetry.io/collector/config"
|
||||
"go.opentelemetry.io/collector/exporter/exporterhelper"
|
||||
"go.opentelemetry.io/collector/internal/testcomponents"
|
||||
"go.opentelemetry.io/collector/receiver/receiverhelper"
|
||||
)
|
||||
|
@ -69,7 +68,7 @@ func newBadProcessorFactory() component.ProcessorFactory {
|
|||
}
|
||||
|
||||
func newBadExporterFactory() component.ExporterFactory {
|
||||
return exporterhelper.NewFactory("bf", func() config.Exporter {
|
||||
return component.NewExporterFactory("bf", func() config.Exporter {
|
||||
return &struct {
|
||||
config.ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
|
||||
}{
|
||||
|
|
Loading…
Reference in New Issue