Skip to content

Commit 96b677b

Browse files
authored
Update azure-ad-authentication.md for Python sdk
1 parent 182b36f commit 96b677b

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

articles/azure-monitor/app/azure-ad-authentication.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,65 @@ After setting it, restart your application. It now sends telemetry to Applicatio
233233

234234
### [Python](#tab/python)
235235

236-
To configure a secure connection to Azure using OpenTelemetry, see [Enable Microsoft Entra ID (formerly Azure AD) authentication](./opentelemetry-configuration.md?tabs=python#enable-microsoft-entra-id-formerly-azure-ad-authentication).
236+
Azure Monitor OpenTelemetry Distro and Azure monitor OpenTelemery exporters for Python support the credential classes provided by [Azure Identity](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/identity/identity#credential-classes).
237+
238+
- We recommend `DefaultAzureCredential` for local development.
239+
- We recommend `ManagedIdentityCredential` for system-assigned and user-assigned managed identities.
240+
- For system-assigned, use the default constructor without parameters.
241+
- For user-assigned, provide the client ID to the constructor.
242+
- We recommend `ClientSecretCredential` for service principals.
243+
- Provide the tenant ID, client ID, and client secret to the constructor.
244+
245+
If using `azure-monitor-opentelemetry`
246+
```python
247+
import os
248+
# You will need to install azure-identity
249+
from azure.identity import ManagedIdentityCredential
250+
from azure.monitor.opentelemetry import configure_azure_monitor
251+
from opentelemetry import trace
252+
253+
configure_azure_monitor(
254+
connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"],
255+
credential=credential,
256+
)
257+
258+
tracer = trace.get_tracer(__name__)
259+
260+
with tracer.start_as_current_span("hello"):
261+
print("Hello, World!")
262+
263+
```
264+
265+
If using `azure-monitor-opentelemetry-exporter`
266+
```python
267+
import os
268+
# You will need to install azure-identity
269+
from azure.identity import ClientSecretCredential
270+
from opentelemetry import trace
271+
from opentelemetry.sdk.trace import TracerProvider
272+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
273+
274+
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
275+
276+
credential = ClientSecretCredential(
277+
tenant_id="<tenant_id",
278+
client_id="<client_id>",
279+
client_secret="<client_secret>",
280+
)
281+
exporter = AzureMonitorTraceExporter.from_connection_string(
282+
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"],
283+
credential=credential
284+
)
285+
286+
trace.set_tracer_provider(TracerProvider())
287+
tracer = trace.get_tracer(__name__)
288+
span_processor = BatchSpanProcessor(exporter)
289+
trace.get_tracer_provider().add_span_processor(span_processor)
290+
291+
with tracer.start_as_current_span("hello with aad managed identity"):
292+
print("Hello, World!")
293+
294+
```
237295

238296
To configure using OpenCensus (deprecated), see [Configure and enable Microsoft Entra ID-based authentication](/previous-versions/azure/azure-monitor/app/opencensus-python#configure-and-enable-microsoft-entra-id-based-authentication).
239297

0 commit comments

Comments
 (0)