Enable Azure Monitor OpenTelemetry for .NET, Node.js, Python, and Java applications
Article
This article describes how to enable and configure OpenTelemetry-based data collection within Azure Monitor Application Insights. The Azure Monitor OpenTelemetry Distro:
If you don't rely on any properties listed in the not-supported table, the ApplicationInsights shim will be your easiest path forward once out of beta.
If you rely on any of those properties, proceed with the Azure Monitor OpenTelemetry Distro. We'll provide a migration guide soon.
Tip
If you're migrating from the Application Insights Classic API, see our migration documentation.
If you are upgrading from an earlier 3.x version, you may be impacted by changing defaults or slight differences in the data we collect. For more information, see the migration section in the release notes.
3.5.0,
3.4.0,
3.3.0,
3.2.0, and
3.1.0
Import the Azure.Monitor.OpenTelemetry.AspNetCore namespace, add OpenTelemetry, and configure it to use Azure Monitor in your program.cs class:
// Import the Azure.Monitor.OpenTelemetry.AspNetCore namespace.
using Azure.Monitor.OpenTelemetry.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
// Add OpenTelemetry and configure it to use Azure Monitor.
builder.Services.AddOpenTelemetry().UseAzureMonitor();
var app = builder.Build();
app.Run();
Add the Azure Monitor Exporter to each OpenTelemetry signal in the program.cs class:
// Create a new tracer provider builder and add an Azure Monitor trace exporter to the tracer provider builder.
// It is important to keep the TracerProvider instance active throughout the process lifetime.
// See https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/docs/trace#tracerprovider-management
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAzureMonitorTraceExporter();
// Add an Azure Monitor metric exporter to the metrics provider builder.
// It is important to keep the MetricsProvider instance active throughout the process lifetime.
// See https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/docs/metrics#meterprovider-management
var metricsProvider = Sdk.CreateMeterProviderBuilder()
.AddAzureMonitorMetricExporter();
// Create a new logger factory.
// It is important to keep the LoggerFactory instance active throughout the process lifetime.
// See https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/docs/logs#logger-management
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(logging =>
{
logging.AddAzureMonitorLogExporter();
});
});
Autoinstrumentation is enabled through configuration changes. No code changes are required.
Point the Java virtual machine (JVM) to the jar file by adding -javaagent:"path/to/applicationinsights-agent-3.7.1.jar" to your application's JVM args.
Note
Sampling is enabled by default at a rate of 5 requests per second, aiding in cost management. Telemetry data may be missing in scenarios exceeding this rate. For more information on modifying sampling configuration, see sampling overrides.
Autoinstrumentation is enabled through configuration changes. No code changes are required.
// Import the `useAzureMonitor()` function from the `@azure/monitor-opentelemetry` package.
const { useAzureMonitor } = require("@azure/monitor-opentelemetry");
// Call the `useAzureMonitor()` function to configure OpenTelemetry to use Azure Monitor.
useAzureMonitor();
import logging
# Import the `configure_azure_monitor()` function from the
# `azure.monitor.opentelemetry` package.
from azure.monitor.opentelemetry import configure_azure_monitor
# Configure OpenTelemetry to use Azure Monitor with the
# APPLICATIONINSIGHTS_CONNECTION_STRING environment variable.
configure_azure_monitor(
logger_name="<your_logger_namespace>", # Set the namespace for the logger in which you would like to collect telemetry for if you are collecting logging telemetry. This is imperative so you do not collect logging telemetry from the SDK itself.
)
logger = logging.getLogger("<your_logger_namespace>") # Logging telemetry will be collected from logging calls made with this logger and all of it's children loggers.
Copy the connection string from your Application Insights resource
The connection string is unique and specifies where the Azure Monitor OpenTelemetry Distro sends the telemetry it collects.
Tip
If you don't already have an Application Insights resource, create one following this guide. We recommend you create a new resource rather than using an existing one.
To copy the connection string:
Go to the Overview pane of your Application Insights resource.
Find your connection string.
Hover over the connection string and select the Copy to clipboard icon.
Paste the connection string in your environment
To paste your connection string, select from the following options:
Important
We recommend setting the connection string through code only in local development and test environments.
For production, use an environment variable or configuration file (Java only).
Set via environment variable - recommended
Replace <Your connection string> in the following command with your connection string.
Create a configuration file named applicationinsights.json, and place it in the same directory as applicationinsights-agent-3.7.1.jar with the following content:
If you set the connection string in multiple places, the environment variable will be prioritized in the following order:
Code
Environment variable
Configuration file
Confirm data is flowing
Run your application, then open Application Insights in the Azure portal. It might take a few minutes for data to show up.
Application Insights is now enabled for your application. The following steps are optional and allow for further customization.
Important
If you have two or more services that emit telemetry to the same Application Insights resource, you're required to set Cloud Role Names to represent them properly on the Application Map.
As part of using Application Insights instrumentation, we collect and send diagnostic data to Microsoft. This data helps us run and improve Application Insights. To learn more, see Statsbeat in Azure Application Insights.
Sample applications
Azure Monitor OpenTelemetry sample applications are available for all supported languages: