processorhelper

package module
v0.130.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2025 License: Apache-2.0 Imports: 14 Imported by: 192

Documentation

Overview

Example

Example demonstrates the usage of the processor factory.

package main

import (
	"context"
	"fmt"

	"go.opentelemetry.io/collector/component"
	"go.opentelemetry.io/collector/consumer"
	"go.opentelemetry.io/collector/pdata/pmetric"
	"go.opentelemetry.io/collector/processor"
	"go.opentelemetry.io/collector/processor/processorhelper"
)

// typeStr defines the unique type identifier for the processor.
var typeStr = component.MustNewType("example")

// exampleConfig holds configuration settings for the processor.
type exampleConfig struct{}

// exampleProcessor implements the OpenTelemetry processor interface.
type exampleProcessor struct {
	cancel context.CancelFunc
	config exampleConfig
}

// Example demonstrates the usage of the processor factory.
func main() {
	// Instantiate the processor factory and print its type.
	exampleProcessor := NewFactory()
	fmt.Println(exampleProcessor.Type())

}

// NewFactory creates a new processor factory.
func NewFactory() processor.Factory {
	return processor.NewFactory(
		typeStr,
		createDefaultConfig,
		processor.WithMetrics(createExampleProcessor, component.StabilityLevelAlpha),
	)
}

// createDefaultConfig returns the default configuration for the processor.
func createDefaultConfig() component.Config {
	return &exampleConfig{}
}

// createExampleProcessor initializes an instance of the example processor.
func createExampleProcessor(ctx context.Context, params processor.Settings, baseCfg component.Config, next consumer.Metrics) (processor.Metrics, error) {
	// Convert baseCfg to the correct type.
	cfg := baseCfg.(*exampleConfig)

	// Create a new processor instance.
	pcsr := newExampleProcessor(ctx, cfg)

	// Wrap the processor with the helper utilities.
	return processorhelper.NewMetrics(
		ctx,
		params,
		cfg,
		next,
		pcsr.consumeMetrics,
		processorhelper.WithCapabilities(consumer.Capabilities{MutatesData: true}),
		processorhelper.WithShutdown(pcsr.shutdown),
	)
}

// newExampleProcessor constructs a new instance of the example processor.
func newExampleProcessor(ctx context.Context, cfg *exampleConfig) *exampleProcessor {
	pcsr := &exampleProcessor{
		config: *cfg,
	}

	// Create a cancelable context.
	_, pcsr.cancel = context.WithCancel(ctx)

	return pcsr
}

// ConsumeMetrics modify metrics adding one attribute to resource.
func (pcsr *exampleProcessor) consumeMetrics(_ context.Context, md pmetric.Metrics) (pmetric.Metrics, error) {
	rm := md.ResourceMetrics()
	for i := 0; i < rm.Len(); i++ {
		rm.At(i).Resource().Attributes().PutStr("processed_by", "exampleProcessor")
	}

	return md, nil
}

// Shutdown properly stops the processor and releases resources.
func (pcsr *exampleProcessor) shutdown(_ context.Context) error {
	pcsr.cancel()
	return nil
}
Output:

example

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrSkipProcessingData = errors.New("sentinel error to skip processing data from the remainder of the pipeline")

ErrSkipProcessingData is a sentinel value to indicate when traces or metrics should intentionally be dropped from further processing in the pipeline because the data is determined to be irrelevant. A processor can return this error to stop further processing without propagating an error back up the pipeline to logs.

Functions

func NewLogs

func NewLogs(
	_ context.Context,
	set processor.Settings,
	_ component.Config,
	nextConsumer consumer.Logs,
	logsFunc ProcessLogsFunc,
	options ...Option,
) (processor.Logs, error)

NewLogs creates a processor.Logs that ensure context propagation and the right tags are set.

func NewMetrics

func NewMetrics(
	_ context.Context,
	set processor.Settings,
	_ component.Config,
	nextConsumer consumer.Metrics,
	metricsFunc ProcessMetricsFunc,
	options ...Option,
) (processor.Metrics, error)

NewMetrics creates a processor.Metrics that ensure context propagation and the right tags are set.

func NewTraces

func NewTraces(
	_ context.Context,
	set processor.Settings,
	_ component.Config,
	nextConsumer consumer.Traces,
	tracesFunc ProcessTracesFunc,
	options ...Option,
) (processor.Traces, error)

NewTraces creates a processor.Traces that ensure context propagation and the right tags are set.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option apply changes to internalOptions.

func WithCapabilities

func WithCapabilities(capabilities consumer.Capabilities) Option

WithCapabilities overrides the default GetCapabilities function for an processor. The default GetCapabilities function returns mutable capabilities.

func WithShutdown

func WithShutdown(shutdown component.ShutdownFunc) Option

WithShutdown overrides the default Shutdown function for an processor. The default shutdown function does nothing and always returns nil.

func WithStart

func WithStart(start component.StartFunc) Option

WithStart overrides the default Start function for an processor. The default shutdown function does nothing and always returns nil.

type ProcessLogsFunc

type ProcessLogsFunc func(context.Context, plog.Logs) (plog.Logs, error)

ProcessLogsFunc is a helper function that processes the incoming data and returns the data to be sent to the next component. If error is returned then returned data are ignored. It MUST not call the next component.

type ProcessMetricsFunc

type ProcessMetricsFunc func(context.Context, pmetric.Metrics) (pmetric.Metrics, error)

ProcessMetricsFunc is a helper function that processes the incoming data and returns the data to be sent to the next component. If error is returned then returned data are ignored. It MUST not call the next component.

type ProcessTracesFunc

type ProcessTracesFunc func(context.Context, ptrace.Traces) (ptrace.Traces, error)

ProcessTracesFunc is a helper function that processes the incoming data and returns the data to be sent to the next component. If error is returned then returned data are ignored. It MUST not call the next component.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL