Debian Codegraph @

scraper/xscraper/factory.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package xscraper // import "go.opentelemetry.io/collector/scraper/xscraper"
5
6 import (
7 "context"
8
9 "go.opentelemetry.io/collector/component"
10 "go.opentelemetry.io/collector/pipeline"
11 "go.opentelemetry.io/collector/scraper"
12 )
13
14 type Factory interface {
15 scraper.Factory
16
17 // CreateProfiles creates a Profiles scraper based on this config.
18 // If the scraper type does not support profiles,
19 // this function returns the error [pipeline.ErrSignalNotSupported].
20 CreateProfiles(ctx context.Context, set scraper.Settings, cfg component.Config) (Profiles, error)
21
22 // ProfilesStability gets the stability level of the Profiles scraper.
23 ProfilesStability() component.StabilityLevel
24 }
25
26 // FactoryOption apply changes to Options.
27 type FactoryOption interface {
28 // applyOption applies the option.
29 applyOption(o *factoryOpts)
30 }
31
32 type factoryOpts struct {
33 opts []scraper.FactoryOption
34 *factory
35 }
36
37 var _ FactoryOption = (*factoryOptionFunc)(nil)
38
39 // factoryOptionFunc is a FactoryOption created through a function.
40 type factoryOptionFunc func(*factoryOpts)
41
42 func (f factoryOptionFunc) applyOption(o *factoryOpts) {
43 f(o)
44 }
45
46 type factory struct {
47 scraper.Factory
48
49 createProfilesFunc CreateProfilesFunc
50 profilesStabilityLevel component.StabilityLevel
51 }
52
53 func (f *factory) ProfilesStability() component.StabilityLevel {
54 return f.profilesStabilityLevel
55 }
56
57 func (f *factory) CreateProfiles(ctx context.Context, set scraper.Settings, cfg component.Config) (Profiles, error) {
58 if f.createProfilesFunc == nil {
59 return nil, pipeline.ErrSignalNotSupported
60 }
61 return f.createProfilesFunc(ctx, set, cfg)
62 }
63
64 // WithLogs overrides the default "error not supported" implementation for CreateLogs and the default "undefined" stability level.
65 func WithLogs(createLogs scraper.CreateLogsFunc, sl component.StabilityLevel) FactoryOption {
66 return factoryOptionFunc(func(o *factoryOpts) {
67 o.opts = append(o.opts, scraper.WithLogs(createLogs, sl))
68 })
69 }
70
71 // WithMetrics overrides the default "error not supported" implementation for CreateMetrics and the default "undefined" stability level.
72 func WithMetrics(createMetrics scraper.CreateMetricsFunc, sl component.StabilityLevel) FactoryOption {
73 return factoryOptionFunc(func(o *factoryOpts) {
74 o.opts = append(o.opts, scraper.WithMetrics(createMetrics, sl))
75 })
76 }
77
78 // CreateProfilesFunc is the equivalent of Factory.CreateProfiles().
79 type CreateProfilesFunc func(context.Context, scraper.Settings, component.Config) (Profiles, error)
80
81 // WithProfiles overrides the default "error not supported" implementation for CreateProfiles and the default "undefined" stability level.
82 func WithProfiles(createProfiles CreateProfilesFunc, sl component.StabilityLevel) FactoryOption {
83 return factoryOptionFunc(func(o *factoryOpts) {
84 o.profilesStabilityLevel = sl
85 o.createProfilesFunc = createProfiles
86 })
87 }
88
89 // NewFactory returns a Factory.
90 func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
91 opts := factoryOpts{factory: &factory{}}
92 for _, opt := range options {
93 opt.applyOption(&opts)
94 }
95 opts.Factory = scraper.NewFactory(cfgType, createDefaultConfig, opts.opts...)
96 return opts.factory
97 }