Skip to content

Commit

Permalink
Add from_connection_string method to exporter (#16818)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Feb 22, 2021
1 parent 2e8ff8c commit 890de77
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 23 deletions.
3 changes: 3 additions & 0 deletions sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 12 additions & 5 deletions sdk/monitor/azure-monitor-opentelemetry-exporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 "]
)

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand Down

0 comments on commit 890de77

Please sign in to comment.