Skip to content

mdatagen Histogram Metric Compilation Error #13575

@lukeod

Description

@lukeod

Component(s)

cmd/mdatagen

Problem

When defining a histogram metric in the metrics: section of metadata.yaml, mdatagen generates Go code that fails to compile. The build fails with the following error:

internal/metadata/generated_metrics.go:50:5: dp.SetDoubleValue undefined (type pmetric.HistogramDataPoint has no field or method SetDoubleValue)

How to Reproduce

  1. Create a minimal project structure with the following files:

    metadata.yaml:

    type: test
    
    status:
      class: receiver
      stability:
        development: [metrics]
    
    metrics:
      test.duration:
        description: Test histogram metric
        enabled: true
        unit: s
        histogram:
          value_type: double
        attributes: []

    doc.go:

    // Package testhistogramissue demonstrates mdatagen histogram bug.
    package testhistogramissue

    go.mod:

    module testhistogramissue
    
    go 1.22
  2. Run mdatagen to generate the component files:

    mdatagen metadata.yaml
  3. Install dependencies:

    go get go.opentelemetry.io/collector/pdata@v1.37.0
    go get go.opentelemetry.io/collector/component@v1.37.0
    go get go.opentelemetry.io/collector/receiver@v1.37.0
    go get go.opentelemetry.io/collector/confmap@v1.37.0
  4. Attempt to build the generated code:

    go build ./internal/metadata

    The build will fail with the error shown above.

Generated Code Issue

The problematic code is generated in internal/metadata/generated_metrics.go at line 50:

func (m *metricTestDuration) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val float64) {
    if !m.config.Enabled {
        return
    }
    dp := m.data.Histogram().DataPoints().AppendEmpty()
    dp.SetStartTimestamp(start)
    dp.SetTimestamp(ts)
    dp.SetDoubleValue(val)  // ← This line causes the compilation error
}

Root Cause

The code generation template incorrectly assumes all metric types use the same data point methods. The template generates:

dp.SetDoubleValue(val)

This works for pmetric.NumberDataPoint (used by Gauge and Sum metrics) but fails for pmetric.HistogramDataPoint.

What HistogramDataPoint Actually Requires

The pmetric.HistogramDataPoint type doesn't have SetDoubleValue() or SetIntValue() methods. Instead, it requires:

  • SetCount(uint64) - to set the number of observations
  • SetSum(float64) - to set the sum of observations
  • ExplicitBounds().FromRaw([]float64) - to set bucket boundaries
  • BucketCounts().FromRaw([]uint64) - to set the count per bucket

The current mdatagen template doesn't account for this structural difference between histogram and scalar metric types.

Environment

  • OpenTelemetry Collector version: v1.37.0
  • Go version: 1.24.4
  • mdatagen: v0.131.0

Impact

This issue prevents the use of histogram metrics in the metrics: section of any OpenTelemetry Collector component. Currently, it seems that no components in opentelemetry-collector-contrib use histograms for component metrics. There are cases where it should arguably be used - such as latency distributions.

Additional Context

  • This issue only affects component metrics (in the metrics: section)
  • Histogram metrics in the telemetry: section work correctly as they use a different code generation path via the OpenTelemetry SDK
  • The workaround is to use Gauge metrics instead of histograms, though this loses the benefits of histogram aggregation

Collector version

v1.37.0

Environment information

Environment

  • OpenTelemetry Collector version: v1.37.0
  • Go version: 1.24.4
  • mdatagen: v0.131.0

OpenTelemetry Collector configuration

Log output

Additional context

No response

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions