Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Created January 11, 2021 13:30
Show Gist options
  • Save ruanbekker/e5b1e7895f62b020ff29b5f40767190c to your computer and use it in GitHub Desktop.
Save ruanbekker/e5b1e7895f62b020ff29b5f40767190c to your computer and use it in GitHub Desktop.
Python Flask with Prometheus Basic Example
$ curl http://localhost:5000/metrics
# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 317.0
python_gc_objects_collected_total{generation="1"} 71.0
python_gc_objects_collected_total{generation="2"} 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable object found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 66.0
python_gc_collections_total{generation="1"} 6.0
python_gc_collections_total{generation="2"} 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="7",patchlevel="9",version="3.7.9"} 1.0
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 2.9003776e+07
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 2.4039424e+07
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.61037167605e+09
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 0.17
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 7.0
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 1.048576e+06
# HELP number_of_requests_total The number of requests, its a counter so the value can increase or reset to zero.
# TYPE number_of_requests_total counter
number_of_requests_total 1.0
# HELP number_of_requests_created The number of requests, its a counter so the value can increase or reset to zero.
# TYPE number_of_requests_created gauge
number_of_requests_created 1.6103716764095376e+09
# HELP current_memory_usage_locally The current value of memory usage, its a gauge so it can go up or down.
# TYPE current_memory_usage_locally gauge
current_memory_usage_locally{server_name="server-a"} 62497.0
from flask import Flask, Response
from prometheus_client import Counter, Gauge, start_http_server, generate_latest
import random
import logging
logger = logging.getLogger(__name__)
app = Flask(__name__)
CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8')
number_of_requests = Counter(
'number_of_requests',
'The number of requests, its a counter so the value can increase or reset to zero.'
)
current_memory_usage = Gauge(
'current_memory_usage_locally',
'The current value of memory usage, its a gauge so it can go up or down.',
['server_name']
)
@app.route('/metrics', methods=['GET'])
def get_data():
"""Returns all data as plaintext."""
number_of_requests.inc()
current_memory_usage.labels('server-a').set(random.randint(10000,90000))
return Response(generate_latest(), mimetype=CONTENT_TYPE_LATEST)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
@Only-a-User
Copy link

Funnily enough, exactly what I needed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment