requests
This commit is contained in:
parent
eb53012a8c
commit
a10e2c68d1
|
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
- Add span name callback
|
||||||
|
([#157](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/157))
|
||||||
|
|
||||||
## Version 0.15b0
|
## Version 0.15b0
|
||||||
|
|
||||||
Released 2020-11-02
|
Released 2020-11-02
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ _SUPPRESS_REQUESTS_INSTRUMENTATION_KEY = "suppress_requests_instrumentation"
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
# pylint: disable=R0915
|
# pylint: disable=R0915
|
||||||
def _instrument(tracer_provider=None, span_callback=None):
|
def _instrument(tracer_provider=None, span_callback=None, name_callback=get_default_span_name):
|
||||||
"""Enables tracing of all requests calls that go through
|
"""Enables tracing of all requests calls that go through
|
||||||
:code:`requests.session.Session.request` (this includes
|
:code:`requests.session.Session.request` (this includes
|
||||||
:code:`requests.get`, etc.)."""
|
:code:`requests.get`, etc.)."""
|
||||||
|
|
@ -124,7 +124,7 @@ def _instrument(tracer_provider=None, span_callback=None):
|
||||||
# See
|
# See
|
||||||
# https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md#http-client
|
# https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md#http-client
|
||||||
method = method.upper()
|
method = method.upper()
|
||||||
span_name = "HTTP {}".format(method)
|
span_name = name_callback(method, url)
|
||||||
|
|
||||||
recorder = RequestsInstrumentor().metric_recorder
|
recorder = RequestsInstrumentor().metric_recorder
|
||||||
|
|
||||||
|
|
@ -217,6 +217,12 @@ def _uninstrument_from(instr_root, restore_as_bound_func=False):
|
||||||
setattr(instr_root, instr_func_name, original)
|
setattr(instr_root, instr_func_name, original)
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=unused-argument
|
||||||
|
def get_default_span_name(method_name, url):
|
||||||
|
"""Default implementation for name_callback, returns HTTP {method_name}."""
|
||||||
|
return "HTTP {}".format(method_name).strip()
|
||||||
|
|
||||||
|
|
||||||
class RequestsInstrumentor(BaseInstrumentor, MetricMixin):
|
class RequestsInstrumentor(BaseInstrumentor, MetricMixin):
|
||||||
"""An instrumentor for requests
|
"""An instrumentor for requests
|
||||||
See `BaseInstrumentor`
|
See `BaseInstrumentor`
|
||||||
|
|
@ -229,10 +235,14 @@ class RequestsInstrumentor(BaseInstrumentor, MetricMixin):
|
||||||
**kwargs: Optional arguments
|
**kwargs: Optional arguments
|
||||||
``tracer_provider``: a TracerProvider, defaults to global
|
``tracer_provider``: a TracerProvider, defaults to global
|
||||||
``span_callback``: An optional callback invoked before returning the http response. Invoked with Span and requests.Response
|
``span_callback``: An optional callback invoked before returning the http response. Invoked with Span and requests.Response
|
||||||
|
``name_callback``: Callback which calculates a generic span name for an
|
||||||
|
outgoing HTTP request based on the method and url.
|
||||||
|
Optional: Defaults to get_default_span_name.
|
||||||
"""
|
"""
|
||||||
_instrument(
|
_instrument(
|
||||||
tracer_provider=kwargs.get("tracer_provider"),
|
tracer_provider=kwargs.get("tracer_provider"),
|
||||||
span_callback=kwargs.get("span_callback"),
|
span_callback=kwargs.get("span_callback"),
|
||||||
|
name_callback=kwargs.get("name_callback"),
|
||||||
)
|
)
|
||||||
self.init_metrics(
|
self.init_metrics(
|
||||||
__name__, __version__,
|
__name__, __version__,
|
||||||
|
|
|
||||||
|
|
@ -107,6 +107,28 @@ class RequestsIntegrationTestBase(abc.ABC):
|
||||||
self.assertEqual(view_data.aggregator.current.count, 1)
|
self.assertEqual(view_data.aggregator.current.count, 1)
|
||||||
self.assertGreaterEqual(view_data.aggregator.current.sum, 0)
|
self.assertGreaterEqual(view_data.aggregator.current.sum, 0)
|
||||||
|
|
||||||
|
def test_name_callback(self):
|
||||||
|
def name_callback():
|
||||||
|
return "test_name"
|
||||||
|
RequestsInstrumentor().uninstrument()
|
||||||
|
RequestsInstrumentor().instrument(name_callback=name_callback)
|
||||||
|
result = self.perform_request(self.URL)
|
||||||
|
self.assertEqual(result.text, "Hello!")
|
||||||
|
span = self.assert_span()
|
||||||
|
|
||||||
|
self.assertEqual(span.name, "test_name")
|
||||||
|
|
||||||
|
def test_name_callback_default(self):
|
||||||
|
def name_callback():
|
||||||
|
return 123
|
||||||
|
RequestsInstrumentor().uninstrument()
|
||||||
|
RequestsInstrumentor().instrument(name_callback=name_callback)
|
||||||
|
result = self.perform_request(self.URL)
|
||||||
|
self.assertEqual(result.text, "Hello!")
|
||||||
|
span = self.assert_span()
|
||||||
|
|
||||||
|
self.assertEqual(span.name, "HTTP GET")
|
||||||
|
|
||||||
def test_not_foundbasic(self):
|
def test_not_foundbasic(self):
|
||||||
url_404 = "http://httpbin.org/status/404"
|
url_404 = "http://httpbin.org/status/404"
|
||||||
httpretty.register_uri(
|
httpretty.register_uri(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue