exposition.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. from __future__ import unicode_literals
  2. import base64
  3. from contextlib import closing
  4. import os
  5. import socket
  6. import sys
  7. import threading
  8. from wsgiref.simple_server import make_server, WSGIServer, WSGIRequestHandler
  9. from .openmetrics import exposition as openmetrics
  10. from .registry import REGISTRY
  11. from .utils import floatToGoString
  12. try:
  13. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  14. from SocketServer import ThreadingMixIn
  15. from urllib2 import build_opener, Request, HTTPHandler
  16. from urllib import quote_plus
  17. from urlparse import parse_qs, urlparse
  18. except ImportError:
  19. # Python 3
  20. from http.server import BaseHTTPRequestHandler, HTTPServer
  21. from socketserver import ThreadingMixIn
  22. from urllib.request import build_opener, Request, HTTPHandler
  23. from urllib.parse import quote_plus, parse_qs, urlparse
  24. CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8')
  25. """Content type of the latest text format"""
  26. PYTHON26_OR_OLDER = sys.version_info < (2, 7)
  27. PYTHON376_OR_NEWER = sys.version_info > (3, 7, 5)
  28. def _bake_output(registry, accept_header, params):
  29. """Bake output for metrics output."""
  30. encoder, content_type = choose_encoder(accept_header)
  31. if 'name[]' in params:
  32. registry = registry.restricted_registry(params['name[]'])
  33. output = encoder(registry)
  34. return str('200 OK'), (str('Content-Type'), content_type), output
  35. def make_wsgi_app(registry=REGISTRY):
  36. """Create a WSGI app which serves the metrics from a registry."""
  37. def prometheus_app(environ, start_response):
  38. # Prepare parameters
  39. accept_header = environ.get('HTTP_ACCEPT')
  40. params = parse_qs(environ.get('QUERY_STRING', ''))
  41. # Bake output
  42. status, header, output = _bake_output(registry, accept_header, params)
  43. # Return output
  44. start_response(status, [header])
  45. return [output]
  46. return prometheus_app
  47. class _SilentHandler(WSGIRequestHandler):
  48. """WSGI handler that does not log requests."""
  49. def log_message(self, format, *args):
  50. """Log nothing."""
  51. class ThreadingWSGIServer(ThreadingMixIn, WSGIServer):
  52. """Thread per request HTTP server."""
  53. # Make worker threads "fire and forget". Beginning with Python 3.7 this
  54. # prevents a memory leak because ``ThreadingMixIn`` starts to gather all
  55. # non-daemon threads in a list in order to join on them at server close.
  56. daemon_threads = True
  57. def start_wsgi_server(port, addr='', registry=REGISTRY):
  58. """Starts a WSGI server for prometheus metrics as a daemon thread."""
  59. app = make_wsgi_app(registry)
  60. httpd = make_server(addr, port, app, ThreadingWSGIServer, handler_class=_SilentHandler)
  61. t = threading.Thread(target=httpd.serve_forever)
  62. t.daemon = True
  63. t.start()
  64. start_http_server = start_wsgi_server
  65. def generate_latest(registry=REGISTRY):
  66. """Returns the metrics from the registry in latest text format as a string."""
  67. def sample_line(line):
  68. if line.labels:
  69. labelstr = '{{{0}}}'.format(','.join(
  70. ['{0}="{1}"'.format(
  71. k, v.replace('\\', r'\\').replace('\n', r'\n').replace('"', r'\"'))
  72. for k, v in sorted(line.labels.items())]))
  73. else:
  74. labelstr = ''
  75. timestamp = ''
  76. if line.timestamp is not None:
  77. # Convert to milliseconds.
  78. timestamp = ' {0:d}'.format(int(float(line.timestamp) * 1000))
  79. return '{0}{1} {2}{3}\n'.format(
  80. line.name, labelstr, floatToGoString(line.value), timestamp)
  81. output = []
  82. for metric in registry.collect():
  83. try:
  84. mname = metric.name
  85. mtype = metric.type
  86. # Munging from OpenMetrics into Prometheus format.
  87. if mtype == 'counter':
  88. mname = mname + '_total'
  89. elif mtype == 'info':
  90. mname = mname + '_info'
  91. mtype = 'gauge'
  92. elif mtype == 'stateset':
  93. mtype = 'gauge'
  94. elif mtype == 'gaugehistogram':
  95. # A gauge histogram is really a gauge,
  96. # but this captures the structure better.
  97. mtype = 'histogram'
  98. elif mtype == 'unknown':
  99. mtype = 'untyped'
  100. output.append('# HELP {0} {1}\n'.format(
  101. mname, metric.documentation.replace('\\', r'\\').replace('\n', r'\n')))
  102. output.append('# TYPE {0} {1}\n'.format(mname, mtype))
  103. om_samples = {}
  104. for s in metric.samples:
  105. for suffix in ['_created', '_gsum', '_gcount']:
  106. if s.name == metric.name + suffix:
  107. # OpenMetrics specific sample, put in a gauge at the end.
  108. om_samples.setdefault(suffix, []).append(sample_line(s))
  109. break
  110. else:
  111. output.append(sample_line(s))
  112. except Exception as exception:
  113. exception.args = (exception.args or ('',)) + (metric,)
  114. raise
  115. for suffix, lines in sorted(om_samples.items()):
  116. output.append('# HELP {0}{1} {2}\n'.format(metric.name, suffix,
  117. metric.documentation.replace('\\', r'\\').replace('\n', r'\n')))
  118. output.append('# TYPE {0}{1} gauge\n'.format(metric.name, suffix))
  119. output.extend(lines)
  120. return ''.join(output).encode('utf-8')
  121. def choose_encoder(accept_header):
  122. accept_header = accept_header or ''
  123. for accepted in accept_header.split(','):
  124. if accepted.split(';')[0].strip() == 'application/openmetrics-text':
  125. return (openmetrics.generate_latest,
  126. openmetrics.CONTENT_TYPE_LATEST)
  127. return generate_latest, CONTENT_TYPE_LATEST
  128. class MetricsHandler(BaseHTTPRequestHandler):
  129. """HTTP handler that gives metrics from ``REGISTRY``."""
  130. registry = REGISTRY
  131. def do_GET(self):
  132. # Prepare parameters
  133. registry = self.registry
  134. accept_header = self.headers.get('Accept')
  135. params = parse_qs(urlparse(self.path).query)
  136. # Bake output
  137. status, header, output = _bake_output(registry, accept_header, params)
  138. # Return output
  139. self.send_response(int(status.split(' ')[0]))
  140. self.send_header(*header)
  141. self.end_headers()
  142. self.wfile.write(output)
  143. def log_message(self, format, *args):
  144. """Log nothing."""
  145. @classmethod
  146. def factory(cls, registry):
  147. """Returns a dynamic MetricsHandler class tied
  148. to the passed registry.
  149. """
  150. # This implementation relies on MetricsHandler.registry
  151. # (defined above and defaulted to REGISTRY).
  152. # As we have unicode_literals, we need to create a str()
  153. # object for type().
  154. cls_name = str(cls.__name__)
  155. MyMetricsHandler = type(cls_name, (cls, object),
  156. {"registry": registry})
  157. return MyMetricsHandler
  158. def write_to_textfile(path, registry):
  159. """Write metrics to the given path.
  160. This is intended for use with the Node exporter textfile collector.
  161. The path must end in .prom for the textfile collector to process it."""
  162. tmppath = '%s.%s.%s' % (path, os.getpid(), threading.current_thread().ident)
  163. with open(tmppath, 'wb') as f:
  164. f.write(generate_latest(registry))
  165. # rename(2) is atomic.
  166. os.rename(tmppath, path)
  167. def default_handler(url, method, timeout, headers, data):
  168. """Default handler that implements HTTP/HTTPS connections.
  169. Used by the push_to_gateway functions. Can be re-used by other handlers."""
  170. def handle():
  171. request = Request(url, data=data)
  172. request.get_method = lambda: method
  173. for k, v in headers:
  174. request.add_header(k, v)
  175. resp = build_opener(HTTPHandler).open(request, timeout=timeout)
  176. if resp.code >= 400:
  177. raise IOError("error talking to pushgateway: {0} {1}".format(
  178. resp.code, resp.msg))
  179. return handle
  180. def basic_auth_handler(url, method, timeout, headers, data, username=None, password=None):
  181. """Handler that implements HTTP/HTTPS connections with Basic Auth.
  182. Sets auth headers using supplied 'username' and 'password', if set.
  183. Used by the push_to_gateway functions. Can be re-used by other handlers."""
  184. def handle():
  185. """Handler that implements HTTP Basic Auth.
  186. """
  187. if username is not None and password is not None:
  188. auth_value = '{0}:{1}'.format(username, password).encode('utf-8')
  189. auth_token = base64.b64encode(auth_value)
  190. auth_header = b'Basic ' + auth_token
  191. headers.append(['Authorization', auth_header])
  192. default_handler(url, method, timeout, headers, data)()
  193. return handle
  194. def push_to_gateway(
  195. gateway, job, registry, grouping_key=None, timeout=30,
  196. handler=default_handler):
  197. """Push metrics to the given pushgateway.
  198. `gateway` the url for your push gateway. Either of the form
  199. 'http://pushgateway.local', or 'pushgateway.local'.
  200. Scheme defaults to 'http' if none is provided
  201. `job` is the job label to be attached to all pushed metrics
  202. `registry` is an instance of CollectorRegistry
  203. `grouping_key` please see the pushgateway documentation for details.
  204. Defaults to None
  205. `timeout` is how long push will attempt to connect before giving up.
  206. Defaults to 30s, can be set to None for no timeout.
  207. `handler` is an optional function which can be provided to perform
  208. requests to the 'gateway'.
  209. Defaults to None, in which case an http or https request
  210. will be carried out by a default handler.
  211. If not None, the argument must be a function which accepts
  212. the following arguments:
  213. url, method, timeout, headers, and content
  214. May be used to implement additional functionality not
  215. supported by the built-in default handler (such as SSL
  216. client certicates, and HTTP authentication mechanisms).
  217. 'url' is the URL for the request, the 'gateway' argument
  218. described earlier will form the basis of this URL.
  219. 'method' is the HTTP method which should be used when
  220. carrying out the request.
  221. 'timeout' requests not successfully completed after this
  222. many seconds should be aborted. If timeout is None, then
  223. the handler should not set a timeout.
  224. 'headers' is a list of ("header-name","header-value") tuples
  225. which must be passed to the pushgateway in the form of HTTP
  226. request headers.
  227. The function should raise an exception (e.g. IOError) on
  228. failure.
  229. 'content' is the data which should be used to form the HTTP
  230. Message Body.
  231. This overwrites all metrics with the same job and grouping_key.
  232. This uses the PUT HTTP method."""
  233. _use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler)
  234. def pushadd_to_gateway(
  235. gateway, job, registry, grouping_key=None, timeout=30,
  236. handler=default_handler):
  237. """PushAdd metrics to the given pushgateway.
  238. `gateway` the url for your push gateway. Either of the form
  239. 'http://pushgateway.local', or 'pushgateway.local'.
  240. Scheme defaults to 'http' if none is provided
  241. `job` is the job label to be attached to all pushed metrics
  242. `registry` is an instance of CollectorRegistry
  243. `grouping_key` please see the pushgateway documentation for details.
  244. Defaults to None
  245. `timeout` is how long push will attempt to connect before giving up.
  246. Defaults to 30s, can be set to None for no timeout.
  247. `handler` is an optional function which can be provided to perform
  248. requests to the 'gateway'.
  249. Defaults to None, in which case an http or https request
  250. will be carried out by a default handler.
  251. See the 'prometheus_client.push_to_gateway' documentation
  252. for implementation requirements.
  253. This replaces metrics with the same name, job and grouping_key.
  254. This uses the POST HTTP method."""
  255. _use_gateway('POST', gateway, job, registry, grouping_key, timeout, handler)
  256. def delete_from_gateway(
  257. gateway, job, grouping_key=None, timeout=30, handler=default_handler):
  258. """Delete metrics from the given pushgateway.
  259. `gateway` the url for your push gateway. Either of the form
  260. 'http://pushgateway.local', or 'pushgateway.local'.
  261. Scheme defaults to 'http' if none is provided
  262. `job` is the job label to be attached to all pushed metrics
  263. `grouping_key` please see the pushgateway documentation for details.
  264. Defaults to None
  265. `timeout` is how long delete will attempt to connect before giving up.
  266. Defaults to 30s, can be set to None for no timeout.
  267. `handler` is an optional function which can be provided to perform
  268. requests to the 'gateway'.
  269. Defaults to None, in which case an http or https request
  270. will be carried out by a default handler.
  271. See the 'prometheus_client.push_to_gateway' documentation
  272. for implementation requirements.
  273. This deletes metrics with the given job and grouping_key.
  274. This uses the DELETE HTTP method."""
  275. _use_gateway('DELETE', gateway, job, None, grouping_key, timeout, handler)
  276. def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler):
  277. gateway_url = urlparse(gateway)
  278. # See https://bugs.python.org/issue27657 for details on urlparse in py>=3.7.6.
  279. if not gateway_url.scheme or (
  280. (PYTHON376_OR_NEWER or PYTHON26_OR_OLDER)
  281. and gateway_url.scheme not in ['http', 'https']
  282. ):
  283. gateway = 'http://{0}'.format(gateway)
  284. url = '{0}/metrics/{1}/{2}'.format(gateway, *_escape_grouping_key("job", job))
  285. data = b''
  286. if method != 'DELETE':
  287. data = generate_latest(registry)
  288. if grouping_key is None:
  289. grouping_key = {}
  290. url += ''.join(
  291. '/{0}/{1}'.format(*_escape_grouping_key(str(k), str(v)))
  292. for k, v in sorted(grouping_key.items()))
  293. handler(
  294. url=url, method=method, timeout=timeout,
  295. headers=[('Content-Type', CONTENT_TYPE_LATEST)], data=data,
  296. )()
  297. def _escape_grouping_key(k, v):
  298. if v == "" :
  299. # Per https://github.com/prometheus/pushgateway/pull/346.
  300. return k + "@base64", "="
  301. elif '/' in v:
  302. # Added in Pushgateway 0.9.0.
  303. return k + "@base64", base64.urlsafe_b64encode(v.encode("utf-8")).decode("utf-8")
  304. else:
  305. return k, quote_plus(v)
  306. def instance_ip_grouping_key():
  307. """Grouping key with instance set to the IP Address of this host."""
  308. with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as s:
  309. s.connect(('localhost', 0))
  310. return {'instance': s.getsockname()[0]}
  311. try:
  312. # Python >3.5 only
  313. from .asgi import make_asgi_app
  314. except:
  315. pass