On Sunday, 2 January 2022 at 03:05:16 UTC ee1 wrote:

> 1 - With this client, is it possible, and is anyone aware of a simple 
> example, of only executing the metric-generating function when a scrape 
> comes in from prometheus,?
>

Yes: for a gauge, you can register a callback function.  The function will 
be triggered on a scrape.
https://github.com/prometheus/client_python#gauge

Another way is using Custom Collectors:
https://github.com/prometheus/client_python#custom-collectors

(Obviously for counters, you need to increment or accumulate the counter 
whenever the metric-generating event happens - otherwise you wouldn't know 
the total value when the scrape occurs)

 

>
> I notice that the HTTP server included ignores the URI of the request and 
> returns the metrics regardless.  e.g. This works just fine: 
> http://localhost:8000/foo?doesntmatter=anything
>
> 2 - Similarly is it possible/available Python example, of using the 
> included HTTP server but parsing HTTP parameters that come in from 
> prometheus in order to write a multi-target exporter like described here 
> <https://prometheus.io/docs/guides/multi-target-exporter/>? 
>
>
Bolt it onto your own http.server 
<https://docs.python.org/3/library/http.server.html> instead. See the docs 
here: https://github.com/prometheus/client_python#http

"To add Prometheus exposition to an existing HTTP server, see the 
MetricsHandler 
<https://github.com/prometheus/client_python/blob/v0.12.0/prometheus_client/exposition.py#L229-L247>
 class 
which provides a BaseHTTPRequestHandler. It also serves as a simple example 
of how to write a custom endpoint."
 

-- 
You received this message because you are subscribed to the Google Groups 
"Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/prometheus-users/869e8b4a-982c-43f2-ad9f-6247bb1ce259n%40googlegroups.com.

Reply via email to