@@ -10,21 +10,110 @@ pip install prometheus_client
10
10
11
11
This package can be found on [ PyPI] ( https://pypi.python.org/pypi/prometheus_client ) .
12
12
13
- ## Example Usage
13
+ ## Instrumenting
14
+
15
+ Three types of metric are offered: Counter, Gauge, and Summary.
16
+ See the documentation on [ metric types] ( http://prometheus.io/docs/concepts/metric_types/ )
17
+ and [ instrumentation best practices] ( http://prometheus.io/docs/practices/instrumentation/#counter-vs.-gauge-vs.-summary )
18
+ on how to use them.
19
+
20
+ ### Counter
21
+
22
+ Counters go up, and reset when the process restarts.
23
+
14
24
15
25
``` python
16
- from prometheus_client import *
17
- from prometheus_client import MetricsHandler
26
+ from prometheus_client import Counter
27
+ c = Counter(' my_failures_total' , ' Description of counter' )
28
+ c.inc() # Increment by 1
29
+ c.inc(10 ) # Increment by given value
30
+ ```
31
+
32
+ There are utilities to count exceptions raised:
33
+
34
+ ``` python
35
+ @c.countExceptions ()
36
+ def f ():
37
+ pass
38
+
39
+ with c.countExceptions():
40
+ pass
41
+
42
+ with c.countExceptions(ValueError ):
43
+ pass
44
+ ```
45
+
46
+ ### Gauge
47
+
48
+ Gauges can go up and down.
49
+
50
+
51
+ ``` python
52
+ from prometheus_client import Gauge
53
+ g = Gauge(' my_inprogress_requests' , ' Description of gauge' )
54
+ g.inc() # Increment by 1
55
+ g.dev(10 ) # Decrement by given value
56
+ g.set(7 ) # Set to a given value
57
+ ```
58
+
59
+ There are utilities for common use cases:
60
+
61
+ ``` python
62
+ g.setToCurrentTime() # Set to current unixtime
18
63
19
- c = Counter(' cc' , ' A counter' )
20
- c.inc()
64
+ # Increment when entered, decrement when exited.
65
+ @g.trackInprogress ()
66
+ def f ():
67
+ pass
21
68
22
- g = Gauge(' gg' , ' A gauge' )
23
- g.set(17 )
69
+ with g.trackInprogress():
70
+ pass
71
+ ```
72
+
73
+ ### Summaries
74
+
75
+ Summaries track the size and number of events.
76
+
77
+ ``` python
78
+ from prometheus_client import Summary
79
+ s = Summary(' request_latency_seconds' , ' Description of summary' )
80
+ s.observe(5 ) # Observe 5 (seconds)
81
+ ```
82
+
83
+ There are utilities for common use cases:
84
+
85
+ ``` python
86
+ # Increment when entered, decrement when exited.
87
+ @g.time ()
88
+ def f ():
89
+ pass
90
+
91
+ with g.time():
92
+ pass
93
+ ```
24
94
25
- s = Summary(' ss' , ' A summary' , [' a' , ' b' ])
26
- s.labels(' c' , ' d' ).observe(17 )
95
+ ### Labels
27
96
97
+ All metrics can have labels, allowing grouping of related time series.
98
+
99
+ See the best practices on [ naming] ( http://prometheus.io/docs/practices/naming/ )
100
+ and [ labels] ( http://prometheus.io/docs/practices/instrumentation/#use-labels ) .
101
+
102
+ Taking a counter as an example:
103
+
104
+ ``` python
105
+ from prometheus_client import Counter
106
+ c = Counter(' my_requests_total' , ' HTTP Failures' , [' method' , ' endpoint' ])
107
+ c.labels(' get' , ' /' ).inc()
108
+ c.labels(' post' , ' /submit' ).inc()
109
+ ```
110
+
111
+ ## Exporting
112
+
113
+ Metrics are exposed over HTTP, to be read by the Prometheus server. For example:
114
+
115
+ ``` python
116
+ from prometheus_client import MetricsHandler
28
117
from BaseHTTPServer import HTTPServer
29
118
server_address = (' ' , 8000 )
30
119
httpd = HTTPServer(server_address, MetricsHandler)
0 commit comments