Skip to content

Prometheus exporter target_info key-value pairs must be string #3676

@kornerc

Description

@kornerc

Describe your environment
...

Steps to reproduce
Prometheus expects labels to be string key-value pairs.
Currently, the labels are not cast to string for the target_info

Here is a minimal example which causes this problem:

import time

from prometheus_client import start_http_server

from opentelemetry import metrics
from opentelemetry.exporter.prometheus import PrometheusMetricReader
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.resources import SERVICE_NAME, Resource

resource = Resource(attributes={
    SERVICE_NAME: "your-service-name",
    "foo": "bar",
    "hist": 1,
})

start_http_server(port=8000, addr="localhost")
reader = PrometheusMetricReader()
provider = MeterProvider(resource=resource, metric_readers=[reader])
metrics.set_meter_provider(provider)

meter = metrics.get_meter("my.meter.name")

work_counter = meter.create_counter(
    "work.counter", unit="1", description="Counts the amount of work done"
)

while True:
    work_counter.add(1, {"work.type": "Debug"})
    print("doing some work...")
    time.sleep(1)

It raises an exception in the Prometheus client: AttributeError: ("'int' object has no attribute 'replace'", Metric(target, Target metadata, info ... here prometheus_client\openmetrics\exposition.py", line 33, in <listcomp> k, v.replace('\\', r'\\').replace('\n', r'\n').replace('"', r'\"'))

What is the expected behavior?
Find a string representation for the target_info labels as it is done for the other metrics.

What is the actual behavior?
The target_info labels are exported in their original representation (non-strings).

Additional context
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions