Skip to content

Commit 890de77

Browse files
authored
Add from_connection_string method to exporter (Azure#16818)
1 parent 2e8ff8c commit 890de77

File tree

11 files changed

+57
-23
lines changed

11 files changed

+57
-23
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## 1.0.0b4 (Unreleased)
44

5+
**Features**
6+
- Add `from_connection_string` method to instantiate exporters
7+
([#16818](https://github.com/Azure/azure-sdk-for-python/pull/16818))
58

69
## 1.0.0b3 (2021-02-11)
710

sdk/monitor/azure-monitor-opentelemetry-exporter/README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@ Please find the samples linked below for demonstration as to how to authenticate
3232

3333
```Python
3434
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
35-
exporter = AzureMonitorTraceExporter(
36-
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "]
35+
exporter = AzureMonitorTraceExporter.from_connection_string(
36+
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
3737
)
3838
```
3939

40+
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.
41+
42+
```python
43+
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
44+
exporter = AzureMonitorTraceExporter()
45+
```
46+
4047
## Key concepts
4148

4249
Some of the key concepts for the Azure monitor exporter include:
@@ -73,7 +80,7 @@ from opentelemetry.sdk.trace import TracerProvider
7380
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
7481
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
7582

76-
exporter = AzureMonitorTraceExporter(
83+
exporter = AzureMonitorTraceExporter.from_connection_string(
7784
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "]
7885
)
7986

@@ -110,8 +117,8 @@ tracer = trace.get_tracer(__name__)
110117
# This line causes your calls made with the requests library to be tracked.
111118
RequestsInstrumentor().instrument()
112119
span_processor = BatchExportSpanProcessor(
113-
AzureMonitorTraceExporter(
114-
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "]
120+
AzureMonitorTraceExporter.from_connection_string(
121+
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "]
115122
)
116123
)
117124
trace.get_tracer_provider().add_span_processor(span_processor)

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/_base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class BaseExporter:
3535
def __init__(self, **kwargs: Any) -> None:
3636
"""Azure Monitor base exporter for OpenTelemetry.
3737
38-
:keyword str connection_string: The connection string to be used for authentication.
3938
:keyword str api_version: The service API version used. Defaults to latest.
4039
:rtype: None
4140
"""

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/trace/_exporter.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@
2828

2929

3030
class AzureMonitorTraceExporter(BaseExporter, SpanExporter):
31-
"""Azure Monitor base exporter for OpenTelemetry.
32-
33-
:param options: Exporter configuration options.
34-
:type options: ~azure.monitor.opentelemetry.exporter.options.ExporterOptions
35-
"""
31+
"""Azure Monitor base exporter for OpenTelemetry."""
3632

3733
def export(self, spans: Sequence[Span], **kwargs: Any) -> SpanExportResult: # pylint: disable=unused-argument
3834
"""Export data
@@ -68,6 +64,20 @@ def _span_to_envelope(self, span: Span) -> TelemetryItem:
6864
envelope.instrumentation_key = self._instrumentation_key
6965
return envelope
7066

67+
@classmethod
68+
def from_connection_string(cls, conn_str: str, **kwargs: Any) -> "AzureMonitorTraceExporter":
69+
"""
70+
Create an AzureMonitorTraceExporter from a connection string.
71+
72+
This is the recommended way of instantation if a connection string is passed in explicitly.
73+
If a user wants to use a connection string provided by environment variable, the constructor
74+
of the exporter can be called directly.
75+
76+
:param str conn_str: The connection string to be used for authentication.
77+
:keyword str api_version: The service API version used. Defaults to latest.
78+
:returns an instance of ~AzureMonitorTraceExporter
79+
"""
80+
return cls(connection_string=conn_str, **kwargs)
7181

7282
# pylint: disable=too-many-statements
7383
# pylint: disable=too-many-branches

sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
tracer = trace.get_tracer(__name__)
1919
RequestsInstrumentor().instrument()
2020
span_processor = BatchExportSpanProcessor(
21-
AzureMonitorTraceExporter(
22-
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
21+
AzureMonitorTraceExporter.from_connection_string(
22+
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
2323
)
2424
)
2525
trace.get_tracer_provider().add_span_processor(span_processor)

sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
trace.set_tracer_provider(TracerProvider())
2525
tracer = trace.get_tracer(__name__)
2626

27-
exporter = AzureMonitorTraceExporter(
28-
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
27+
exporter = AzureMonitorTraceExporter.from_connection_string(
28+
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
2929
)
3030

3131
# SpanExporter receives the spans and send them to the target location.

sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_receive.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
# azure monitor trace exporter to send telemetry to appinsights
2929
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
3030
span_processor = BatchExportSpanProcessor(
31-
AzureMonitorTraceExporter(
32-
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
31+
AzureMonitorTraceExporter.from_connection_string(
32+
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
3333
)
3434
)
3535
trace.get_tracer_provider().add_span_processor(span_processor)

sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_send.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
# azure monitor trace exporter to send telemetry to appinsights
2929
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
3030
span_processor = BatchExportSpanProcessor(
31-
AzureMonitorTraceExporter(
32-
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
31+
AzureMonitorTraceExporter.from_connection_string(
32+
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
3333
)
3434
)
3535
trace.get_tracer_provider().add_span_processor(span_processor)

sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
# azure monitor trace exporter to send telemetry to appinsights
2727
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
2828
span_processor = BatchExportSpanProcessor(
29-
AzureMonitorTraceExporter(
30-
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
29+
AzureMonitorTraceExporter.from_connection_string(
30+
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
3131
)
3232
)
3333
trace.get_tracer_provider().add_span_processor(span_processor)

sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_trace.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
1313

1414

15-
exporter = AzureMonitorTraceExporter(
16-
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
15+
exporter = AzureMonitorTraceExporter.from_connection_string(
16+
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
1717
)
1818

19+
# You can also instantiate the exporter via the constructor
20+
# The connection string will be automatically populated via the
21+
# APPLICATIONINSIGHTS_CONNECTION_STRING environment variable
22+
# exporter = AzureMonitorTraceExporter()
23+
1924
trace.set_tracer_provider(TracerProvider())
2025
tracer = trace.get_tracer(__name__)
2126
span_processor = BatchExportSpanProcessor(exporter)

0 commit comments

Comments
 (0)