@@ -25,14 +25,19 @@ Use one of the following three ways to configure the connection string:
25
25
26
26
- Add ` UseAzureMonitor() ` to your application startup. Depending on your version of .NET, it is in either your ` startup.cs ` or ` program.cs ` class.
27
27
``` csharp
28
+ // Create a new ASP.NET Core web application builder.
28
29
var builder = WebApplication .CreateBuilder (args );
29
30
31
+ // Add the OpenTelemetry telemetry service to the application.
32
+ // This service will collect and send telemetry data to Azure Monitor.
30
33
builder .Services .AddOpenTelemetry ().UseAzureMonitor (options => {
31
34
options .ConnectionString = " <Your Connection String>" ;
32
35
});
33
36
37
+ // Build the ASP.NET Core web application.
34
38
var app = builder .Build ();
35
39
40
+ // Start the ASP.NET Core web application.
36
41
app .Run ();
37
42
```
38
43
- Set an environment variable :
@@ -60,18 +65,24 @@ Use one of the following two ways to configure the connection string:
60
65
61
66
- Add the Azure Monitor Exporter to each OpenTelemetry signal in application startup .
62
67
```csharp
68
+ // Create a new OpenTelemetry tracer provider.
69
+ // It is important to keep the TracerProvider instance active throughout the process lifetime.
63
70
var tracerProvider = Sdk .CreateTracerProviderBuilder ()
64
71
.AddAzureMonitorTraceExporter (options =>
65
72
{
66
73
options .ConnectionString = " <Your Connection String>" ;
67
74
});
68
75
76
+ // Create a new OpenTelemetry meter provider.
77
+ // It is important to keep the MetricsProvider instance active throughout the process lifetime.
69
78
var metricsProvider = Sdk .CreateMeterProviderBuilder ()
70
79
.AddAzureMonitorMetricExporter (options =>
71
80
{
72
81
options .ConnectionString = " <Your Connection String>" ;
73
82
});
74
83
84
+ // Create a new logger factory.
85
+ // It is important to keep the LoggerFactory instance active throughout the process lifetime.
75
86
var loggerFactory = LoggerFactory .Create (builder =>
76
87
{
77
88
builder .AddOpenTelemetry (options =>
@@ -152,20 +163,29 @@ Set the Cloud Role Name and the Cloud Role Instance via [Resource](https://githu
152
163
153
164
```csharp
154
165
// Setting role name and role instance
166
+
167
+ // Create a dictionary of resource attributes.
155
168
var resourceAttributes = new Dictionary <string , object > {
156
169
{ " service.name" , " my-service" },
157
170
{ " service.namespace" , " my-namespace" },
158
171
{ " service.instance.id" , " my-instance" }};
159
172
173
+ // Create a new ASP.NET Core web application builder.
160
174
var builder = WebApplication .CreateBuilder (args );
161
175
176
+ // Add the OpenTelemetry telemetry service to the application.
177
+ // This service will collect and send telemetry data to Azure Monitor.
162
178
builder .Services .AddOpenTelemetry ().UseAzureMonitor ();
179
+
180
+ // Configure the OpenTelemetry tracer provider to add the resource attributes to all traces.
163
181
builder .Services .ConfigureOpenTelemetryTracerProvider ((sp , builder ) =>
164
182
builder .ConfigureResource (resourceBuilder =>
165
183
resourceBuilder .AddAttributes (resourceAttributes )));
166
184
185
+ // Build the ASP.NET Core web application.
167
186
var app = builder .Build ();
168
187
188
+ // Start the ASP.NET Core web application.
169
189
app .Run ();
170
190
```
171
191
@@ -175,22 +195,32 @@ Set the Cloud Role Name and the Cloud Role Instance via [Resource](https://githu
175
195
176
196
```csharp
177
197
// Setting role name and role instance
198
+
199
+ // Create a dictionary of resource attributes.
178
200
var resourceAttributes = new Dictionary <string , object > {
179
201
{ " service.name" , " my-service" },
180
202
{ " service.namespace" , " my-namespace" },
181
203
{ " service.instance.id" , " my-instance" }};
204
+
205
+ // Create a resource builder.
182
206
var resourceBuilder = ResourceBuilder .CreateDefault ().AddAttributes (resourceAttributes );
183
207
208
+ // Create a new OpenTelemetry tracer provider and set the resource builder.
209
+ // It is important to keep the TracerProvider instance active throughout the process lifetime.
184
210
var tracerProvider = Sdk .CreateTracerProviderBuilder ()
185
211
// Set ResourceBuilder on the TracerProvider.
186
212
.SetResourceBuilder (resourceBuilder )
187
213
.AddAzureMonitorTraceExporter ();
188
214
215
+ // Create a new OpenTelemetry meter provider and set the resource builder.
216
+ // It is important to keep the MetricsProvider instance active throughout the process lifetime.
189
217
var metricsProvider = Sdk .CreateMeterProviderBuilder ()
190
218
// Set ResourceBuilder on the MeterProvider.
191
219
.SetResourceBuilder (resourceBuilder )
192
220
.AddAzureMonitorMetricExporter ();
193
221
222
+ // Create a new logger factory and add the OpenTelemetry logger provider with the resource builder.
223
+ // It is important to keep the LoggerFactory instance active throughout the process lifetime.
194
224
var loggerFactory = LoggerFactory .Create (builder =>
195
225
{
196
226
builder .AddOpenTelemetry (options =>
@@ -261,15 +291,21 @@ You may want to enable sampling to reduce your data ingestion volume, which redu
261
291
The sampler expects a sample rate of between 0 and 1 inclusive . A rate of 0 . 1 means approximately 10 % of your traces are sent .
262
292
263
293
```csharp
294
+ // Create a new ASP.NET Core web application builder.
264
295
var builder = WebApplication .CreateBuilder (args );
265
296
297
+ // Add the OpenTelemetry telemetry service to the application.
298
+ // This service will collect and send telemetry data to Azure Monitor.
266
299
builder .Services .AddOpenTelemetry ().UseAzureMonitor (o =>
267
300
{
301
+ // Set the sampling ratio to 10%. This means that 10% of all traces will be sampled and sent to Azure Monitor.
268
302
o .SamplingRatio = 0 . 1 F ;
269
303
});
270
304
305
+ // Build the ASP.NET Core web application.
271
306
var app = builder .Build ();
272
307
308
+ // Start the ASP.NET Core web application.
273
309
app .Run ();
274
310
```
275
311
@@ -278,9 +314,12 @@ app.Run();
278
314
The sampler expects a sample rate of between 0 and 1 inclusive . A rate of 0 . 1 means approximately 10 % of your traces are sent .
279
315
280
316
```csharp
317
+ // Create a new OpenTelemetry tracer provider.
318
+ // It is important to keep the TracerProvider instance active throughout the process lifetime.
281
319
var tracerProvider = Sdk .CreateTracerProviderBuilder ()
282
320
.AddAzureMonitorTraceExporter (options =>
283
- {
321
+ {
322
+ // Set the sampling ratio to 10%. This means that 10% of all traces will be sampled and sent to Azure Monitor.
284
323
options .SamplingRatio = 0 . 1 F ;
285
324
});
286
325
```
@@ -341,14 +380,23 @@ We support the credential classes provided by [Azure Identity](https://github.co
341
380
342
381
1 . Provide the desired credential class :
343
382
```csharp
383
+ // Create a new ASP.NET Core web application builder.
344
384
var builder = WebApplication .CreateBuilder (args );
345
385
386
+ // Add the OpenTelemetry telemetry service to the application.
387
+ // This service will collect and send telemetry data to Azure Monitor.
346
388
builder .Services .AddOpenTelemetry ().UseAzureMonitor (options => {
389
+ // Set the Azure Monitor credential to the DefaultAzureCredential.
390
+ // This credential will use the Azure identity of the current user or
391
+ // the service principal that the application is running as to authenticate
392
+ // to Azure Monitor.
347
393
options .Credential = new DefaultAzureCredential ();
348
394
});
349
395
396
+ // Build the ASP.NET Core web application.
350
397
var app = builder .Build ();
351
398
399
+ // Start the ASP.NET Core web application.
352
400
app .Run ();
353
401
```
354
402
@@ -370,20 +418,27 @@ We support the credential classes provided by [Azure Identity](https://github.co
370
418
371
419
1 . Provide the desired credential class :
372
420
```csharp
421
+ // Create a DefaultAzureCredential.
373
422
var credential = new DefaultAzureCredential ();
374
423
424
+ // Create a new OpenTelemetry tracer provider and set the credential.
425
+ // It is important to keep the TracerProvider instance active throughout the process lifetime.
375
426
var tracerProvider = Sdk .CreateTracerProviderBuilder ()
376
427
.AddAzureMonitorTraceExporter (options =>
377
428
{
378
429
options .Credential = credential ;
379
430
});
380
431
432
+ // Create a new OpenTelemetry meter provider and set the credential.
433
+ // It is important to keep the MetricsProvider instance active throughout the process lifetime.
381
434
var metricsProvider = Sdk .CreateMeterProviderBuilder ()
382
435
.AddAzureMonitorMetricExporter (options =>
383
436
{
384
437
options .Credential = credential ;
385
438
});
386
439
440
+ // Create a new logger factory and add the OpenTelemetry logger provider with the credential.
441
+ // It is important to keep the LoggerFactory instance active throughout the process lifetime.
387
442
var loggerFactory = LoggerFactory .Create (builder =>
388
443
{
389
444
builder .AddOpenTelemetry (options =>
@@ -447,15 +502,22 @@ The Distro package includes the AzureMonitorExporter, which by default uses one
447
502
To override the default directory , you should set `AzureMonitorOptions .StorageDirectory `.
448
503
449
504
```csharp
505
+ // Create a new ASP.NET Core web application builder.
450
506
var builder = WebApplication .CreateBuilder (args );
451
507
508
+ // Add the OpenTelemetry telemetry service to the application.
509
+ // This service will collect and send telemetry data to Azure Monitor.
452
510
builder .Services .AddOpenTelemetry ().UseAzureMonitor (options =>
453
511
{
512
+ // Set the Azure Monitor storage directory to "C:\\SomeDirectory".
513
+ // This is the directory where the OpenTelemetry SDK will store any telemetry data that cannot be sent to Azure Monitor immediately.
454
514
options .StorageDirectory = " C:\\ SomeDirectory" ;
455
515
});
456
516
517
+ // Build the ASP.NET Core web application.
457
518
var app = builder .Build ();
458
519
520
+ // Start the ASP.NET Core web application.
459
521
app .Run ();
460
522
```
461
523
@@ -476,24 +538,36 @@ By default, the AzureMonitorExporter uses one of the following locations for off
476
538
To override the default directory , you should set `AzureMonitorExporterOptions .StorageDirectory `.
477
539
478
540
```csharp
541
+ // Create a new OpenTelemetry tracer provider and set the storage directory.
542
+ // It is important to keep the TracerProvider instance active throughout the process lifetime.
479
543
var tracerProvider = Sdk .CreateTracerProviderBuilder ()
480
544
.AddAzureMonitorTraceExporter (options =>
481
545
{
546
+ // Set the Azure Monitor storage directory to "C:\\SomeDirectory".
547
+ // This is the directory where the OpenTelemetry SDK will store any trace data that cannot be sent to Azure Monitor immediately.
482
548
options .StorageDirectory = " C:\\ SomeDirectory" ;
483
549
});
484
550
551
+ // Create a new OpenTelemetry meter provider and set the storage directory.
552
+ // It is important to keep the MetricsProvider instance active throughout the process lifetime.
485
553
var metricsProvider = Sdk .CreateMeterProviderBuilder ()
486
554
.AddAzureMonitorMetricExporter (options =>
487
555
{
556
+ // Set the Azure Monitor storage directory to "C:\\SomeDirectory".
557
+ // This is the directory where the OpenTelemetry SDK will store any metric data that cannot be sent to Azure Monitor immediately.
488
558
options .StorageDirectory = " C:\\ SomeDirectory" ;
489
559
});
490
560
561
+ // Create a new logger factory and add the OpenTelemetry logger provider with the storage directory.
562
+ // It is important to keep the LoggerFactory instance active throughout the process lifetime.
491
563
var loggerFactory = LoggerFactory .Create (builder =>
492
564
{
493
565
builder .AddOpenTelemetry (options =>
494
566
{
495
567
options .AddAzureMonitorLogExporter (options =>
496
568
{
569
+ // Set the Azure Monitor storage directory to "C:\\SomeDirectory".
570
+ // This is the directory where the OpenTelemetry SDK will store any log data that cannot be sent to Azure Monitor immediately.
497
571
options .StorageDirectory = " C:\\ SomeDirectory" ;
498
572
});
499
573
});
@@ -590,14 +664,22 @@ You might want to enable the OpenTelemetry Protocol (OTLP) Exporter alongside th
590
664
1 . Add the following code snippet . This example assumes you have an OpenTelemetry Collector with an OTLP receiver running . For details , see the [example on GitHub ](https :// github.com/open-telemetry/opentelemetry-dotnet/blob/main/examples/Console/TestOtlpExporter.cs).
591
665
592
666
```csharp
667
+ // Create a new ASP.NET Core web application builder.
593
668
var builder = WebApplication .CreateBuilder (args );
594
669
670
+ // Add the OpenTelemetry telemetry service to the application.
671
+ // This service will collect and send telemetry data to Azure Monitor.
595
672
builder .Services .AddOpenTelemetry ().UseAzureMonitor ();
673
+
674
+ // Add the OpenTelemetry OTLP exporter to the application.
675
+ // This exporter will send telemetry data to an OTLP receiver, such as Prometheus
596
676
builder .Services .AddOpenTelemetry ().WithTracing (builder => builder .AddOtlpExporter ());
597
677
builder .Services .AddOpenTelemetry ().WithMetrics (builder => builder .AddOtlpExporter ());
598
678
679
+ // Build the ASP.NET Core web application.
599
680
var app = builder .Build ();
600
681
682
+ // Start the ASP.NET Core web application.
601
683
app .Run ();
602
684
```
603
685
@@ -612,10 +694,14 @@ You might want to enable the OpenTelemetry Protocol (OTLP) Exporter alongside th
612
694
1 . Add the following code snippet . This example assumes you have an OpenTelemetry Collector with an OTLP receiver running . For details , see the [example on GitHub ](https :// github.com/open-telemetry/opentelemetry-dotnet/blob/main/examples/Console/TestOtlpExporter.cs).
613
695
614
696
```csharp
697
+ // Create a new OpenTelemetry tracer provider and add the Azure Monitor trace exporter and the OTLP trace exporter.
698
+ // It is important to keep the TracerProvider instance active throughout the process lifetime.
615
699
var tracerProvider = Sdk .CreateTracerProviderBuilder ()
616
700
.AddAzureMonitorTraceExporter ()
617
701
.AddOtlpExporter ();
618
702
703
+ // Create a new OpenTelemetry meter provider and add the Azure Monitor metric exporter and the OTLP metric exporter.
704
+ // It is important to keep the MetricsProvider instance active throughout the process lifetime.
619
705
var metricsProvider = Sdk .CreateMeterProviderBuilder ()
620
706
.AddAzureMonitorMetricExporter ()
621
707
.AddOtlpExporter ();
@@ -715,4 +801,4 @@ For more information about OpenTelemetry SDK configuration, see the [OpenTelemet
715
801
716
802
-- -
717
803
718
- [! INCLUDE [azure - monitor - app - insights - opentelemetry - support ](.. / includes / azure - monitor - app - insights - opentelemetry - support .md )]
804
+ [! INCLUDE [azure - monitor - app - insights - opentelemetry - support ](.. / includes / azure - monitor - app - insights - opentelemetry - support .md )]
0 commit comments