Skip to content

Commit abf0952

Browse files
committed
Adding comments to .NET code samples
1 parent d77124f commit abf0952

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

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

Lines changed: 72 additions & 1 deletion
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,21 @@ 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.
6369
var tracerProvider = Sdk.CreateTracerProviderBuilder()
6470
.AddAzureMonitorTraceExporter(options =>
6571
{
6672
options.ConnectionString = "<Your Connection String>";
6773
});
6874

75+
// Create a new OpenTelemetry meter provider.
6976
var metricsProvider = Sdk.CreateMeterProviderBuilder()
7077
.AddAzureMonitorMetricExporter(options =>
7178
{
7279
options.ConnectionString = "<Your Connection String>";
7380
});
7481

82+
// Create a new logger factory.
7583
var loggerFactory = LoggerFactory.Create(builder =>
7684
{
7785
builder.AddOpenTelemetry(options =>
@@ -152,20 +160,29 @@ Set the Cloud Role Name and the Cloud Role Instance via [Resource](https://githu
152160
153161
```csharp
154162
// Setting role name and role instance
163+
164+
// Create a dictionary of resource attributes.
155165
var resourceAttributes = new Dictionary<string, object> {
156166
{ "service.name", "my-service" },
157167
{ "service.namespace", "my-namespace" },
158168
{ "service.instance.id", "my-instance" }};
159169

170+
// Create a new ASP.NET Core web application builder.
160171
var builder = WebApplication.CreateBuilder(args);
161172

173+
// Add the OpenTelemetry telemetry service to the application.
174+
// This service will collect and send telemetry data to Azure Monitor.
162175
builder.Services.AddOpenTelemetry().UseAzureMonitor();
176+
177+
// Configure the OpenTelemetry tracer provider to add the resource attributes to all traces.
163178
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) =>
164179
builder.ConfigureResource(resourceBuilder =>
165180
resourceBuilder.AddAttributes(resourceAttributes)));
166181

182+
// Build the ASP.NET Core web application.
167183
var app = builder.Build();
168184

185+
// Start the ASP.NET Core web application.
169186
app.Run();
170187
```
171188

@@ -175,22 +192,29 @@ Set the Cloud Role Name and the Cloud Role Instance via [Resource](https://githu
175192
176193
```csharp
177194
// Setting role name and role instance
195+
196+
// Create a dictionary of resource attributes.
178197
var resourceAttributes = new Dictionary<string, object> {
179198
{ "service.name", "my-service" },
180199
{ "service.namespace", "my-namespace" },
181200
{ "service.instance.id", "my-instance" }};
201+
202+
// Create a resource builder.
182203
var resourceBuilder = ResourceBuilder.CreateDefault().AddAttributes(resourceAttributes);
183204

205+
// Create a new OpenTelemetry tracer provider and set the resource builder.
184206
var tracerProvider = Sdk.CreateTracerProviderBuilder()
185207
// Set ResourceBuilder on the TracerProvider.
186208
.SetResourceBuilder(resourceBuilder)
187209
.AddAzureMonitorTraceExporter();
188210

211+
// Create a new OpenTelemetry meter provider and set the resource builder.
189212
var metricsProvider = Sdk.CreateMeterProviderBuilder()
190213
// Set ResourceBuilder on the MeterProvider.
191214
.SetResourceBuilder(resourceBuilder)
192215
.AddAzureMonitorMetricExporter();
193216

217+
// Create a new logger factory and add the OpenTelemetry logger provider with the resource builder.
194218
var loggerFactory = LoggerFactory.Create(builder =>
195219
{
196220
builder.AddOpenTelemetry(options =>
@@ -261,15 +285,21 @@ You may want to enable sampling to reduce your data ingestion volume, which redu
261285
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.
262286

263287
```csharp
288+
// Create a new ASP.NET Core web application builder.
264289
var builder = WebApplication.CreateBuilder(args);
265290

291+
// Add the OpenTelemetry telemetry service to the application.
292+
// This service will collect and send telemetry data to Azure Monitor.
266293
builder.Services.AddOpenTelemetry().UseAzureMonitor(o =>
267294
{
295+
// Set the sampling ratio to 10%. This means that 10% of all traces will be sampled and sent to Azure Monitor.
268296
o.SamplingRatio = 0.1F;
269297
});
270298

299+
// Build the ASP.NET Core web application.
271300
var app = builder.Build();
272301

302+
// Start the ASP.NET Core web application.
273303
app.Run();
274304
```
275305

@@ -278,9 +308,11 @@ app.Run();
278308
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.
279309

280310
```csharp
311+
// Create a new OpenTelemetry tracer provider.
281312
var tracerProvider = Sdk.CreateTracerProviderBuilder()
282313
.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.
284316
options.SamplingRatio = 0.1F;
285317
});
286318
```
@@ -341,14 +373,23 @@ We support the credential classes provided by [Azure Identity](https://github.co
341373

342374
1. Provide the desired credential class:
343375
```csharp
376+
// Create a new ASP.NET Core web application builder.
344377
var builder = WebApplication.CreateBuilder(args);
345378

379+
// Add the OpenTelemetry telemetry service to the application.
380+
// This service will collect and send telemetry data to Azure Monitor.
346381
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.
347386
options.Credential = new DefaultAzureCredential();
348387
});
349388

389+
// Build the ASP.NET Core web application.
350390
var app = builder.Build();
351391

392+
// Start the ASP.NET Core web application.
352393
app.Run();
353394
```
354395

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

371412
1. Provide the desired credential class:
372413
```csharp
414+
// Create a DefaultAzureCredential.
373415
var credential = new DefaultAzureCredential();
374416

417+
// Create a new OpenTelemetry tracer provider and set the credential.
375418
var tracerProvider = Sdk.CreateTracerProviderBuilder()
376419
.AddAzureMonitorTraceExporter(options =>
377420
{
378421
options.Credential = credential;
379422
});
380423

424+
// Create a new OpenTelemetry meter provider and set the credential.
381425
var metricsProvider = Sdk.CreateMeterProviderBuilder()
382426
.AddAzureMonitorMetricExporter(options =>
383427
{
384428
options.Credential = credential;
385429
});
386430

431+
// Create a new logger factory and add the OpenTelemetry logger provider with the credential.
387432
var loggerFactory = LoggerFactory.Create(builder =>
388433
{
389434
builder.AddOpenTelemetry(options =>
@@ -447,15 +492,22 @@ The Distro package includes the AzureMonitorExporter, which by default uses one
447492
To override the default directory, you should set `AzureMonitorOptions.StorageDirectory`.
448493

449494
```csharp
495+
// Create a new ASP.NET Core web application builder.
450496
var builder = WebApplication.CreateBuilder(args);
451497

498+
// Add the OpenTelemetry telemetry service to the application.
499+
// This service will collect and send telemetry data to Azure Monitor.
452500
builder.Services.AddOpenTelemetry().UseAzureMonitor(options =>
453501
{
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.
454504
options.StorageDirectory = "C:\\SomeDirectory";
455505
});
456506

507+
// Build the ASP.NET Core web application.
457508
var app = builder.Build();
458509

510+
// Start the ASP.NET Core web application.
459511
app.Run();
460512
```
461513

@@ -476,24 +528,33 @@ By default, the AzureMonitorExporter uses one of the following locations for off
476528
To override the default directory, you should set `AzureMonitorExporterOptions.StorageDirectory`.
477529

478530
```csharp
531+
// Create a new OpenTelemetry tracer provider and set the storage directory.
479532
var tracerProvider = Sdk.CreateTracerProviderBuilder()
480533
.AddAzureMonitorTraceExporter(options =>
481534
{
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.
482537
options.StorageDirectory = "C:\\SomeDirectory";
483538
});
484539

540+
// Create a new OpenTelemetry meter provider and set the storage directory.
485541
var metricsProvider = Sdk.CreateMeterProviderBuilder()
486542
.AddAzureMonitorMetricExporter(options =>
487543
{
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.
488546
options.StorageDirectory = "C:\\SomeDirectory";
489547
});
490548

549+
// Create a new logger factory and add the OpenTelemetry logger provider with the storage directory.
491550
var loggerFactory = LoggerFactory.Create(builder =>
492551
{
493552
builder.AddOpenTelemetry(options =>
494553
{
495554
options.AddAzureMonitorLogExporter(options =>
496555
{
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.
497558
options.StorageDirectory = "C:\\SomeDirectory";
498559
});
499560
});
@@ -590,14 +651,22 @@ You might want to enable the OpenTelemetry Protocol (OTLP) Exporter alongside th
590651
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).
591652
592653
```csharp
654+
// Create a new ASP.NET Core web application builder.
593655
var builder = WebApplication.CreateBuilder(args);
594656

657+
// Add the OpenTelemetry telemetry service to the application.
658+
// This service will collect and send telemetry data to Azure Monitor.
595659
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
596663
builder.Services.AddOpenTelemetry().WithTracing(builder => builder.AddOtlpExporter());
597664
builder.Services.AddOpenTelemetry().WithMetrics(builder => builder.AddOtlpExporter());
598665

666+
// Build the ASP.NET Core web application.
599667
var app = builder.Build();
600668

669+
// Start the ASP.NET Core web application.
601670
app.Run();
602671
```
603672

@@ -612,10 +681,12 @@ You might want to enable the OpenTelemetry Protocol (OTLP) Exporter alongside th
612681
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).
613682
614683
```csharp
684+
// Create a new OpenTelemetry tracer provider and add the Azure Monitor trace exporter and the OTLP trace exporter.
615685
var tracerProvider = Sdk.CreateTracerProviderBuilder()
616686
.AddAzureMonitorTraceExporter()
617687
.AddOtlpExporter();
618688

689+
// Create a new OpenTelemetry meter provider and add the Azure Monitor metric exporter and the OTLP metric exporter.
619690
var metricsProvider = Sdk.CreateMeterProviderBuilder()
620691
.AddAzureMonitorMetricExporter()
621692
.AddOtlpExporter();

0 commit comments

Comments
 (0)