<p>is any one tell me if there is python code to collect Prometheus metrics in Kubernetes cluster? I have 3 nodes connected with Kubernetes cluster, Prometheus already installed and all nodes are up and connected using Kubernetes cluster, I just want to know how I can export these metrics using python code? Thanks a lot </p>

MOBT
<p>You can follow this <a href="https://blog.codeship.com/monitoring-your-synchronous-python-web-applications-using-prometheus/" rel="nofollow noreferrer">guide</a> and this <a href="https://sysdig.com/blog/prometheus-metrics/" rel="nofollow noreferrer">guide</a>. Essentially you will be using <a href="https://github.com/prometheus/client_python" rel="nofollow noreferrer">prometheus python client library</a> to export metrics.</p>

<pre><code>import prometheus_client as prom
import random
import time

req_summary = prom.Summary('python_my_req_example', 'Time spent processing a request')


@req_summary.time()
def process_request(t):
   time.sleep(t)


if __name__ == '__main__':

   counter = prom.Counter('python_my_counter', 'This is my counter')
   gauge = prom.Gauge('python_my_gauge', 'This is my gauge')
   histogram = prom.Histogram('python_my_histogram', 'This is my histogram')
   summary = prom.Summary('python_my_summary', 'This is my summary')
   prom.start_http_server(8080)

   while True:
       counter.inc(random.random())
       gauge.set(random.random() * 15 - 5)
       histogram.observe(random.random() * 10)
       summary.observe(random.random() * 10)
       process_request(random.random() * 5)

       time.sleep(1)
</code></pre>
