8000 Merge pull request #59 from ClusterHQ/twisted-resource · devopstiger/client_python@ad9b902 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad9b902

Browse files
committed
Merge pull request prometheus#59 from ClusterHQ/twisted-resource
Add a twisted resource for exposing metrics via twisted.web.
2 parents 54062b7 + d771b38 commit ad9b902

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,24 @@ To add Prometheus exposition to an existing HTTP server, see the `MetricsHandler
229229
which provides a `BaseHTTPRequestHandler`. It also serves as a simple example of how
230230
to write a custom endpoint.
231231

232+
#### Twisted
233+
234+
To use prometheus with [twisted](https://twistedmatrix.com/), there is `MetricsResource` which exposes metrics as a twisted resource.
235+
236+
```python
237+
from prometheus_client.twisted import MetricsResource
238+
from twisted.web.server import Site
239+
from twisted.web.resource import Resource
240+
from twisted.internet import reactor
241+
242+
root = Resource()
243+
root.putChild(b'metrics', MetricsResource())
244+
245+
factory = Site(root)
246+
reactor.listenTCP(8000, factory)
247+
reactor.run()
248+
```
249+
232250
### Node exporter textfile collector
233251

234252
The [textfile collector](https://github.com/prometheus/node_exporter#textfile-collector)

prometheus_client/twisted/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from ._exposition import MetricsResource
2+
3+
__all__ = ['MetricsResource']
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from __future__ import absolute_import, unicode_literals
2+
from .. import REGISTRY, generate_latest, CONTENT_TYPE_LATEST
3+
4+
from twisted.web.resource import Resource
5+
6+
7+
class MetricsResource(Resource):
8+
"""
9+
Twisted ``Resource`` that serves prometheus metrics.
10+
"""
11+
isLeaf = True
12+
13+
def __init__(self, registry=REGISTRY):
14+
self.registry = registry
15+
16+
def render_GET(self, request):
17+
request.setHeader(b'Content-Type', CONTENT_TYPE_LATEST.encode('ascii'))
18+
return generate_latest(self.registry)

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
license = "Apache Software License 2.0",
1212
keywords = "prometheus monitoring instrumentation client",
1313
url = "https://github.com/prometheus/client_python",
14-
packages=['prometheus_client', 'prometheus_client.bridge'],
14+
packages=['prometheus_client', 'prometheus_client.bridge', 'prometheus_client.twisted'],
15+
extras_requires={
16+
'twisted': ['twisted'],
17+
},
1518
test_suite="tests",
1619
classifiers=[
1720
"Development Status :: 4 - Beta",

tests/test_twisted.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from __future__ import absolute_import, unicode_literals
2+
3+
from unittest import skipUnless
4+
5+
from prometheus_client import Counter
6+
from prometheus_client import CollectorRegistry, generate_latest
7+
8+
try:
9+
from prometheus_client.twisted import MetricsResource
10+
11+
from twisted.trial.unittest import TestCase
12+
from twisted.web.server import Site
13+
from twisted.web.resource import Resource
14+
from twisted.internet import reactor
15+
from twisted.web.client import Agent
16+
from twisted.web.client import readBody
17+
HAVE_TWISTED = True
18+
except ImportError:
19+
from unittest import TestCase
20+
HAVE_TWISTED = False
21+
22+
23+
class MetricsResourceTest(TestCase):
24+
@skipUnless(HAVE_TWISTED, "Don't have twisted installed.")
25+
def setUp(self):
26+
self.registry = CollectorRegistry()
27+
28+
def test_reports_metrics(self):
29+
"""
30+
``MetricsResource`` serves the metrics from the provided registry.
31+
"""
32+
c = Counter('cc', 'A counter', registry=self.registry)
33+
c.inc()
34+
35+
root = Resource()
36+
root.putChild(b'metrics', MetricsResource(registry=self.registry))
37+
server = reactor.listenTCP(0, Site(root))
38+
self.addCleanup(server.stopListening)
39+
40+
agent = Agent(reactor)
41+
port = server.getHost().port
42+
url = u"http://localhost:{port}/metrics".format(port=port)
43+
d = agent.request(b"GET", url.encode("ascii"))
44+
45+
d.addCallback(readBody)
46+
d.addCallback(self.assertEqual, generate_latest(self.registry))
47+
48+
return d

0 commit comments

Comments
 (0)
0