Skip to content

Commit 1679377

Browse files
committed
Add support for textfile collector.
1 parent 1c603be commit 1679377

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ c.labels('post', '/submit').inc()
110110

111111
## Exporting
112112

113-
Metrics are exposed over HTTP, to be read by the Prometheus server. For example:
113+
There are several options for exporting metrics.
114+
115+
## HTTP handler
116+
117+
Metrics are usuall exposed over HTTP, to be read by the Prometheus server. For example:
114118

115119
```python
116120
from prometheus_client import MetricsHandler
@@ -121,3 +125,22 @@ httpd.serve_forever()
121125
```
122126

123127
Visit [http://localhost:8000/](http://localhost:8000/) to view the metrics.
128+
129+
## Node exporter textfile collector
130+
131+
The [textfile collector](https://github.com/prometheus/node_exporter#textfile-collector)
132+
allows machine-level statistics to be exported out via the Node exporter.
133+
134+
This is useful for monitoring cronjobs, or for writing cronjobs to expose metrics
135+
about your system that the Node exporter does not support or would not make sense
136+
to perform at every scrape (for example, anything involving subprocesses).
137+
138+
```python
139+
from prometheus_client import CollectorRegistry,Gauge,write_to_textfile
140+
registry = CollectorRegistry()
141+
g = Gauge('raid_status', '1 if raid array is okay', registry=registry)
142+
g.set(1)
143+
write_to_textfile('/configured/textfile/path/raid.prom', registry)
144+
```
145+
146+
A separate registry is used, as the default registry may contain other metrics.

prometheus_client/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import copy
44
import re
5+
import os
56
import time
7+
import threading
68
from contextlib import contextmanager
79
from BaseHTTPServer import BaseHTTPRequestHandler
810
from functools import wraps
@@ -318,6 +320,17 @@ def do_GET(self):
318320
self.end_headers()
319321
self.wfile.write(generate_latest(REGISTRY))
320322

323+
def write_to_textfile(path, registry):
324+
'''Write metrics to the given path.
325+
326+
This is intended for use with the Node exporter textfile collector.
327+
The path must end in .prom for the textfile collector to process it.'''
328+
tmppath = '%s.%s.%s' % (path, os.getpid(), threading.current_thread().ident)
329+
with open(tmppath, 'wb') as f:
330+
f.write(generate_latest(registry))
331+
# rename(2) is atomic.
332+
os.rename(tmppath, path)
333+
321334

322335
if __name__ == '__main__':
323336
c = Counter('cc', 'A counter')

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def read(fname):
1010

1111
setup(
1212
name = "prometheus_client",
13-
version = "0.0.2",
13+
version = "0.0.3",
1414
author = "Brian Brazil",
1515
author_email = "brian.brazil@gmail.com",
1616
description = ("Python client for the Prometheus monitoring system."),

0 commit comments

Comments
 (0)