diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md index acc1d286dc90..a994d947c39c 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md @@ -2,6 +2,9 @@ ## 1.0.0b4 (Unreleased) + **Features** + - Add `from_connection_string` method to instantiate exporters + ([#16818](https://github.com/Azure/azure-sdk-for-python/pull/16818)) ## 1.0.0b3 (2021-02-11) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md index 16188cfdaa58..0cd7638bd67d 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/README.md @@ -32,11 +32,18 @@ Please find the samples linked below for demonstration as to how to authenticate ```Python from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter -exporter = AzureMonitorTraceExporter( - connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "] +exporter = AzureMonitorTraceExporter.from_connection_string( + connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] ) ``` +You can also instantiate the exporter directly via the constructor. In this case, the connection string will be automatically populated from the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable. + +```python +from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter +exporter = AzureMonitorTraceExporter() +``` + ## Key concepts Some of the key concepts for the Azure monitor exporter include: @@ -73,7 +80,7 @@ from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchExportSpanProcessor from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter -exporter = AzureMonitorTraceExporter( +exporter = AzureMonitorTraceExporter.from_connection_string( connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "] ) @@ -110,8 +117,8 @@ tracer = trace.get_tracer(__name__) # This line causes your calls made with the requests library to be tracked. RequestsInstrumentor().instrument() span_processor = BatchExportSpanProcessor( - AzureMonitorTraceExporter( - connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "] + AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "] ) ) trace.get_tracer_provider().add_span_processor(span_processor) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/_base.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/_base.py index b6d20f14b316..ac8661d63fcc 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/_base.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/_base.py @@ -35,7 +35,6 @@ class BaseExporter: def __init__(self, **kwargs: Any) -> None: """Azure Monitor base exporter for OpenTelemetry. - :keyword str connection_string: The connection string to be used for authentication. :keyword str api_version: The service API version used. Defaults to latest. :rtype: None """ diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/trace/_exporter.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/trace/_exporter.py index 17ed0ba1d679..5860610de047 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/trace/_exporter.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/trace/_exporter.py @@ -28,11 +28,7 @@ class AzureMonitorTraceExporter(BaseExporter, SpanExporter): - """Azure Monitor base exporter for OpenTelemetry. - - :param options: Exporter configuration options. - :type options: ~azure.monitor.opentelemetry.exporter.options.ExporterOptions - """ + """Azure Monitor base exporter for OpenTelemetry.""" def export(self, spans: Sequence[Span], **kwargs: Any) -> SpanExportResult: # pylint: disable=unused-argument """Export data @@ -68,6 +64,20 @@ def _span_to_envelope(self, span: Span) -> TelemetryItem: envelope.instrumentation_key = self._instrumentation_key return envelope + @classmethod + def from_connection_string(cls, conn_str: str, **kwargs: Any) -> "AzureMonitorTraceExporter": + """ + Create an AzureMonitorTraceExporter from a connection string. + + This is the recommended way of instantation if a connection string is passed in explicitly. + If a user wants to use a connection string provided by environment variable, the constructor + of the exporter can be called directly. + + :param str conn_str: The connection string to be used for authentication. + :keyword str api_version: The service API version used. Defaults to latest. + :returns an instance of ~AzureMonitorTraceExporter + """ + return cls(connection_string=conn_str, **kwargs) # pylint: disable=too-many-statements # pylint: disable=too-many-branches diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_client.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_client.py index cf91536b5203..8cc0f160e336 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_client.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_client.py @@ -18,8 +18,8 @@ tracer = trace.get_tracer(__name__) RequestsInstrumentor().instrument() span_processor = BatchExportSpanProcessor( - AzureMonitorTraceExporter( - connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] + AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] ) ) trace.get_tracer_provider().add_span_processor(span_processor) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_server.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_server.py index 9881732e09fd..35c0f1450100 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_server.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_server.py @@ -24,8 +24,8 @@ trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) -exporter = AzureMonitorTraceExporter( - connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] +exporter = AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] ) # SpanExporter receives the spans and send them to the target location. diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_receive.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_receive.py index 1c93e4883c37..a0cf9083a585 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_receive.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_receive.py @@ -28,8 +28,8 @@ # azure monitor trace exporter to send telemetry to appinsights from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter span_processor = BatchExportSpanProcessor( - AzureMonitorTraceExporter( - connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] + AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] ) ) trace.get_tracer_provider().add_span_processor(span_processor) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_send.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_send.py index fd79a61a63cf..71156d47f92b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_send.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_send.py @@ -28,8 +28,8 @@ # azure monitor trace exporter to send telemetry to appinsights from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter span_processor = BatchExportSpanProcessor( - AzureMonitorTraceExporter( - connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] + AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] ) ) trace.get_tracer_provider().add_span_processor(span_processor) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_storage.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_storage.py index 5e0b7410c53f..d46a59ce961e 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_storage.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_storage.py @@ -26,8 +26,8 @@ # azure monitor trace exporter to send telemetry to appinsights from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter span_processor = BatchExportSpanProcessor( - AzureMonitorTraceExporter( - connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] + AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] ) ) trace.get_tracer_provider().add_span_processor(span_processor) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_trace.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_trace.py index d7ac8f63b39f..cca1fbfd179d 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_trace.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_trace.py @@ -12,10 +12,15 @@ from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter -exporter = AzureMonitorTraceExporter( - connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] +exporter = AzureMonitorTraceExporter.from_connection_string( + os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] ) +# You can also instantiate the exporter via the constructor +# The connection string will be automatically populated via the +# APPLICATIONINSIGHTS_CONNECTION_STRING environment variable +# exporter = AzureMonitorTraceExporter() + trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) span_processor = BatchExportSpanProcessor(exporter) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/trace/test_trace.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/trace/test_trace.py index 7fb0c9cfd6f1..d3013da4a3f7 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/trace/test_trace.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/trace/test_trace.py @@ -53,6 +53,16 @@ def test_constructor(self): "4321abcd-5678-4efa-8abc-1234567890ab", ) + def test_from_connection_string(self): + exporter = AzureMonitorTraceExporter.from_connection_string( + "InstrumentationKey=4321abcd-5678-4efa-8abc-1234567890ab" + ) + self.assertTrue(isinstance(exporter, AzureMonitorTraceExporter)) + self.assertEqual( + exporter._instrumentation_key, + "4321abcd-5678-4efa-8abc-1234567890ab", + ) + def test_export_empty(self): exporter = self._exporter result = exporter.export([])