Skip to content

Commit 5915b38

Browse files
Merge pull request #237310 from TimothyMothra/patch-1
Update opentelemetry-enable.md - cleaning up .NET examples
2 parents 89633d7 + 6503fc4 commit 5915b38

File tree

2 files changed

+65
-52
lines changed

2 files changed

+65
-52
lines changed

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

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,42 @@ A connection string in Application Insights defines the target location for send
2020

2121
### [.NET](#tab/net)
2222

23-
Currently unavailable.
23+
Use one of the following three ways to configure the connection string:
24+
25+
- Add `UseAzureMonitor()` to your application startup. Depending on your version of .NET, this will be in either your `startup.cs` or `program.cs` class.
26+
```csharp
27+
using Azure.Monitor.OpenTelemetry.AspNetCore;
28+
using Microsoft.AspNetCore.Builder;
29+
using Microsoft.Extensions.DependencyInjection;
30+
31+
var builder = WebApplication.CreateBuilder(args);
32+
33+
builder.Services.AddOpenTelemetry().UseAzureMonitor(options => {
34+
options.ConnectionString = "<Your Connection String>";
35+
});
36+
37+
var app = builder.Build();
38+
39+
app.Run();
40+
```
41+
- Set an environment variable:
42+
```console
43+
APPLICATIONINSIGHTS_CONNECTION_STRING=<Your Connection String>
44+
```
45+
- Add the following section to your `appsettings.json` config file:
46+
```json
47+
{
48+
"AzureMonitor": {
49+
"ConnectionString": "<Your Connection String>"
50+
}
51+
}
52+
```
53+
54+
> [!NOTE]
55+
> If you set the connection string in more than one place, we adhere to the following precedence:
56+
> 1. Code
57+
> 2. Environment Variable
58+
> 3. Configuration File
2459

2560
### [Java](#tab/java)
2661

@@ -187,9 +222,26 @@ export OTEL_TRACES_SAMPLER_ARG=0.1
187222
You might want to enable Azure Active Directory (Azure AD) Authentication for a more secure connection to Azure, which prevents unauthorized telemetry from being ingested into your subscription.
188223

189224
#### [.NET](#tab/net)
190-
225+
226+
We support the credential classes provided by [Azure Identity](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/identity/Azure.Identity#credential-classes).
227+
228+
- We recommend `DefaultAzureCredential` for local development.
229+
- We recommend `ManagedIdentityCredential` for system-assigned and user-assigned managed identities.
230+
- For system-assigned, use the default constructor without parameters.
231+
- For user-assigned, provide the client ID to the constructor.
232+
- We recommend `ClientSecretCredential` for service principals.
233+
- Provide the tenant ID, client ID, and client secret to the constructor.
234+
191235
```csharp
192-
Currently unavailable.
236+
var builder = WebApplication.CreateBuilder(args);
237+
238+
builder.Services.AddOpenTelemetry().UseAzureMonitor(options => {
239+
options.Credential = new DefaultAzureCredential();
240+
});
241+
242+
var app = builder.Build();
243+
244+
app.Run();
193245
```
194246

195247
#### [Java](#tab/java)
@@ -396,7 +448,12 @@ The following OpenTelemetry configurations can be accessed through environment v
396448

397449
### [.NET](#tab/net)
398450

399-
Currently unavailable.
451+
| Environment variable | Description |
452+
| -------------------------- | -------------------------------------------------- |
453+
| `APPLICATIONINSIGHTS_CONNECTION_STRING` | Set this to the connection string for your Application Insights resource. |
454+
| `APPLICATIONINSIGHTS_STATSBEAT_DISABLED` | Set this to `true` to opt-out of internal metrics collection. |
455+
| `OTEL_RESOURCE_ATTRIBUTES` | Key-value pairs to be used as resource attributes. See the [Resource SDK specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.5.0/specification/resource/sdk.md#specifying-resource-information-via-an-environment-variable) for more details. |
456+
| `OTEL_SERVICE_NAME` | Sets the value of the `service.name` resource attribute. If `service.name` is also provided in `OTEL_RESOURCE_ATTRIBUTES`, then `OTEL_SERVICE_NAME` takes precedence. |
400457

401458
### [Java](#tab/java)
402459

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

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,7 @@ using Microsoft.Extensions.DependencyInjection;
136136

137137
var builder = WebApplication.CreateBuilder(args);
138138

139-
builder.Services.AddOpenTelemetry().UseAzureMonitor(
140-
141-
//Uncomment the line below when setting the Application Insights Connection String via code
142-
//options => options.ConnectionString = "<Your Connection String>"
143-
144-
);
139+
builder.Services.AddOpenTelemetry().UseAzureMonitor();
145140

146141
var app = builder.Build();
147142

@@ -487,7 +482,7 @@ The following table represents the currently supported custom telemetry types:
487482
| Language | Custom Events | Custom Metrics | Dependencies | Exceptions | Page Views | Requests | Traces |
488483
|-------------------------------------------|---------------|----------------|--------------|------------|------------|----------|--------|
489484
| **ASP.NET Core** | | | | | | | |
490-
| &nbsp;&nbsp;&nbsp;OpenTelemetry API | | | Yes | Yes | | Yes | |
485+
| &nbsp;&nbsp;&nbsp;OpenTelemetry API | | Yes | Yes | Yes | | Yes | |
491486
| &nbsp;&nbsp;&nbsp;iLogger API | | | | | | | Yes |
492487
| &nbsp;&nbsp;&nbsp;AI Classic API | | | | | | | |
493488
| | | | | | | | |
@@ -750,45 +745,6 @@ private static IEnumerable<Measurement<int>> GetThreadState(Process process)
750745
}
751746
```
752747

753-
754-
```csharp
755-
using System.Diagnostics.Metrics;
756-
using Azure.Monitor.OpenTelemetry.Exporter;
757-
using OpenTelemetry;
758-
using OpenTelemetry.Metrics;
759-
760-
public class Program
761-
{
762-
internal static readonly Meter meter = new("OTel.AzureMonitor.Demo");
763-
764-
public static void Main()
765-
{
766-
using var meterProvider = Sdk.CreateMeterProviderBuilder()
767-
.AddMeter("OTel.AzureMonitor.Demo")
768-
.AddAzureMonitorMetricExporter(o =>
769-
{
770-
o.ConnectionString = "<Your Connection String>";
771-
})
772-
.Build();
773-
774-
var process = Process.GetCurrentProcess();
775-
776-
ObservableGauge<int> myObservableGauge = meter.CreateObservableGauge("Thread.State", () => GetThreadState(process));
777-
778-
System.Console.WriteLine("Press Enter key to exit.");
779-
System.Console.ReadLine();
780-
}
781-
782-
private static IEnumerable<Measurement<int>> GetThreadState(Process process)
783-
{
784-
foreach (ProcessThread thread in process.Threads)
785-
{
786-
yield return new((int)thread.ThreadState, new("ProcessId", process.Id), new("ThreadId", thread.Id));
787-
}
788-
}
789-
}
790-
```
791-
792748
#### [Java](#tab/java)
793749

794750
```Java
@@ -1314,11 +1270,11 @@ To add span attributes, use either of the following two ways:
13141270
* Add a custom span processor.
13151271
13161272
> [!TIP]
1317-
> The advantage of using options provided by instrumentation libraries, when they're available, is that the entire context is available. As a result, users can select to add or filter more attributes. For example, the enrich option in the HttpClient instrumentation library gives users access to the httpRequestMessage itself. They can select anything from it and store it as an attribute.
1273+
> The advantage of using options provided by instrumentation libraries, when they're available, is that the entire context is available. As a result, users can select to add or filter more attributes. For example, the enrich option in the HttpClient instrumentation library gives users access to the [HttpRequestMessage](/dotnet/api/system.net.http.httprequestmessage) and the [HttpResponseMessage](/dotnet/api/system.net.http.httpresponsemessage) itself. They can select anything from it and store it as an attribute.
13181274

13191275
1. Many instrumentation libraries provide an enrich option. For guidance, see the readme files of individual instrumentation libraries:
13201276
- [ASP.NET Core](https://github.com/open-telemetry/opentelemetry-dotnet/blob/1.0.0-rc9.14/src/OpenTelemetry.Instrumentation.AspNetCore/README.md#enrich)
1321-
- [HttpClient and HttpWebRequest](https://github.com/open-telemetry/opentelemetry-dotnet/blob/1.0.0-rc9.14/src/OpenTelemetry.Instrumentation.Http/README.md#enrich)
1277+
- [HttpClient](https://github.com/open-telemetry/opentelemetry-dotnet/blob/1.0.0-rc9.14/src/OpenTelemetry.Instrumentation.Http/README.md#enrich)
13221278

13231279
1. Use a custom processor:
13241280

0 commit comments

Comments
 (0)