| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package extension // import "go.opentelemetry.io/collector/extension" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
|
| 10 |
"go.opentelemetry.io/collector/component" |
| 11 |
) |
| 12 |
|
| 13 |
// Extension is the interface for objects hosted by the OpenTelemetry Collector that |
| 14 |
// don't participate directly on data pipelines but provide some functionality |
| 15 |
// to the service, examples: health check endpoint, z-pages, etc. |
| 16 |
type Extension interface { |
| 17 |
component.Component |
| 18 |
} |
| 19 |
|
| 20 |
// Settings is passed to Factory.Create(...) function. |
| 21 |
type Settings struct { |
| 22 |
// ID returns the ID of the component that will be created. |
| 23 |
ID component.ID |
| 24 |
|
| 25 |
component.TelemetrySettings |
| 26 |
|
| 27 |
// BuildInfo can be used by components for informational purposes |
| 28 |
BuildInfo component.BuildInfo |
| 29 |
|
| 30 |
// prevent unkeyed literal initialization |
| 31 |
_ struct{} |
| 32 |
} |
| 33 |
|
| 34 |
// CreateFunc is the equivalent of Factory.Create(...) function. |
| 35 |
type CreateFunc func(context.Context, Settings, component.Config) (Extension, error) |
| 36 |
|
| 37 |
type Factory interface { |
| 38 |
component.Factory |
| 39 |
|
| 40 |
// Create an extension based on the given config. |
| 41 |
Create(ctx context.Context, set Settings, cfg component.Config) (Extension, error) |
| 42 |
|
| 43 |
// Stability gets the stability level of the Extension. |
| 44 |
Stability() component.StabilityLevel |
| 45 |
|
| 46 |
unexportedFactoryFunc() |
| 47 |
} |
| 48 |
|
| 49 |
type factory struct { |
| 50 |
cfgType component.Type |
| 51 |
component.CreateDefaultConfigFunc |
| 52 |
createFunc CreateFunc |
| 53 |
extensionStability component.StabilityLevel |
| 54 |
} |
| 55 |
|
| 56 |
func (f *factory) Type() component.Type { |
| 57 |
return f.cfgType |
| 58 |
} |
| 59 |
|
| 60 |
func (f *factory) unexportedFactoryFunc() {} |
| 61 |
|
| 62 |
func (f *factory) Stability() component.StabilityLevel { |
| 63 |
return f.extensionStability |
| 64 |
} |
| 65 |
|
| 66 |
func (f *factory) Create(ctx context.Context, set Settings, cfg component.Config) (Extension, error) { |
| 67 |
if set.ID.Type() != f.cfgType { |
| 68 |
return nil, fmt.Errorf("component type mismatch: component ID %q does not have type %q", set.ID, f.cfgType) |
| 69 |
} |
| 70 |
|
| 71 |
return f.createFunc(ctx, set, cfg) |
| 72 |
} |
| 73 |
|
| 74 |
// NewFactory returns a new Factory based on this configuration. |
| 75 |
func NewFactory( |
| 76 |
cfgType component.Type, |
| 77 |
createDefaultConfig component.CreateDefaultConfigFunc, |
| 78 |
createServiceExtension CreateFunc, |
| 79 |
sl component.StabilityLevel, |
| 80 |
) Factory { |
| 81 |
return &factory{ |
| 82 |
cfgType: cfgType, |
| 83 |
CreateDefaultConfigFunc: createDefaultConfig, |
| 84 |
createFunc: createServiceExtension, |
| 85 |
extensionStability: sl, |
| 86 |
} |
| 87 |
} |