Thank you sir for that guidance. Tested the callback function approach and
that seems to be simple and fits the use case well. This seems to work:
from prometheus_client import start_http_server, Gauge
import random
import time
g = Gauge('some_test_metric', 'TEST METRIC')
def test_gauge():
x = random.random()
print(x)
return(x)
g.set_function(lambda: test_gauge())
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(8000)
while True:
time.sleep(5)
Will have to do some more head scratching on bolting it on to own HTTP
server (due to my own noobness in this area) but thanks for pointing out
the direction.
On Sunday, January 2, 2022 at 4:57:45 AM UTC-5 Brian Candler wrote:
> 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/0d8ef332-b757-42ab-ae40-1dd3ad65603dn%40googlegroups.com.