|
1 | 1 | # Azure Monitor Distro client library for .NET |
2 | 2 |
|
| 3 | +The Azure Monitor Distro is a client library that sends telemetry data to Azure Monitor following the OpenTelemetry Specification. This library can be used to instrument your ASP.NET Core applications to collect and send telemetry data to Azure Monitor for analysis and monitoring, powering experiences in Application Insights. |
| 4 | + |
3 | 5 | ## Getting started |
4 | 6 |
|
5 | 7 | ### Prerequisites |
6 | 8 |
|
| 9 | +- **Azure Subscription:** To use Azure services, including Azure Monitor Distro, you'll need a subscription. If you do not have an existing Azure account, you may sign up for a [free trial](https://azure.microsoft.com/free/dotnet/) or use your [Visual Studio Subscription](https://visualstudio.microsoft.com/subscriptions/) benefits when you [create an account](https://azure.microsoft.com/account). |
| 10 | +- **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). |
| 11 | +- **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. |
| 12 | + |
7 | 13 | ### Install the package |
8 | 14 |
|
| 15 | +Install the Azure Monitor Distro for .NET from [NuGet](https://www.nuget.org/): |
| 16 | + |
| 17 | +```dotnetcli |
| 18 | +dotnet add package Azure.Monitor.OpenTelemetry --prerelease |
| 19 | +``` |
| 20 | + |
| 21 | +#### Nightly builds |
| 22 | + |
| 23 | +Nightly builds are available from this repo's [dev feed](https://github.com/Azure/azure-sdk-for-net/blob/main/CONTRIBUTING.md#nuget-package-dev-feed). |
| 24 | +These are provided without support and are not intended for production workloads. |
| 25 | + |
| 26 | +### Enabling Azure Monitor OpenTelemetry in your application |
| 27 | + |
| 28 | +The following examples demonstrate how to integrate the Azure Monitor Distro into your application. |
| 29 | + |
| 30 | +#### Example 1 |
| 31 | + |
| 32 | +To enable Azure Monitor Distro, add `AddAzureMonitor()` to your `Program.cs` file and set the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable to the connection string from your Application Insights resource. |
| 33 | + |
| 34 | +```C# |
| 35 | +// This method gets called by the runtime. Use this method to add services to the container. |
| 36 | +var builder = WebApplication.CreateBuilder(args); |
| 37 | + |
| 38 | +// The following line enables Azure Monitor Distro. |
| 39 | +builder.Services.AddAzureMonitor(); |
| 40 | + |
| 41 | +// This code adds other services for your application. |
| 42 | +builder.Services.AddMvc(); |
| 43 | + |
| 44 | +var app = builder.Build(); |
| 45 | +``` |
| 46 | + |
| 47 | +#### Example 2 |
| 48 | + |
| 49 | +To enable Azure Monitor Distro with a hard-coded connection string, add `AddAzureMonitor()` to your `Program.cs` with the `AzureMonitorOptions` containing the connection string. |
| 50 | + |
| 51 | +```C# |
| 52 | +// This method gets called by the runtime. Use this method to add services to the container. |
| 53 | +var builder = WebApplication.CreateBuilder(args); |
| 54 | + |
| 55 | +// The following line enables Azure Monitor Distro with hard-coded connection string. |
| 56 | +builder.Services.AddAzureMonitor(o => o.ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000"); |
| 57 | + |
| 58 | +// This code adds other services for your application. |
| 59 | +builder.Services.AddMvc(); |
| 60 | + |
| 61 | +var app = builder.Build(); |
| 62 | +``` |
| 63 | + |
| 64 | +Note that in the examples above, `AddAzureMonitor` is added to the `IServiceCollection` in the `Program.cs` file. You can also add it in the `ConfigureServices` method of your `Startup.cs` file. |
| 65 | + |
9 | 66 | ### Authenticate the client |
10 | 67 |
|
| 68 | +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][identity], which provides support for authenticating Azure SDK clients with their corresponding Azure services. |
| 69 | + |
| 70 | +```C# |
| 71 | +// Call AddAzureMonitor and set Credential to authenticate through Active Directory. |
| 72 | +builder.Services.AddAzureMonitor(o => |
| 73 | +{ |
| 74 | + o.ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000"; |
| 75 | + o.Credential = new DefaultAzureCredential(); |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +With this configuration, the Azure Monitor Distro will use the credentials of the currently logged-in user or of the service principal to authenticate and send telemetry data to Azure Monitor. |
| 80 | + |
| 81 | +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. |
| 82 | + |
11 | 83 | ## Key concepts |
12 | 84 |
|
| 85 | +The Azure Monitor Distro uses instrumentation libraries from the .NET OpenTelemetry SDK to provide a convenient and easy way to export telemetry data to Azure Monitor. |
| 86 | + |
13 | 87 | ## Examples |
14 | 88 |
|
| 89 | +Refer to [`Program.cs`](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/monitor/Azure.Monitor.OpenTelemetry/tests/Azure.Monitor.OpenTelemetry.Demo/Program.cs) for a complete demo. |
| 90 | + |
15 | 91 | ## Troubleshooting |
16 | 92 |
|
| 93 | +The Azure Monitor Distro uses EventSource for its own internal logging. The logs are available to any EventListener by opting into the source named "OpenTelemetry-AzureMonitor-Exporter". |
| 94 | + |
| 95 | +OpenTelemetry also provides it's own [self-diagnostics feature](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry/README.md#troubleshooting) to collect internal logs. |
| 96 | +An example of this is available in our demo project [here](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Demo/OTEL_DIAGNOSTICS.json). |
| 97 | + |
| 98 | + |
17 | 99 | ## Next steps |
18 | 100 |
|
19 | 101 | For more information on Azure SDK, please refer to [this website](https://azure.github.io/azure-sdk/) |
|
0 commit comments