-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move receiver builder into internal service (#10781)
#### Description This moves the receiver builder out of the `receiver` package, and into `service/internal/builders`. There's no real reason for this struct to be public (folks shouldn't call it), and making it private will allow us to add profiling support to it. #### Link to tracking issue #10375 (review)
- Loading branch information
Showing
15 changed files
with
443 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: deprecation | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
component: receiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Deprecate receiver.Builder, and move it into an internal package of the service module | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [10781] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [api] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package builders // import "go.opentelemetry.io/collector/service/internal/builders" | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"go.uber.org/zap" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/receiver" | ||
"go.opentelemetry.io/collector/receiver/receivertest" | ||
) | ||
|
||
var ( | ||
errNilNextConsumer = errors.New("nil next Consumer") | ||
nopType = component.MustNewType("nop") | ||
) | ||
|
||
// Receiver is an interface that allows using implementations of the builder | ||
// from different packages. | ||
type Receiver interface { | ||
CreateTraces(context.Context, receiver.Settings, consumer.Traces) (receiver.Traces, error) | ||
CreateMetrics(context.Context, receiver.Settings, consumer.Metrics) (receiver.Metrics, error) | ||
CreateLogs(context.Context, receiver.Settings, consumer.Logs) (receiver.Logs, error) | ||
Factory(component.Type) component.Factory | ||
} | ||
|
||
// ReceiverBuilder receiver is a helper struct that given a set of Configs and | ||
// Factories helps with creating receivers. | ||
type ReceiverBuilder struct { | ||
cfgs map[component.ID]component.Config | ||
factories map[component.Type]receiver.Factory | ||
} | ||
|
||
// NewReceiver creates a new ReceiverBuilder to help with creating | ||
// components form a set of configs and factories. | ||
func NewReceiver(cfgs map[component.ID]component.Config, factories map[component.Type]receiver.Factory) *ReceiverBuilder { | ||
return &ReceiverBuilder{cfgs: cfgs, factories: factories} | ||
} | ||
|
||
// CreateTraces creates a Traces receiver based on the settings and config. | ||
func (b *ReceiverBuilder) CreateTraces(ctx context.Context, set receiver.Settings, next consumer.Traces) (receiver.Traces, error) { | ||
if next == nil { | ||
return nil, errNilNextConsumer | ||
} | ||
cfg, existsCfg := b.cfgs[set.ID] | ||
if !existsCfg { | ||
return nil, fmt.Errorf("receiver %q is not configured", set.ID) | ||
} | ||
|
||
f, existsFactory := b.factories[set.ID.Type()] | ||
if !existsFactory { | ||
return nil, fmt.Errorf("receiver factory not available for: %q", set.ID) | ||
} | ||
|
||
logStabilityLevel(set.Logger, f.TracesReceiverStability()) | ||
return f.CreateTracesReceiver(ctx, set, cfg, next) | ||
} | ||
|
||
// CreateMetrics creates a Metrics receiver based on the settings and config. | ||
func (b *ReceiverBuilder) CreateMetrics(ctx context.Context, set receiver.Settings, next consumer.Metrics) (receiver.Metrics, error) { | ||
if next == nil { | ||
return nil, errNilNextConsumer | ||
} | ||
cfg, existsCfg := b.cfgs[set.ID] | ||
if !existsCfg { | ||
return nil, fmt.Errorf("receiver %q is not configured", set.ID) | ||
} | ||
|
||
f, existsFactory := b.factories[set.ID.Type()] | ||
if !existsFactory { | ||
return nil, fmt.Errorf("receiver factory not available for: %q", set.ID) | ||
} | ||
|
||
logStabilityLevel(set.Logger, f.MetricsReceiverStability()) | ||
return f.CreateMetricsReceiver(ctx, set, cfg, next) | ||
} | ||
|
||
// CreateLogs creates a Logs receiver based on the settings and config. | ||
func (b *ReceiverBuilder) CreateLogs(ctx context.Context, set receiver.Settings, next consumer.Logs) (receiver.Logs, error) { | ||
if next == nil { | ||
return nil, errNilNextConsumer | ||
} | ||
cfg, existsCfg := b.cfgs[set.ID] | ||
if !existsCfg { | ||
return nil, fmt.Errorf("receiver %q is not configured", set.ID) | ||
} | ||
|
||
f, existsFactory := b.factories[set.ID.Type()] | ||
if !existsFactory { | ||
return nil, fmt.Errorf("receiver factory not available for: %q", set.ID) | ||
} | ||
|
||
logStabilityLevel(set.Logger, f.LogsReceiverStability()) | ||
return f.CreateLogsReceiver(ctx, set, cfg, next) | ||
} | ||
|
||
func (b *ReceiverBuilder) Factory(componentType component.Type) component.Factory { | ||
return b.factories[componentType] | ||
} | ||
|
||
// logStabilityLevel logs the stability level of a component. The log level is set to info for | ||
// undefined, unmaintained, deprecated and development. The log level is set to debug | ||
// for alpha, beta and stable. | ||
func logStabilityLevel(logger *zap.Logger, sl component.StabilityLevel) { | ||
if sl >= component.StabilityLevelAlpha { | ||
logger.Debug(sl.LogMessage()) | ||
} else { | ||
logger.Info(sl.LogMessage()) | ||
} | ||
} | ||
|
||
// NewNopReceiverConfigsAndFactories returns a configuration and factories that allows building a new nop receiver. | ||
func NewNopReceiverConfigsAndFactories() (map[component.ID]component.Config, map[component.Type]receiver.Factory) { | ||
nopFactory := receivertest.NewNopFactory() | ||
configs := map[component.ID]component.Config{ | ||
component.NewID(nopType): nopFactory.CreateDefaultConfig(), | ||
} | ||
factories := map[component.Type]receiver.Factory{ | ||
nopType: nopFactory, | ||
} | ||
|
||
return configs, factories | ||
} |
Oops, something went wrong.