Integrating Prometheus IO with Python Django application

925 views
Skip to first unread message

joy

unread,
Mar 19, 2015, 6:52:03 AM3/19/15
to prometheus...@googlegroups.com
Hi,

   I am new to Prometheus which I want to integrate with my Python Django application. I have gone through the docs of Prometheus and its python client. I cannot
 figure out two things: 1. I did not find any PushGateway in python by which I can push the metrics and 2. what changes do I need to do in configuration file of Prometheus
 so that I can see and monitor the metrics in Prometheus dashboard. If anyone has any suggestion regrading that, I will be really grateful.


 Thanking you,
  Abhishek 

Brian Brazil

unread,
Mar 19, 2015, 7:12:53 AM3/19/15
to joy, prometheus-developers
On 19 March 2015 at 10:52, joy <abhishek....@gmail.com> wrote:
Hi,

   I am new to Prometheus which I want to integrate with my Python Django application. I have gone through the docs of Prometheus and its python client. I cannot
 figure out two things: 1. I did not find any PushGateway in python by which I can push the metrics

The PushGateway is intended for use my batch jobs, the usual way to expose something like Django for prometheus is over an http endpoint. I'm not very familiar with Django, but https://github.com/prometheus/client_python/blob/master/prometheus_client/__init__.py#L429 should point you in the right direction of how to write a view to do this.

Brian

 
and 2. what changes do I need to do in configuration file of Prometheus
 so that I can see and monitor the metrics in Prometheus dashboard. If anyone has any suggestion regrading that, I will be really grateful.


 Thanking you,
  Abhishek 

--
You received this message because you are subscribed to the Google Groups "Prometheus Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-devel...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matthias Rampke

unread,
Mar 19, 2015, 7:58:33 AM3/19/15
to Brian Brazil, joy, prometheus-developers
Alternatively, you could expose the metrics over a separate port. This has the advantage (given appropriate firewalling / network structure) that you're not showing them to the world.

Fundamentally, keep in mind that Prometheus is pull-based – your application does not know or care about who collects the metrics, it just serves the current values on request (to http://…/metrics). You would configure the prometheus server with a job to scrape them on a regular basis, like [0].

Finally, to create a nice self-refreshing dashboard, you would use PromDash[1], and configure one or more Prometheus servers there. This is one of many possible ways of course.

/MR
--
Matthias Rampke
Systems Engineer

SoundCloud Ltd. | Rheinsberger Str. 76/7710115 Berlin, Germany | +49 173 6395215

Managing Director: Alexander Ljung Incorporated in England & Wales with Company No. 6343600 | Local Branch Office | AG Charlottenburg  | HRB 110657B

joy

unread,
Mar 19, 2015, 8:45:09 AM3/19/15
to prometheus...@googlegroups.com
Hi,


  From this what I have understood is that, the metrics those I want to monitor, I have to register them by CollectorRegistry().register() , but I cannot figure
 out how to register a metrics. Prometheus is already running on port 9090 which I have already set up locally. So if possible, could you please one small example in python regarding how to add such a metric to that server. If I am wrong, please rectify. 

  Another thing is, a Django server is running on my local on port 80. So I have modified the prometheus.conf file by adding one target as http://localhost:80/metrics thinking that Prometheus server will pull the data from my Django app. But while restarting the server again modifying those changes I am seeing 404 error for the target though my django app is still running on port 80 in my local. What mistakes I am doing, I cannot understand.

Thanking you,
  Abhishek

Brian Brazil

unread,
Mar 19, 2015, 8:50:46 AM3/19/15
to joy, prometheus-developers
On 19 March 2015 at 12:45, joy <abhishek....@gmail.com> wrote:
Hi,


  From this what I have understood is that, the metrics those I want to monitor, I have to register them by CollectorRegistry().register() , but I cannot figure
 out how to register a metrics.
 
Metrics are registered by default, so for example:
c = Counter('my_failures_total', 'Description of counter')
is enough to get that metric in the default registry.

 
Prometheus is already running on port 9090 which I have already set up locally. So if possible, could you please one small example in python regarding how to add such a metric to that server. If I am wrong, please rectify. 
  Another thing is, a Django server is running on my local on port 80. So I have modified the prometheus.conf file by adding one target as http://localhost:80/metrics thinking that Prometheus server will pull the data from my Django app. But while restarting the server again modifying those changes I am seeing 404 error for the target though my django app is still running on port 80 in my local. What mistakes I am doing, I cannot understand.

You'll need to write a django view that'll call generate_latest, and add that to your URLconf.

Brian
 

Thanking you,
  Abhishek
  

On Thursday, 19 March 2015 16:22:03 UTC+5:30, joy wrote:
Hi,

   I am new to Prometheus which I want to integrate with my Python Django application. I have gone through the docs of Prometheus and its python client. I cannot
 figure out two things: 1. I did not find any PushGateway in python by which I can push the metrics and 2. what changes do I need to do in configuration file of Prometheus
 so that I can see and monitor the metrics in Prometheus dashboard. If anyone has any suggestion regrading that, I will be really grateful.


 Thanking you,
  Abhishek 

--

joy

unread,
Mar 19, 2015, 10:11:58 AM3/19/15
to prometheus...@googlegroups.com
Hi,

  I have done following:

    implemented one API like that and added corresponding url in URLConf:

       def metrics(request):
          from prometheus_client import generate_latest, Counter
          d = Counter('my_failures_total', 'Description of counter')
          return HttpResponse(generate_latest(), mimetype='application/json')

 and modified the target in prometheus.conf like following:


Now while restarting the prometheus server and checking the metrics in localhost:9090/graphs, I cannot see the counter my_failures_total, and it is showing no
datapoints found with that name and one error is coming in status like Unrecognized API version. I cannot understand what mistakes I am doing here.


Thanking you,
  Abhishek

On Thursday, 19 March 2015 16:22:03 UTC+5:30, joy wrote:

Brian Brazil

unread,
Mar 19, 2015, 10:14:53 AM3/19/15
to joy, prometheus-developers
On 19 March 2015 at 14:11, joy <abhishek....@gmail.com> wrote:
Hi,

  I have done following:

    implemented one API like that and added corresponding url in URLConf:

       def metrics(request):
          from prometheus_client import generate_latest, Counter 
          d = Counter('my_failures_total', 'Description of counter')

You should only define the Counter once, usually at file level.
 
          return HttpResponse(generate_latest(), mimetype='application/json')

You should set the mimetype to CONTENT_TYPE_LATEST, that should fix the api error.

Brian
 

 and modified the target in prometheus.conf like following:


Now while restarting the prometheus server and checking the metrics in localhost:9090/graphs, I cannot see the counter my_failures_total, and it is showing no
datapoints found with that name and one error is coming in status like Unrecognized API version. I cannot understand what mistakes I am doing here.


Thanking you,
  Abhishek

On Thursday, 19 March 2015 16:22:03 UTC+5:30, joy wrote:
Hi,

   I am new to Prometheus which I want to integrate with my Python Django application. I have gone through the docs of Prometheus and its python client. I cannot
 figure out two things: 1. I did not find any PushGateway in python by which I can push the metrics and 2. what changes do I need to do in configuration file of Prometheus
 so that I can see and monitor the metrics in Prometheus dashboard. If anyone has any suggestion regrading that, I will be really grateful.


 Thanking you,
  Abhishek 

--

Julius Volz

unread,
Mar 19, 2015, 10:16:10 AM3/19/15
to joy, prometheus-developers
Hey,

Also, as a first step before trying configuring Prometheus to scrape your target, you can manually check whether your target outputs any metrics by simply navigating to it in a browser. If the metrics endpoint doesn't work, some Django configuration bits are still missing (unfortunately, I don't know anything about Django, so can't really help there) and there's no use trying to scrape it with Prometheus yet.

Cheers,
Julius

--

rajshre...@gmail.com

unread,
Jan 10, 2018, 5:28:04 AM1/10/18
to Prometheus Developers
In django application where we need to configure our metrices?Can anyone guide mme?
Reply all
Reply to author
Forward
0 new messages