Debian Codegraph @

scraper/factory.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package scraper // import "go.opentelemetry.io/collector/scraper"
5
6 import (
7 "context"
8
9 "go.opentelemetry.io/collector/component"
10 "go.opentelemetry.io/collector/pipeline"
11 )
12
13 // Settings configures scraper creators.
14 type Settings struct {
15 // ID returns the ID of the component that will be created.
16 ID component.ID
17
18 component.TelemetrySettings
19
20 // BuildInfo can be used by components for informational purposes.
21 BuildInfo component.BuildInfo
22
23 // prevent unkeyed literal initialization
24 _ struct{}
25 }
26
27 // Factory is factory interface for scrapers.
28 //
29 // This interface cannot be directly implemented. Implementations must
30 // use the NewFactory to implement it.
31 type Factory interface {
32 component.Factory
33
34 // CreateLogs creates a Logs scraper based on this config.
35 // If the scraper type does not support logs,
36 // this function returns the error [pipeline.ErrSignalNotSupported].
37 CreateLogs(ctx context.Context, set Settings, cfg component.Config) (Logs, error)
38
39 // CreateMetrics creates a Metrics scraper based on this config.
40 // If the scraper type does not support metrics,
41 // this function returns the error [pipeline.ErrSignalNotSupported].
42 CreateMetrics(ctx context.Context, set Settings, cfg component.Config) (Metrics, error)
43
44 // LogsStability gets the stability level of the Logs scraper.
45 LogsStability() component.StabilityLevel
46
47 // MetricsStability gets the stability level of the Metrics scraper.
48 MetricsStability() component.StabilityLevel
49
50 unexportedFactoryFunc()
51 }
52
53 // FactoryOption apply changes to Options.
54 type FactoryOption interface {
55 // applyOption applies the option.
56 applyOption(o *factory)
57 }
58
59 var _ FactoryOption = (*factoryOptionFunc)(nil)
60
61 // factoryOptionFunc is a FactoryOption created through a function.
62 type factoryOptionFunc func(*factory)
63
64 func (f factoryOptionFunc) applyOption(o *factory) {
65 f(o)
66 }
67
68 type factory struct {
69 cfgType component.Type
70 component.CreateDefaultConfigFunc
71 createLogsFunc CreateLogsFunc
72 createMetricsFunc CreateMetricsFunc
73 logsStabilityLevel component.StabilityLevel
74 metricsStabilityLevel component.StabilityLevel
75 }
76
77 func (f *factory) Type() component.Type {
78 return f.cfgType
79 }
80
81 func (f *factory) unexportedFactoryFunc() {}
82
83 func (f *factory) LogsStability() component.StabilityLevel {
84 return f.logsStabilityLevel
85 }
86
87 func (f *factory) MetricsStability() component.StabilityLevel {
88 return f.metricsStabilityLevel
89 }
90
91 func (f *factory) CreateLogs(ctx context.Context, set Settings, cfg component.Config) (Logs, error) {
92 if f.createLogsFunc == nil {
93 return nil, pipeline.ErrSignalNotSupported
94 }
95 return f.createLogsFunc(ctx, set, cfg)
96 }
97
98 func (f *factory) CreateMetrics(ctx context.Context, set Settings, cfg component.Config) (Metrics, error) {
99 if f.createMetricsFunc == nil {
100 return nil, pipeline.ErrSignalNotSupported
101 }
102 return f.createMetricsFunc(ctx, set, cfg)
103 }
104
105 // CreateLogsFunc is the equivalent of Factory.CreateLogs().
106 type CreateLogsFunc func(context.Context, Settings, component.Config) (Logs, error)
107
108 // CreateMetricsFunc is the equivalent of Factory.CreateMetrics().
109 type CreateMetricsFunc func(context.Context, Settings, component.Config) (Metrics, error)
110
111 // WithLogs overrides the default "error not supported" implementation for CreateLogs and the default "undefined" stability level.
112 func WithLogs(createLogs CreateLogsFunc, sl component.StabilityLevel) FactoryOption {
113 return factoryOptionFunc(func(o *factory) {
114 o.logsStabilityLevel = sl
115 o.createLogsFunc = createLogs
116 })
117 }
118
119 // WithMetrics overrides the default "error not supported" implementation for CreateMetrics and the default "undefined" stability level.
120 func WithMetrics(createMetrics CreateMetricsFunc, sl component.StabilityLevel) FactoryOption {
121 return factoryOptionFunc(func(o *factory) {
122 o.metricsStabilityLevel = sl
123 o.createMetricsFunc = createMetrics
124 })
125 }
126
127 // NewFactory returns a Factory.
128 func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
129 f := &factory{
130 cfgType: cfgType,
131 CreateDefaultConfigFunc: createDefaultConfig,
132 }
133 for _, opt := range options {
134 opt.applyOption(f)
135 }
136 return f
137 }