Discussions

python - Expose package metrics to Prometheus with prometheus_client - Stack Overflow
Configure the Prometheus server to target this metric server|endpoint · If you run the Three Step Demo example on the page that you reference, and then browse http://localhost:8000, you should see something like: # HELP python_gc_objects_collected_total Objects collected during gc # TYPE ... More on stackoverflow.com
🌐 stackoverflow.com
FastAPI and Prometheus endpoint
You won't be able to start 2 servers/processes listening on the same port. First one to start, wins. The other will fail to bind as the port is already in use. What you could do though is start both on different ports, say 8080 and 8081 or something, then put nginx in front of it all and proxy through to which ever backend based on the requested path. As a bonus you can terminate TLS at the nginx level using a single cert etc etc too. More on reddit.com
🌐 r/FastAPI
5
1
December 16, 2022
python webhook for prometheus alerts

It comes in automatically. You don't need to specify what to send in AlertManager. You'll get everything.

Try a pprint of the data that your webhook receives:

{'annotations': {'description': 'Swap is filling up (>85%)\n'
                                '  VALUE = 92.06349206349206\n'
                                '  LABELS: map[ec2_Name:yyyy '
                                    ....
                                'instance:yyy '
                                'job:xxx]',
                 'summary': 'Host swap is filling up (instance '
                            'xxxx)'},
 'endsAt': '0001-01-01T00:00:00Z',
 'fingerprint': 'f69e8abd63b73b89',
 'generatorURL': '...',
 'labels': {'alertname': 'HostSwapIsFillingUp',
...
 'startsAt': '2021-07-09T12:08:47.428Z',
 'status': 'firing'}
More on reddit.com
🌐 r/PrometheusMonitoring
1
0
July 9, 2021
Prometheus Middleware is out!
This is great work! Thanks for sharing Will try it out soon More on reddit.com
🌐 r/FastAPI
10
14
October 12, 2020
🌐
Sysdig
sysdig.com › blog › prometheus-metrics
Prometheus metrics / OpenMetrics code instrumentation. | Sysdig
March 27, 2026 - $ git clone https://github.com/sysdiglabs/custom-metrics-examples $ docker build custom-metrics-examples/prometheus/python -t prometheus-python $ docker run -d --rm --name prometheus-python -p 8080:8080 -p 80:80 prometheus-python
🌐
Medium
medium.com › @letathenasleep › exposing-python-metrics-with-prometheus-c5c837c21e4d
Exposing Python Metrics with Prometheus | by Adso | Medium | Medium
July 6, 2023 - First, let’s create a Python API app using Flask. Create a new directory for your project and navigate to it. Then, create a file named app.py with the following code: from flask import Flask, jsonify, request from prometheus_client import ...
Starred by 109 users
Forked by 44 users
Languages: Python
🌐
Prometheus
prometheus.io › docs › instrumenting › clientlibs
Client libraries | Prometheus
Choose a Prometheus client library that matches the language in which your application is written. This lets you define and expose internal metrics via an HTTP endpoint on your application’s instance: Go · Java or Scala · Node.js · Python · Ruby · Rust ·
🌐
Better Stack
betterstack.com › community › guides › monitoring › prometheus-python-metrics
Python Monitoring with Prometheus (Beginner's Guide) | Better Stack Community
February 17, 2025 - To demonstrate Prometheus instrumentation in Python applications, let's set up a simple "Hello World" Flask application along with the Prometheus server.
🌐
GitHub
gist.github.com › tembleking › 0f8c474bf1c93d7f75f1a35c8ace134a
Prometheus Python Example · GitHub
Prometheus Python Example. GitHub Gist: instantly share code, notes, and snippets.
Find elsewhere
🌐
ITNEXT
itnext.io › prometheus-building-a-custom-prometheus-exporter-in-python-988908327600
Prometheus: Building a Custom Prometheus Exporter in Python | by Arseny Zinchenko (setevoy) | ITNEXT
May 21, 2023 - For example, we can refer to some API and get data from it, in this example it will be Jenkins: #!/usr/bin/env python import time import random from prometheus_client import start_http_server, Gauge from api4jenkins import Jenkins jenkins_client = Jenkins('http://localhost:8080/', auth=('admin', 'admin')) jenkins_jobs_counter = Gauge('jenkins_jobs_count', "Number of Jenkins jobs") def get_metrics(): jenkins_jobs_counter.set(len(list(jenkins_client.iter_jobs()))) if __name__ == '__main__': start_http_server(9000) while True: get_metrics() time.sleep(15)
🌐
MetricFire
metricfire.com › blog › how-to-monitor-python-applications-with-prometheus
How to monitor Python Applications with Prometheus | MetricFire
September 15, 2023 - This method is our favorite here at MetricFire. We actually use this method to monitor our own application with Prometheus. This method entails using the Prometheus Python Client, which handles multi-process apps on gunicorn application server.
🌐
Full Stack Python
fullstackpython.com › prometheus.html
Prometheus - Full Stack Python
This primer on Prometheus walks through installation, configuration and metrics collection. Monitoring synchronous Python web apps with Prometheus and its asynchronous monitoring counterpart are two tutorials that show how to add middleware to your web apps that allows Prometheus metrics collection.
🌐
client_python
prometheus.github.io › client_python
client_python
This tutorial shows the quickest way to get started with the Prometheus Python library.
🌐
DBI Services
dbi-services.com › accueil › instrument your python application with prometheus (part1)
Instrument your python application with Prometheus (Part1)
July 10, 2023 - Let’s start by installing the Prometheus client. To do this, we will execute the command pip install prometheus-client ... This command will allow us to install the library so that we can access it in our Python code.
🌐
DEV Community
dev.to › leapcell › understanding-prometheus-and-monitoring-python-applications-3d0p
Understanding Prometheus and Monitoring Python Applications - DEV Community
May 28, 2025 - For example, if the initial value is 1024 bytes and processes consume an additional 300 bytes, the gauge rises to 1324 bytes. If processes release 100 bytes, it drops to 1224 bytes.
🌐
Blog
asserts.ai › home › monitoring python using prometheus
Monitoring Python Using Prometheus - Asserts
June 20, 2023 - That's all there is to it, the standard Prometheus /metrics endpoint will be exposed along with any endpoints of the application. The following metrics for each endpoint will be available: ... Unlike commercial products, there's no limit to the number of additional metrics you can add. metrics.info('version_info', 'application version', version='1.0.1') Following on from the previous example the Prometheus Flask Exporter library provides a convenience call to create additional gauges to hold for example version or build information.
Top answer
1 of 1
7

Prometheus server scrapes HTTP endpoints that provide metrics. This differs from some other metric systems where the metrics are pushed to the metric system. Because Prometheus scrapes metric endpoints, you need to do two things:

  1. Expose the metrics from your client using an HTTP server|endpoint
  2. Configure the Prometheus server to target this metric server|endpoint

If you run the Three Step Demo example on the page that you reference, and then browse http://localhost:8000, you should see something like:

# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 357.0
python_gc_objects_collected_total{generation="1"} 0.0
python_gc_objects_collected_total{generation="2"} 0.0
...
...
# HELP request_processing_seconds Time spent processing request
# TYPE request_processing_seconds summary
request_processing_seconds_count 4.0
request_processing_seconds_sum 2.0374009040533565
# HELP request_processing_seconds_created Time spent processing request
# TYPE request_processing_seconds_created gauge
request_processing_seconds_created 1.6004497426536365e+09

This is the page that your Prometheus server would be configured to scrape. You can see, e.g. python_gc_objects_collected_total counter is provided. Prometheus metrics are human readable which is useful.

If you combine, your sandbox1.py into this example:

from prometheus_client import start_http_server, Counter, Summary
import random
import time

# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds',
                       'Time spent processing request')


# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
    """A dummy function that takes some time."""
    time.sleep(t)


if __name__ == '__main__':
    my_counter1 = Counter('my_counter1', 'My counter')
    # Start up the server to expose the metrics.
    start_http_server(8000)
    # Generate some requests.
    while True:
        my_counter1.inc()
        process_request(random.random())

And run the code again, you should now see:

# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 357.0
python_gc_objects_collected_total{generation="1"} 0.0
python_gc_objects_collected_total{generation="2"} 0.0
...
...
# HELP my_counter1_total My counter
# TYPE my_counter1_total counter
my_counter1_total 13.0
# HELP my_counter1_created My counter
# TYPE my_counter1_created gauge
my_counter1_created 1.6004498408280134e+09

NOTE At the bottom of the page is my_counter1_total and my_counter1_created which correspond to your my_counter1 = Counter("my_counter1","My counter")

If you point a Prometheus server at this target (localhost:8000), you should be able to e.g. graph the my_counter1_total counter and the my_counter1_created gauge.

Create a file called prometheus.yml:

global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  # Self
  - job_name: "prometheus-server"
    static_configs:
      - targets:
          - "localhost:9090"

  # Python example
  - job_name: "63957470"
    static_configs:
      - targets:
          - "localhost:8000"

And then run Prometheus:

docker run \
--interactive --tty \
--net=host \
--volume=${PWD}/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus@sha256:f3ada803723ccbc443ebea19f7ab24d3323def496e222134bf9ed54ae5b787bd

NOTE This assumes that prometheus.yml is in the working directory

The, you can browse Prometheus on http://localhost:9090

You can see your code configured as a target: http://localhost:9090/targets

And you can query|graph your metrics. Type e.g. my_counter1_ to see both and then my_counter1_total for the Counter:

🌐
Medium
leapcell.medium.com › understanding-prometheus-and-monitoring-python-applications-37abf0712e2f
Understanding Prometheus and Monitoring Python Applications | by Leapcell | Medium
May 28, 2025 - For example, if the initial value is 1024 bytes and processes consume an additional 300 bytes, the gauge rises to 1324 bytes. If processes release 100 bytes, it drops to 1224 bytes.
🌐
Linux Hint
linuxhint.com › monitor-python-applications-prometheus
Monitoring Python Applications using Prometheus – Linux Hint
In this example, it says that 95% of the requests took less than 0.19580645161290322s or 195ms to respond. This information can help you determine the performance of the web server. You can monitor the memory usage of your Python app using Prometheus.
🌐
GitHub
github.com › Lispython › prometheus_client_example
GitHub - Lispython/prometheus_client_example: Python prometheus client usage examples
Python prometheus client usage examples. Contribute to Lispython/prometheus_client_example development by creating an account on GitHub.
Author: Lispython