Skip to content

Commit 056f707

Browse files
[AzureMonitorDistro] Update Readme with advanced configuration (Azure#35569)
* Advanced configuration * tab to space. * fix code block * add link * PR feedback * PR feedback * pr feedback * feedback
1 parent 79bd0cd commit 056f707

File tree

1 file changed

+124
-0
lines changed
  • sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore

1 file changed

+124
-0
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ The Azure Monitor Distro is a client library that sends telemetry data to Azure
1010
- **Azure Application Insights Connection String:** To send telemetry data to the monitoring service you'll need connection string from Azure Application Insights. If you are not familiar with creating Azure resources, you may wish to follow the step-by-step guide for [Create an Application Insights resource](https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource) and [copy the connection string](https://docs.microsoft.com/azure/azure-monitor/app/sdk-connection-string?tabs=net#find-your-connection-string).
1111
- **ASP.NET Core App:** An ASP.NET Core application is required to instrument it with Azure Monitor Distro. You can either bring your own app or follow the [Get started with ASP.NET Core MVC](https://docs.microsoft.com/aspnet/core/tutorials/first-mvc-app/start-mvc) to create a new one.
1212

13+
### What is Included in the Distro
14+
15+
The Azure Monitor Distro is a distribution of the .NET OpenTelemetry SDK and related instrumentation libraries. It includes the following components:
16+
17+
* Traces
18+
* [ASP.NET Core Instrumentation Library](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNetCore/) provides automatic tracing for incoming HTTP requests to ASP.NET Core applications.
19+
* [HTTP Client Instrumentation Library](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.Http/) provides automatic tracing for outgoing HTTP requests made using [System.Net.Http.HttpClient](https://docs.microsoft.com/dotnet/api/system.net.http.httpclient) and [System.Net.HttpWebRequest](https://docs.microsoft.com/dotnet/api/system.net.httpwebrequest).
20+
* [SQL Client Instrumentation Library](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.SqlClient) provides automatic tracing for SQL queries executed using the [Microsoft.Data.SqlClient](https://www.nuget.org/packages/Microsoft.Data.SqlClient) and [System.Data.SqlClient](https://www.nuget.org/packages/System.Data.SqlClient) packages.
21+
* [Application Insights Sampler](https://www.nuget.org/packages/OpenTelemetry.Extensions.AzureMonitor/) provides a sampling that is compatible with Application Insights sampling.
22+
23+
* Metrics
24+
* [ASP.NET Core Instrumentation Library](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNetCore/) provides automatic collection of common ASP.NET Core metrics.
25+
* [HTTP Client Instrumentation Library](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.Http/) provides automatic collection of HTTP client metrics.
26+
27+
* [Logs](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/logs/getting-started/README.md)
28+
29+
* [Azure Monitor Exporter](https://www.nuget.org/packages/Azure.Monitor.OpenTelemetry.Exporter/) allows sending traces, metrics, and logs data to Azure Monitor.
30+
1331
### Install the package
1432

1533
#### Latest Version: [![Nuget](https://img.shields.io/nuget/vpre/Azure.Monitor.OpenTelemetry.AspNetCore.svg)](https://www.nuget.org/packages/Azure.Monitor.OpenTelemetry.AspNetCore/)
@@ -65,6 +83,9 @@ var app = builder.Build();
6583

6684
Note that in the examples above, `UseAzureMonitor` is added to the `IServiceCollection` in the `Program.cs` file. You can also add it in the `ConfigureServices` method of your `Startup.cs` file.
6785

86+
> **Note**
87+
> Multiple calls to `AddOpenTelemetry.UseAzureMonitor()` will **NOT** result in multiple providers. Only a single `TracerProvider`, `MeterProvider` and `LoggerProvider` will be created in the target `IServiceCollection`. To establish multiple providers use the `Sdk.CreateTracerProviderBuilder()` and/or `Sdk.CreateMeterProviderBuilder()` and/or `LoggerFactory.CreateLogger` methods with the [Azure Monitor Exporter](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter) instead of using Azure Monitor Distro.
88+
6889
### Authenticate the client
6990

7091
Azure Active Directory (AAD) authentication is an optional feature that can be used with Azure Monitor Distro. To enable AAD authentication, set the `Credential` property in `AzureMonitorOptions`. This is made easy with the [Azure Identity library](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/identity/Azure.Identity/README.md), which provides support for authenticating Azure SDK clients with their corresponding Azure services.
@@ -82,6 +103,109 @@ With this configuration, the Azure Monitor Distro will use the credentials of th
82103

83104
Note that the `Credential` property is optional. If it is not set, Azure Monitor Distro will use the Instrumentation Key from the Connection String to send data to Azure Monitor.
84105

106+
### Advanced configuration
107+
108+
The Azure Monitor Distro includes .NET OpenTelemetry instrumentation for [ASP.NET Core](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNetCore/), [HttpClient](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.Http/), and [SQLClient](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.SqlClient). However, you can customize the instrumentation included or use additional instrumentation on your own using the OpenTelemetry API. Here are some examples of how to customize the instrumentation:
109+
110+
#### Customizing AspNetCoreInstrumentationOptions
111+
112+
```C#
113+
builder.Services.AddOpenTelemetry().UseAzureMonitor();
114+
builder.Services.Configure<AspNetCoreInstrumentationOptions>(options =>
115+
{
116+
options.RecordException = true;
117+
options.Filter = (httpContext) =>
118+
{
119+
// only collect telemetry about HTTP GET requests
120+
return HttpMethods.IsGet(httpContext.Request.Method);
121+
};
122+
});
123+
```
124+
125+
#### Customizing HttpClientInstrumentationOptions
126+
127+
```C#
128+
builder.Services.AddOpenTelemetry().UseAzureMonitor();
129+
builder.Services.Configure<HttpClientInstrumentationOptions>(options =>
130+
{
131+
options.RecordException = true;
132+
options.FilterHttpRequestMessage = (httpRequestMessage) =>
133+
{
134+
// only collect telemetry about HTTP GET requests
135+
return HttpMethods.IsGet(httpRequestMessage.Method.Method);
136+
};
137+
});
138+
```
139+
140+
#### Customizing SqlClientInstrumentationOptions
141+
142+
```C#
143+
builder.Services.AddOpenTelemetry().UseAzureMonitor();
144+
builder.Services.Configure<SqlClientInstrumentationOptions>(options =>
145+
{
146+
options.SetDbStatementForStoredProcedure = false;
147+
});
148+
```
149+
150+
#### Customizing Sampling Percentage
151+
152+
When using the Azure Monitor Distro, the sampling percentage for telemetry data is set to 100% (1.0F) by default. For example, let's say you want to set the sampling percentage to 90%. You can achieve this by modifying the code as follows:
153+
154+
``` C#
155+
builder.Services.AddOpenTelemetry().UseAzureMonitor();
156+
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.SetSampler(new ApplicationInsightsSampler(0.9F)));
157+
```
158+
159+
#### Adding Custom ActivitySource to Traces
160+
161+
```C#
162+
builder.Services.AddOpenTelemetry().UseAzureMonitor();
163+
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.AddSource("MyCompany.MyProduct.MyLibrary"));
164+
```
165+
166+
#### Adding Custom Meter to Metrics
167+
168+
```C#
169+
builder.Services.AddOpenTelemetry().UseAzureMonitor();
170+
builder.Services.ConfigureOpenTelemetryMeterProvider((sp, builder) => builder.AddMeter("MyCompany.MyProduct.MyLibrary"));
171+
```
172+
173+
#### Adding Additional Instrumentation
174+
175+
If you need to instrument a library or framework that isn't included in the Azure Monitor Distro, you can add additional instrumentation using the OpenTelemetry Instrumentation packages. For example, to add instrumentation for gRPC clients, you can add the [OpenTelemetry.Instrumentation.GrpcNetClient](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.GrpcNetClient/) package and use the following code:
176+
177+
```C#
178+
builder.Services.AddOpenTelemetry().UseAzureMonitor();
179+
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.AddGrpcClientInstrumentation());
180+
```
181+
182+
#### Adding Another Exporter
183+
184+
Azure Monitor Distro uses the Azure Monitor exporter to send data to Application Insights. However, if you need to send data to other services, including Application Insights, you can add another exporter. For example, to add the Console exporter, you can install the [OpenTelemetry.Exporter.Console](https://www.nuget.org/packages/OpenTelemetry.Exporter.Console) package and use the following code:
185+
186+
```C#
187+
builder.Services.AddOpenTelemetry().UseAzureMonitor();
188+
builder.Services.ConfigureOpenTelemetryMeterProvider((sp, builder) => builder.AddConsoleExporter());
189+
```
190+
191+
#### Adding Custom Resource
192+
193+
To modify the resource, use the following code.
194+
195+
```C#
196+
builder.Services.AddOpenTelemetry().UseAzureMonitor();
197+
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.ConfigureResource(resourceBuilder => resourceBuilder.AddService("service-name")));
198+
```
199+
200+
It is also possible to configure the `Resource` by using following
201+
environmental variables:
202+
203+
| Environment variable | Description |
204+
| -------------------------- | -------------------------------------------------- |
205+
| `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. |
206+
| `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. |
207+
208+
85209
## Key concepts
86210

87211
The Azure Monitor Distro is a distribution package which enables users to send telemetry data to Azure Monitor. It includes the .NET OpenTelemetry SDK and instrumentation libraries for [ASP.NET Core](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNetCore/), [HttpClient](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.Http/), and [SQLClient](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.SqlClient).

0 commit comments

Comments
 (0)