🌐
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.
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.

🌐
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
🌐
Google Groups
groups.google.com › g › prometheus-users › c › 83pEAX44L3M
Easy way to parse out rule expression to extract value (remove comparator)
June 2, 2021 - In general yes, the PromQL Go library lets you parse an expression and then traverse its abstract syntax tree, using the types from the "ast" package in https://github.com/prometheus/prometheus/blob/main/promql/parser/ast.go.
🌐
GitHub
github.com › yunyu › parse-prometheus-text-format › blob › master › README.md
parse-prometheus-text-format/README.md at master · yunyu/parse-prometheus-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.
Author: yunyu