client_python icon indicating copy to clipboard operation
client_python copied to clipboard

OpenMetrics support for textfile collector

Open petarmaric opened this issue 2 years ago • 6 comments

Currently prometheus_client.exposition.write_to_textfile doesn't support the OpenMetrics text format, which I'm addressing with this (private) hack:

from prometheus_client.openmetrics.exposition import (
    generate_latest as generate_latest_openmetrics,
)
from prometheus_client.registry import CollectorRegistry

def write_openmetrics_to_textfile(path: str, registry: CollectorRegistry) -> None:
    """
    Fork of `prometheus_client.exposition.write_to_textfile`, as the project
    didn't provide an OpenMetrics variant of this helper function.
    """
    with open(path, "wb") as fp:
        fp.write(generate_latest_openmetrics(registry))

Would your project be interested in a non-hacky (as is the case above) OpenMetrics support for textfile collector?

petarmaric avatar Sep 18 '23 10:09 petarmaric

Hello, I am not opposed to adding a function to prometheus_client.openmetrics for textfile if it doesn't add confusion, but I am curious what your use case is? As far as I know the node exporter textfile collector only supports the Prometheus format, but I could have missed an update.

csmarchbanks avatar Sep 18 '23 21:09 csmarchbanks

Thank you for the interest :) Honestly, can't give too many specifics right now, as I'm waiting for approval from the company I work for to actually Open Source the tool I'm building.

What I can say right now is that it's a console app and Python API for converting reports outputted by "a tool" into the OpenMetrics text format. Such metrics are then meant to be pushed into Grafana (data source), via VictoriaMetrics API.

petarmaric avatar Sep 20 '23 07:09 petarmaric

Once it's Open Sourced I'll be sure to provide a link here, so you can see the use-case more clearly - there are a few reasons I've opted in for OpenMetrics in lieu of Prometheus text format...

petarmaric avatar Sep 20 '23 07:09 petarmaric

As promised, https://pypi.org/project/locust_csv2openmetrics/ has (finally) hit 1.0.0 today.

We use this tool to convert locust’s load test CSV formatted reports into the OpenMetrics text format, pushing the obtained metrics into Grafana (data source), via VictoriaMetrics API.

petarmaric avatar Nov 20 '23 14:11 petarmaric

Because these load tests are run non-continuously and at irregular time intervals we need to push their metrics into Grafana (data source), as opposed to the much more common way of Prometheus pulling data from a load-tests-like service to collect the metrics. I hope this clarifies our use case.

petarmaric avatar Nov 20 '23 14:11 petarmaric

That use case makes sense to me, it is also the format you need to backfill data into Prometheus using promtool. That said, it looks like what you did is the correct thing to do, call generate_latest and save to a file. The reason write_to_textfile exists is that atomic writes are necessary in order to use the textfile exporter, but backfilling data does not require an atomic write.

csmarchbanks avatar Nov 21 '23 20:11 csmarchbanks