| 1 | --- |
| 2 | title: Labels |
| 3 | weight: 7 |
| 4 | --- |
| 5 | |
| 6 | All metrics can have labels, allowing grouping of related time series. |
| 7 | |
| 8 | See the best practices on [naming](http://prometheus.io/docs/practices/naming/) |
| 9 | and [labels](http://prometheus.io/docs/practices/instrumentation/#use-labels). |
| 10 | |
| 11 | Taking a counter as an example: |
| 12 | |
| 13 | ```python |
| 14 | from prometheus_client import Counter |
| 15 | c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint']) |
| 16 | c.labels('get', '/').inc() |
| 17 | c.labels('post', '/submit').inc() |
| 18 | ``` |
| 19 | |
| 20 | Labels can also be passed as keyword-arguments: |
| 21 | |
| 22 | ```python |
| 23 | from prometheus_client import Counter |
| 24 | c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint']) |
| 25 | c.labels(method='get', endpoint='/').inc() |
| 26 | c.labels(method='post', endpoint='/submit').inc() |
| 27 | ``` |
| 28 | |
| 29 | Metrics with labels are not initialized when declared, because the client can't |
| 30 | know what values the label can have. It is recommended to initialize the label |
| 31 | values by calling the `.labels()` method alone: |
| 32 | |
| 33 | ```python |
| 34 | from prometheus_client import Counter |
| 35 | c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint']) |
| 36 | c.labels('get', '/') |
| 37 | c.labels('post', '/submit') |
| 38 | ``` |