| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package builders |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
|
| 10 |
"go.opentelemetry.io/collector/component" |
| 11 |
"go.opentelemetry.io/collector/exporter" |
| 12 |
"go.opentelemetry.io/collector/exporter/exportertest" |
| 13 |
"go.opentelemetry.io/collector/exporter/xexporter" |
| 14 |
"go.opentelemetry.io/collector/pipeline" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
type ExporterBuilder struct { |
| 19 |
cfgs map[component.ID]component.Config |
| 20 |
factories map[component.Type]exporter.Factory |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
func NewExporter(cfgs map[component.ID]component.Config, factories map[component.Type]exporter.Factory) *ExporterBuilder { |
| 25 |
return &ExporterBuilder{cfgs: cfgs, factories: factories} |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
func (b *ExporterBuilder) CreateTraces(ctx context.Context, set exporter.Settings) (exporter.Traces, error) { |
| 30 |
cfg, existsCfg := b.cfgs[set.ID] |
| 31 |
if !existsCfg { |
| 32 |
return nil, fmt.Errorf("exporter %q is not configured", set.ID) |
| 33 |
} |
| 34 |
|
| 35 |
f, existsFactory := b.factories[set.ID.Type()] |
| 36 |
if !existsFactory { |
| 37 |
return nil, fmt.Errorf("exporter factory not available for: %q", set.ID) |
| 38 |
} |
| 39 |
|
| 40 |
logStabilityLevel(set.Logger, f.TracesStability()) |
| 41 |
return f.CreateTraces(ctx, set, cfg) |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func (b *ExporterBuilder) CreateMetrics(ctx context.Context, set exporter.Settings) (exporter.Metrics, error) { |
| 46 |
cfg, existsCfg := b.cfgs[set.ID] |
| 47 |
if !existsCfg { |
| 48 |
return nil, fmt.Errorf("exporter %q is not configured", set.ID) |
| 49 |
} |
| 50 |
|
| 51 |
f, existsFactory := b.factories[set.ID.Type()] |
| 52 |
if !existsFactory { |
| 53 |
return nil, fmt.Errorf("exporter factory not available for: %q", set.ID) |
| 54 |
} |
| 55 |
|
| 56 |
logStabilityLevel(set.Logger, f.MetricsStability()) |
| 57 |
return f.CreateMetrics(ctx, set, cfg) |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
func (b *ExporterBuilder) CreateLogs(ctx context.Context, set exporter.Settings) (exporter.Logs, error) { |
| 62 |
cfg, existsCfg := b.cfgs[set.ID] |
| 63 |
if !existsCfg { |
| 64 |
return nil, fmt.Errorf("exporter %q is not configured", set.ID) |
| 65 |
} |
| 66 |
|
| 67 |
f, existsFactory := b.factories[set.ID.Type()] |
| 68 |
if !existsFactory { |
| 69 |
return nil, fmt.Errorf("exporter factory not available for: %q", set.ID) |
| 70 |
} |
| 71 |
|
| 72 |
logStabilityLevel(set.Logger, f.LogsStability()) |
| 73 |
return f.CreateLogs(ctx, set, cfg) |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
func (b *ExporterBuilder) CreateProfiles(ctx context.Context, set exporter.Settings) (xexporter.Profiles, error) { |
| 78 |
cfg, existsCfg := b.cfgs[set.ID] |
| 79 |
if !existsCfg { |
| 80 |
return nil, fmt.Errorf("exporter %q is not configured", set.ID) |
| 81 |
} |
| 82 |
|
| 83 |
expFact, existsFactory := b.factories[set.ID.Type()] |
| 84 |
if !existsFactory { |
| 85 |
return nil, fmt.Errorf("exporter factory not available for: %q", set.ID) |
| 86 |
} |
| 87 |
|
| 88 |
f, ok := expFact.(xexporter.Factory) |
| 89 |
if !ok { |
| 90 |
return nil, pipeline.ErrSignalNotSupported |
| 91 |
} |
| 92 |
|
| 93 |
logStabilityLevel(set.Logger, f.ProfilesStability()) |
| 94 |
return f.CreateProfiles(ctx, set, cfg) |
| 95 |
} |
| 96 |
|
| 97 |
func (b *ExporterBuilder) Factory(componentType component.Type) component.Factory { |
| 98 |
return b.factories[componentType] |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
func NewNopExporterConfigsAndFactories() (map[component.ID]component.Config, map[component.Type]exporter.Factory) { |
| 103 |
nopFactory := exportertest.NewNopFactory() |
| 104 |
configs := map[component.ID]component.Config{ |
| 105 |
component.NewID(NopType): nopFactory.CreateDefaultConfig(), |
| 106 |
} |
| 107 |
factories := map[component.Type]exporter.Factory{ |
| 108 |
NopType: nopFactory, |
| 109 |
} |
| 110 |
|
| 111 |
return configs, factories |
| 112 |
} |