8000 Exemplars not working · Issue #932 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

Exemplars not working #932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mumrah opened this issue Jul 4, 2023 · 3 comments
Closed

Exemplars not working #932

mumrah opened this issue Jul 4, 2023 · 3 comments

Comments

@mumrah
Copy link
mumrah commented Jul 4, 2023

I am not able to get exemplars working in version 0.17.0. Here is a basic reproduction:

from prometheus_client import start_http_server, Summary, Counter
import random
import time

# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
COUNTER = Counter("sample_count", "Sample counter")

# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
    """A dummy function that takes some time."""
    COUNTER.inc(1, exemplar={"foo": "bar"})
    time.sleep(t)

if __name__ == '__main__':
    # Start up the server to expose the metrics.
    start_http_server(8000)
    # Generate some requests.
    while True:
        process_request(random.random())
# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 246.0
python_gc_objects_collected_total{generation="1"} 114.0
python_gc_objects_collected_total{generation="2"} 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable objects found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 39.0
python_gc_collections_total{generation="1"} 3.0
python_gc_collections_total{generation="2"} 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="9",patchlevel="16",version="3.9.16"} 1.0
# HELP request_processing_seconds Time spent processing request
# TYPE request_processing_seconds summary
request_processing_seconds_count 17.0
request_processing_seconds_sum 6.808643133
# HELP request_processing_seconds_created Time spent processing request
# TYPE request_processing_seconds_created gauge
request_processing_seconds_created 1.688505443345786e+09
# HELP sample_count_total Sample counter
# TYPE sample_count_total counter
sample_count_total 18.0
# HELP sample_count_created Sample counter
# TYPE sample_count_created gauge
sample_count_created 1.6885054433458078e+09

I also tried the text exporter and had the same result.

@mumrah
Copy link
Author
mumrah commented Jul 4, 2023

I tried something like this

diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py
index deaa6ed..b1f77ce 100644
--- a/prometheus_client/exposition.py
+++ b/prometheus_client/exposition.py
@@ -192,7 +192,11 @@ def generate_latest(registry: CollectorRegistry = REGISTRY) -> bytes:
         if line.timestamp is not None:
             # Convert to milliseconds.
             timestamp = f' {int(float(line.timestamp) * 1000):d}'
-        return f'{line.name}{labelstr} {floatToGoString(line.value)}{timestamp}\n'
+
+        exemplar = ''
+        if line.exemplar:
+            exemplar = ' # {' + ','.join(f'{key}="{value}"' for key, value in line.exemplar.labels.items()) + '}'
+        return f'{line.name}{labelstr} {floatToGoString(line.value)}{timestamp}{exemplar}\n'
 
     output = []
     for metric in registry.collect():

and it seemed to work. No clue if this is the right way to fix this though..

< A81B !-- '"` -->

@csmarchbanks
Copy link
Member

Hello, exemplars are only supported in the OpenMetrics exposition format, not in the basic Prometheus exposition format. If you add the header Accept: application/openmetrics-text to your request do you see exemplars?

Note that Prometheus will automatically negotiate to use OpenMetrics.

@mumrah
Copy link
Author
mumrah commented Jul 6, 2023

Ah, thanks! After restarting Prometheus with --enable-feature=exemplar-storage I started seeing my exemplars show up.

Maybe a sentence clarifying this in the "Exemplars" section of the README would help other newbies like me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0