File: README.md

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (498 lines) | stat: -rw-r--r-- 19,323 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
---
page_type: sample
languages:
  - python
products:
  - azure-monitor
---

<!-- cSpell:disable -->
# Microsoft Azure Monitor Opentelemetry Exporter Trace Python Samples

These code samples show common champion scenario operations with the AzureMonitorTraceExporter.

* Span Event: [sample_span_event.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_span_event.py)
* Trace: [sample_trace.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_trace.py)

* Collector: [sample_collector.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/collector/sample_collector.py)
* Jaeger: [sample_jaeger.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_jaeger.py)

* Django instrumentation: [manage.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/django/sample/manage.py)
* Flask instrumentation: [sample_flask.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_flask.py)
* Requests instrumentation: [sample_requests.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_requests.py)

* Psycopg2 instrumentation: [sample_psycopg2.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_psycopg2.py)

* Emitting metrics with instrumentations: [sample_metrics.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_metrics.py)

* Azure AppConfig Add Config Setting: [sample_app_config.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_app_config.py)
* Azure Communication Chat Create Client/Thread: [sample_comm_chat.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_chat.py)
* Azure Communication Phone Numbers List Purchased Numbers: [sample_comm_phone.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_phone.py)
* Azure Communication SMS Send Message: [sample_comm_sms.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_comm_sms.py)
* Azure CosmosDb Create Db/Container: [sample_cosmos.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_cosmos.py)
* Azure EventHub Send EventData: [sample_event_hub.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_event_hub.py)
* Azure EventHub Blob Storage Checkpoint Store: [sample_blob_checkpoint.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_blob_checkpoint.py)
* Azure EventGrid Send Event: [sample_event_grid.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_event_grid.py)
* Azure Form Recognizer Analyze Document: [sample_form_recognizer.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_form_recognizer.py)
* Azure KeyVault Create Certificate: [sample_key_cert.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_key_cert.py)
* Azure KeyVault Set Secret: [sample_key_secret.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_key_secret.py)
* Azure KeyVault Create Keys: [sample_key_keys.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_key_keys.py)
* Azure Service Bus Send: [sample_servicebus_send.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_send.py)
* Azure Service Bus Receive: [sample_servicebus_receive.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_servicebus_receive.py)
* Azure Storage Blob Create Container: [sample_storage_blob.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_storage_blob.py)
* Azure Azure Text Analytics Extract Key Phrases: [sample_text_analytics.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/traces/sample_text_analytics.py)

## Installation

```sh
$ pip install azure-monitor-opentelemetry-exporter --pre
```

## Run the Applications

### Trace

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # from this directory
$ python sample_trace.py
```

### Span Event

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # from this directory
$ python sample_span_event.py
```

### Django instrumentation

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ pip install opentelemetry-instrumentation-django
$ # from the django directory
$ python manage.py runserver
```

* Open http://localhost:8000/

### Flask instrumentation

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ pip install opentelemetry-instrumentation-flask
$ # from this directory
$ python sample_flask.py
```

* Open http://localhost:8080/

### Requests instrumentation

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ pip install opentelemetry-instrumentation-requests
$ # from this directory
$ python sample_requests.py
```

### Psycopg2 instrumentation

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Ensure you have a database named `test` with a table named `test_table`, make sure `test_table` has a field called `test_field`
* Supply your `user` and `password` for your database if needed
* Run the sample

```sh
$ pip install opentelemetry-instrumentation-psycopg2
$ # from this directory
$ python sample_psycopg2.py
```

### Emitting metrics with instrumentations

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ pip install opentelemetry-instrumentation-flask
$ pip install opentelemetry-instrumentation-requests
$ # from this directory
$ python sample_metrics.py
```

Calls made with the underlying instrumented libraries will track metrics information in the metrics explorer view in Application Insights. Only certain instrumentations support metrics collection, refer to https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/README.md for the full list. This also assumes you are using the latest versions of the instrumentation libraries.

### Collector

* Start the Collector locally to see how the Collector works in practice.

* From the same folder as collector/otel-collector-config.yaml and collector/docker-compose.yml, start the Docker container.

```sh
$ docker-compose up
```

* Install the OpenTelemetry OTLP Exporter

```sh
$ pip install opentelemetry-exporter-otlp
```

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
# from collector directory
$ python sample_collector.py
```

* You should be able to see your traces in the Zipkin backend as well as Azure Monitor Application Insights backend.

### Jaeger

* The Jaeger project provides an all-in-one Docker container with a UI, database, and consumer. Run the following command to start Jaeger:

```sh
$ docker run -p 16686:16686 -p 6831:6831/udp jaegertracing/all-in-one
```

* This command starts Jaeger locally on port 16686 and exposes the Jaeger thrift agent on port 6831. You can visit Jaeger at http://localhost:16686.

* Install the OpenTelemetry Jaeger Exporter

```sh
$ pip install opentelemetry-exporter-jaeger
```

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # from this directory
$ python sample_jaeger.py
```

* You should be able to see your traces in the Jaeger backend as well as Azure Monitor Application Insights backend.

### Azure AppConfig Add Config Setting

The following sample assumes that you have setup an Azure App Configuration [Store](https://learn.microsoft.com/azure/azure-app-configuration/quickstart-python).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # azure-appconfiguration library
$ pip install azure-appconfiguration
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_app_config.py
```

### Azure Communication Chat Create Client/Thread

The following sample assumes that you have setup an Azure Communication Services [resource](https://learn.microsoft.com/azure/communication-services/quickstarts/create-communication-resource).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # azure-communication-chat library
$ pip install azure-communication-chat
$ # azure-communication-identity library for authentication
$ pip install azure-communication-identity
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_comm_chat.py
```

### Azure Communication Phone Numbers List Purchased Numbers

The following sample assumes that you have setup an Azure Communication Services [resource](https://learn.microsoft.com/azure/communication-services/quickstarts/create-communication-resource).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # azure-communication-phonenumbers library
$ pip install azure-communication-phonenumbers
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_comm_phone.py
```

### Azure Communication SMS Send Message

The following sample assumes that you have setup an Azure Communication Services [resource](https://learn.microsoft.com/azure/communication-services/quickstarts/create-communication-resource).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # azure-communication-sms library
$ pip install azure-communication-sms
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_comm_sms.py
```

### Azure CosmosDb Create Db/Container

The following sample assumes that you have setup Azure CosmosDb [account](https://learn.microsoft.com/azure/cosmos-db/sql/create-cosmosdb-resources-portal).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
* Update `ACCOUNT_URI` environment variable
* Update `ACCOUNT_KEY` environment variable

* Run the sample

```sh
$ # azure-cosmos library
$ pip install azure-cosmos
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_cosmos.py
```

### Azure EventHub Send EventData

The following sample assumes that you have setup an Azure EventHubs namespace and [EventHub](https://learn.microsoft.com/azure/event-hubs/event-hubs-create).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
* Update `EVENT_HUB_CONN_STR` environment variable
* Update `EVENT_HUB_NAME` environment variable

* Run the sample

```sh
$ # azure-eventhub library
$ pip install azure-eventhub
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_event_hub.py
```

### Azure EventHub Blob Storage Checkpoint Store

The following sample assumes that you have setup an Azure EventHubs namespace, [EventHub](https://learn.microsoft.com/azure/event-hubs/event-hubs-create) and Azure Blob [storage](https://learn.microsoft.com/azure/storage/blobs/storage-quickstart-blobs-portal).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
* Update `EVENT_HUB_CONN_STR` environment variable
* Update `EVENT_HUB_NAME` environment variable
* Update `AZURE_STORAGE_CONN_STR` environment variable

* Run the sample

```sh
$ # azure-eventhub-checkpointstoreblob library
$ pip install azure-eventhub-checkpointstoreblob
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_blob_checkpoint.py
```

### Azure EventGrid Send Event

The following sample assumes that you have setup an Azure Event Grid [Topic](https://learn.microsoft.com/azure/event-grid/custom-event-quickstart-portal).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
* Update `EG_ACCESS_KEY` environment variable
* Update `EG_TOPIC_HOSTNAME` environment variable

* Run the sample

```sh
$ # azure-azure-eventgrid library
$ pip install azure-eventgrid
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_event_grid.py
```

### Azure Form Recognizer Analyze Document

The following sample assumes that you have setup an Azure Form Recognizer [Resource](https://learn.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # azure-ai-formrecognizer library
$ pip install azure-ai-formrecognizer
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_form_recognizer.py
```

### Azure KeyVault Create Certificate

The following sample assumes that you have setup an Azure Key Vault [resource](https://learn.microsoft.com/azure/key-vault/general/quick-create-portal) and a service [principal](https://learn.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal) for authentication.

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # azure-keyvault-certificates library
$ pip install azure-keyvault-certificates
$ # azure-identity library for authentication
$ pip install azure-identity
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_key_cert.py
```

### Azure KeyVault Set Secret

The following sample assumes that you have setup an Azure Key Vault [resource](https://learn.microsoft.com/azure/key-vault/general/quick-create-portal) and a service [principal](https://learn.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal) for authentication.

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # azure-keyvault-secrets library
$ pip install azure-keyvault-secrets
$ # azure-identity library for authentication
$ pip install azure-identity
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_key_secret.py
```

### Azure KeyVault Create Keys

The following sample assumes that you have setup an Azure Key Vault [resource](https://learn.microsoft.com/azure/key-vault/general/quick-create-portal) and a service [principal](https://learn.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal) for authentication.

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # azure-keyvault-keys library
$ pip install azure-keyvault-keys
$ # azure-identity library for authentication
$ pip install azure-identity
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_key_keys.py
```

### Azure Service Bus Send

The following sample assumes that you have setup an Azure Service Bus [namespace](https://learn.microsoft.com/azure/service-bus-messaging/service-bus-quickstart-portal).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
* Update `SERVICE_BUS_CONN_STR` environment variable
* Update `SERVICE_BUS_QUEUE_NAME` environment variable

* Run the sample

```sh
$ # azure-servicebus library
$ pip install azure-servicebus
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_servicebus_send.py
```

### Azure Service Bus Receive

The following sample assumes that you have setup an Azure Service Bus [namespace](https://learn.microsoft.com/azure/service-bus-messaging/service-bus-quickstart-portal).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
* Update `SERVICE_BUS_CONN_STR` environment variable
* Update `SERVICE_BUS_QUEUE_NAME` environment variable

* Run the sample

```sh
$ # azure-servicebus library
$ pip install azure-servicebus
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_servicebus_receive.py
```

### Azure Storage Blob Create Container

The following sample assumes that you have setup Azure Blob [storage](https://learn.microsoft.com/azure/storage/blobs/storage-quickstart-blobs-portal).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
* Update `AZURE_STORAGE_CONNECTION_STRING` environment variable
* Update `AZURE_STORAGE_BLOB_CONTAINER_NAME` environment variable

* Run the sample

```sh
$ # azure-storage-blob library
$ pip install azure-storage-blob
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_storage_blob.py
```

### Azure Text Analytics Extract Key Phrases

The following sample assumes that you have setup an Azure Cognitive Services [Resource](https://learn.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account).

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # azure-ai-textanalytics library
$ pip install azure-ai-textanalytics
$ # azure sdk core tracing library for opentelemetry
$ pip install azure-core-tracing-opentelemetry
$ # from this directory
$ python sample_text_analytics.py
```

## Explore the data

After running the applications, data would be available in [Azure](
https://learn.microsoft.com/azure/azure-monitor/app/app-insights-overview#where-do-i-see-my-telemetry)

<!-- cSpell:enable -->