| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package builders // import "go.opentelemetry.io/collector/service/internal/builders" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
|
| 10 |
"go.opentelemetry.io/collector/component" |
| 11 |
"go.opentelemetry.io/collector/consumer" |
| 12 |
"go.opentelemetry.io/collector/consumer/xconsumer" |
| 13 |
"go.opentelemetry.io/collector/pipeline" |
| 14 |
"go.opentelemetry.io/collector/processor" |
| 15 |
"go.opentelemetry.io/collector/processor/processortest" |
| 16 |
"go.opentelemetry.io/collector/processor/xprocessor" |
| 17 |
) |
| 18 |
|
| 19 |
// ProcessorBuilder processor is a helper struct that given a set of Configs |
| 20 |
// and Factories helps with creating processors. |
| 21 |
type ProcessorBuilder struct { |
| 22 |
cfgs map[component.ID]component.Config |
| 23 |
factories map[component.Type]processor.Factory |
| 24 |
} |
| 25 |
|
| 26 |
// NewProcessor creates a new ProcessorBuilder to help with creating components form a set of configs and factories. |
| 27 |
func NewProcessor(cfgs map[component.ID]component.Config, factories map[component.Type]processor.Factory) *ProcessorBuilder { |
| 28 |
return &ProcessorBuilder{cfgs: cfgs, factories: factories} |
| 29 |
} |
| 30 |
|
| 31 |
// CreateTraces creates a Traces processor based on the settings and config. |
| 32 |
func (b *ProcessorBuilder) CreateTraces(ctx context.Context, set processor.Settings, next consumer.Traces) (processor.Traces, error) { |
| 33 |
if next == nil { |
| 34 |
return nil, errNilNextConsumer |
| 35 |
} |
| 36 |
cfg, existsCfg := b.cfgs[set.ID] |
| 37 |
if !existsCfg { |
| 38 |
return nil, fmt.Errorf("processor %q is not configured", set.ID) |
| 39 |
} |
| 40 |
|
| 41 |
f, existsFactory := b.factories[set.ID.Type()] |
| 42 |
if !existsFactory { |
| 43 |
return nil, fmt.Errorf("processor factory not available for: %q", set.ID) |
| 44 |
} |
| 45 |
|
| 46 |
logStabilityLevel(set.Logger, f.TracesStability()) |
| 47 |
return f.CreateTraces(ctx, set, cfg, next) |
| 48 |
} |
| 49 |
|
| 50 |
// CreateMetrics creates a Metrics processor based on the settings and config. |
| 51 |
func (b *ProcessorBuilder) CreateMetrics(ctx context.Context, set processor.Settings, next consumer.Metrics) (processor.Metrics, error) { |
| 52 |
if next == nil { |
| 53 |
return nil, errNilNextConsumer |
| 54 |
} |
| 55 |
cfg, existsCfg := b.cfgs[set.ID] |
| 56 |
if !existsCfg { |
| 57 |
return nil, fmt.Errorf("processor %q is not configured", set.ID) |
| 58 |
} |
| 59 |
|
| 60 |
f, existsFactory := b.factories[set.ID.Type()] |
| 61 |
if !existsFactory { |
| 62 |
return nil, fmt.Errorf("processor factory not available for: %q", set.ID) |
| 63 |
} |
| 64 |
|
| 65 |
logStabilityLevel(set.Logger, f.MetricsStability()) |
| 66 |
return f.CreateMetrics(ctx, set, cfg, next) |
| 67 |
} |
| 68 |
|
| 69 |
// CreateLogs creates a Logs processor based on the settings and config. |
| 70 |
func (b *ProcessorBuilder) CreateLogs(ctx context.Context, set processor.Settings, next consumer.Logs) (processor.Logs, error) { |
| 71 |
if next == nil { |
| 72 |
return nil, errNilNextConsumer |
| 73 |
} |
| 74 |
cfg, existsCfg := b.cfgs[set.ID] |
| 75 |
if !existsCfg { |
| 76 |
return nil, fmt.Errorf("processor %q is not configured", set.ID) |
| 77 |
} |
| 78 |
|
| 79 |
f, existsFactory := b.factories[set.ID.Type()] |
| 80 |
if !existsFactory { |
| 81 |
return nil, fmt.Errorf("processor factory not available for: %q", set.ID) |
| 82 |
} |
| 83 |
|
| 84 |
logStabilityLevel(set.Logger, f.LogsStability()) |
| 85 |
return f.CreateLogs(ctx, set, cfg, next) |
| 86 |
} |
| 87 |
|
| 88 |
// CreateProfiles creates a Profiles processor based on the settings and config. |
| 89 |
func (b *ProcessorBuilder) CreateProfiles(ctx context.Context, set processor.Settings, next xconsumer.Profiles) (xprocessor.Profiles, error) { |
| 90 |
if next == nil { |
| 91 |
return nil, errNilNextConsumer |
| 92 |
} |
| 93 |
cfg, existsCfg := b.cfgs[set.ID] |
| 94 |
if !existsCfg { |
| 95 |
return nil, fmt.Errorf("processor %q is not configured", set.ID) |
| 96 |
} |
| 97 |
|
| 98 |
procFact, existsFactory := b.factories[set.ID.Type()] |
| 99 |
if !existsFactory { |
| 100 |
return nil, fmt.Errorf("processor factory not available for: %q", set.ID) |
| 101 |
} |
| 102 |
|
| 103 |
f, ok := procFact.(xprocessor.Factory) |
| 104 |
if !ok { |
| 105 |
return nil, pipeline.ErrSignalNotSupported |
| 106 |
} |
| 107 |
logStabilityLevel(set.Logger, f.ProfilesStability()) |
| 108 |
return f.CreateProfiles(ctx, set, cfg, next) |
| 109 |
} |
| 110 |
|
| 111 |
func (b *ProcessorBuilder) Factory(componentType component.Type) component.Factory { |
| 112 |
return b.factories[componentType] |
| 113 |
} |
| 114 |
|
| 115 |
// NewNopProcessorConfigsAndFactories returns a configuration and factories that allows building a new nop processor. |
| 116 |
func NewNopProcessorConfigsAndFactories() (map[component.ID]component.Config, map[component.Type]processor.Factory) { |
| 117 |
nopFactory := processortest.NewNopFactory() |
| 118 |
configs := map[component.ID]component.Config{ |
| 119 |
component.NewID(NopType): nopFactory.CreateDefaultConfig(), |
| 120 |
} |
| 121 |
factories := map[component.Type]processor.Factory{ |
| 122 |
NopType: nopFactory, |
| 123 |
} |
| 124 |
|
| 125 |
return configs, factories |
| 126 |
} |