Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Resolves #114] Signed-off-by: Joshua Carp <jm.carp@gmail.com>
- Loading branch information
Showing
6 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from ._exposition import MetricsHandler | ||
|
||
__all__ = ['MetricsHandler'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
from .. import REGISTRY, exposition | ||
|
||
import tornado.web | ||
|
||
|
||
class MetricsHandler(tornado.web.RequestHandler): | ||
""" | ||
Tornado ``Handler`` that serves prometheus metrics. | ||
""" | ||
def initialize(self, registry=REGISTRY): | ||
self.registry = registry | ||
|
||
def get(self): | ||
encoder, content_type = exposition.choose_encoder(self.request.headers.get('Accept')) | ||
self.set_header('Content-Type', content_type) | ||
self.write(encoder(self.registry)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
import sys | ||
|
||
if sys.version_info < (2, 7): | ||
from unittest2 import skipUnless | ||
else: | ||
from unittest import skipUnless | ||
|
||
from prometheus_client import Counter | ||
from prometheus_client import CollectorRegistry, generate_latest | ||
|
||
try: | ||
from prometheus_client.tornado import MetricsHandler | ||
|
||
import tornado.ioloop | ||
import tornado.web | ||
import tornado.testing | ||
TestCase = tornado.testing.AsyncHTTPTestCase | ||
HAVE_TORNADO = True | ||
except ImportError: | ||
from unittest import TestCase | ||
HAVE_TORNADO = False | ||
|
||
|
||
class MetricsResourceTest(TestCase): | ||
def get_app(self): | ||
self.registry = CollectorRegistry() | ||
return tornado.web.Application([ | ||
(r'/metrics', MetricsHandler, {'registry': self.registry}), | ||
]) | ||
|
||
@skipUnless(HAVE_TORNADO, "Don't have tornado installed.") | ||
def test_reports_metrics(self): | ||
""" | ||
``MetricsHandler`` serves the metrics from the provided registry. | ||
""" | ||
c = Counter('cc', 'A counter', registry=self.registry) | ||
c.inc() | ||
|
||
resp = self.fetch('/metrics') | ||
|
||
self.assertEqual(resp.code, 200) | ||
self.assertEqual(resp.body, generate_latest(self.registry)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters