| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xreceiver // import "go.opentelemetry.io/collector/receiver/xreceiver" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
|
| 9 |
"go.opentelemetry.io/collector/component" |
| 10 |
"go.opentelemetry.io/collector/consumer/xconsumer" |
| 11 |
"go.opentelemetry.io/collector/pipeline" |
| 12 |
"go.opentelemetry.io/collector/receiver" |
| 13 |
"go.opentelemetry.io/collector/receiver/internal" |
| 14 |
) |
| 15 |
|
| 16 |
// Profiles receiver receives profiles. |
| 17 |
// Its purpose is to translate data from any format to the collector's internal profile format. |
| 18 |
// Profiles receiver feeds a xconsumer.Profiles with data. |
| 19 |
// |
| 20 |
// For example, it could be a pprof data source which translates pprof profiles into pprofile.Profiles. |
| 21 |
type Profiles interface { |
| 22 |
component.Component |
| 23 |
} |
| 24 |
|
| 25 |
// Factory is a factory interface for receivers. |
| 26 |
// |
| 27 |
// This interface cannot be directly implemented. Implementations must |
| 28 |
// use the NewFactory to implement it. |
| 29 |
type Factory interface { |
| 30 |
receiver.Factory |
| 31 |
|
| 32 |
// CreateProfiles creates a Profiles based on this config. |
| 33 |
// If the receiver type does not support tracing or if the config is not valid |
| 34 |
// an error will be returned instead. `next` is never nil. |
| 35 |
CreateProfiles(ctx context.Context, set receiver.Settings, cfg component.Config, next xconsumer.Profiles) (Profiles, error) |
| 36 |
|
| 37 |
// ProfilesStability gets the stability level of the Profiles receiver. |
| 38 |
ProfilesStability() component.StabilityLevel |
| 39 |
} |
| 40 |
|
| 41 |
// CreateProfilesFunc is the equivalent of Factory.CreateProfiles. |
| 42 |
type CreateProfilesFunc func(context.Context, receiver.Settings, component.Config, xconsumer.Profiles) (Profiles, error) |
| 43 |
|
| 44 |
// FactoryOption apply changes to Factory. |
| 45 |
type FactoryOption interface { |
| 46 |
// applyOption applies the option. |
| 47 |
applyOption(o *factoryOpts) |
| 48 |
} |
| 49 |
|
| 50 |
// factoryOptionFunc is a FactoryOption created through a function. |
| 51 |
type factoryOptionFunc func(*factoryOpts) |
| 52 |
|
| 53 |
func (f factoryOptionFunc) applyOption(o *factoryOpts) { |
| 54 |
f(o) |
| 55 |
} |
| 56 |
|
| 57 |
type factory struct { |
| 58 |
receiver.Factory |
| 59 |
createProfilesFunc CreateProfilesFunc |
| 60 |
profilesStabilityLevel component.StabilityLevel |
| 61 |
} |
| 62 |
|
| 63 |
func (f *factory) ProfilesStability() component.StabilityLevel { |
| 64 |
return f.profilesStabilityLevel |
| 65 |
} |
| 66 |
|
| 67 |
func (f *factory) CreateProfiles(ctx context.Context, set receiver.Settings, cfg component.Config, next xconsumer.Profiles) (Profiles, error) { |
| 68 |
if f.createProfilesFunc == nil { |
| 69 |
return nil, pipeline.ErrSignalNotSupported |
| 70 |
} |
| 71 |
if set.ID.Type() != f.Type() { |
| 72 |
return nil, internal.ErrIDMismatch(set.ID, f.Type()) |
| 73 |
} |
| 74 |
return f.createProfilesFunc(ctx, set, cfg, next) |
| 75 |
} |
| 76 |
|
| 77 |
type factoryOpts struct { |
| 78 |
opts []receiver.FactoryOption |
| 79 |
*factory |
| 80 |
} |
| 81 |
|
| 82 |
// WithTraces overrides the default "error not supported" implementation for Factory.CreateTraces and the default "undefined" stability level. |
| 83 |
func WithTraces(createTraces receiver.CreateTracesFunc, sl component.StabilityLevel) FactoryOption { |
| 84 |
return factoryOptionFunc(func(o *factoryOpts) { |
| 85 |
o.opts = append(o.opts, receiver.WithTraces(createTraces, sl)) |
| 86 |
}) |
| 87 |
} |
| 88 |
|
| 89 |
// WithMetrics overrides the default "error not supported" implementation for Factory.CreateMetrics and the default "undefined" stability level. |
| 90 |
func WithMetrics(createMetrics receiver.CreateMetricsFunc, sl component.StabilityLevel) FactoryOption { |
| 91 |
return factoryOptionFunc(func(o *factoryOpts) { |
| 92 |
o.opts = append(o.opts, receiver.WithMetrics(createMetrics, sl)) |
| 93 |
}) |
| 94 |
} |
| 95 |
|
| 96 |
// WithLogs overrides the default "error not supported" implementation for Factory.CreateLogs and the default "undefined" stability level. |
| 97 |
func WithLogs(createLogs receiver.CreateLogsFunc, sl component.StabilityLevel) FactoryOption { |
| 98 |
return factoryOptionFunc(func(o *factoryOpts) { |
| 99 |
o.opts = append(o.opts, receiver.WithLogs(createLogs, sl)) |
| 100 |
}) |
| 101 |
} |
| 102 |
|
| 103 |
// WithProfiles overrides the default "error not supported" implementation for Factory.CreateProfiles and the default "undefined" stability level. |
| 104 |
func WithProfiles(createProfiles CreateProfilesFunc, sl component.StabilityLevel) FactoryOption { |
| 105 |
return factoryOptionFunc(func(o *factoryOpts) { |
| 106 |
o.profilesStabilityLevel = sl |
| 107 |
o.createProfilesFunc = createProfiles |
| 108 |
}) |
| 109 |
} |
| 110 |
|
| 111 |
// NewFactory returns a Factory. |
| 112 |
func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory { |
| 113 |
opts := factoryOpts{factory: &factory{}} |
| 114 |
for _, opt := range options { |
| 115 |
opt.applyOption(&opts) |
| 116 |
} |
| 117 |
opts.Factory = receiver.NewFactory(cfgType, createDefaultConfig, opts.opts...) |
| 118 |
return opts.factory |
| 119 |
} |