Skip to content

Commit b7883db

Browse files
pvaneckallenkim0129
authored andcommitted
[Core] Update DistributedTracingPolicy spans (Azure#39959)
- Renamed some Microsoft specific span attributes - Updated default HTTP span name to just the HTTP method. Signed-off-by: Paul Van Eck <[email protected]>
1 parent 1aa2d3a commit b7883db

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

sdk/core/azure-core-tracing-opentelemetry/tests/test_storage_live.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_blob_service_client_tracing(self, config, tracing_helper):
2323
# We expect 3 spans, one for the root span, one for the method call, and one for the HTTP request.
2424
assert len(spans) == 3
2525
span_names_list = [span.name for span in spans]
26-
assert span_names_list == ["/", "BlobServiceClient.get_service_properties", "root"]
26+
assert span_names_list == ["GET", "BlobServiceClient.get_service_properties", "root"]
2727

2828
http_span: ReadableSpan = spans[0]
2929
assert http_span.kind == SpanKind.CLIENT

sdk/core/azure-core/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
### Breaking Changes
2323

2424
- Removed automatic tracing enablement for the OpenTelemetry plugin if `opentelemetry` was imported. To enable tracing with the plugin, please import `azure.core.settings.settings` and set `settings.tracing_implementation` to `"opentelemetry"`. #39563
25+
- In `DistributedTracingPolicy`, the default span name is now just the HTTP method (e.g., "GET", "POST") and no longer includes the URL path. This change was made to converge with the OpenTelemetry HTTP semantic conventions. The full URL is still included in the span attributes.
26+
- Renamed span attributes in `DistributedTracingPolicy`:
27+
- "x-ms-client-request-id" is now "az.client_request_id"
28+
- "x-ms-request-id" is now "az.service_request_id"
2529

2630
### Bugs Fixed
2731

sdk/core/azure-core/azure/core/pipeline/policies/_distributed_tracing.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ def _default_network_span_namer(http_request: HTTPRequestType) -> str:
6161
:returns: The string to use as network span name
6262
:rtype: str
6363
"""
64-
path = urllib.parse.urlparse(http_request.url).path
65-
if not path:
66-
path = "/"
67-
return path
64+
return http_request.method
6865

6966

7067
class DistributedTracingPolicy(SansIOHTTPPolicy[HTTPRequestType, HTTPResponseType]):
@@ -93,7 +90,9 @@ class DistributedTracingPolicy(SansIOHTTPPolicy[HTTPRequestType, HTTPResponseTyp
9390

9491
# Azure attributes
9592
_REQUEST_ID = "x-ms-client-request-id"
93+
_REQUEST_ID_ATTR = "az.client_request_id"
9694
_RESPONSE_ID = "x-ms-request-id"
95+
_RESPONSE_ID_ATTR = "az.service_request_id"
9796

9897
def __init__(self, *, instrumentation_config: Optional[Mapping[str, Any]] = None, **kwargs: Any):
9998
self._network_span_namer = kwargs.get("network_span_namer", _default_network_span_namer)
@@ -190,9 +189,9 @@ def end_span(
190189
if request.context.get("retry_count"):
191190
attributes[self._HTTP_RESEND_COUNT] = request.context["retry_count"]
192191
if http_request.headers.get(self._REQUEST_ID):
193-
attributes[self._REQUEST_ID] = http_request.headers[self._REQUEST_ID]
192+
attributes[self._REQUEST_ID_ATTR] = http_request.headers[self._REQUEST_ID]
194193
if response and self._RESPONSE_ID in response.headers:
195-
attributes[self._RESPONSE_ID] = response.headers[self._RESPONSE_ID]
194+
attributes[self._RESPONSE_ID_ATTR] = response.headers[self._RESPONSE_ID]
196195

197196
# We'll determine if the span is from a plugin or the core tracing library based on the presence of the
198197
# `set_http_attributes` method.

sdk/core/azure-core/tests/test_tracing_policy.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,24 @@ def test_distributed_tracing_policy_solo(self, tracing_implementation, http_requ
5252

5353
# Check on_response
5454
network_span = root_span.children[0]
55-
assert network_span.name == "/temp"
55+
assert network_span.name == "GET"
5656
assert network_span.attributes.get(HttpSpanMixin._HTTP_METHOD) == "GET"
5757
assert network_span.attributes.get(HttpSpanMixin._HTTP_URL) == "http://localhost/temp?query=query"
5858
assert network_span.attributes.get(HttpSpanMixin._NET_PEER_NAME) == "localhost"
5959
assert network_span.attributes.get(HttpSpanMixin._HTTP_USER_AGENT) is None
60-
assert network_span.attributes.get(policy._RESPONSE_ID) == "some request id"
61-
assert network_span.attributes.get(policy._REQUEST_ID) == "some client request id"
60+
assert network_span.attributes.get(policy._RESPONSE_ID_ATTR) == "some request id"
61+
assert network_span.attributes.get(policy._REQUEST_ID_ATTR) == "some client request id"
6262
assert network_span.attributes.get(HttpSpanMixin._HTTP_STATUS_CODE) == 202
6363
assert policy._ERROR_TYPE not in network_span.attributes
6464

6565
# Check on_exception
6666
network_span = root_span.children[1]
67-
assert network_span.name == "/temp"
67+
assert network_span.name == "GET"
6868
assert network_span.attributes.get(HttpSpanMixin._HTTP_METHOD) == "GET"
6969
assert network_span.attributes.get(HttpSpanMixin._HTTP_URL) == "http://localhost/temp?query=query"
70-
assert network_span.attributes.get(policy._REQUEST_ID) == "some client request id"
70+
assert network_span.attributes.get(policy._REQUEST_ID_ATTR) == "some client request id"
7171
assert network_span.attributes.get(HttpSpanMixin._HTTP_USER_AGENT) is None
72-
assert network_span.attributes.get(policy._RESPONSE_ID) == None
72+
assert network_span.attributes.get(policy._RESPONSE_ID_ATTR) == None
7373
assert network_span.attributes.get(HttpSpanMixin._HTTP_STATUS_CODE) == 504
7474
assert network_span.attributes.get(policy._ERROR_TYPE)
7575

@@ -90,7 +90,7 @@ def test_distributed_tracing_policy_error_response(self, tracing_implementation,
9090

9191
policy.on_response(pipeline_request, PipelineResponse(request, response, PipelineContext(None)))
9292
network_span = root_span.children[0]
93-
assert network_span.name == "/temp"
93+
assert network_span.name == "GET"
9494
assert network_span.attributes.get(policy._ERROR_TYPE) == "403"
9595

9696
@pytest.mark.parametrize("http_request,http_response", request_and_responses_product(HTTP_RESPONSES))
@@ -211,21 +211,21 @@ def test_distributed_tracing_policy_with_user_agent(self, tracing_implementation
211211
user_agent.on_response(pipeline_request, pipeline_response)
212212

213213
network_span = root_span.children[0]
214-
assert network_span.name == "/"
214+
assert network_span.name == "GET"
215215
assert network_span.attributes.get(HttpSpanMixin._HTTP_METHOD) == "GET"
216216
assert network_span.attributes.get(HttpSpanMixin._HTTP_URL) == "http://localhost"
217217
assert network_span.attributes.get(HttpSpanMixin._HTTP_USER_AGENT).endswith("mytools")
218-
assert network_span.attributes.get(policy._RESPONSE_ID) == "some request id"
219-
assert network_span.attributes.get(policy._REQUEST_ID) == "some client request id"
218+
assert network_span.attributes.get(policy._RESPONSE_ID_ATTR) == "some request id"
219+
assert network_span.attributes.get(policy._REQUEST_ID_ATTR) == "some client request id"
220220
assert network_span.attributes.get(HttpSpanMixin._HTTP_STATUS_CODE) == 202
221221

222222
network_span = root_span.children[1]
223-
assert network_span.name == "/"
223+
assert network_span.name == "GET"
224224
assert network_span.attributes.get(HttpSpanMixin._HTTP_METHOD) == "GET"
225225
assert network_span.attributes.get(HttpSpanMixin._HTTP_URL) == "http://localhost"
226226
assert network_span.attributes.get(HttpSpanMixin._HTTP_USER_AGENT).endswith("mytools")
227-
assert network_span.attributes.get(policy._REQUEST_ID) == "some client request id"
228-
assert network_span.attributes.get(policy._RESPONSE_ID) is None
227+
assert network_span.attributes.get(policy._REQUEST_ID_ATTR) == "some client request id"
228+
assert network_span.attributes.get(policy._RESPONSE_ID_ATTR) is None
229229
assert network_span.attributes.get(HttpSpanMixin._HTTP_STATUS_CODE) == 504
230230
# Exception should propagate status for Opencensus
231231
assert network_span.status == "Transport trouble"
@@ -378,7 +378,7 @@ def test_distributed_tracing_policy(self, tracing_helper, http_request, http_res
378378

379379
finished_spans = tracing_helper.exporter.get_finished_spans()
380380
assert len(finished_spans) == 2
381-
assert finished_spans[0].name == "/temp"
381+
assert finished_spans[0].name == "GET"
382382
assert finished_spans[0].parent is root_span.get_span_context()
383383

384384
assert finished_spans[0].attributes.get(policy._HTTP_REQUEST_METHOD) == "GET"
@@ -405,7 +405,7 @@ def test_distributed_tracing_policy_error_response(self, tracing_helper, http_re
405405
policy.on_response(pipeline_request, PipelineResponse(request, response, PipelineContext(None)))
406406

407407
finished_spans = tracing_helper.exporter.get_finished_spans()
408-
assert finished_spans[0].name == "/temp"
408+
assert finished_spans[0].name == "GET"
409409
assert finished_spans[0].attributes.get("error.type") == "403"
410410

411411
@pytest.mark.parametrize("http_request,http_response", request_and_responses_product(HTTP_RESPONSES))
@@ -433,7 +433,7 @@ def test_distributed_tracing_policy_custom_instrumentation_config(
433433
policy.on_response(pipeline_request, PipelineResponse(request, response, PipelineContext(None)))
434434

435435
finished_spans = tracing_helper.exporter.get_finished_spans()
436-
assert finished_spans[0].name == "/temp"
436+
assert finished_spans[0].name == "GET"
437437
assert finished_spans[0].attributes.get(policy._ERROR_TYPE) == "403"
438438
assert finished_spans[0].instrumentation_scope.name == "my-library"
439439
assert finished_spans[0].instrumentation_scope.version == "1.0.0"
@@ -495,7 +495,7 @@ def test_distributed_tracing_policy_with_user_agent_policy(self, tracing_helper,
495495

496496
finished_spans = tracing_helper.exporter.get_finished_spans()
497497
assert len(finished_spans) == 2
498-
assert finished_spans[0].name == "/temp"
498+
assert finished_spans[0].name == "GET"
499499
assert finished_spans[0].parent is root_span.get_span_context()
500500

501501
assert finished_spans[0].attributes.get(policy._HTTP_REQUEST_METHOD) == "GET"
@@ -579,7 +579,7 @@ def test_distributed_tracing_policy_with_tracing_options(self, tracing_helper, h
579579
finished_spans = tracing_helper.exporter.get_finished_spans()
580580
assert len(finished_spans) == 2
581581

582-
assert finished_spans[0].name == "/temp"
582+
assert finished_spans[0].name == "GET"
583583
assert finished_spans[0].parent is root_span.get_span_context()
584584

585585
assert finished_spans[0].attributes.get(policy._HTTP_REQUEST_METHOD) == "GET"
@@ -655,5 +655,5 @@ def test_tracing_impl_takes_precedence(self, tracing_implementation, http_reques
655655

656656
assert len(root_span.children) == 1
657657
network_span = root_span.children[0]
658-
assert network_span.name == "/temp"
658+
assert network_span.name == "GET"
659659
assert network_span.kind == SpanKind.CLIENT

0 commit comments

Comments
 (0)