Search code examples
c#prometheusopen-telemetry

How to have the Prometheus exporter export labels/tags


I am creating the meter like this:

var meter = fact.Create( MeterName, 
  null, 
  new KeyValuePair<string, object?>[] { 
    new( "id", index.Id ), 
    new( "name", index.Name ) } );

I expected id and name to be rendered as labels/tags. But only otel scopes are rendered which I do not need (but could live with if that would be an addition).

How to have the exporter render the labels/tags? I found How can find or correctly export resource labels of OpenTelemetry metrics in Prometeus? but I don't have a config file like this as I am implementing a micro service exposing /metrics endpoint and there the labels are already missing, so a config file is not applicable.

The scraping is not done by a otel exporter but by some scraping configured through annotations in k8s yaml files


Solution

  • I believe this is potentially caused by a bug in OpenTelemetry.Exporter.Prometheus.HttpListener

    I have opened a bug for this here. Which is based on this conversation in their discussion boards.

    But for now, there is a work around.

    You can get those tags to show up, if you pass them to your actual Add/Set/Record operations.

    So for example:

    var tags = new KeyValuePair<string, object?>[] { 
        new( "id", index.Id ), 
        new( "name", index.Name ) } );
    
    // Don't pass the tags in here,
    var meter = fact.Create(MeterName);
    var counter = meter.CreateCounter<int>("counter");
    
    // Instead pass the tags in via Add/Set/Record.
    counter.Add(1, tags);
    

    It does present a few challenges as you'll have to keep the tags in scope for whenever you need them, but it is good enough for now. I'll be monitoring my question on GitHub for an answer.

    EDIT: Open Telemetry recently published a fix which has this bug resolved. You can see this version here: https://github.com/open-telemetry/opentelemetry-dotnet/releases/tag/coreunstable-1.10.0-beta.1

    It was released: 2024-11-12

    If you update, to that unstable version, your bug should be resolved.