Skip to content

Commit

Permalink
Merge pull request #300 from prometheus/openmetrics
Browse files Browse the repository at this point in the history
Initial work on OpenMetrics
  • Loading branch information
brian-brazil authored Sep 7, 2018
2 parents b476bff + eb4ac52 commit 10c8eb4
Show file tree
Hide file tree
Showing 21 changed files with 1,674 additions and 151 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,16 @@ Counters go up, and reset when the process restarts.

```python
from prometheus_client import Counter
c = Counter('my_failures_total', 'Description of counter')
c = Counter('my_failures', 'Description of counter')
c.inc() # Increment by 1
c.inc(1.6) # Increment by given value
```

If there is a suffix of `_total` on the metric name, it will be removed. When
exposing the time series for counter, a `_total` suffix will be added. This is
for compatibility between OpenMetrics and the Prometheus text format, as OpenMetrics
requires the `_total` suffix.

There are utilities to count exceptions raised:

```python
Expand Down Expand Up @@ -169,6 +174,27 @@ with h.time():
pass
```

### Info

Info tracks key-value information, usually about a whole target.

```python
from prometheus_client import Info
i = Info('my_build_version', 'Description of info')
i.info({'version': '1.2.3', 'buildhost': 'foo@bar'})
```

### Enum

Enum tracks which of a set of states something is currently in.

```python
from prometheus_client import Enum
e = Enum('my_task_state', 'Description of enum',
states=['starting', 'running', 'stopped'])
e.state('running')
```

### Labels

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

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

Expand Down
4 changes: 3 additions & 1 deletion prometheus_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from . import process_collector
from . import platform_collector

__all__ = ['Counter', 'Gauge', 'Summary', 'Histogram']
__all__ = ['Counter', 'Gauge', 'Summary', 'Histogram', 'Info', 'Enum']

CollectorRegistry = core.CollectorRegistry
REGISTRY = core.REGISTRY
Expand All @@ -14,6 +14,8 @@
Gauge = core.Gauge
Summary = core.Summary
Histogram = core.Histogram
Info = core.Info
Enum = core.Enum

CONTENT_TYPE_LATEST = exposition.CONTENT_TYPE_LATEST
generate_latest = exposition.generate_latest
Expand Down
8 changes: 4 additions & 4 deletions prometheus_client/bridge/graphite.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ def push(self, prefix=''):
prefixstr = prefix + '.'

for metric in self._registry.collect():
for name, labels, value in metric.samples:
if labels:
for s in metric.samples:
if s.labels:
labelstr = '.' + '.'.join(
['{0}.{1}'.format(
_sanitize(k), _sanitize(v))
for k, v in sorted(labels.items())])
for k, v in sorted(s.labels.items())])
else:
labelstr = ''
output.append('{0}{1}{2} {3} {4}\n'.format(
prefixstr, _sanitize(name), labelstr, float(value), now))
prefixstr, _sanitize(s.name), labelstr, float(s.value), now))

conn = socket.create_connection(self._address, self._timeout)
conn.sendall(''.join(output).encode('ascii'))
Expand Down
Loading

0 comments on commit 10c8eb4

Please sign in to comment.