| 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/extension" |
| 12 |
"go.opentelemetry.io/collector/extension/extensiontest" |
| 13 |
) |
| 14 |
|
| 15 |
// Extension is an interface that allows using implementations of the builder |
| 16 |
// from different packages. |
| 17 |
type Extension interface { |
| 18 |
Create(context.Context, extension.Settings) (extension.Extension, error) |
| 19 |
Factory(component.Type) component.Factory |
| 20 |
} |
| 21 |
|
| 22 |
// ExtensionBuilder is a helper struct that given a set of Configs and Factories helps with creating extensions. |
| 23 |
type ExtensionBuilder struct { |
| 24 |
cfgs map[component.ID]component.Config |
| 25 |
factories map[component.Type]extension.Factory |
| 26 |
} |
| 27 |
|
| 28 |
// NewExtension creates a new ExtensionBuilder to help with creating |
| 29 |
// components form a set of configs and factories. |
| 30 |
func NewExtension(cfgs map[component.ID]component.Config, factories map[component.Type]extension.Factory) *ExtensionBuilder { |
| 31 |
return &ExtensionBuilder{cfgs: cfgs, factories: factories} |
| 32 |
} |
| 33 |
|
| 34 |
// Create creates an extension based on the settings and configs available. |
| 35 |
func (b *ExtensionBuilder) Create(ctx context.Context, set extension.Settings) (extension.Extension, error) { |
| 36 |
cfg, existsCfg := b.cfgs[set.ID] |
| 37 |
if !existsCfg { |
| 38 |
return nil, fmt.Errorf("extension %q is not configured", set.ID) |
| 39 |
} |
| 40 |
|
| 41 |
f, existsFactory := b.factories[set.ID.Type()] |
| 42 |
if !existsFactory { |
| 43 |
return nil, fmt.Errorf("extension factory not available for: %q", set.ID) |
| 44 |
} |
| 45 |
|
| 46 |
sl := f.Stability() |
| 47 |
if sl >= component.StabilityLevelAlpha { |
| 48 |
set.Logger.Debug(sl.LogMessage()) |
| 49 |
} else { |
| 50 |
set.Logger.Info(sl.LogMessage()) |
| 51 |
} |
| 52 |
return f.Create(ctx, set, cfg) |
| 53 |
} |
| 54 |
|
| 55 |
func (b *ExtensionBuilder) Factory(componentType component.Type) component.Factory { |
| 56 |
return b.factories[componentType] |
| 57 |
} |
| 58 |
|
| 59 |
// NewNopExtensionConfigsAndFactories returns a configuration and factories that allows building a new nop processor. |
| 60 |
func NewNopExtensionConfigsAndFactories() (map[component.ID]component.Config, map[component.Type]extension.Factory) { |
| 61 |
nopFactory := extensiontest.NewNopFactory() |
| 62 |
configs := map[component.ID]component.Config{ |
| 63 |
component.NewID(NopType): nopFactory.CreateDefaultConfig(), |
| 64 |
} |
| 65 |
factories := map[component.Type]extension.Factory{ |
| 66 |
NopType: nopFactory, |
| 67 |
} |
| 68 |
return configs, factories |
| 69 |
} |