🌐
client_python
prometheus.github.io › client_python › parser
Parser | client_python - Prometheus
November 14, 2023 - The Python client supports parsing the Prometheus text format. This is intended for advanced use cases where you have servers exposing Prometheus metrics and need to get them into some other system. from prometheus_client.parser import text_string_to_metric_families for family in ...
🌐
GitHub
github.com › prometheus › client_python › blob › master › prometheus_client › parser.py
client_python/prometheus_client/parser.py at master · prometheus/client_python
Prometheus instrumentation library for Python applications - client_python/prometheus_client/parser.py at master · prometheus/client_python
Author: prometheus
Discussions

parsing - Parse prometheus metrics data to add label and re-parse to prometheus metrics format - Stack Overflow
I just want to take out the name,labels,value and timestamp but I can't take it out to process, unlike python · from prometheus_client.parser import text_string_to_metric_families import requests metrics = """ # HELP net_conntrack_dialer_conn_attempted_total # TYPE net_conntrack_dialer_co... More on stackoverflow.com
🌐 stackoverflow.com
python , how to import prometheus endpoint metrics for processing by another system
Hopefully someone has already done this as I can't much in the way of examples or information. I just want to validate I am on the right track and… More on reddit.com
🌐 r/PrometheusMonitoring
1
0
September 12, 2020
Use official Prometheus Python parser
I'm the maintainer of the Prometheus Python client, and noticed that you're hand parsing the Prometheus metrics format. Your implementation is not quite correct (see https://gitlab.com/gitl... More on github.com
🌐 github.com
5
July 14, 2017
go - How to parse Prometheus data - Stack Overflow
I have been able to obtain the metrices by sending an HTTP GET as follows: # TYPE net_conntrack_dialer_conn_attempted_total untyped net_conntrack_dialer_conn_attempted_total{dialer_name="feder... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Robust Perception
robustperception.io › productive-prometheus-python-parsing
Productive Prometheus Python Parsing – Robust Perception | Prometheus Monitoring Experts
May 8, 2017 - First install the python client and requests, if you don't already have them: $ pip install prometheus_client requests · Then we can fetch the data we need, and print it: from prometheus_client.parser import text_string_to_metric_families import requests metrics = requests.get("http://demo.robustperception.io:9100/metrics").content for family in text_string_to_metric_families(metrics): for sample in family.samples: print("Name: {0} Labels: {1} Value: {2}".format(*sample)) These few lines of code can be extended to do whatever you like!
🌐
PyPI
pypi.org › project › promql-parser
promql-parser · PyPI
promql-parser Python binding, a PromQL parser for Python. pip install promql-parser · import promql_parser ast = promql_parser.parse('prometheus_http_requests_total{code="200", job="prometheus"}') print(ast) This work is released under the MIT license. A copy of the license is provided in the LICENSE file.
      » pip install promql-parser
    
Published: Apr 06, 2026
Version: 0.8.0
🌐
GitHub
github.com › prometheus › client_python › blob › master › prometheus_client › openmetrics › parser.py
client_python/prometheus_client/openmetrics/parser.py at master · prometheus/client_python
Prometheus instrumentation library for Python applications - client_python/prometheus_client/openmetrics/parser.py at master · prometheus/client_python
Author: prometheus
🌐
Stack Overflow
stackoverflow.com › questions › 66059132 › parse-prometheus-metrics-data-to-add-label-and-re-parse-to-prometheus-metrics-fo
parsing - Parse prometheus metrics data to add label and re-parse to prometheus metrics format - Stack Overflow
package main import ( "flag" "fmt" "log" "os" dto "github.com/prometheus/client_model/go" "github.com/prometheus/common/expfmt" ) func fatal(err error) { if err != nil { log.Fatalln(err) } } func parseMF(path string) (map[string]*dto.MetricFamily, error) { reader, err := os.Open(path) if err != nil { return nil, err } var parser expfmt.TextParser mf, err := parser.TextToMetricFamilies(reader) if err != nil { return nil, err } return mf, nil } func main() { f := flag.String("f", "", "set filepath") flag.Parse() mf, err := parseMF(*f) fatal(err) for k, v := range mf { fmt.Println("KEY: ", k) fmt.Println("VAL: ", v) } }
🌐
Reddit
reddit.com › r/prometheusmonitoring › python , how to import prometheus endpoint metrics for processing by another system
r/PrometheusMonitoring on Reddit: python , how to import prometheus endpoint metrics for processing by another system
September 12, 2020 - Looking at the prometheus client python, there is an example at the bottom of the page. from prometheus_client.parser import text_string_to_metric_families for family in text_string_to_metric_families(u"my_gauge 1.0\n"): for sample in family.samples: print("Name: {0} Labels: {1} Value: {2}".format(*sample)) I'm struggling with what to send the function, sending everything as below throws a typeerror.
🌐
PyPI
pypi.org › project › prometheus-client › 0.14.1
prometheus-client · PyPI
The Python client supports parsing the Prometheus text format. This is intended for advanced use cases where you have servers exposing Prometheus metrics and need to get them into some other system. from prometheus_client.parser import ...
🌐
GitHub
github.com › yunyu › parse-prometheus-text-format
GitHub - yunyu/parse-prometheus-text-format: JavaScript parser for the Prometheus metrics text format
A module that parses the Prometheus text format. The output aims to be compatible with that of prom2json. The code for parsing individual samples was ported from the Prometheus Python Client.
Starred by 39 users
Forked by 14 users
Languages: JavaScript 100.0% | JavaScript 100.0%
Find elsewhere
🌐
GitHub
github.com › messense › py-promql-parser
GitHub - messense/py-promql-parser: PromQL parser for Python
promql-parser Python binding, a PromQL parser for Python. pip install promql-parser · import promql_parser ast = promql_parser.parse('prometheus_http_requests_total{code="200", job="prometheus"}') print(ast) This work is released under the MIT license.
Starred by 26 users
Forked by 6 users
Languages: Rust 75.4% | Python 24.6% | Rust 75.4% | Python 24.6%
🌐
Devon Burriss' Blog
devonburriss.me › prometheus-parser-fennel
Creating a Prometheus parser: Fennel - Devon Burriss' Blog
December 24, 2020 - It can parse Prometheus text to objects, and turn these metric objects into valid Prometheus text. This was my first time using a library to do a custom parser. In the past when I had needed to parse text I had used a state machine and consumed a character at a time.
🌐
GitHub
github.com › DataDog › dd-agent › issues › 3439
Use official Prometheus Python parser · Issue #3439 · DataDog/dd-agent
July 14, 2017 - I'm the maintainer of the Prometheus Python client, and noticed that you're hand parsing the Prometheus metrics format. Your implementation is not quite correct (see https://gitlab.com/gitlab-org/gitlab-ce/issues/35028) and will need to be maintained in the face of any future changes. I'd recommend switching to the python parser provided by the client as it was created for use cases like these and is in use by other monitoring systems.
Author: DataDog
🌐
GitHub
github.com › prometheus › client_python › blob › master › tests › openmetrics › test_parser.py
client_python/tests/openmetrics/test_parser.py at master · prometheus/client_python
Prometheus instrumentation library for Python applications - client_python/tests/openmetrics/test_parser.py at master · prometheus/client_python
Author: prometheus
🌐
npm
npmjs.com › package › parse-prometheus-text-format
parse-prometheus-text-format - npm
August 12, 2019 - A module that parses the Prometheus text format. The output aims to be compatible with that of prom2json. The code for parsing individual samples was ported from the Prometheus Python Client.
      » npm install parse-prometheus-text-format
    
Published: Aug 12, 2019
Version: 1.1.1
Author: Yunyu Lin
🌐
Medium
medium.com › @shaharewq0 › creating-a-custom-prometheus-exporter-with-python-257b6492a0a0
Creating a Custom Prometheus Exporter with Python | by Shahar Cohen hadad | Medium
September 7, 2024 - In this article I tried to explain how to write a Prometheus exporter using python. Notice the core library is the Prometheus client, it is the one that let us define the structure of our metric and is the foundation from it we are able to scrape our data to Prometheus.
🌐
Google Groups
groups.google.com › g › prometheus-users › c › 1dtFMQH0fRs
Prometheus Python Client - How to collect only on scrape and control targets?
January 2, 2022 - The Python client examples I've been able to find all, including the one here, start the HTTP server on the side, and run the metric-generating function in a loop regardless of whether, or at what frequency, prometheus is scraping that metric.
Top answer
1 of 1
24

There's a nice package already available to do that and it's by the Prometheus's Authors itself.

They have written a bunch of Go libraries that are shared across Prometheus components and libraries. They are considered internal to Prometheus but you can use them.

Refer: github.com/prometheus/common doc. There's a package called expfmt that can decode and encode the Prometheus's Exposition Format (Link). Yes, it follows the EBNF syntax so ebnf package could also be used but you're getting expfmt right out of the box.

Package used: expfmt

Sample Input:

# HELP net_conntrack_dialer_conn_attempted_total
# TYPE net_conntrack_dialer_conn_attempted_total untyped
net_conntrack_dialer_conn_attempted_total{dialer_name="federate",instance="localhost:9090",job="prometheus"} 1 1608520832877

Sample Program:

package main

import (
    "flag"
    "fmt"
    "log"
    "os"

    dto "github.com/prometheus/client_model/go"
    "github.com/prometheus/common/expfmt"
)

func fatal(err error) {
    if err != nil {
        log.Fatalln(err)
    }
}

func parseMF(path string) (map[string]*dto.MetricFamily, error) {
    reader, err := os.Open(path)
    if err != nil {
        return nil, err
    }

    var parser expfmt.TextParser
    mf, err := parser.TextToMetricFamilies(reader)
    if err != nil {
        return nil, err
    }
    return mf, nil
}

func main() {
    f := flag.String("f", "", "set filepath")
    flag.Parse()

    mf, err := parseMF(*f)
    fatal(err)

    for k, v := range mf {
        fmt.Println("KEY: ", k)
        fmt.Println("VAL: ", v)
    }
}

Sample Output:

KEY:  net_conntrack_dialer_conn_attempted_total
VAL:  name:"net_conntrack_dialer_conn_attempted_total" type:UNTYPED metric:<label:<name:"dialer_name" value:"federate" > label:<name:"instance" value:"localhost:9090" > label:<name:"job" value:"prometheus" > untyped:<value:1 > timestamp_ms:1608520832877 >

So, expfmt is a good choice for your use-case.

Update: Formatting problem in OP's posted input:

Refer:

  1. https://github.com/prometheus/pushgateway/issues/147#issuecomment-368215305

  2. https://github.com/prometheus/pushgateway#command-line

Note that in the text protocol, each line has to end with a line-feed
character (aka 'LF' or '\n'). Ending a line in other ways, e.g. with 
'CR' aka '\r', 'CRLF' aka '\r\n', or just the end of the packet, will
result in a protocol error.

But from the error message, I could see \r char is present in in the put which is not acceptable by design. So use \n for line endings.

🌐
Reddit
reddit.com › r/prometheusmonitoring › what is prometheus promql parser?
r/PrometheusMonitoring on Reddit: What is prometheus promql parser?
June 30, 2023 -

Hey folks. I was looking into how to better do monitoring/dashboarding as code. I thought about parsing promql, so you can go between in-dashboard-ui experimenting and IDE experimenting as seamlessly as possible. Clearly it must be parsed to be linted and operated on.

I found that, obviously, Prometheus has a promql parser. But I can't get any info on it. Can I leverage it to do what I want? what does the "parsed" version of a promql query look like? How does it handle variables?

I saw this post about a rust promql parser, where they mention AST (abstract syntax tree). Does prometheus promql parser do something similar?

Any help with info on prometheus promql parser or going from UI to IDE would be much appreciated.

Top answer
1 of 3
4
The PromQL parser entry point is here: https://github.com/prometheus/prometheus/blob/main/promql/parser/parse.go#L172 what does the "parsed" version of a promql query look like? A parsed promql query is a series of different golang structs that all conform to the interfaces in https://github.com/prometheus/prometheus/blob/main/promql/parser/ast.go How does it handle variables? Do you mean Grafana variables? Its doesn't - they are handled by Grafana. If you're processing queries with them in, you'll need to handle them yourself, like this: https://github.com/grafana/dashboard-linter/blob/main/lint/variables.go#L179 I'd checkout this linter project, as it does something similar to what you're proposing I think.
2 of 3
1
This is from https://en.wikipedia.org/wiki/Parsing A parser is a software component that takes input data (frequently text) and builds a data structure – often some kind of parse tree, abstract syntax tree or other hierarchical structure, giving a structural representation of the input while checking for correct syntax. As for promql-parser , this is written in Rust and is designed to be highly-compatible as Prometheus's PromQL Parser, it covers most (99%+) of the test cases from Prometheus parser module. PromQL is just a query language to retrieve data from Prometheus and Prometheus-compatible time-series database, and you can draw your dashboards using the data. Also, you can use SQL to do the same thing in order to draw panels using your data. You can visit Grafana datasources to see how Grafana manages different datasources. monitoring/dashboarding as code In my opinion, this hugely depends on your Dashboard solution, not just PromQL Parser. If you choose Grafana as your dashboarding tool, and using Prometheus-compatible time-series database or servece to retrive data, PromQL Parser can only help you with expression linting, formatting and validation, and nothing else. The parser itself can't help you to get the detail of your data, thus has no ability to lint, or inspect. Also Grafana is on its way to supply the Dashboard as Code ability, like https://github.com/grafana/grafonnet , https://github.com/grafana/grizzly , and maybe you will have to spend much time on the ability of linting or formatting dashboard code, rendering dashboard, etc.
🌐
PyPI
pypi.org › project › pygments-promql
pygments-promql · PyPI
This Python package provides a Pygments lexer for the Prometheus Query Language.
      » pip install pygments-promql
    
Published: Apr 14, 2026
Version: 0.2.6