Files
Bogdan DrutuandGitHub 8a94f0eb89 Clarify that data cannot be accessed after Consume* func is called (#12284)
The problem here is that we have components like batchprocessor or
others which are keeping a reference of the data and eventually
modifying them without any concurrent access protection. Hence, it is
required for the callers to not access any data that were passed
downstream.

This is not a backwards incompatible change, since pdata is not
thread-safe this is a MUST to have here (consider a bug for not being
documented) otherwise things will not work in the processing pipelines.

Signed-off-by: Bogdan Drutu <[email protected]>
2025-02-06 16:50:29 +00:00

45 lines
1.4 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package consumer // import "go.opentelemetry.io/collector/consumer"
import (
"context"
"go.opentelemetry.io/collector/consumer/internal"
"go.opentelemetry.io/collector/pdata/pmetric"
)
// Metrics is an interface that receives pmetric.Metrics, processes it
// as needed, and sends it to the next processing node if any or to the destination.
type Metrics interface {
internal.BaseConsumer
// ConsumeMetrics processes the metrics. After the function returns, the metrics are no longer accessible,
// and accessing them is considered undefined behavior.
ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error
}
// ConsumeMetricsFunc is a helper function that is similar to ConsumeMetrics.
type ConsumeMetricsFunc func(ctx context.Context, md pmetric.Metrics) error
// ConsumeMetrics calls f(ctx, md).
func (f ConsumeMetricsFunc) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
return f(ctx, md)
}
type baseMetrics struct {
*internal.BaseImpl
ConsumeMetricsFunc
}
// NewMetrics returns a Metrics configured with the provided options.
func NewMetrics(consume ConsumeMetricsFunc, options ...Option) (Metrics, error) {
if consume == nil {
return nil, errNilFunc
}
return &baseMetrics{
BaseImpl: internal.NewBaseImpl(options...),
ConsumeMetricsFunc: consume,
}, nil
}