|
11 | 11 | # pylint: disable=import-error
|
12 | 12 | from opentelemetry.trace import get_tracer_provider, set_tracer_provider
|
13 | 13 | from opentelemetry.sdk import trace, resources
|
| 14 | +from opentelemetry.sdk.trace import TracerProvider |
| 15 | + |
14 | 16 | from opentelemetry.sdk.trace.export import SpanExportResult
|
| 17 | +from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter |
| 18 | +from opentelemetry.sdk.trace.export import SimpleSpanProcessor |
15 | 19 | from opentelemetry.sdk.util.instrumentation import InstrumentationScope
|
16 | 20 | from opentelemetry.semconv.attributes.exception_attributes import (
|
17 | 21 | EXCEPTION_ESCAPED,
|
|
22 | 26 | from opentelemetry.trace import Link, SpanContext, SpanKind
|
23 | 27 | from opentelemetry.trace.status import Status, StatusCode
|
24 | 28 |
|
| 29 | +from azure.core.settings import settings |
| 30 | +from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan |
| 31 | + |
| 32 | +try: |
| 33 | + from azure.core.instrumentation import get_tracer as get_azure_sdk_tracer |
| 34 | +except ImportError: |
| 35 | + # azure.core.instrumentation is not available in older versions of azure-core |
| 36 | + get_azure_sdk_tracer = None |
25 | 37 | from azure.monitor.opentelemetry.exporter.export._base import ExportResult
|
26 | 38 | from azure.monitor.opentelemetry.exporter.export.trace._exporter import (
|
27 | 39 | AzureMonitorTraceExporter,
|
|
31 | 43 | from azure.monitor.opentelemetry.exporter._constants import (
|
32 | 44 | _AZURE_SDK_NAMESPACE_NAME,
|
33 | 45 | _AZURE_SDK_OPENTELEMETRY_NAME,
|
| 46 | + _AZURE_AI_SDK_NAME, |
34 | 47 | )
|
35 | 48 | from azure.monitor.opentelemetry.exporter._generated.models import ContextTagKeys
|
36 | 49 | from azure.monitor.opentelemetry.exporter._utils import azure_monitor_context
|
@@ -1128,7 +1141,7 @@ def test_span_envelope_server_http(self):
|
1128 | 1141 | "url.path": "/path",
|
1129 | 1142 | "url.query": "query",
|
1130 | 1143 | "server.address": "www.example.org",
|
1131 |
| - "server.port": "80" |
| 1144 | + "server.port": "80", |
1132 | 1145 | }
|
1133 | 1146 | envelope = exporter._span_to_envelope(span)
|
1134 | 1147 | self.assertEqual(envelope.data.base_data.url, "https://www.example.org:80/path?query")
|
@@ -1687,8 +1700,89 @@ def test_check_instrumentation_span_not_instrumentation(self):
|
1687 | 1700 |
|
1688 | 1701 | def test_check_instrumentation_span_azure_sdk(self):
|
1689 | 1702 | span = mock.Mock()
|
1690 |
| - span.attributes = {_AZURE_SDK_NAMESPACE_NAME: "Microsoft.EventHub"} |
1691 |
| - span.instrumentation_scope.name = "__main__" |
| 1703 | + span.attributes = {} |
| 1704 | + span.instrumentation_scope.name = "azure.foo.bar.__init__" |
| 1705 | + |
1692 | 1706 | with mock.patch("azure.monitor.opentelemetry.exporter._utils.add_instrumentation") as add:
|
1693 | 1707 | _check_instrumentation_span(span)
|
1694 | 1708 | add.assert_called_once_with(_AZURE_SDK_OPENTELEMETRY_NAME)
|
| 1709 | + |
| 1710 | + @mock.patch("opentelemetry.trace.get_tracer_provider") |
| 1711 | + def test_check_instrumentation_span_azure_sdk_otel_span(self, mock_get_tracer_provider): |
| 1712 | + mock_get_tracer_provider.return_value = self.get_tracer_provider() |
| 1713 | + |
| 1714 | + with OpenTelemetrySpan() as azure_sdk_span: |
| 1715 | + with mock.patch("azure.monitor.opentelemetry.exporter._utils.add_instrumentation") as add: |
| 1716 | + _check_instrumentation_span(azure_sdk_span.span_instance) |
| 1717 | + add.assert_called_once_with(_AZURE_SDK_OPENTELEMETRY_NAME) |
| 1718 | + |
| 1719 | + with mock.patch("azure.monitor.opentelemetry.exporter._utils.add_instrumentation") as add: |
| 1720 | + azure_sdk_span.add_attribute(_AZURE_SDK_NAMESPACE_NAME, "Microsoft.ServiceBus") |
| 1721 | + _check_instrumentation_span(azure_sdk_span.span_instance) |
| 1722 | + add.assert_called_once_with(_AZURE_SDK_OPENTELEMETRY_NAME) |
| 1723 | + |
| 1724 | + with mock.patch("azure.monitor.opentelemetry.exporter._utils.add_instrumentation") as add: |
| 1725 | + azure_sdk_span.add_attribute(_AZURE_SDK_NAMESPACE_NAME, "Microsoft.CognitiveServices") |
| 1726 | + _check_instrumentation_span(azure_sdk_span.span_instance) |
| 1727 | + add.assert_called_once_with(_AZURE_AI_SDK_NAME) |
| 1728 | + |
| 1729 | + @mock.patch("opentelemetry.trace.get_tracer_provider") |
| 1730 | + def test_check_instrumentation_span_azure_sdk_span_impl(self, mock_get_tracer_provider): |
| 1731 | + mock_get_tracer_provider.return_value = self.get_tracer_provider() |
| 1732 | + |
| 1733 | + settings.tracing_implementation = "opentelemetry" |
| 1734 | + try: |
| 1735 | + azure_sdk_span = settings.tracing_implementation()(name="test") |
| 1736 | + |
| 1737 | + with mock.patch("azure.monitor.opentelemetry.exporter._utils.add_instrumentation") as add: |
| 1738 | + _check_instrumentation_span(azure_sdk_span.span_instance) |
| 1739 | + add.assert_called_once_with(_AZURE_SDK_OPENTELEMETRY_NAME) |
| 1740 | + |
| 1741 | + with mock.patch("azure.monitor.opentelemetry.exporter._utils.add_instrumentation") as add: |
| 1742 | + azure_sdk_span.add_attribute(_AZURE_SDK_NAMESPACE_NAME, "Microsoft.ServiceBus") |
| 1743 | + _check_instrumentation_span(azure_sdk_span.span_instance) |
| 1744 | + add.assert_called_once_with(_AZURE_SDK_OPENTELEMETRY_NAME) |
| 1745 | + |
| 1746 | + with mock.patch("azure.monitor.opentelemetry.exporter._utils.add_instrumentation") as add: |
| 1747 | + azure_sdk_span.add_attribute(_AZURE_SDK_NAMESPACE_NAME, "Microsoft.CognitiveServices") |
| 1748 | + _check_instrumentation_span(azure_sdk_span.span_instance) |
| 1749 | + add.assert_called_once_with(_AZURE_AI_SDK_NAME) |
| 1750 | + |
| 1751 | + finally: |
| 1752 | + settings.tracing_implementation = None |
| 1753 | + |
| 1754 | + @mock.patch("opentelemetry.trace.get_tracer_provider") |
| 1755 | + def test_check_instrumentation_span_azure_sdk_get_tracer(self, mock_get_tracer_provider): |
| 1756 | + mock_get_tracer_provider.return_value = self.get_tracer_provider() |
| 1757 | + |
| 1758 | + if not get_azure_sdk_tracer: |
| 1759 | + self.skipTest("azure.core.instrumentation is not available") |
| 1760 | + |
| 1761 | + azure_sdk_tracer = get_azure_sdk_tracer(library_name="azure-foo-bar") |
| 1762 | + azure_sdk_span = azure_sdk_tracer.start_span(name="test") |
| 1763 | + |
| 1764 | + with mock.patch("azure.monitor.opentelemetry.exporter._utils.add_instrumentation") as add: |
| 1765 | + _check_instrumentation_span(azure_sdk_span) |
| 1766 | + add.assert_called_once_with(_AZURE_SDK_OPENTELEMETRY_NAME) |
| 1767 | + |
| 1768 | + with mock.patch("azure.monitor.opentelemetry.exporter._utils.add_instrumentation") as add: |
| 1769 | + azure_sdk_span.set_attribute(_AZURE_SDK_NAMESPACE_NAME, "Microsoft.ServiceBus") |
| 1770 | + _check_instrumentation_span(azure_sdk_span) |
| 1771 | + add.assert_called_once_with(_AZURE_SDK_OPENTELEMETRY_NAME) |
| 1772 | + |
| 1773 | + with mock.patch("azure.monitor.opentelemetry.exporter._utils.add_instrumentation") as add: |
| 1774 | + azure_sdk_span.set_attribute(_AZURE_SDK_NAMESPACE_NAME, "Microsoft.CognitiveServices") |
| 1775 | + _check_instrumentation_span(azure_sdk_span) |
| 1776 | + add.assert_called_once_with(_AZURE_AI_SDK_NAME) |
| 1777 | + |
| 1778 | + not_azure_sdk_span = get_azure_sdk_tracer(library_name="not-azure-foo-bar").start_span(name="test") |
| 1779 | + with mock.patch("azure.monitor.opentelemetry.exporter._utils.add_instrumentation") as add: |
| 1780 | + _check_instrumentation_span(not_azure_sdk_span) |
| 1781 | + add.assert_not_called() |
| 1782 | + |
| 1783 | + def get_tracer_provider(self): |
| 1784 | + tracer_provider = TracerProvider() |
| 1785 | + span_exporter = InMemorySpanExporter() |
| 1786 | + processor = SimpleSpanProcessor(span_exporter) |
| 1787 | + tracer_provider.add_span_processor(processor) |
| 1788 | + return tracer_provider |
0 commit comments