diff --git a/.chloggen/fix-incorrectly-deduplicated-exporterhelper-metric.yaml b/.chloggen/fix-incorrectly-deduplicated-exporterhelper-metric.yaml new file mode 100644 index 00000000000..01e6ce6f27d --- /dev/null +++ b/.chloggen/fix-incorrectly-deduplicated-exporterhelper-metric.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: exporterhelper + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix incorrect deduplication of otelcol_exporter_queue_size and otelcol_exporter_queue_capacity metrics if multiple exporters are used. + +# One or more tracking issues or pull requests related to the change +issues: [10444] + +# 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: [] diff --git a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go index f56e18ba5dc..8e737c817c9 100644 --- a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go +++ b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go @@ -67,11 +67,14 @@ func (builder *TelemetryBuilder) InitQueueLength(cb func() int64) error { "queue_length", metric.WithDescription("This metric is optional and therefore not initialized in NewTelemetryBuilder."), metric.WithUnit("1"), - metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error { - o.Observe(cb(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), ) + if err != nil { + return err + } + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.ObserveInt64(builder.QueueLength, cb(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.QueueLength) return err } @@ -98,12 +101,13 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme "process_runtime_total_alloc_bytes", metric.WithDescription("Cumulative bytes allocated for heap objects (see 'go doc runtime.MemStats.TotalAlloc')"), metric.WithUnit("By"), - metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error { - o.Observe(builder.observeProcessRuntimeTotalAllocBytes(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), ) errs = errors.Join(errs, err) + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.ObserveInt64(builder.ProcessRuntimeTotalAllocBytes, builder.observeProcessRuntimeTotalAllocBytes(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.ProcessRuntimeTotalAllocBytes) + errs = errors.Join(errs, err) builder.RequestDuration, err = builder.meter.Float64Histogram( "request_duration", metric.WithDescription("Duration of request"), diff --git a/cmd/mdatagen/templates/telemetry.go.tmpl b/cmd/mdatagen/templates/telemetry.go.tmpl index 992df6bf328..240a2a90985 100644 --- a/cmd/mdatagen/templates/telemetry.go.tmpl +++ b/cmd/mdatagen/templates/telemetry.go.tmpl @@ -60,7 +60,7 @@ func WithAttributeSet(set attribute.Set) telemetryBuilderOption { {{- end }} {{- range $name, $metric := .Telemetry.Metrics }} - {{- if $metric.Optional }} +{{- if $metric.Optional }} // Init{{ $name.Render }} configures the {{ $name.Render }} metric. func (builder *TelemetryBuilder) Init{{ $name.Render }}({{ if $metric.Data.Async -}}cb func() {{ $metric.Data.BasicType }}{{- end }}) error { var err error @@ -71,13 +71,16 @@ func (builder *TelemetryBuilder) Init{{ $name.Render }}({{ if $metric.Data.Async {{- if eq $metric.Data.Type "Histogram" -}} {{ if $metric.Data.Boundaries -}}metric.WithExplicitBucketBoundaries([]float64{ {{- range $metric.Data.Boundaries }} {{.}}, {{- end }} }...),{{- end }} {{- end }} - {{ if $metric.Data.Async -}} - metric.With{{ casesTitle $metric.Data.BasicType }}Callback(func(_ context.Context, o metric.{{ casesTitle $metric.Data.BasicType }}Observer) error { - o.Observe(cb(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), - {{- end }} ) + {{- if $metric.Data.Async }} + if err != nil { + return err + } + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.Observe{{ casesTitle $metric.Data.BasicType }}(builder.{{ $name.Render }}, cb(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.{{ $name.Render }}) + {{- end }} return err } @@ -117,14 +120,15 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme {{- if eq $metric.Data.Type "Histogram" -}} {{ if $metric.Data.Boundaries -}}metric.WithExplicitBucketBoundaries([]float64{ {{- range $metric.Data.Boundaries }} {{.}}, {{- end }} }...),{{- end }} {{- end }} - {{ if $metric.Data.Async -}} - metric.With{{ casesTitle $metric.Data.BasicType }}Callback(func(_ context.Context, o metric.{{ casesTitle $metric.Data.BasicType }}Observer) error { - o.Observe(builder.observe{{ $name.Render }}(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), - {{- end }} ) errs = errors.Join(errs, err) + {{- if $metric.Data.Async }} + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.Observe{{ casesTitle $metric.Data.BasicType }}(builder.{{ $name.Render }}, builder.observe{{ $name.Render }}(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.{{ $name.Render }}) + errs = errors.Join(errs, err) + {{- end }} {{- end }} {{- end }} return &builder, errs diff --git a/exporter/exporterhelper/internal/metadata/generated_telemetry.go b/exporter/exporterhelper/internal/metadata/generated_telemetry.go index 4383809885e..02eb0be9b4f 100644 --- a/exporter/exporterhelper/internal/metadata/generated_telemetry.go +++ b/exporter/exporterhelper/internal/metadata/generated_telemetry.go @@ -66,11 +66,14 @@ func (builder *TelemetryBuilder) InitExporterQueueCapacity(cb func() int64) erro "exporter_queue_capacity", metric.WithDescription("Fixed capacity of the retry queue (in batches)"), metric.WithUnit("1"), - metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error { - o.Observe(cb(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), ) + if err != nil { + return err + } + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.ObserveInt64(builder.ExporterQueueCapacity, cb(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.ExporterQueueCapacity) return err } @@ -81,11 +84,14 @@ func (builder *TelemetryBuilder) InitExporterQueueSize(cb func() int64) error { "exporter_queue_size", metric.WithDescription("Current size of the retry queue (in batches)"), metric.WithUnit("1"), - metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error { - o.Observe(cb(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), ) + if err != nil { + return err + } + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.ObserveInt64(builder.ExporterQueueSize, cb(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.ExporterQueueSize) return err } diff --git a/processor/batchprocessor/internal/metadata/generated_telemetry.go b/processor/batchprocessor/internal/metadata/generated_telemetry.go index d94151a61d2..a83123aba25 100644 --- a/processor/batchprocessor/internal/metadata/generated_telemetry.go +++ b/processor/batchprocessor/internal/metadata/generated_telemetry.go @@ -96,12 +96,13 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme "processor_batch_metadata_cardinality", metric.WithDescription("Number of distinct metadata value combinations being processed"), metric.WithUnit("1"), - metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error { - o.Observe(builder.observeProcessorBatchMetadataCardinality(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), ) errs = errors.Join(errs, err) + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.ObserveInt64(builder.ProcessorBatchMetadataCardinality, builder.observeProcessorBatchMetadataCardinality(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.ProcessorBatchMetadataCardinality) + errs = errors.Join(errs, err) builder.ProcessorBatchTimeoutTriggerSend, err = builder.meter.Int64Counter( "processor_batch_timeout_trigger_send", metric.WithDescription("Number of times the batch was sent due to a timeout trigger"), diff --git a/service/internal/metadata/generated_telemetry.go b/service/internal/metadata/generated_telemetry.go index dac0e287f35..a7929da8e7e 100644 --- a/service/internal/metadata/generated_telemetry.go +++ b/service/internal/metadata/generated_telemetry.go @@ -119,61 +119,67 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme "process_cpu_seconds", metric.WithDescription("Total CPU user and system time in seconds"), metric.WithUnit("s"), - metric.WithFloat64Callback(func(_ context.Context, o metric.Float64Observer) error { - o.Observe(builder.observeProcessCPUSeconds(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), ) errs = errors.Join(errs, err) + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.ObserveFloat64(builder.ProcessCPUSeconds, builder.observeProcessCPUSeconds(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.ProcessCPUSeconds) + errs = errors.Join(errs, err) builder.ProcessMemoryRss, err = builder.meter.Int64ObservableGauge( "process_memory_rss", metric.WithDescription("Total physical memory (resident set size)"), metric.WithUnit("By"), - metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error { - o.Observe(builder.observeProcessMemoryRss(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), ) errs = errors.Join(errs, err) + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.ObserveInt64(builder.ProcessMemoryRss, builder.observeProcessMemoryRss(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.ProcessMemoryRss) + errs = errors.Join(errs, err) builder.ProcessRuntimeHeapAllocBytes, err = builder.meter.Int64ObservableGauge( "process_runtime_heap_alloc_bytes", metric.WithDescription("Bytes of allocated heap objects (see 'go doc runtime.MemStats.HeapAlloc')"), metric.WithUnit("By"), - metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error { - o.Observe(builder.observeProcessRuntimeHeapAllocBytes(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), ) errs = errors.Join(errs, err) + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.ObserveInt64(builder.ProcessRuntimeHeapAllocBytes, builder.observeProcessRuntimeHeapAllocBytes(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.ProcessRuntimeHeapAllocBytes) + errs = errors.Join(errs, err) builder.ProcessRuntimeTotalAllocBytes, err = builder.meter.Int64ObservableCounter( "process_runtime_total_alloc_bytes", metric.WithDescription("Cumulative bytes allocated for heap objects (see 'go doc runtime.MemStats.TotalAlloc')"), metric.WithUnit("By"), - metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error { - o.Observe(builder.observeProcessRuntimeTotalAllocBytes(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), ) errs = errors.Join(errs, err) + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.ObserveInt64(builder.ProcessRuntimeTotalAllocBytes, builder.observeProcessRuntimeTotalAllocBytes(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.ProcessRuntimeTotalAllocBytes) + errs = errors.Join(errs, err) builder.ProcessRuntimeTotalSysMemoryBytes, err = builder.meter.Int64ObservableGauge( "process_runtime_total_sys_memory_bytes", metric.WithDescription("Total bytes of memory obtained from the OS (see 'go doc runtime.MemStats.Sys')"), metric.WithUnit("By"), - metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error { - o.Observe(builder.observeProcessRuntimeTotalSysMemoryBytes(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), ) errs = errors.Join(errs, err) + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.ObserveInt64(builder.ProcessRuntimeTotalSysMemoryBytes, builder.observeProcessRuntimeTotalSysMemoryBytes(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.ProcessRuntimeTotalSysMemoryBytes) + errs = errors.Join(errs, err) builder.ProcessUptime, err = builder.meter.Float64ObservableCounter( "process_uptime", metric.WithDescription("Uptime of the process"), metric.WithUnit("s"), - metric.WithFloat64Callback(func(_ context.Context, o metric.Float64Observer) error { - o.Observe(builder.observeProcessUptime(), metric.WithAttributeSet(builder.attributeSet)) - return nil - }), ) errs = errors.Join(errs, err) + _, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { + o.ObserveFloat64(builder.ProcessUptime, builder.observeProcessUptime(), metric.WithAttributeSet(builder.attributeSet)) + return nil + }, builder.ProcessUptime) + errs = errors.Join(errs, err) return &builder, errs }