編輯

共用方式為


Enable Azure Monitor OpenTelemetry for .NET, Node.js, Python, and Java applications

This article describes how to enable and configure OpenTelemetry-based data collection within Application Insights. The Azure Monitor OpenTelemetry Distro:

  • Provides an OpenTelemetry distribution which includes support for features specific to Azure Monitor.
  • Enables automatic telemetry by including OpenTelemetry instrumentation libraries for collecting traces, metrics, logs, and exceptions.
  • Allows collecting custom telemetry.
  • Supports Live Metrics to monitor and collect more telemetry from live, in-production web applications.

For more information about the advantages of using the Azure Monitor OpenTelemetry Distro, see Why should I use the Azure Monitor OpenTelemetry Distro.

To learn more about collecting data using OpenTelemetry, check out Data Collection Basics or the OpenTelemetry FAQ.

OpenTelemetry release status

OpenTelemetry offerings are available for .NET, Node.js, Python, and Java applications. For a feature-by-feature release status, see the FAQ.

Enable OpenTelemetry with Application Insights

Follow the steps in this section to instrument your application with OpenTelemetry. Select a tab for langauge-specific instructions.

Note

.NET covers multiple scenarios, including classic ASP.NET, console apps, Windows Forms (WinForms), and more.

Prerequisites

Tip

If you're migrating from the Application Insights Classic API, see our migration documentation.

Install the client library

Install the latest Azure.Monitor.OpenTelemetry.AspNetCore NuGet package:

dotnet add package Azure.Monitor.OpenTelemetry.AspNetCore 

Modify your application

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();

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:

  1. Go to the Overview pane of your Application Insights resource.
  2. Find your connection string.
  3. Hover over the connection string and select the Copy to clipboard icon.

Screenshot that shows Application Insights overview and connection string.

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.

    APPLICATIONINSIGHTS_CONNECTION_STRING=<Your connection string>
    
  • Set via configuration file - Java only

    Create a configuration file named applicationinsights.json, and place it in the same directory as applicationinsights-agent-3.5.4.jar with the following content:

    {
      "connectionString": "<Your connection string>"
    }
    

    Replace <Your connection string> in the preceding JSON with your unique connection string.

  • Set via code - ASP.NET Core, Node.js, and Python only

    See connection string configuration for an example of setting connection string via code.

Note

If you set the connection string in multiple places, the environment variable will be prioritized in the following order:

  1. Code
  2. Environment variable
  3. 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.

Screenshot of the Application Insights Overview tab with server requests and server response time highlighted.

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:

Next steps

Frequently asked questions

This section provides answers to common questions.

What is OpenTelemetry?

It's a new open-source standard for observability. Learn more at OpenTelemetry.

Why is Microsoft Azure Monitor investing in OpenTelemetry?

Microsoft is investing in OpenTelemetry for the following reasons:

  • It's vendor-neutral and provides consistent APIs/SDKs across languages.
  • Over time, we believe OpenTelemetry will enable Azure Monitor customers to observe applications written in languages beyond our supported languages.
  • It expands the types of data you can collect through a rich set of instrumentation libraries.
  • OpenTelemetry Software Development Kits (SDKs) tend to be more performant at scale than their predecessors, the Application Insights SDKs.
  • OpenTelemetry aligns with Microsoft's strategy to embrace open source.

What's the status of OpenTelemetry?

See OpenTelemetry Status.

What is the Azure Monitor OpenTelemetry Distro?

You can think of it as a thin wrapper that bundles together all the OpenTelemetry components for a first-class experience on Azure. This wrapper is also called a distribution in OpenTelemetry.

Why should I use the Azure Monitor OpenTelemetry Distro?

There are several advantages to using the Azure Monitor OpenTelemetry Distro over native OpenTelemetry from the community:

In the spirit of OpenTelemetry, we designed the distro to be open and extensible. For example, you can add:

  • An OpenTelemetry Protocol (OTLP) exporter and send to a second destination simultaneously
  • Other instrumentation libraries not included in the distro

Because the Distro provides an OpenTelemetry distribution, the Distro supports anything supported by OpenTelemetry. For example, you can add more telemetry processors, exporters, or instrumentation libraries, if OpenTelemetry supports them.

Note

The Distro sets the sampler to a custom, fixed-rate sampler for Application Insights. You can change this to a different sampler, but doing so might disable some of the Distro's included capabilities. For more information about the supported sampler, see the Enable Sampling section of Configure Azure Monitor OpenTelemetry.

For languages without a supported standalone OpenTelemetry exporter, the Azure Monitor OpenTelemetry Distro is the only currently supported way to use OpenTelemetry with Azure Monitor. For languages with a supported standalone OpenTelemetry exporter, you have the option of using either the Azure Monitor OpenTelemetry Distro or the appropriate standalone OpenTelemetry exporter depending on your telemetry scenario. For more information, see When should I use the Azure Monitor OpenTelemetry exporter?.

How can I test out the Azure Monitor OpenTelemetry Distro?

Check out our enablement docs for .NET, Java, JavaScript (Node.js), and Python.

Should I use OpenTelemetry or the Application Insights SDK?

We recommend using the OpenTelemetry Distro unless you require a feature that is only available with formal support in the Application Insights SDK.

Adopting OpenTelemetry now prevents having to migrate at a later date.

When should I use the Azure Monitor OpenTelemetry exporter?

For ASP.NET Core, Java, Node.js, and Python, we recommend using the Azure Monitor OpenTelemetry Distro. It's one line of code to get started.

For all other .NET scenarios, including classic ASP.NET, console apps, Windows Forms (WinForms), etc., we recommend using the .NET Azure Monitor OpenTelemetry exporter: Azure.Monitor.OpenTelemetry.Exporter.

For more complex Python telemetry scenarios that require advanced configuration, we recommend using the Python Azure Monitor OpenTelemetry Exporter.

What's the current release state of features within the Azure Monitor OpenTelemetry Distro?

The following chart breaks out OpenTelemetry feature support for each language.

Feature .NET Node.js Python Java
Distributed tracing
Custom metrics
Standard metrics
Fixed-rate sampling
Offline storage and automatic retries
Exception reporting
Logs collection ⚠️
Custom Events ⚠️ ⚠️ ⚠️
Microsoft Entra authentication
Live metrics
Live Metrics Filtering
Detect Resource Context for VM/VMSS and App Service
Detect Resource Context for Azure Kubernetes Service (AKS) and Functions
Availability Testing Events generated using the Track Availability API
Filter requests, dependencies, logs, and exceptions by anonymous user ID and synthetic source
Filter dependencies, logs, and exceptions by operation name
Adaptive sampling
Profiler ⚠️
Snapshot Debugger

Key

Can OpenTelemetry be used for web browsers?

Yes, but we don't recommend it and Azure doesn't support it. OpenTelemetry JavaScript is heavily optimized for Node.js. Instead, we recommend using the Application Insights JavaScript SDK.

When can we expect the OpenTelemetry SDK to be available for use in web browsers?

The OpenTelemetry web SDK doesn't have a determined availability timeline. We're likely several years away from a browser SDK that is a viable alternative to the Application Insights JavaScript SDK.

Can I test OpenTelemetry in a web browser today?

The OpenTelemetry web sandbox is a fork designed to make OpenTelemetry work in a browser. It's not yet possible to send telemetry to Application Insights. The SDK doesn't define general client events.

Is running Application Insights alongside competitor agents like AppDynamics, DataDog, and NewRelic supported?

This practice isn't something we plan to test or support, although our Distros allow you to export to an OTLP endpoint alongside Azure Monitor simultaneously.

Can I use preview features in production environments?

We don't recommend it. See Supplemental terms of use for Microsoft Azure previews.

What's the difference between manual and automatic instrumentation?

See the OpenTelemetry Overview.

Can I use the OpenTelemetry Collector?

Some customers use the OpenTelemetry Collector as an agent alternative, even though Microsoft doesn't officially support an agent-based approach for application monitoring yet. In the meantime, the open-source community contributed an OpenTelemetry Collector Azure Monitor Exporter that some customers are using to send data to Azure Monitor Application Insights. This is not supported by Microsoft.

What's the difference between OpenCensus and OpenTelemetry?

OpenCensus is the precursor to OpenTelemetry. Microsoft helped bring together OpenTracing and OpenCensus to create OpenTelemetry, a single observability standard for the world. The current production-recommended Python SDK for Azure Monitor is based on OpenCensus. Microsoft is committed to making Azure Monitor based on OpenTelemetry.

In Grafana, why do I see Status: 500. Can't visualize trace events using the trace visualizer?

You could be trying to visualize raw text logs rather than OpenTelemetry traces.

In Application Insights, the 'Traces' table stores raw text logs for diagnostic purposes. They aid in identifying and correlating traces associated with user requests, other events, and exception reports. However, the 'Traces' table doesn't directly contribute to the end-to-end transaction view (waterfall chart) in visualization tools like Grafana.

With the growing adoption of cloud-native practices, there's an evolution in telemetry collection and terminology. OpenTelemetry became a standard for collecting and instrumenting telemetry data. In this context, the term 'Traces' took on a new meaning. Rather than raw logs, 'Traces' in OpenTelemetry refer to a richer, structured form of telemetry that includes spans, which represent individual units of work. These spans are crucial for constructing detailed transaction views, enabling better monitoring and diagnostics of cloud-native applications.

Troubleshooting

Step 1: Enable diagnostic logging

The Azure Monitor Exporter uses EventSource for its internal logging. The exporter logs are available to any EventListener by opting in to the source that's named OpenTelemetry-AzureMonitor-Exporter. For troubleshooting steps, see OpenTelemetry Troubleshooting on GitHub.

Step 2: Test connectivity between your application host and the ingestion service

Application Insights SDKs and agents send telemetry to get ingested as REST calls at our ingestion endpoints. To test connectivity from your web server or application host computer to the ingestion service endpoints, use cURL commands or raw REST requests from PowerShell. For more information, see Troubleshoot missing application telemetry in Azure Monitor Application Insights.

Known issues

The following items are known issues for the Azure Monitor OpenTelemetry Exporters:

  • The operation name is missing from dependency telemetry. The missing operation name causes failures and adversely affects performance tab experience.

  • The device model is missing from request and dependency telemetry. The missing device model adversely affects device cohort analysis.

Support

Select a tab for the language of your choice to discover support options.

OpenTelemetry feedback

To provide feedback: