// Code generated by mdatagen. DO NOT EDIT.

package metadata

import (
	"errors"
	"sync"

	"go.opentelemetry.io/otel/metric"
	"go.opentelemetry.io/otel/trace"

	"go.opentelemetry.io/collector/component"
)

func Meter(settings component.TelemetrySettings) metric.Meter {
	return settings.MeterProvider.Meter("go.opentelemetry.io/collector/processor/processorhelper")
}

func Tracer(settings component.TelemetrySettings) trace.Tracer {
	return settings.TracerProvider.Tracer("go.opentelemetry.io/collector/processor/processorhelper")
}

// TelemetryBuilder provides an interface for components to report telemetry
// as defined in metadata and user config.
type TelemetryBuilder struct {
	meter                     metric.Meter
	mu                        sync.Mutex
	registrations             []metric.Registration
	ProcessorIncomingItems    metric.Int64Counter
	ProcessorInternalDuration metric.Float64Histogram
	ProcessorOutgoingItems    metric.Int64Counter
}

// TelemetryBuilderOption applies changes to default builder.
type TelemetryBuilderOption interface {
	apply(*TelemetryBuilder)
}

type telemetryBuilderOptionFunc func(mb *TelemetryBuilder)

func (tbof telemetryBuilderOptionFunc) apply(mb *TelemetryBuilder) {
	tbof(mb)
}

// Shutdown unregister all registered callbacks for async instruments.
func (builder *TelemetryBuilder) Shutdown() {
	builder.mu.Lock()
	defer builder.mu.Unlock()
	for _, reg := range builder.registrations {
		reg.Unregister()
	}
}

// NewTelemetryBuilder provides a struct with methods to update all internal telemetry
// for a component
func NewTelemetryBuilder(settings component.TelemetrySettings, options ...TelemetryBuilderOption) (*TelemetryBuilder, error) {
	builder := TelemetryBuilder{}
	for _, op := range options {
		op.apply(&builder)
	}
	builder.meter = Meter(settings)
	var err, errs error
	builder.ProcessorIncomingItems, err = builder.meter.Int64Counter(
		"otelcol_processor_incoming_items",
		metric.WithDescription("Number of items passed to the processor. [Alpha]"),
		metric.WithUnit("{item}"),
	)
	errs = errors.Join(errs, err)
	builder.ProcessorInternalDuration, err = builder.meter.Float64Histogram(
		"otelcol_processor_internal_duration",
		metric.WithDescription("Duration of time taken to process a batch of telemetry data through the processor. [Alpha]"),
		metric.WithUnit("s"),
	)
	errs = errors.Join(errs, err)
	builder.ProcessorOutgoingItems, err = builder.meter.Int64Counter(
		"otelcol_processor_outgoing_items",
		metric.WithDescription("Number of items emitted from the processor. [Alpha]"),
		metric.WithUnit("{item}"),
	)
	errs = errors.Join(errs, err)
	return &builder, errs
}
