Skip to content

Commit 726cbd0

Browse files
Merge pull request #251787 from AaronMaxwell/aaronmax-otel-dotnet-comments-1
Adding comments to .NET code samples
2 parents 81be57e + c3bd875 commit 726cbd0

File tree

1 file changed

+88
-2
lines changed

1 file changed

+88
-2
lines changed

articles/azure-monitor/app/opentelemetry-configuration.md

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ Use one of the following three ways to configure the connection string:
2525

2626
- Add `UseAzureMonitor()` to your application startup. Depending on your version of .NET, it is in either your `startup.cs` or `program.cs` class.
2727
```csharp
28+
// Create a new ASP.NET Core web application builder.
2829
var builder = WebApplication.CreateBuilder(args);
2930

31+
// Add the OpenTelemetry telemetry service to the application.
32+
// This service will collect and send telemetry data to Azure Monitor.
3033
builder.Services.AddOpenTelemetry().UseAzureMonitor(options => {
3134
options.ConnectionString = "<Your Connection String>";
3235
});
3336

37+
// Build the ASP.NET Core web application.
3438
var app = builder.Build();
3539

40+
// Start the ASP.NET Core web application.
3641
app.Run();
3742
```
3843
- Set an environment variable:
@@ -60,18 +65,24 @@ Use one of the following two ways to configure the connection string:
6065

6166
- Add the Azure Monitor Exporter to each OpenTelemetry signal in application startup.
6267
```csharp
68+
// Create a new OpenTelemetry tracer provider.
69+
// It is important to keep the TracerProvider instance active throughout the process lifetime.
6370
var tracerProvider = Sdk.CreateTracerProviderBuilder()
6471
.AddAzureMonitorTraceExporter(options =>
6572
{
6673
options.ConnectionString = "<Your Connection String>";
6774
});
6875

76+
// Create a new OpenTelemetry meter provider.
77+
// It is important to keep the MetricsProvider instance active throughout the process lifetime.
6978
var metricsProvider = Sdk.CreateMeterProviderBuilder()
7079
.AddAzureMonitorMetricExporter(options =>
7180
{
7281
options.ConnectionString = "<Your Connection String>";
7382
});
7483

84+
// Create a new logger factory.
85+
// It is important to keep the LoggerFactory instance active throughout the process lifetime.
7586
var loggerFactory = LoggerFactory.Create(builder =>
7687
{
7788
builder.AddOpenTelemetry(options =>
@@ -152,20 +163,29 @@ Set the Cloud Role Name and the Cloud Role Instance via [Resource](https://githu
152163
153164
```csharp
154165
// Setting role name and role instance
166+
167+
// Create a dictionary of resource attributes.
155168
var resourceAttributes = new Dictionary<string, object> {
156169
{ "service.name", "my-service" },
157170
{ "service.namespace", "my-namespace" },
158171
{ "service.instance.id", "my-instance" }};
159172

173+
// Create a new ASP.NET Core web application builder.
160174
var builder = WebApplication.CreateBuilder(args);
161175

176+
// Add the OpenTelemetry telemetry service to the application.
177+
// This service will collect and send telemetry data to Azure Monitor.
162178
builder.Services.AddOpenTelemetry().UseAzureMonitor();
179+
180+
// Configure the OpenTelemetry tracer provider to add the resource attributes to all traces.
163181
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) =>
164182
builder.ConfigureResource(resourceBuilder =>
165183
resourceBuilder.AddAttributes(resourceAttributes)));
166184

185+
// Build the ASP.NET Core web application.
167186
var app = builder.Build();
168187

188+
// Start the ASP.NET Core web application.
169189
app.Run();
170190
```
171191

@@ -175,22 +195,32 @@ Set the Cloud Role Name and the Cloud Role Instance via [Resource](https://githu
175195
176196
```csharp
177197
// Setting role name and role instance
198+
199+
// Create a dictionary of resource attributes.
178200
var resourceAttributes = new Dictionary<string, object> {
179201
{ "service.name", "my-service" },
180202
{ "service.namespace", "my-namespace" },
181203
{ "service.instance.id", "my-instance" }};
204+
205+
// Create a resource builder.
182206
var resourceBuilder = ResourceBuilder.CreateDefault().AddAttributes(resourceAttributes);
183207

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.
184210
var tracerProvider = Sdk.CreateTracerProviderBuilder()
185211
// Set ResourceBuilder on the TracerProvider.
186212
.SetResourceBuilder(resourceBuilder)
187213
.AddAzureMonitorTraceExporter();
188214

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.
189217
var metricsProvider = Sdk.CreateMeterProviderBuilder()
190218
// Set ResourceBuilder on the MeterProvider.
191219
.SetResourceBuilder(resourceBuilder)
192220
.AddAzureMonitorMetricExporter();
193221

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.
194224
var loggerFactory = LoggerFactory.Create(builder =>
195225
{
196226
builder.AddOpenTelemetry(options =>
@@ -261,15 +291,21 @@ You may want to enable sampling to reduce your data ingestion volume, which redu
261291
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.
262292

263293
```csharp
294+
// Create a new ASP.NET Core web application builder.
264295
var builder = WebApplication.CreateBuilder(args);
265296

297+
// Add the OpenTelemetry telemetry service to the application.
298+
// This service will collect and send telemetry data to Azure Monitor.
266299
builder.Services.AddOpenTelemetry().UseAzureMonitor(o =>
267300
{
301+
// Set the sampling ratio to 10%. This means that 10% of all traces will be sampled and sent to Azure Monitor.
268302
o.SamplingRatio = 0.1F;
269303
});
270304

305+
// Build the ASP.NET Core web application.
271306
var app = builder.Build();
272307

308+
// Start the ASP.NET Core web application.
273309
app.Run();
274310
```
275311

@@ -278,9 +314,12 @@ app.Run();
278314
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.
279315

280316
```csharp
317+
// Create a new OpenTelemetry tracer provider.
318+
// It is important to keep the TracerProvider instance active throughout the process lifetime.
281319
var tracerProvider = Sdk.CreateTracerProviderBuilder()
282320
.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.
284323
options.SamplingRatio = 0.1F;
285324
});
286325
```
@@ -341,14 +380,23 @@ We support the credential classes provided by [Azure Identity](https://github.co
341380

342381
1. Provide the desired credential class:
343382
```csharp
383+
// Create a new ASP.NET Core web application builder.
344384
var builder = WebApplication.CreateBuilder(args);
345385

386+
// Add the OpenTelemetry telemetry service to the application.
387+
// This service will collect and send telemetry data to Azure Monitor.
346388
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.
347393
options.Credential = new DefaultAzureCredential();
348394
});
349395

396+
// Build the ASP.NET Core web application.
350397
var app = builder.Build();
351398

399+
// Start the ASP.NET Core web application.
352400
app.Run();
353401
```
354402

@@ -370,20 +418,27 @@ We support the credential classes provided by [Azure Identity](https://github.co
370418

371419
1. Provide the desired credential class:
372420
```csharp
421+
// Create a DefaultAzureCredential.
373422
var credential = new DefaultAzureCredential();
374423

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.
375426
var tracerProvider = Sdk.CreateTracerProviderBuilder()
376427
.AddAzureMonitorTraceExporter(options =>
377428
{
378429
options.Credential = credential;
379430
});
380431

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.
381434
var metricsProvider = Sdk.CreateMeterProviderBuilder()
382435
.AddAzureMonitorMetricExporter(options =>
383436
{
384437
options.Credential = credential;
385438
});
386439

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.
387442
var loggerFactory = LoggerFactory.Create(builder =>
388443
{
389444
builder.AddOpenTelemetry(options =>
@@ -447,15 +502,22 @@ The Distro package includes the AzureMonitorExporter, which by default uses one
447502
To override the default directory, you should set `AzureMonitorOptions.StorageDirectory`.
448503

449504
```csharp
505+
// Create a new ASP.NET Core web application builder.
450506
var builder = WebApplication.CreateBuilder(args);
451507

508+
// Add the OpenTelemetry telemetry service to the application.
509+
// This service will collect and send telemetry data to Azure Monitor.
452510
builder.Services.AddOpenTelemetry().UseAzureMonitor(options =>
453511
{
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.
454514
options.StorageDirectory = "C:\\SomeDirectory";
455515
});
456516

517+
// Build the ASP.NET Core web application.
457518
var app = builder.Build();
458519

520+
// Start the ASP.NET Core web application.
459521
app.Run();
460522
```
461523

@@ -476,24 +538,36 @@ By default, the AzureMonitorExporter uses one of the following locations for off
476538
To override the default directory, you should set `AzureMonitorExporterOptions.StorageDirectory`.
477539

478540
```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.
479543
var tracerProvider = Sdk.CreateTracerProviderBuilder()
480544
.AddAzureMonitorTraceExporter(options =>
481545
{
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.
482548
options.StorageDirectory = "C:\\SomeDirectory";
483549
});
484550

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.
485553
var metricsProvider = Sdk.CreateMeterProviderBuilder()
486554
.AddAzureMonitorMetricExporter(options =>
487555
{
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.
488558
options.StorageDirectory = "C:\\SomeDirectory";
489559
});
490560

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.
491563
var loggerFactory = LoggerFactory.Create(builder =>
492564
{
493565
builder.AddOpenTelemetry(options =>
494566
{
495567
options.AddAzureMonitorLogExporter(options =>
496568
{
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.
497571
options.StorageDirectory = "C:\\SomeDirectory";
498572
});
499573
});
@@ -590,14 +664,22 @@ You might want to enable the OpenTelemetry Protocol (OTLP) Exporter alongside th
590664
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).
591665
592666
```csharp
667+
// Create a new ASP.NET Core web application builder.
593668
var builder = WebApplication.CreateBuilder(args);
594669

670+
// Add the OpenTelemetry telemetry service to the application.
671+
// This service will collect and send telemetry data to Azure Monitor.
595672
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
596676
builder.Services.AddOpenTelemetry().WithTracing(builder => builder.AddOtlpExporter());
597677
builder.Services.AddOpenTelemetry().WithMetrics(builder => builder.AddOtlpExporter());
598678

679+
// Build the ASP.NET Core web application.
599680
var app = builder.Build();
600681

682+
// Start the ASP.NET Core web application.
601683
app.Run();
602684
```
603685

@@ -612,10 +694,14 @@ You might want to enable the OpenTelemetry Protocol (OTLP) Exporter alongside th
612694
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).
613695
614696
```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.
615699
var tracerProvider = Sdk.CreateTracerProviderBuilder()
616700
.AddAzureMonitorTraceExporter()
617701
.AddOtlpExporter();
618702

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.
619705
var metricsProvider = Sdk.CreateMeterProviderBuilder()
620706
.AddAzureMonitorMetricExporter()
621707
.AddOtlpExporter();
@@ -715,4 +801,4 @@ For more information about OpenTelemetry SDK configuration, see the [OpenTelemet
715801
716802
---
717803

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

Comments
 (0)