64
64
65
65
# pylint: disable=unused-argument
66
66
# pylint: disable=R0915
67
- def _instrument (tracer_provider = None , span_callback = None ):
67
+ def _instrument (tracer_provider = None , span_callback = None , name_callback = None ):
68
68
"""Enables tracing of all requests calls that go through
69
69
:code:`requests.session.Session.request` (this includes
70
70
:code:`requests.get`, etc.)."""
@@ -124,7 +124,11 @@ def _instrumented_requests_call(
124
124
# See
125
125
# https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md#http-client
126
126
method = method .upper ()
127
- span_name = "HTTP {}" .format (method )
127
+ span_name = ""
128
+ if name_callback is not None :
129
+ span_name = name_callback ()
130
+ if not span_name or not isinstance (span_name , str ):
131
+ span_name = get_default_span_name (method )
128
132
129
133
recorder = RequestsInstrumentor ().metric_recorder
130
134
@@ -217,6 +221,11 @@ def _uninstrument_from(instr_root, restore_as_bound_func=False):
217
221
setattr (instr_root , instr_func_name , original )
218
222
219
223
224
+ def get_default_span_name (method ):
225
+ """Default implementation for name_callback, returns HTTP {method_name}."""
226
+ return "HTTP {}" .format (method ).strip ()
227
+
228
+
220
229
class RequestsInstrumentor (BaseInstrumentor , MetricMixin ):
221
230
"""An instrumentor for requests
222
231
See `BaseInstrumentor`
@@ -229,10 +238,14 @@ def _instrument(self, **kwargs):
229
238
**kwargs: Optional arguments
230
239
``tracer_provider``: a TracerProvider, defaults to global
231
240
``span_callback``: An optional callback invoked before returning the http response. Invoked with Span and requests.Response
241
+ ``name_callback``: Callback which calculates a generic span name for an
242
+ outgoing HTTP request based on the method and url.
243
+ Optional: Defaults to get_default_span_name.
232
244
"""
233
245
_instrument (
234
246
tracer_provider = kwargs .get ("tracer_provider" ),
235
247
span_callback = kwargs .get ("span_callback" ),
248
+ name_callback = kwargs .get ("name_callback" ),
236
249
)
237
250
self .init_metrics (
238
251
__name__ , __version__ ,
0 commit comments