8000 Merge pull request #300 from prometheus/openmetrics · SysOfM/client_python@10c8eb4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 10c8eb4

Browse files
authored
Merge pull request prometheus#300 from prometheus/openmetrics
Initial work on OpenMetrics
2 parents b476bff + eb4ac52 commit 10c8eb4

21 files changed

+1674
-151
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,16 @@ Counters go up, and reset when the process restarts.
6767

6868
```python
6969
from prometheus_client import Counter
70-
c = Counter('my_failures_total', 'Description of counter')
70+
c = Counter('my_failures', 'Description of counter')
7171
c.inc() # Increment by 1
7272
c.inc(1.6) # Increment by given value
7373
```
7474

75+
If there is a suffix of `_total` on the metric name, it will be removed. When
76+
exposing the time series for counter, a `_total` suffix will be added. This is
77+
for compatibility between OpenMetrics and the Prometheus text format, as OpenMetrics
78+
requires the `_total` suffix.
79+
7580
There are utilities to count exceptions raised:
7681

7782
```python
@@ -169,6 +174,27 @@ with h.time():
169174
pass
170175
```
171176

177+
### Info
178+
179+
Info tracks key-value information, usually about a whole target.
180+
181+
```python
182+
from prometheus_client import Info
183+
i = Info('my_build_version', 'Description of info')
184+
i.info({'version': '1.2.3', 'buildhost': 'foo@bar'})
185+
```
186+
187+
### Enum
188+
189+
Enum tracks which of a set of states something is currently in.
190+
191+
```python
192+
from prometheus_client import Enum
193+
e = Enum('my_task_state', 'Description of enum',
194+
states=['starting', 'running', 'stopped'])
195+
e.state('running')
196+
```
197+
172198
### Labels
173199

174200
All metrics can have labels, allowing grouping of related time series.
@@ -444,6 +470,7 @@ This comes with a number of limitations:
444470

445471
- Registries can not be used as normal, all instantiated metrics are exported
446472
- Custom collectors do not work (e.g. cpu and memory metrics)
473+
- Info and Enum metrics do not work
447474
- The pushgateway cannot be used
448475
- Gauges cannot use the `pid` label
449476

prometheus_client/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from . import process_collector
66
from . import platform_collector
77

8-
__all__ = ['Counter', 'Gauge', 'Summary', 'Histogram']
8+
__all__ = ['Counter', 'Gauge', 'Summary', 'Histogram', 'Info', 'Enum']
99

1010
CollectorRegistry = core.CollectorRegistry
1111
REGISTRY = core.REGISTRY
@@ -14,6 +14,8 @@
1414
Gauge = core.Gauge
1515
Summary = core.Summary
1616
Histogram = core.Histogram
17+
Info = core.Info
18+
Enum = core.Enum
1719

1820
CONTENT_TYPE_LATEST = exposition.CONTENT_TYPE_LATEST
1921
generate_latest = exposition.generate_latest

prometheus_client/bridge/graphite.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ def push(self, prefix=''):
6060
prefixstr = prefix + '.'
6161

6262
for metric in self._registry.collect():
63-
for name, labels, value in metric.samples:
64-
if labels:
63+
for s in metric.samples:
64+
if s.labels:
6565
labelstr = '.' + '.'.join(
6666
['{0}.{1}'.format(
6767
_sanitize(k), _sanitize(v))
68-
for k, v in sorted(labels.items())])
68+
for k, v in sorted(s.labels.items())])
6969
else:
7070
labelstr = ''
7171
output.append('{0}{1}{2} {3} {4}\n'.format(
72-
prefixstr, _sanitize(name), labelstr, float(value), now))
72+
prefixstr, _sanitize(s.name), labelstr, float(s.value), now))
7373

7474
conn = socket.create_connection(self._address, self._timeout)
7575
conn.sendall(''.join(output).encode('ascii'))

0 commit comments

Comments
 (0)
0