Skip to content

[bug] wrong precision for Histogram buckets with float type #6803

@tbolon

Description

@tbolon

Package

OpenTelemetry.Exporter.OpenTelemetryProtocol

Package Version

Package Name Version
OpenTelemetry.Exporter.OpenTelemetryProtocol 1.14.0

Runtime Version

net10.0, net472

Description

When using an Histogram for a float type (because second is now the recommended time unit) and giving a custom InstrumentAdvice with float values for HistogramBucketBoundaries, the generated buckets are generated with rounding errors due to float lack of precision.

For example, a bucket with a limit of 0.025f will be emitted with a tag named "le0.02500000037252903".

Note that as a workaround, using double will fix this problem.

Steps to Reproduce

  1. Create and start a local otel collector configured with debug traces:
receivers:
  otlp:
    protocols:
      http:
        endpoint: 0.0.0.0:4320

exporters:
  debug:
    verbosity: normal

service:
  pipelines:
    metrics:
      receivers: [otlp]
      exporters: [debug]
  1. Create the following program.cs:
#:package OpenTelemetry.Exporter.OpenTelemetryProtocol@1.14.0

using System.Diagnostics.Metrics;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;

using (Sdk.CreateMeterProviderBuilder().ConfigureResource(r => r.AddService("MyService")).AddMeter("MyApp").AddOtlpExporter(o =>
{
    o.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
    o.Endpoint = new Uri("http://localhost:4320/v1/metrics");
}).Build())
{
    var meter = new Meter("MyApp", "1.0");
    var histogram = meter.CreateHistogram<float>(
        "my_histogram",
        advice: new InstrumentAdvice<float>
        {
            HistogramBucketBoundaries = [
                0.001f, 0.005f, 0.01f, 0.025f, 0.05f, 0.1f, 0.25f, 0.5f, 1f, 2.5f, 5f, 10f, 30f, 60f, 120f]
        });

    while (Console.ReadKey().Key != ConsoleKey.Escape)
    {
        var value = Random.Shared.NextSingle() * 10;
        histogram.Record(value);
        Console.WriteLine($"Recorded value: {value}");
    }
}
  1. Start the program, press some keys and wait the collector to show some logs.

Expected Result

(newlines added for clarity)

ScopeMetrics #0 [email protected]
my_histogram{} count=34 sum=150.80813431739807 min=0.18132150173187256 max=9.395732879638672
  le0.001=0
  le0.005=0
  le0.01=0
  le0.025=0
  le0.05=0
  le0.10=0
  le0.25=2
  le0.5=2
  le1=3
  le2.5=4
  le5=7
  le10=16
  le30=0
  le60=0
  le120=0
  0
        {"resource": {"service.instance.id": "4f79c7e7-8560-4109-a8a4-9f921e51a1e5", "service.name": "otelcol-contrib", "service.version": "0.142.0"}, "otelcol.component.id": "debug", "otelcol.component.kind": "exporter", "otelcol.signal": "metrics"}

Actual Result

(newlines added for clarity)

ScopeMetrics #0 [email protected]
my_histogram{} count=34 sum=150.80813431739807 min=0.18132150173187256 max=9.395732879638672
  le0.0010000000474974513=0
  le0.004999999888241291=0
  le0.009999999776482582=0
  le0.02500000037252903=0
  le0.05000000074505806=0
  le0.10000000149011612=0
  le0.25=2
  le0.5=2
  le1=3
  le2.5=4
  le5=7
  le10=16
  le30=0
  le60=0
  le120=0
  0
        {"resource": {"service.instance.id": "4f79c7e7-8560-4109-a8a4-9f921e51a1e5", "service.name": "otelcol-contrib", "service.version": "0.142.0"}, "otelcol.component.id": "debug", "otelcol.component.kind": "exporter", "otelcol.signal": "metrics"}

Additional Context

Maybe this behavior should be considered as normal, in this case perhaps it should be documented somewhere that float histogram should be avoided and replaced by double.

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