promotheus monitoring a simple application

Viewed 302

Im trying to monitor a simple application using prometheus but not sure where to start.

I created a simple test function that will sleep for 10 seconds and I track with prometheus - Summary metric.

from prometheus_client import Summary
import time
from datetime import datetime

TF_CALL_SUMMARY = Summary("call_seconds", "Time spent inside test function")

def test():

    while True:
        t1 = datetime.now()
            time.sleep(10)      
            t2 = datetime.now()
            delta = t2 - t1 
        TF_CALL_SUMMARY.observe(delta.total_seconds())
        print delta

print 'start application'
test()
print 'end application'

Now this is not a web application that can have a /metric endpoint.

How can I export this Metric to my prometheus server?

1 Answers

You need to expose the data to Promethueus:

from prometheus_client import start_http_server

if __name__ == '__main__':
    start_http_server(8000)

and then you'll need to configure Prometheus to scrape this data source. Add something like the following to your Prometheus configuration file:

# Prometheus.yml
scrape_configs:
  - job: "python" 
    static_configs:
      - targets: ["localhost:8000"]

Here's a tutorial detailing the instrumentation of a Python application which will be of use to you.

Related