Skip to content

Commit 098b1e3

Browse files
Merge pull request #238046 from TimothyMothra/patch-2
Update opentelemetry-enable.md - cleanup .NET examples
2 parents cfdedc7 + 1954043 commit 098b1e3

File tree

2 files changed

+122
-85
lines changed

2 files changed

+122
-85
lines changed

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

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,17 @@ The sampler expects a sample rate of between 0 and 1 inclusive. A rate of 0.1 me
272272

273273
In this example, we utilize the `ApplicationInsightsSampler`, which offers compatibility with Application Insights SDKs.
274274

275-
```dotnetcli
276-
dotnet add package --prerelease OpenTelemetry.Extensions.AzureMonitor
277-
```
275+
1. Install the latest [OpenTelemetry.Extensions.AzureMonitor](https://www.nuget.org/packages/OpenTelemetry.Extensions.AzureMonitor) package:
276+
```dotnetcli
277+
dotnet add package --prerelease OpenTelemetry.Extensions.AzureMonitor
278+
```
278279

279-
```csharp
280-
var tracerProvider = Sdk.CreateTracerProviderBuilder()
281-
.SetSampler(new ApplicationInsightsSampler(new ApplicationInsightsSamplerOptions { SamplingRatio = 1.0F }))
282-
.AddAzureMonitorTraceExporter();
283-
```
280+
1. Add the following code snippet.
281+
```csharp
282+
var tracerProvider = Sdk.CreateTracerProviderBuilder()
283+
.SetSampler(new ApplicationInsightsSampler(new ApplicationInsightsSamplerOptions { SamplingRatio = 0.1F }))
284+
.AddAzureMonitorTraceExporter();
285+
```
284286

285287
#### [Java](#tab/java)
286288

@@ -327,17 +329,23 @@ We support the credential classes provided by [Azure Identity](https://github.co
327329
- We recommend `ClientSecretCredential` for service principals.
328330
- Provide the tenant ID, client ID, and client secret to the constructor.
329331

330-
```csharp
331-
var builder = WebApplication.CreateBuilder(args);
332+
1. Install the latest [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) package:
333+
```dotnetcli
334+
dotnet add package Azure.Identity
335+
```
336+
337+
1. Provide the desired credential class:
338+
```csharp
339+
var builder = WebApplication.CreateBuilder(args);
332340

333-
builder.Services.AddOpenTelemetry().UseAzureMonitor(options => {
334-
options.Credential = new DefaultAzureCredential();
335-
});
341+
builder.Services.AddOpenTelemetry().UseAzureMonitor(options => {
342+
options.Credential = new DefaultAzureCredential();
343+
});
336344

337-
var app = builder.Build();
345+
var app = builder.Build();
338346

339-
app.Run();
340-
```
347+
app.Run();
348+
```
341349

342350
#### [.NET](#tab/net)
343351

@@ -350,32 +358,38 @@ We support the credential classes provided by [Azure Identity](https://github.co
350358
- We recommend `ClientSecretCredential` for service principals.
351359
- Provide the tenant ID, client ID, and client secret to the constructor.
352360

353-
```csharp
354-
var credential = new DefaultAzureCredential();
361+
1. Install the latest [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) package:
362+
```dotnetcli
363+
dotnet add package Azure.Identity
364+
```
355365

356-
var tracerProvider = Sdk.CreateTracerProviderBuilder()
357-
.AddAzureMonitorTraceExporter(options =>
358-
{
359-
options.Credential = credential;
360-
});
366+
1. Provide the desired credential class:
367+
```csharp
368+
var credential = new DefaultAzureCredential();
361369

362-
var metricsProvider = Sdk.CreateMeterProviderBuilder()
363-
.AddAzureMonitorMetricExporter(options =>
364-
{
365-
options.Credential = credential;
366-
});
370+
var tracerProvider = Sdk.CreateTracerProviderBuilder()
371+
.AddAzureMonitorTraceExporter(options =>
372+
{
373+
options.Credential = credential;
374+
});
367375

368-
var loggerFactory = LoggerFactory.Create(builder =>
369-
{
370-
builder.AddOpenTelemetry(options =>
371-
{
372-
options.AddAzureMonitorLogExporter(options =>
376+
var metricsProvider = Sdk.CreateMeterProviderBuilder()
377+
.AddAzureMonitorMetricExporter(options =>
373378
{
374379
options.Credential = credential;
375380
});
381+
382+
var loggerFactory = LoggerFactory.Create(builder =>
383+
{
384+
builder.AddOpenTelemetry(options =>
385+
{
386+
options.AddAzureMonitorLogExporter(options =>
387+
{
388+
options.Credential = credential;
389+
});
390+
});
376391
});
377-
});
378-
```
392+
```
379393

380394
#### [Java](#tab/java)
381395

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

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ The [OpenTelemetry Specification](https://github.com/open-telemetry/opentelemetr
568568
describes the instruments and provides examples of when you might use each one.
569569

570570
> [!TIP]
571-
> The histogram is the most versatile and most closely equivalent to the Application Insights Track Metric [Classic API](api-custom-events-metrics.md). Azure Monitor currently flattens the histogram instrument into our five supported aggregation types, and support for percentiles is underway. Although less versatile, other OpenTelemetry instruments have a lesser impact on your application's performance.
571+
> The histogram is the most versatile and most closely equivalent to the Application Insights GetMetric [Classic API](api-custom-events-metrics.md). Azure Monitor currently flattens the histogram instrument into our five supported aggregation types, and support for percentiles is underway. Although less versatile, other OpenTelemetry instruments have a lesser impact on your application's performance.
572572
573573
#### Histogram Example
574574

@@ -613,10 +613,7 @@ public class Program
613613
{
614614
using var meterProvider = Sdk.CreateMeterProviderBuilder()
615615
.AddMeter("OTel.AzureMonitor.Demo")
616-
.AddAzureMonitorMetricExporter(o =>
617-
{
618-
o.ConnectionString = "<Your Connection String>";
619-
})
616+
.AddAzureMonitorMetricExporter()
620617
.Build();
621618

622619
Histogram<long> myFruitSalePrice = meter.CreateHistogram<long>("FruitSalePrice");
@@ -730,10 +727,7 @@ public class Program
730727
{
731728
using var meterProvider = Sdk.CreateMeterProviderBuilder()
732729
.AddMeter("OTel.AzureMonitor.Demo")
733-
.AddAzureMonitorMetricExporter(o =>
734-
{
735-
o.ConnectionString = "<Your Connection String>";
736-
})
730+
.AddAzureMonitorMetricExporter()
737731
.Build();
738732

739733
Counter<long> myFruitCounter = meter.CreateCounter<long>("MyFruitCounter");
@@ -858,10 +852,7 @@ public class Program
858852
{
859853
using var meterProvider = Sdk.CreateMeterProviderBuilder()
860854
.AddMeter("OTel.AzureMonitor.Demo")
861-
.AddAzureMonitorMetricExporter(o =>
862-
{
863-
o.ConnectionString = "<Your Connection String>";
864-
})
855+
.AddAzureMonitorMetricExporter()
865856
.Build();
866857

867858
var process = Process.GetCurrentProcess();
@@ -961,37 +952,75 @@ to draw attention in relevant experiences including the failures section and end
961952

962953
#### [ASP.NET Core](#tab/aspnetcore)
963954

964-
```csharp
965-
using (var activity = activitySource.StartActivity("ExceptionExample"))
966-
{
967-
try
968-
{
969-
throw new Exception("Test exception");
970-
}
971-
catch (Exception ex)
972-
{
973-
activity?.SetStatus(ActivityStatusCode.Error);
974-
activity?.RecordException(ex);
975-
}
976-
}
977-
```
955+
- To log an Exception using an Activity:
956+
```csharp
957+
using (var activity = activitySource.StartActivity("ExceptionExample"))
958+
{
959+
try
960+
{
961+
throw new Exception("Test exception");
962+
}
963+
catch (Exception ex)
964+
{
965+
activity?.SetStatus(ActivityStatusCode.Error);
966+
activity?.RecordException(ex);
967+
}
968+
}
969+
```
970+
- To log an Exception using ILogger:
971+
```csharp
972+
var logger = loggerFactory.CreateLogger(logCategoryName);
973+
974+
try
975+
{
976+
throw new Exception("Test Exception");
977+
}
978+
catch (Exception ex)
979+
{
980+
logger.Log(
981+
logLevel: LogLevel.Error,
982+
eventId: 0,
983+
exception: ex,
984+
message: "Hello {name}.",
985+
args: new object[] { "World" });
986+
}
987+
```
978988

979989
#### [.NET](#tab/net)
980990

981-
```csharp
982-
using (var activity = activitySource.StartActivity("ExceptionExample"))
983-
{
984-
try
985-
{
986-
throw new Exception("Test exception");
987-
}
988-
catch (Exception ex)
989-
{
990-
activity?.SetStatus(ActivityStatusCode.Error);
991-
activity?.RecordException(ex);
992-
}
993-
}
994-
```
991+
- To log an Exception using an Activity:
992+
```csharp
993+
using (var activity = activitySource.StartActivity("ExceptionExample"))
994+
{
995+
try
996+
{
997+
throw new Exception("Test exception");
998+
}
999+
catch (Exception ex)
1000+
{
1001+
activity?.SetStatus(ActivityStatusCode.Error);
1002+
activity?.RecordException(ex);
1003+
}
1004+
}
1005+
```
1006+
- To log an Exception using ILogger:
1007+
```csharp
1008+
var logger = loggerFactory.CreateLogger("ExceptionExample");
1009+
1010+
try
1011+
{
1012+
throw new Exception("Test Exception");
1013+
}
1014+
catch (Exception ex)
1015+
{
1016+
logger.Log(
1017+
logLevel: LogLevel.Error,
1018+
eventId: 0,
1019+
exception: ex,
1020+
message: "Hello {name}.",
1021+
args: new object[] { "World" });
1022+
}
1023+
```
9951024

9961025
#### [Java](#tab/java)
9971026

@@ -1119,7 +1148,7 @@ For code representing a background job not captured by an instrumentation librar
11191148
```csharp
11201149
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
11211150
.AddSource("ActivitySourceName")
1122-
.AddAzureMonitorTraceExporter(o => o.ConnectionString = "<Your Connection String>")
1151+
.AddAzureMonitorTraceExporter()
11231152
.Build();
11241153

11251154
var activitySource = new ActivitySource("ActivitySourceName");
@@ -1512,10 +1541,7 @@ To add span attributes, use either of the following two ways:
15121541
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
15131542
.AddSource("OTel.AzureMonitor.Demo")
15141543
.AddProcessor(new ActivityEnrichingProcessor())
1515-
.AddAzureMonitorTraceExporter(o =>
1516-
{
1517-
o.ConnectionString = "<Your Connection String>"
1518-
})
1544+
.AddAzureMonitorTraceExporter()
15191545
.Build();
15201546
```
15211547
@@ -1855,10 +1881,7 @@ You might use the following ways to filter out telemetry before it leaves your a
18551881
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
18561882
.AddSource("OTel.AzureMonitor.Demo")
18571883
.AddProcessor(new ActivityFilteringProcessor())
1858-
.AddAzureMonitorTraceExporter(o =>
1859-
{
1860-
o.ConnectionString = "<Your Connection String>"
1861-
})
1884+
.AddAzureMonitorTraceExporter()
18621885
.Build();
18631886
```
18641887

0 commit comments

Comments
 (0)