@@ -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,21 @@ 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.
63
69
var tracerProvider = Sdk .CreateTracerProviderBuilder ()
64
70
.AddAzureMonitorTraceExporter (options =>
65
71
{
66
72
options .ConnectionString = " <Your Connection String>" ;
67
73
});
68
74
75
+ // Create a new OpenTelemetry meter provider.
69
76
var metricsProvider = Sdk .CreateMeterProviderBuilder ()
70
77
.AddAzureMonitorMetricExporter (options =>
71
78
{
72
79
options .ConnectionString = " <Your Connection String>" ;
73
80
});
74
81
82
+ // Create a new logger factory.
75
83
var loggerFactory = LoggerFactory .Create (builder =>
76
84
{
77
85
builder .AddOpenTelemetry (options =>
@@ -152,20 +160,29 @@ Set the Cloud Role Name and the Cloud Role Instance via [Resource](https://githu
152
160
153
161
```csharp
154
162
// Setting role name and role instance
163
+
164
+ // Create a dictionary of resource attributes.
155
165
var resourceAttributes = new Dictionary <string , object > {
156
166
{ " service.name" , " my-service" },
157
167
{ " service.namespace" , " my-namespace" },
158
168
{ " service.instance.id" , " my-instance" }};
159
169
170
+ // Create a new ASP.NET Core web application builder.
160
171
var builder = WebApplication .CreateBuilder (args );
161
172
173
+ // Add the OpenTelemetry telemetry service to the application.
174
+ // This service will collect and send telemetry data to Azure Monitor.
162
175
builder .Services .AddOpenTelemetry ().UseAzureMonitor ();
176
+
177
+ // Configure the OpenTelemetry tracer provider to add the resource attributes to all traces.
163
178
builder .Services .ConfigureOpenTelemetryTracerProvider ((sp , builder ) =>
164
179
builder .ConfigureResource (resourceBuilder =>
165
180
resourceBuilder .AddAttributes (resourceAttributes )));
166
181
182
+ // Build the ASP.NET Core web application.
167
183
var app = builder .Build ();
168
184
185
+ // Start the ASP.NET Core web application.
169
186
app .Run ();
170
187
```
171
188
@@ -175,22 +192,29 @@ Set the Cloud Role Name and the Cloud Role Instance via [Resource](https://githu
175
192
176
193
```csharp
177
194
// Setting role name and role instance
195
+
196
+ // Create a dictionary of resource attributes.
178
197
var resourceAttributes = new Dictionary <string , object > {
179
198
{ " service.name" , " my-service" },
180
199
{ " service.namespace" , " my-namespace" },
181
200
{ " service.instance.id" , " my-instance" }};
201
+
202
+ // Create a resource builder.
182
203
var resourceBuilder = ResourceBuilder .CreateDefault ().AddAttributes (resourceAttributes );
183
204
205
+ // Create a new OpenTelemetry tracer provider and set the resource builder.
184
206
var tracerProvider = Sdk .CreateTracerProviderBuilder ()
185
207
// Set ResourceBuilder on the TracerProvider.
186
208
.SetResourceBuilder (resourceBuilder )
187
209
.AddAzureMonitorTraceExporter ();
188
210
211
+ // Create a new OpenTelemetry meter provider and set the resource builder.
189
212
var metricsProvider = Sdk .CreateMeterProviderBuilder ()
190
213
// Set ResourceBuilder on the MeterProvider.
191
214
.SetResourceBuilder (resourceBuilder )
192
215
.AddAzureMonitorMetricExporter ();
193
216
217
+ // Create a new logger factory and add the OpenTelemetry logger provider with the resource builder.
194
218
var loggerFactory = LoggerFactory .Create (builder =>
195
219
{
196
220
builder .AddOpenTelemetry (options =>
@@ -261,15 +285,21 @@ You may want to enable sampling to reduce your data ingestion volume, which redu
261
285
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
286
263
287
```csharp
288
+ // Create a new ASP.NET Core web application builder.
264
289
var builder = WebApplication .CreateBuilder (args );
265
290
291
+ // Add the OpenTelemetry telemetry service to the application.
292
+ // This service will collect and send telemetry data to Azure Monitor.
266
293
builder .Services .AddOpenTelemetry ().UseAzureMonitor (o =>
267
294
{
295
+ // Set the sampling ratio to 10%. This means that 10% of all traces will be sampled and sent to Azure Monitor.
268
296
o .SamplingRatio = 0 . 1 F ;
269
297
});
270
298
299
+ // Build the ASP.NET Core web application.
271
300
var app = builder .Build ();
272
301
302
+ // Start the ASP.NET Core web application.
273
303
app .Run ();
274
304
```
275
305
@@ -278,9 +308,11 @@ app.Run();
278
308
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
309
280
310
```csharp
311
+ // Create a new OpenTelemetry tracer provider.
281
312
var tracerProvider = Sdk .CreateTracerProviderBuilder ()
282
313
.AddAzureMonitorTraceExporter (options =>
283
- {
314
+ {
315
+ // Set the sampling ratio to 10%. This means that 10% of all traces will be sampled and sent to Azure Monitor.
284
316
options .SamplingRatio = 0 . 1 F ;
285
317
});
286
318
```
@@ -341,14 +373,23 @@ We support the credential classes provided by [Azure Identity](https://github.co
341
373
342
374
1 . Provide the desired credential class :
343
375
```csharp
376
+ // Create a new ASP.NET Core web application builder.
344
377
var builder = WebApplication .CreateBuilder (args );
345
378
379
+ // Add the OpenTelemetry telemetry service to the application.
380
+ // This service will collect and send telemetry data to Azure Monitor.
346
381
builder .Services .AddOpenTelemetry ().UseAzureMonitor (options => {
382
+ // Set the Azure Monitor credential to the DefaultAzureCredential.
383
+ // This credential will use the Azure identity of the current user or
384
+ // the service principal that the application is running as to authenticate
385
+ // to Azure Monitor.
347
386
options .Credential = new DefaultAzureCredential ();
348
387
});
349
388
389
+ // Build the ASP.NET Core web application.
350
390
var app = builder .Build ();
351
391
392
+ // Start the ASP.NET Core web application.
352
393
app .Run ();
353
394
```
354
395
@@ -370,20 +411,24 @@ We support the credential classes provided by [Azure Identity](https://github.co
370
411
371
412
1 . Provide the desired credential class :
372
413
```csharp
414
+ // Create a DefaultAzureCredential.
373
415
var credential = new DefaultAzureCredential ();
374
416
417
+ // Create a new OpenTelemetry tracer provider and set the credential.
375
418
var tracerProvider = Sdk .CreateTracerProviderBuilder ()
376
419
.AddAzureMonitorTraceExporter (options =>
377
420
{
378
421
options .Credential = credential ;
379
422
});
380
423
424
+ // Create a new OpenTelemetry meter provider and set the credential.
381
425
var metricsProvider = Sdk .CreateMeterProviderBuilder ()
382
426
.AddAzureMonitorMetricExporter (options =>
383
427
{
384
428
options .Credential = credential ;
385
429
});
386
430
431
+ // Create a new logger factory and add the OpenTelemetry logger provider with the credential.
387
432
var loggerFactory = LoggerFactory .Create (builder =>
388
433
{
389
434
builder .AddOpenTelemetry (options =>
@@ -447,15 +492,22 @@ The Distro package includes the AzureMonitorExporter, which by default uses one
447
492
To override the default directory , you should set `AzureMonitorOptions .StorageDirectory `.
448
493
449
494
```csharp
495
+ // Create a new ASP.NET Core web application builder.
450
496
var builder = WebApplication .CreateBuilder (args );
451
497
498
+ // Add the OpenTelemetry telemetry service to the application.
499
+ // This service will collect and send telemetry data to Azure Monitor.
452
500
builder .Services .AddOpenTelemetry ().UseAzureMonitor (options =>
453
501
{
502
+ // Set the Azure Monitor storage directory to "C:\\SomeDirectory".
503
+ // This is the directory where the OpenTelemetry SDK will store any telemetry data that cannot be sent to Azure Monitor immediately.
454
504
options .StorageDirectory = " C:\\ SomeDirectory" ;
455
505
});
456
506
507
+ // Build the ASP.NET Core web application.
457
508
var app = builder .Build ();
458
509
510
+ // Start the ASP.NET Core web application.
459
511
app .Run ();
460
512
```
461
513
@@ -476,24 +528,33 @@ By default, the AzureMonitorExporter uses one of the following locations for off
476
528
To override the default directory , you should set `AzureMonitorExporterOptions .StorageDirectory `.
477
529
478
530
```csharp
531
+ // Create a new OpenTelemetry tracer provider and set the storage directory.
479
532
var tracerProvider = Sdk .CreateTracerProviderBuilder ()
480
533
.AddAzureMonitorTraceExporter (options =>
481
534
{
535
+ // Set the Azure Monitor storage directory to "C:\\SomeDirectory".
536
+ // This is the directory where the OpenTelemetry SDK will store any trace data that cannot be sent to Azure Monitor immediately.
482
537
options .StorageDirectory = " C:\\ SomeDirectory" ;
483
538
});
484
539
540
+ // Create a new OpenTelemetry meter provider and set the storage directory.
485
541
var metricsProvider = Sdk .CreateMeterProviderBuilder ()
486
542
.AddAzureMonitorMetricExporter (options =>
487
543
{
544
+ // Set the Azure Monitor storage directory to "C:\\SomeDirectory".
545
+ // This is the directory where the OpenTelemetry SDK will store any metric data that cannot be sent to Azure Monitor immediately.
488
546
options .StorageDirectory = " C:\\ SomeDirectory" ;
489
547
});
490
548
549
+ // Create a new logger factory and add the OpenTelemetry logger provider with the storage directory.
491
550
var loggerFactory = LoggerFactory .Create (builder =>
492
551
{
493
552
builder .AddOpenTelemetry (options =>
494
553
{
495
554
options .AddAzureMonitorLogExporter (options =>
496
555
{
556
+ // Set the Azure Monitor storage directory to "C:\\SomeDirectory".
557
+ // This is the directory where the OpenTelemetry SDK will store any log data that cannot be sent to Azure Monitor immediately.
497
558
options .StorageDirectory = " C:\\ SomeDirectory" ;
498
559
});
499
560
});
@@ -590,14 +651,22 @@ You might want to enable the OpenTelemetry Protocol (OTLP) Exporter alongside th
590
651
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
652
592
653
```csharp
654
+ // Create a new ASP.NET Core web application builder.
593
655
var builder = WebApplication .CreateBuilder (args );
594
656
657
+ // Add the OpenTelemetry telemetry service to the application.
658
+ // This service will collect and send telemetry data to Azure Monitor.
595
659
builder .Services .AddOpenTelemetry ().UseAzureMonitor ();
660
+
661
+ // Add the OpenTelemetry OTLP exporter to the application.
662
+ // This exporter will send telemetry data to an OTLP receiver, such as Prometheus
596
663
builder .Services .AddOpenTelemetry ().WithTracing (builder => builder .AddOtlpExporter ());
597
664
builder .Services .AddOpenTelemetry ().WithMetrics (builder => builder .AddOtlpExporter ());
598
665
666
+ // Build the ASP.NET Core web application.
599
667
var app = builder .Build ();
600
668
669
+ // Start the ASP.NET Core web application.
601
670
app .Run ();
602
671
```
603
672
@@ -612,10 +681,12 @@ You might want to enable the OpenTelemetry Protocol (OTLP) Exporter alongside th
612
681
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
682
614
683
```csharp
684
+ // Create a new OpenTelemetry tracer provider and add the Azure Monitor trace exporter and the OTLP trace exporter.
615
685
var tracerProvider = Sdk .CreateTracerProviderBuilder ()
616
686
.AddAzureMonitorTraceExporter ()
617
687
.AddOtlpExporter ();
618
688
689
+ // Create a new OpenTelemetry meter provider and add the Azure Monitor metric exporter and the OTLP metric exporter.
619
690
var metricsProvider = Sdk .CreateMeterProviderBuilder ()
620
691
.AddAzureMonitorMetricExporter ()
621
692
.AddOtlpExporter ();
0 commit comments