From ba7e4fcc2930206f02b466d5e99941aee6d65dfa Mon Sep 17 00:00:00 2001 From: Radhika Gupta Date: Wed, 28 Jan 2026 16:16:22 -0800 Subject: [PATCH 1/3] Add missing copyright headers and ensuring consistency across files --- .../tests/customer_sdk_stats/test_app.py | 281 ++++++++++++++++++ .../opentelemetry/_diagnostics/__init__.py | 5 + .../sample_managed_credential.py | 6 +- .../sample_secret_credential.py | 6 +- .../samples/logging/custom_event.py | 6 +- .../samples/metrics/attributes.py | 5 +- .../samples/metrics/instruments.py | 6 +- .../samples/metrics/live_metrics.py | 6 +- .../samples/metrics/views.py | 5 +- .../samples/tracing/azure_ai_inference.py | 6 + .../samples/tracing/azure_blob_storage.py | 6 + .../samples/tracing/db_psycopg2.py | 1 + .../tracing/django/sample/example/__init__.py | 5 +- .../tracing/django/sample/example/admin.py | 6 +- .../tracing/django/sample/example/apps.py | 6 +- .../sample/example/migrations/__init__.py | 5 +- .../tracing/django/sample/example/models.py | 6 +- .../tracing/django/sample/example/tests.py | 6 +- .../tracing/django/sample/example/urls.py | 6 +- .../samples/tracing/django/sample/manage.py | 6 +- .../tracing/django/sample/sample/__init__.py | 5 +- .../tracing/django/sample/sample/asgi.py | 6 +- .../tracing/django/sample/sample/settings.py | 6 +- .../tracing/django/sample/sample/urls.py | 6 +- .../tracing/django/sample/sample/wsgi.py | 6 +- .../samples/tracing/http_fastapi.py | 1 + .../samples/tracing/http_flask.py | 1 + .../samples/tracing/http_requests.py | 1 + .../samples/tracing/http_urllib.py | 1 + .../samples/tracing/http_urllib3.py | 1 + .../tracing/instrumentation_options.py | 1 + .../samples/tracing/manually_instrumented.py | 1 + .../tracing/sampling_configurations.py | 6 + .../autoinstrumentation/test_configurator.py | 6 + .../tests/autoinstrumentation/test_distro.py | 6 + .../tests/conftest.py | 1 + .../tests/instrumentation/test_psycopg2.py | 1 + 37 files changed, 421 insertions(+), 20 deletions(-) create mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/tests/customer_sdk_stats/test_app.py diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/customer_sdk_stats/test_app.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/customer_sdk_stats/test_app.py new file mode 100644 index 000000000000..cf30fab26205 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/customer_sdk_stats/test_app.py @@ -0,0 +1,281 @@ +import os +import sys +import time +from unittest.mock import patch, Mock +from azure.core.exceptions import ServiceRequestError + +# Set up environment variables FIRST +os.environ["APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW"] = "true" +os.environ["APPLICATIONINSIGHTS_STATSBEAT_DISABLED_ALL"] = "true" +os.environ["APPLICATIONINSIGHTS_SDKSTATS_EXPORT_INTERVAL"] = "25" # 25 seconds for testing + +# Add the package to the path +sys.path.insert(0, r"C:\azure-sdk-for-python\sdk\monitor\azure-monitor-opentelemetry-exporter") + +from azure.monitor.opentelemetry.exporter.export.trace._exporter import AzureMonitorTraceExporter +from azure.monitor.opentelemetry.exporter.statsbeat.customer._manager import CustomerSdkStatsManager +from azure.monitor.opentelemetry.exporter._storage import StorageExportResult +from azure.monitor.opentelemetry.exporter.export._base import ExportResult +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchSpanProcessor +from opentelemetry import trace +from opentelemetry.trace import SpanKind + + +def print_customer_sdkstats_state(customer_sdkstats): + """Print the internal state of customer sdkstats""" + if customer_sdkstats and customer_sdkstats.is_enabled: + print("📊 Customer SDKStats internal state:") + print(f" Success count: {dict(customer_sdkstats._counters.total_item_success_count)}") + print(f" Drop count: {dict(customer_sdkstats._counters.total_item_drop_count)}") + print(f" Retry count: {dict(customer_sdkstats._counters.total_item_retry_count)}") + else: + print("❌ Customer SDKStats not available") + + +def main(): + print("=" * 70) + print("Customer SDKStats Demo - Success, Retry, and Dropped Items") + print("=" * 70) + print() + + # Configuration + connection_string = "InstrumentationKey=363331ca-f431-4119-bdcd-31a75920f958;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/" + + print("Configuration:") + print(f" Customer SDKStats: {os.environ.get('APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW', 'false')}") + print( + f" SDKStats Export Interval: {os.environ.get('APPLICATIONINSIGHTS_SDKSTATS_EXPORT_INTERVAL', '900')} seconds" + ) + print(f" Connection String: {connection_string[:50]}...") + print() + + # Set up tracing + print("🔧 Setting up Azure Monitor tracing...") + trace.set_tracer_provider(TracerProvider()) + tracer = trace.get_tracer(__name__) + + azure_exporter = AzureMonitorTraceExporter(connection_string=connection_string) + span_processor = BatchSpanProcessor(azure_exporter) + trace.get_tracer_provider().add_span_processor(span_processor) + + print("✅ Azure Monitor tracing configured") + + # Initialize customer sdkstats + customer_sdkstats = CustomerSdkStatsManager(connection_string) + if customer_sdkstats and customer_sdkstats.is_enabled: + print("✅ Customer SDKStats initialized") + else: + print("❌ Customer SDKStats not enabled") + + print_customer_sdkstats_state(customer_sdkstats) + print() + + # Step 1: Generate successful telemetry + print("🚀 Step 1: Generate successful dependency telemetry...") + for i in range(3): + with tracer.start_as_current_span(f"successful_dependency_{i}", kind=SpanKind.CLIENT) as span: + span.set_attribute("http.method", "GET") + span.set_attribute("http.url", f"https://api.example.com/item/{i}") + span.set_attribute("http.status_code", 200) + + print(" Generated 3 successful dependency spans") + span_processor.force_flush() + time.sleep(2) + + print_customer_sdkstats_state(customer_sdkstats) + print() + + # Step 2: Generate retry items (network errors) + print("🚀 Step 2: Simulate network errors to generate retry items...") + + # Mock at the HTTP request level so the retry tracking logic in _base.py gets triggered + from unittest.mock import MagicMock + + def mock_request_network_error(*args, **kwargs): + print(" 🌐 Simulated network error at HTTP level") + raise ServiceRequestError("Connection timeout") + + # Patch the azure-core client's track method which is called by _transmit + from azure.monitor.opentelemetry.exporter._generated import AzureMonitorClient + + with patch.object(AzureMonitorClient, "track", side_effect=mock_request_network_error): + print(" 📦 Mocking AzureMonitorClient.track() to raise ServiceRequestError") + print(" 🎯 Generating telemetry that will trigger retry tracking...") + + for i in range(4): + with tracer.start_as_current_span(f"retry_dependency_{i}", kind=SpanKind.CLIENT) as span: + span.set_attribute("http.method", "POST") + span.set_attribute("http.url", f"https://api.retry.com/data/{i}") + span.set_attribute("http.status_code", 200) + span.set_attribute("test.scenario", "network_error") + + print(" Generated 4 dependency spans with network error simulation") + print(" 🔄 Forcing export (should trigger retry tracking)...") + span_processor.force_flush() + time.sleep(2) + + print_customer_sdkstats_state(customer_sdkstats) + print() + + # Step 2.5: Generate retry items with integer retry codes (HTTP status codes like 429) + print("🚀 Step 2.5: Simulate HTTP 429 (Too Many Requests) to generate retry items with integer retry code...") + + from azure.core.exceptions import HttpResponseError + from azure.core.pipeline.transport import HttpResponse + + def mock_track_429(*args, **kwargs): + print(" 🔄 Track mocked: raising HttpResponseError with status 429") + # Create a mock response object + mock_response = Mock(spec=HttpResponse) + mock_response.status_code = 429 + mock_response.reason = "Too Many Requests" + raise HttpResponseError(response=mock_response) + + with patch.object(AzureMonitorClient, "track", side_effect=mock_track_429): + + print(" 📦 Mocking AzureMonitorClient.track() to raise HTTP 429 (retryable)") + print(" 🎯 Generating telemetry that will be retried with integer retry code 429...") + + for i in range(3): + with tracer.start_as_current_span(f"retryable_429_dependency_{i}", kind=SpanKind.CLIENT) as span: + span.set_attribute("http.method", "POST") + span.set_attribute("http.url", f"https://api.retryable.com/data/{i}") + span.set_attribute("http.status_code", 200) + span.set_attribute("test.scenario", "http_429_retry") + + print(" Generated 3 dependency spans that will be retried with HTTP 429") + print(" 🔄 Forcing export (should trigger retry tracking with retry_code=429)...") + span_processor.force_flush() + time.sleep(2) + + print_customer_sdkstats_state(customer_sdkstats) + print() + + # Step 3: Generate dropped items (storage disabled) + print("🚀 Step 3: Simulate storage failures to generate dropped items...") + + def mock_storage_disabled(*args, **kwargs): + print(" 📁 Storage mocked: returning CLIENT_STORAGE_DISABLED") + return StorageExportResult.CLIENT_STORAGE_DISABLED + + def mock_transmit_failed_retryable(*args, **kwargs): + print(" 🌐 Transmission mocked: returning FAILED_RETRYABLE") + return ExportResult.FAILED_RETRYABLE + + with patch.object(azure_exporter.storage, "put", side_effect=mock_storage_disabled), patch.object( + azure_exporter, "_transmit", side_effect=mock_transmit_failed_retryable + ): + + print(" 📦 Mocking transmission failure + storage disabled") + print(" 🎯 Generating telemetry that will be dropped...") + + for i in range(5): + with tracer.start_as_current_span(f"dropped_dependency_{i}", kind=SpanKind.CLIENT) as span: + span.set_attribute("http.method", "POST") + span.set_attribute("http.url", f"https://api.dropped.com/upload/{i}") + span.set_attribute("http.status_code", 200) + span.set_attribute("test.scenario", "storage_disabled") + + print(" Generated 5 dependency spans that will be dropped") + print(" 🔄 Forcing export (should trigger dropped tracking)...") + span_processor.force_flush() + time.sleep(2) + + print_customer_sdkstats_state(customer_sdkstats) + print() + + # Step 3.5: Generate dropped items with integer drop codes (HTTP status codes) + print("🚀 Step 3.5: Simulate HTTP 402 (Throttle/Quota) to generate dropped items with integer drop code...") + + from azure.core.exceptions import HttpResponseError + from azure.core.pipeline.transport import HttpResponse + + def mock_track_402(*args, **kwargs): + print(" 🚫 Track mocked: raising HttpResponseError with status 402") + # Create a mock response object + mock_response = Mock(spec=HttpResponse) + mock_response.status_code = 402 + mock_response.reason = "Payment Required - Quota Exceeded" + raise HttpResponseError(response=mock_response) + + with patch.object(AzureMonitorClient, "track", side_effect=mock_track_402): + + print(" 📦 Mocking AzureMonitorClient.track() to raise HTTP 402 (throttle/quota)") + print(" 🎯 Generating telemetry that will be dropped with integer drop code 402...") + + for i in range(3): + with tracer.start_as_current_span(f"throttled_dependency_{i}", kind=SpanKind.CLIENT) as span: + span.set_attribute("http.method", "POST") + span.set_attribute("http.url", f"https://api.throttled.com/data/{i}") + span.set_attribute("http.status_code", 200) + span.set_attribute("test.scenario", "http_402_throttle") + + print(" Generated 3 dependency spans that will be dropped with HTTP 402") + print(" 🔄 Forcing export (should trigger dropped tracking with drop_code=402)...") + span_processor.force_flush() + time.sleep(2) + + print_customer_sdkstats_state(customer_sdkstats) + print() + + # Step 4: Generate more successful telemetry + print("🚀 Step 4: Generate final successful telemetry...") + for i in range(2): + with tracer.start_as_current_span(f"final_dependency_{i}", kind=SpanKind.CLIENT) as span: + span.set_attribute("http.method", "GET") + span.set_attribute("http.url", f"https://api.final.com/item/{i}") + span.set_attribute("http.status_code", 200) + + print(" Generated 2 final successful dependency spans") + span_processor.force_flush() + time.sleep(2) + + print_customer_sdkstats_state(customer_sdkstats) + print() + + # Wait for customer sdkstats export + print("⏱️ Waiting 30 seconds for customer sdkstats export...") + for i in range(30): + if i % 5 == 0: + print(f" Waiting... {i+1}/30s") + time.sleep(1) + + print() + print("=" * 70) + print("DEMO COMPLETED") + print("=" * 70) + print() + print("📊 Final customer sdkstats state:") + print_customer_sdkstats_state(customer_sdkstats) + print() + print("🔍 Check Application Insights for these metrics:") + print(" - 'itemSuccessCount' (telemetry_type='dependency') ← Should show ~5 items") + print(" - 'itemRetryCount' (telemetry_type='dependency') ← Should show ~7 items") + print(" • retry_code='CLIENT_TIMEOUT' (4 items)") + print(" • retry_code='429' (3 items)") + print(" - 'itemDroppedCount' (telemetry_type='dependency') ← Should show ~8 items") + print(" • drop_code='CLIENT_STORAGE_DISABLED' (5 items)") + print(" • drop_code='402' (3 items)") + print() + print("Expected results:") + print(" ✅ Success: ~5 items (3 initial + 2 final)") + print(" 🔄 Retry: ~7 items (4 network timeout + 3 HTTP 429)") + print(" ❌ Dropped: ~8 items (5 storage disabled + 3 HTTP 402)") + print() + print("Query in Application Insights:") + print("customMetrics") + print("| where name in ('itemSuccessCount', 'itemRetryCount', 'itemDroppedCount')") + print("| where timestamp > ago(5m)") + print("| extend telemetry_type = tostring(customDimensions.telemetry_type)") + print("| extend drop_code = tostring(customDimensions['drop.code'])") + print("| extend retry_code = tostring(customDimensions['retry.code'])") + print("| project timestamp, name, value, telemetry_type, drop_code, retry_code") + print("| order by timestamp desc") + print() + print("✅ Demo completed!") + print() + + +if __name__ == "__main__": + main() diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/__init__.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/__init__.py index e69de29bb2d1..0bdee620366b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/__init__.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/__init__.py @@ -0,0 +1,5 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/authentication/sample_managed_credential.py b/sdk/monitor/azure-monitor-opentelemetry/samples/authentication/sample_managed_credential.py index dd8d8106e216..df0574f30d1b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/authentication/sample_managed_credential.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/authentication/sample_managed_credential.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + """ An example to show an application using Opentelemetry tracing api and sdk with a Azure Managed Identity Credential. Credentials are used for Azure Active Directory/EntraId Authentication. diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/authentication/sample_secret_credential.py b/sdk/monitor/azure-monitor-opentelemetry/samples/authentication/sample_secret_credential.py index 8e3d5aea3973..45c32d689989 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/authentication/sample_secret_credential.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/authentication/sample_secret_credential.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + """ An example to show an application using Opentelemetry tracing api and sdk with a Azure Client Secret Credential. Credentials are used for Azure Active Directory/EntraId Authentication. diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/logging/custom_event.py b/sdk/monitor/azure-monitor-opentelemetry/samples/logging/custom_event.py index 93e4777f5b4c..a15fbf62be42 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/logging/custom_event.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/logging/custom_event.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + """ An example to show an application using Opentelemetry logging sdk. Logging calls to the standard Python logging library are tracked and telemetry is exported to application insights with the AzureMonitorLogExporter. diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/attributes.py b/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/attributes.py index 5060e721220f..7e676cc2d527 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/attributes.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/attributes.py @@ -1,5 +1,8 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- from azure.monitor.opentelemetry import configure_azure_monitor from opentelemetry import metrics diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/instruments.py b/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/instruments.py index e62940a0ff4f..882218981dca 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/instruments.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/instruments.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + from typing import Iterable from azure.monitor.opentelemetry import configure_azure_monitor diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/live_metrics.py b/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/live_metrics.py index b7e6b42407c5..742633419f4e 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/live_metrics.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/live_metrics.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + """ This example shows how configure live metrics to be enabled. It sets up a minimal example of sending dependency, trace and exception telemetry to demonstrate the capabilities and collection set of live metrics. diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/views.py b/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/views.py index 2260e13a6607..1affc1dcd684 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/views.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/views.py @@ -1,5 +1,8 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- from azure.monitor.opentelemetry import configure_azure_monitor from opentelemetry import metrics diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/azure_ai_inference.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/azure_ai_inference.py index 727e01e36353..90e2caec8426 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/azure_ai_inference.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/azure_ai_inference.py @@ -1,3 +1,9 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + from os import environ import os diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/azure_blob_storage.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/azure_blob_storage.py index f4b4877dd422..5a31c6e49dfc 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/azure_blob_storage.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/azure_blob_storage.py @@ -1,3 +1,9 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + from os import environ from azure.monitor.opentelemetry import configure_azure_monitor diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py index d69784e8e13e..c6897b15412a 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See License in the project root for # license information. # -------------------------------------------------------------------------- + import psycopg2 from azure.monitor.opentelemetry import configure_azure_monitor diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/__init__.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/__init__.py index 5b7f7a925cc0..0bdee620366b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/__init__.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/__init__.py @@ -1,2 +1,5 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/admin.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/admin.py index 3f8b9caa30c7..e1ceac9b70c3 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/admin.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/admin.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + from django.contrib import admin # Register your models here. diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/apps.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/apps.py index cfad6058d247..6be675c6738f 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/apps.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/apps.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + from django.apps import AppConfig diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/migrations/__init__.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/migrations/__init__.py index 5b7f7a925cc0..0bdee620366b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/migrations/__init__.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/migrations/__init__.py @@ -1,2 +1,5 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/models.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/models.py index 526ea7fddb97..95f07cd2eeed 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/models.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/models.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + from django.db import models # Create your models here. diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/tests.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/tests.py index 7705f147602a..81c546d8d6f1 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/tests.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/tests.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + from django.test import TestCase # Create your tests here. diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/urls.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/urls.py index 27499cbfbc19..bb3eb29b488c 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/urls.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/example/urls.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + from django.urls import path from . import views diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/manage.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/manage.py index 222c1993146e..cb16334e3e45 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/manage.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/manage.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" import os diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/__init__.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/__init__.py index 5b7f7a925cc0..0bdee620366b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/__init__.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/__init__.py @@ -1,2 +1,5 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/asgi.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/asgi.py index a299d1a45763..0f2410b4887c 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/asgi.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/asgi.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + # cSpell:disable """ ASGI config for sample project. diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/settings.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/settings.py index 28f3b356de4a..6ae8bbf93bc5 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/settings.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/settings.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + """ Django settings for sample project. diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/urls.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/urls.py index 851a91e5ce26..e7d5ceffd855 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/urls.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/urls.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + """sample URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/wsgi.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/wsgi.py index 28fbcdcc2cd7..c4ad859f4931 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/wsgi.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/django/sample/sample/wsgi.py @@ -1,5 +1,9 @@ +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + """ WSGI config for sample project. diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py index 096e5f757fe0..2abd4c3cd86a 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_fastapi.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See License in the project root for # license information. # -------------------------------------------------------------------------- + import fastapi import uvicorn diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_flask.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_flask.py index 33c982f77389..4729866e540e 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_flask.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_flask.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See License in the project root for # license information. # -------------------------------------------------------------------------- + from azure.monitor.opentelemetry import configure_azure_monitor # Configure Azure monitor collection telemetry pipeline diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_requests.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_requests.py index d9a992fb4b87..66f8b1b09eeb 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_requests.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_requests.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See License in the project root for # license information. # -------------------------------------------------------------------------- + import logging import requests # type: ignore[import-untyped] diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_urllib.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_urllib.py index 9b2568ef59f8..7b561922bfc6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_urllib.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_urllib.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See License in the project root for # license information. # -------------------------------------------------------------------------- + import logging from urllib import request diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_urllib3.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_urllib3.py index 82cfdeb3f4dc..f14e57c08dbb 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_urllib3.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_urllib3.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See License in the project root for # license information. # -------------------------------------------------------------------------- + import logging import urllib3 diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/instrumentation_options.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/instrumentation_options.py index 6a307727696b..149601564518 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/instrumentation_options.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/instrumentation_options.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See License in the project root for # license information. # -------------------------------------------------------------------------- + from azure.monitor.opentelemetry import configure_azure_monitor # Enable or disable supported instrumentations with the instrumentation_options parameter diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/manually_instrumented.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/manually_instrumented.py index feaa15f4f5da..19c9433f24d6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/manually_instrumented.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/manually_instrumented.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See License in the project root for # license information. # -------------------------------------------------------------------------- + from azure.monitor.opentelemetry import configure_azure_monitor from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor from sqlalchemy import create_engine, text diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/sampling_configurations.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/sampling_configurations.py index bb7e151a82b0..747b34312e22 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/sampling_configurations.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/sampling_configurations.py @@ -1,3 +1,9 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + from azure.monitor.opentelemetry import configure_azure_monitor from opentelemetry import trace diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py index c43021656bbb..97416438ecf1 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py @@ -1,3 +1,9 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + import warnings from unittest import TestCase from unittest.mock import patch diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py index 623e5ed37069..65babb43144a 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py @@ -1,3 +1,9 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + from os import environ import warnings from unittest import TestCase diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/conftest.py b/sdk/monitor/azure-monitor-opentelemetry/tests/conftest.py index 87ce6297470c..e326362a530d 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/conftest.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/conftest.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- + import os from tempfile import mkstemp diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/instrumentation/test_psycopg2.py b/sdk/monitor/azure-monitor-opentelemetry/tests/instrumentation/test_psycopg2.py index 8c08d8605ab2..96fada6fd49f 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/instrumentation/test_psycopg2.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/instrumentation/test_psycopg2.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See License in the project root for # license information. # -------------------------------------------------------------------------- + import os import pytest import sys From 0249e27e8226ff54681460c2e77adcea4d3dc015 Mon Sep 17 00:00:00 2001 From: Radhika Gupta Date: Wed, 28 Jan 2026 16:25:03 -0800 Subject: [PATCH 2/3] Remove sample test app --- .../tests/customer_sdk_stats/test_app.py | 281 ------------------ 1 file changed, 281 deletions(-) delete mode 100644 sdk/monitor/azure-monitor-opentelemetry-exporter/tests/customer_sdk_stats/test_app.py diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/customer_sdk_stats/test_app.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/customer_sdk_stats/test_app.py deleted file mode 100644 index cf30fab26205..000000000000 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/customer_sdk_stats/test_app.py +++ /dev/null @@ -1,281 +0,0 @@ -import os -import sys -import time -from unittest.mock import patch, Mock -from azure.core.exceptions import ServiceRequestError - -# Set up environment variables FIRST -os.environ["APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW"] = "true" -os.environ["APPLICATIONINSIGHTS_STATSBEAT_DISABLED_ALL"] = "true" -os.environ["APPLICATIONINSIGHTS_SDKSTATS_EXPORT_INTERVAL"] = "25" # 25 seconds for testing - -# Add the package to the path -sys.path.insert(0, r"C:\azure-sdk-for-python\sdk\monitor\azure-monitor-opentelemetry-exporter") - -from azure.monitor.opentelemetry.exporter.export.trace._exporter import AzureMonitorTraceExporter -from azure.monitor.opentelemetry.exporter.statsbeat.customer._manager import CustomerSdkStatsManager -from azure.monitor.opentelemetry.exporter._storage import StorageExportResult -from azure.monitor.opentelemetry.exporter.export._base import ExportResult -from opentelemetry.sdk.trace import TracerProvider -from opentelemetry.sdk.trace.export import BatchSpanProcessor -from opentelemetry import trace -from opentelemetry.trace import SpanKind - - -def print_customer_sdkstats_state(customer_sdkstats): - """Print the internal state of customer sdkstats""" - if customer_sdkstats and customer_sdkstats.is_enabled: - print("📊 Customer SDKStats internal state:") - print(f" Success count: {dict(customer_sdkstats._counters.total_item_success_count)}") - print(f" Drop count: {dict(customer_sdkstats._counters.total_item_drop_count)}") - print(f" Retry count: {dict(customer_sdkstats._counters.total_item_retry_count)}") - else: - print("❌ Customer SDKStats not available") - - -def main(): - print("=" * 70) - print("Customer SDKStats Demo - Success, Retry, and Dropped Items") - print("=" * 70) - print() - - # Configuration - connection_string = "InstrumentationKey=363331ca-f431-4119-bdcd-31a75920f958;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/" - - print("Configuration:") - print(f" Customer SDKStats: {os.environ.get('APPLICATIONINSIGHTS_SDKSTATS_ENABLED_PREVIEW', 'false')}") - print( - f" SDKStats Export Interval: {os.environ.get('APPLICATIONINSIGHTS_SDKSTATS_EXPORT_INTERVAL', '900')} seconds" - ) - print(f" Connection String: {connection_string[:50]}...") - print() - - # Set up tracing - print("🔧 Setting up Azure Monitor tracing...") - trace.set_tracer_provider(TracerProvider()) - tracer = trace.get_tracer(__name__) - - azure_exporter = AzureMonitorTraceExporter(connection_string=connection_string) - span_processor = BatchSpanProcessor(azure_exporter) - trace.get_tracer_provider().add_span_processor(span_processor) - - print("✅ Azure Monitor tracing configured") - - # Initialize customer sdkstats - customer_sdkstats = CustomerSdkStatsManager(connection_string) - if customer_sdkstats and customer_sdkstats.is_enabled: - print("✅ Customer SDKStats initialized") - else: - print("❌ Customer SDKStats not enabled") - - print_customer_sdkstats_state(customer_sdkstats) - print() - - # Step 1: Generate successful telemetry - print("🚀 Step 1: Generate successful dependency telemetry...") - for i in range(3): - with tracer.start_as_current_span(f"successful_dependency_{i}", kind=SpanKind.CLIENT) as span: - span.set_attribute("http.method", "GET") - span.set_attribute("http.url", f"https://api.example.com/item/{i}") - span.set_attribute("http.status_code", 200) - - print(" Generated 3 successful dependency spans") - span_processor.force_flush() - time.sleep(2) - - print_customer_sdkstats_state(customer_sdkstats) - print() - - # Step 2: Generate retry items (network errors) - print("🚀 Step 2: Simulate network errors to generate retry items...") - - # Mock at the HTTP request level so the retry tracking logic in _base.py gets triggered - from unittest.mock import MagicMock - - def mock_request_network_error(*args, **kwargs): - print(" 🌐 Simulated network error at HTTP level") - raise ServiceRequestError("Connection timeout") - - # Patch the azure-core client's track method which is called by _transmit - from azure.monitor.opentelemetry.exporter._generated import AzureMonitorClient - - with patch.object(AzureMonitorClient, "track", side_effect=mock_request_network_error): - print(" 📦 Mocking AzureMonitorClient.track() to raise ServiceRequestError") - print(" 🎯 Generating telemetry that will trigger retry tracking...") - - for i in range(4): - with tracer.start_as_current_span(f"retry_dependency_{i}", kind=SpanKind.CLIENT) as span: - span.set_attribute("http.method", "POST") - span.set_attribute("http.url", f"https://api.retry.com/data/{i}") - span.set_attribute("http.status_code", 200) - span.set_attribute("test.scenario", "network_error") - - print(" Generated 4 dependency spans with network error simulation") - print(" 🔄 Forcing export (should trigger retry tracking)...") - span_processor.force_flush() - time.sleep(2) - - print_customer_sdkstats_state(customer_sdkstats) - print() - - # Step 2.5: Generate retry items with integer retry codes (HTTP status codes like 429) - print("🚀 Step 2.5: Simulate HTTP 429 (Too Many Requests) to generate retry items with integer retry code...") - - from azure.core.exceptions import HttpResponseError - from azure.core.pipeline.transport import HttpResponse - - def mock_track_429(*args, **kwargs): - print(" 🔄 Track mocked: raising HttpResponseError with status 429") - # Create a mock response object - mock_response = Mock(spec=HttpResponse) - mock_response.status_code = 429 - mock_response.reason = "Too Many Requests" - raise HttpResponseError(response=mock_response) - - with patch.object(AzureMonitorClient, "track", side_effect=mock_track_429): - - print(" 📦 Mocking AzureMonitorClient.track() to raise HTTP 429 (retryable)") - print(" 🎯 Generating telemetry that will be retried with integer retry code 429...") - - for i in range(3): - with tracer.start_as_current_span(f"retryable_429_dependency_{i}", kind=SpanKind.CLIENT) as span: - span.set_attribute("http.method", "POST") - span.set_attribute("http.url", f"https://api.retryable.com/data/{i}") - span.set_attribute("http.status_code", 200) - span.set_attribute("test.scenario", "http_429_retry") - - print(" Generated 3 dependency spans that will be retried with HTTP 429") - print(" 🔄 Forcing export (should trigger retry tracking with retry_code=429)...") - span_processor.force_flush() - time.sleep(2) - - print_customer_sdkstats_state(customer_sdkstats) - print() - - # Step 3: Generate dropped items (storage disabled) - print("🚀 Step 3: Simulate storage failures to generate dropped items...") - - def mock_storage_disabled(*args, **kwargs): - print(" 📁 Storage mocked: returning CLIENT_STORAGE_DISABLED") - return StorageExportResult.CLIENT_STORAGE_DISABLED - - def mock_transmit_failed_retryable(*args, **kwargs): - print(" 🌐 Transmission mocked: returning FAILED_RETRYABLE") - return ExportResult.FAILED_RETRYABLE - - with patch.object(azure_exporter.storage, "put", side_effect=mock_storage_disabled), patch.object( - azure_exporter, "_transmit", side_effect=mock_transmit_failed_retryable - ): - - print(" 📦 Mocking transmission failure + storage disabled") - print(" 🎯 Generating telemetry that will be dropped...") - - for i in range(5): - with tracer.start_as_current_span(f"dropped_dependency_{i}", kind=SpanKind.CLIENT) as span: - span.set_attribute("http.method", "POST") - span.set_attribute("http.url", f"https://api.dropped.com/upload/{i}") - span.set_attribute("http.status_code", 200) - span.set_attribute("test.scenario", "storage_disabled") - - print(" Generated 5 dependency spans that will be dropped") - print(" 🔄 Forcing export (should trigger dropped tracking)...") - span_processor.force_flush() - time.sleep(2) - - print_customer_sdkstats_state(customer_sdkstats) - print() - - # Step 3.5: Generate dropped items with integer drop codes (HTTP status codes) - print("🚀 Step 3.5: Simulate HTTP 402 (Throttle/Quota) to generate dropped items with integer drop code...") - - from azure.core.exceptions import HttpResponseError - from azure.core.pipeline.transport import HttpResponse - - def mock_track_402(*args, **kwargs): - print(" 🚫 Track mocked: raising HttpResponseError with status 402") - # Create a mock response object - mock_response = Mock(spec=HttpResponse) - mock_response.status_code = 402 - mock_response.reason = "Payment Required - Quota Exceeded" - raise HttpResponseError(response=mock_response) - - with patch.object(AzureMonitorClient, "track", side_effect=mock_track_402): - - print(" 📦 Mocking AzureMonitorClient.track() to raise HTTP 402 (throttle/quota)") - print(" 🎯 Generating telemetry that will be dropped with integer drop code 402...") - - for i in range(3): - with tracer.start_as_current_span(f"throttled_dependency_{i}", kind=SpanKind.CLIENT) as span: - span.set_attribute("http.method", "POST") - span.set_attribute("http.url", f"https://api.throttled.com/data/{i}") - span.set_attribute("http.status_code", 200) - span.set_attribute("test.scenario", "http_402_throttle") - - print(" Generated 3 dependency spans that will be dropped with HTTP 402") - print(" 🔄 Forcing export (should trigger dropped tracking with drop_code=402)...") - span_processor.force_flush() - time.sleep(2) - - print_customer_sdkstats_state(customer_sdkstats) - print() - - # Step 4: Generate more successful telemetry - print("🚀 Step 4: Generate final successful telemetry...") - for i in range(2): - with tracer.start_as_current_span(f"final_dependency_{i}", kind=SpanKind.CLIENT) as span: - span.set_attribute("http.method", "GET") - span.set_attribute("http.url", f"https://api.final.com/item/{i}") - span.set_attribute("http.status_code", 200) - - print(" Generated 2 final successful dependency spans") - span_processor.force_flush() - time.sleep(2) - - print_customer_sdkstats_state(customer_sdkstats) - print() - - # Wait for customer sdkstats export - print("⏱️ Waiting 30 seconds for customer sdkstats export...") - for i in range(30): - if i % 5 == 0: - print(f" Waiting... {i+1}/30s") - time.sleep(1) - - print() - print("=" * 70) - print("DEMO COMPLETED") - print("=" * 70) - print() - print("📊 Final customer sdkstats state:") - print_customer_sdkstats_state(customer_sdkstats) - print() - print("🔍 Check Application Insights for these metrics:") - print(" - 'itemSuccessCount' (telemetry_type='dependency') ← Should show ~5 items") - print(" - 'itemRetryCount' (telemetry_type='dependency') ← Should show ~7 items") - print(" • retry_code='CLIENT_TIMEOUT' (4 items)") - print(" • retry_code='429' (3 items)") - print(" - 'itemDroppedCount' (telemetry_type='dependency') ← Should show ~8 items") - print(" • drop_code='CLIENT_STORAGE_DISABLED' (5 items)") - print(" • drop_code='402' (3 items)") - print() - print("Expected results:") - print(" ✅ Success: ~5 items (3 initial + 2 final)") - print(" 🔄 Retry: ~7 items (4 network timeout + 3 HTTP 429)") - print(" ❌ Dropped: ~8 items (5 storage disabled + 3 HTTP 402)") - print() - print("Query in Application Insights:") - print("customMetrics") - print("| where name in ('itemSuccessCount', 'itemRetryCount', 'itemDroppedCount')") - print("| where timestamp > ago(5m)") - print("| extend telemetry_type = tostring(customDimensions.telemetry_type)") - print("| extend drop_code = tostring(customDimensions['drop.code'])") - print("| extend retry_code = tostring(customDimensions['retry.code'])") - print("| project timestamp, name, value, telemetry_type, drop_code, retry_code") - print("| order by timestamp desc") - print() - print("✅ Demo completed!") - print() - - -if __name__ == "__main__": - main() From 5b54d6983311b06a9718fef7ae2d5b2a0cfdd73c Mon Sep 17 00:00:00 2001 From: Radhika Gupta Date: Wed, 28 Jan 2026 16:36:03 -0800 Subject: [PATCH 3/3] Added CHANGELOG --- sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md index 496476fc53e9..5479d44d416d 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md @@ -9,6 +9,7 @@ ### Bugs Fixed ### Other Changes +- Add missing copyright headers and ensure consistent formatting across files. ## 1.8.5 (2026-01-28)