|
|
@ -1,9 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
"""Code for instrumenting requests calls. Requires requests, obviously."""
|
|
|
|
"""Code for instrumenting requests calls. Requires requests, obviously."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# absolute_import prevents "import requests" in this module from just importing itself
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
|
|
|
|
import urlparse
|
|
|
|
import urlparse
|
|
|
|
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import requests.sessions
|
|
|
|
import prometheus_client as prom
|
|
|
|
import prometheus_client as prom
|
|
|
|
from monotonic import monotonic
|
|
|
|
from monotonic import monotonic
|
|
|
|
|
|
|
|
|
|
|
@ -27,7 +30,7 @@ request_concurrency = prom.Gauge(
|
|
|
|
['name', 'method', 'domain'],
|
|
|
|
['name', 'method', 'domain'],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class InstrumentedSession(requests.Session):
|
|
|
|
class InstrumentedSession(requests.sessions.Session):
|
|
|
|
"""A requests Session that automatically records metrics on requests made.
|
|
|
|
"""A requests Session that automatically records metrics on requests made.
|
|
|
|
Users may optionally pass a 'metric_name' kwarg that will be included as the 'name' label.
|
|
|
|
Users may optionally pass a 'metric_name' kwarg that will be included as the 'name' label.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@ -45,7 +48,11 @@ class InstrumentedSession(requests.Session):
|
|
|
|
request_latency.labels(name, method, domain, "error").observe(latency)
|
|
|
|
request_latency.labels(name, method, domain, "error").observe(latency)
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
request_latency.labels(name, method, domain, response.status_code).observe(response.elapsed)
|
|
|
|
request_latency.labels(name, method, domain, response.status_code).observe(response.elapsed.total_seconds())
|
|
|
|
if 'content-length' in response.headers:
|
|
|
|
try:
|
|
|
|
response_size.labels(name, method, domain, response.status_code).observe(response.headers['content-length'])
|
|
|
|
content_length = int(response.headers['content-length'])
|
|
|
|
|
|
|
|
except (KeyError, ValueError):
|
|
|
|
|
|
|
|
pass # either not present or not valid
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
response_size.labels(name, method, domain, response.status_code).observe(content_length)
|
|
|
|
return response
|
|
|
|
return response
|
|
|
|