|
| 1 | +# ------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | +# ------------------------------------ |
| 5 | +import pytest |
| 6 | +import os |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from azure.storage.blob.aio import BlobServiceClient |
| 10 | +from opentelemetry.trace import SpanKind, StatusCode |
| 11 | +from opentelemetry.sdk.trace import ReadableSpan |
| 12 | +from devtools_testutils import get_credential |
| 13 | + |
| 14 | + |
| 15 | +class TestTracingAsync: |
| 16 | + """Test class for validating async distributed tracing functionality.""" |
| 17 | + |
| 18 | + @pytest.mark.live_test_only |
| 19 | + @pytest.mark.asyncio |
| 20 | + async def test_blob_service_client_tracing_async(self, tracing_helper) -> None: |
| 21 | + """Test that tracing captures async client method calls and HTTP requests. |
| 22 | +
|
| 23 | + This test validates that the distributed tracing functionality properly |
| 24 | + captures spans when using the async Blob service client, ensuring parent-child |
| 25 | + relationships are maintained and attributes are correctly populated. |
| 26 | + """ |
| 27 | + account_url = os.environ.get("AZURE_STORAGE_BLOB_ENDPOINT") |
| 28 | + if not account_url: |
| 29 | + pytest.skip("AZURE_STORAGE_BLOB_ENDPOINT environment variable is not set.") |
| 30 | + |
| 31 | + client = BlobServiceClient(account_url=account_url, credential=get_credential(is_async=True)) |
| 32 | + |
| 33 | + with tracing_helper.tracer.start_as_current_span(name="root") as parent: |
| 34 | + await client.get_service_properties() |
| 35 | + |
| 36 | + # Close the client explicitly |
| 37 | + await client.close() |
| 38 | + |
| 39 | + spans = tracing_helper.exporter.get_finished_spans() |
| 40 | + span_names_list = [span.name for span in spans] |
| 41 | + # Check that last 3 spans are the expected ones (auth-related ones may vary) |
| 42 | + assert span_names_list[-3:] == ["GET", "BlobServiceClient.get_service_properties", "root"] |
| 43 | + |
| 44 | + http_span: ReadableSpan = spans[-3] |
| 45 | + assert http_span.kind == SpanKind.CLIENT |
| 46 | + assert http_span.parent |
| 47 | + assert http_span.parent.span_id == spans[-2].context.span_id |
| 48 | + |
| 49 | + # Validate HTTP span attributes |
| 50 | + assert http_span.attributes |
| 51 | + assert http_span.attributes["http.request.method"] == "GET" |
| 52 | + assert http_span.attributes["url.full"] |
| 53 | + assert http_span.attributes["server.address"] |
| 54 | + assert http_span.attributes["http.response.status_code"] == 200 |
| 55 | + user_agent = http_span.attributes.get("user_agent.original", "") |
| 56 | + assert isinstance(user_agent, str) and "storage" in user_agent |
| 57 | + |
| 58 | + # Validate method span |
| 59 | + method_span: ReadableSpan = spans[-2] |
| 60 | + assert method_span.kind == SpanKind.INTERNAL |
| 61 | + assert method_span.parent |
| 62 | + assert method_span.parent.span_id == spans[-1].context.span_id |
| 63 | + |
| 64 | + @pytest.mark.live_test_only |
| 65 | + @pytest.mark.asyncio |
| 66 | + async def test_error_handling_with_tracing_async(self, tracing_helper) -> None: |
| 67 | + """Test that tracing properly captures error information in async operations. |
| 68 | +
|
| 69 | + This test validates that when exceptions occur during async operations, |
| 70 | + the spans correctly capture the error information. |
| 71 | + """ |
| 72 | + account_name = "nonexistentaccount" |
| 73 | + invalid_url = f"https://{account_name}.blob.core.windows.net/" |
| 74 | + |
| 75 | + client = BlobServiceClient(account_url=invalid_url, credential=get_credential(is_async=True)) |
| 76 | + |
| 77 | + # Expecting this operation to fail |
| 78 | + with tracing_helper.tracer.start_as_current_span(name="root") as parent: |
| 79 | + try: |
| 80 | + await client.get_service_properties() |
| 81 | + except Exception: |
| 82 | + # We expect an exception but want to verify the spans |
| 83 | + pass |
| 84 | + |
| 85 | + # Close the client explicitly |
| 86 | + await client.close() |
| 87 | + |
| 88 | + spans = tracing_helper.exporter.get_finished_spans() |
| 89 | + span_names_list = [span.name for span in spans] |
| 90 | + # Check that last 3 spans are the expected ones (auth-related ones may vary) |
| 91 | + assert span_names_list[-3:] == ["GET", "BlobServiceClient.get_service_properties", "root"] |
| 92 | + |
| 93 | + http_span: ReadableSpan = spans[-3] |
| 94 | + assert http_span.kind == SpanKind.CLIENT |
| 95 | + assert http_span.attributes |
| 96 | + assert http_span.attributes["error.type"] == "azure.core.exceptions.ServiceRequestError" |
| 97 | + assert http_span.status.status_code == StatusCode.ERROR |
0 commit comments