Den här artikeln beskriver konfigurationsinställningar för Azure Monitor OpenTelemetry-distributionen.
Connection string
En anslutningssträng i Application Insights definierar målplatsen för att skicka telemetridata, vilket säkerställer att den når rätt resurs för övervakning och analys.
Använd något av följande tre sätt att konfigurera anslutningssträng:
Lägg till UseAzureMonitor() i startprogrammet i din program.cs klass.
// Create a new ASP.NET Core web application builder.
var builder = WebApplication.CreateBuilder(args);
// Add the OpenTelemetry telemetry service to the application.
// This service will collect and send telemetry data to Azure Monitor.
builder.Services.AddOpenTelemetry().UseAzureMonitor(options => {
options.ConnectionString = "<Your Connection String>";
});
// Build the ASP.NET Core web application.
var app = builder.Build();
// Start the ASP.NET Core web application.
app.Run();
Om du anger anslutningssträng på fler än en plats följer vi följande prioritet:
Kod
Miljövariabel
Konfigurationsfil
Använd något av följande två sätt att konfigurera anslutningssträng:
Lägg till Azure Monitor Exporter till varje OpenTelemetry-signal i programstarten.
// Create a new OpenTelemetry tracer provider.
// It is important to keep the TracerProvider instance active throughout the process lifetime.
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAzureMonitorTraceExporter(options =>
{
options.ConnectionString = "<Your Connection String>";
});
// Create a new OpenTelemetry meter provider.
// It is important to keep the MetricsProvider instance active throughout the process lifetime.
var metricsProvider = Sdk.CreateMeterProviderBuilder()
.AddAzureMonitorMetricExporter(options =>
{
options.ConnectionString = "<Your Connection String>";
});
// Create a new logger factory.
// It is important to keep the LoggerFactory instance active throughout the process lifetime.
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.AddAzureMonitorLogExporter(options =>
{
options.ConnectionString = "<Your Connection String>";
});
});
});
// Import the useAzureMonitor function and the AzureMonitorOpenTelemetryOptions class from the @azure/monitor-opentelemetry package.
const { useAzureMonitor, AzureMonitorOpenTelemetryOptions } = require("@azure/monitor-opentelemetry");
// Create a new AzureMonitorOpenTelemetryOptions object.
const options: AzureMonitorOpenTelemetryOptions = {
azureMonitorExporterOptions: {
connectionString: "<your connection string>"
}
};
// Enable Azure Monitor integration using the useAzureMonitor function and the AzureMonitorOpenTelemetryOptions object.
useAzureMonitor(options);
Använd något av följande två sätt att konfigurera anslutningssträng:
# 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 specified connection string.
# Replace `<your-connection-string>` with the connection string of your Azure Monitor Application Insights resource.
configure_azure_monitor(
connection_string="<your-connection-string>",
)
Ange namnet på molnrollen och molnrollinstansen
För språk som stöds identifierar Azure Monitor OpenTelemetry Distro automatiskt resurskontexten och tillhandahåller standardvärden för egenskaperna Cloud Role Name och Cloud Role Instance för din komponent. Men du kanske vill åsidosätta standardvärdena till något som passar ditt team. Värdet för molnrollens namn visas på programkartan som namnet under en nod.
Ange namnet på molnrollen och molnrollinstansen via resursattribut . Namnet på molnrollen använder service.namespace och service.name attribut, även om det återgår till service.name om service.namespace det inte har angetts. Cloud Role Instance använder service.instance.id attributvärdet. Information om standardattribut för resurser finns i OpenTelemetry Semantiska konventioner.
// Setting role name and role instance
// Create a dictionary of resource attributes.
var resourceAttributes = new Dictionary<string, object> {
{ "service.name", "my-service" },
{ "service.namespace", "my-namespace" },
{ "service.instance.id", "my-instance" }};
// Create a new ASP.NET Core web application builder.
var builder = WebApplication.CreateBuilder(args);
// Add the OpenTelemetry telemetry service to the application.
// This service will collect and send telemetry data to Azure Monitor.
builder.Services.AddOpenTelemetry().UseAzureMonitor();
// Configure the OpenTelemetry tracer provider to add the resource attributes to all traces.
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) =>
builder.ConfigureResource(resourceBuilder =>
resourceBuilder.AddAttributes(resourceAttributes)));
// Build the ASP.NET Core web application.
var app = builder.Build();
// Start the ASP.NET Core web application.
app.Run();
Ange namnet på molnrollen och molnrollinstansen via resursattribut . Namnet på molnrollen använder service.namespace och service.name attribut, även om det återgår till service.name om service.namespace det inte har angetts. Cloud Role Instance använder service.instance.id attributvärdet. Information om standardattribut för resurser finns i OpenTelemetry Semantiska konventioner.
// Setting role name and role instance
// Create a dictionary of resource attributes.
var resourceAttributes = new Dictionary<string, object> {
{ "service.name", "my-service" },
{ "service.namespace", "my-namespace" },
{ "service.instance.id", "my-instance" }};
// Create a resource builder.
var resourceBuilder = ResourceBuilder.CreateDefault().AddAttributes(resourceAttributes);
// Create a new OpenTelemetry tracer provider and set the resource builder.
// It is important to keep the TracerProvider instance active throughout the process lifetime.
var tracerProvider = Sdk.CreateTracerProviderBuilder()
// Set ResourceBuilder on the TracerProvider.
.SetResourceBuilder(resourceBuilder)
.AddAzureMonitorTraceExporter();
// Create a new OpenTelemetry meter provider and set the resource builder.
// It is important to keep the MetricsProvider instance active throughout the process lifetime.
var metricsProvider = Sdk.CreateMeterProviderBuilder()
// Set ResourceBuilder on the MeterProvider.
.SetResourceBuilder(resourceBuilder)
.AddAzureMonitorMetricExporter();
// Create a new logger factory and add the OpenTelemetry logger provider with the resource builder.
// It is important to keep the LoggerFactory instance active throughout the process lifetime.
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
// Set ResourceBuilder on the Logging config.
options.SetResourceBuilder(resourceBuilder);
options.AddAzureMonitorLogExporter();
});
});
Information om hur du anger molnrollinstansen finns i instansen av molnrollen.
Ange namnet på molnrollen och molnrollinstansen via resursattribut . Namnet på molnrollen använder service.namespace och service.name attribut, även om det återgår till service.name om service.namespace det inte har angetts. Cloud Role Instance använder service.instance.id attributvärdet. Information om standardattribut för resurser finns i OpenTelemetry Semantiska konventioner.
// Import the useAzureMonitor function, the AzureMonitorOpenTelemetryOptions class, the Resource class, and the SemanticResourceAttributes class from the @azure/monitor-opentelemetry, @opentelemetry/resources, and @opentelemetry/semantic-conventions packages, respectively.
const { useAzureMonitor, AzureMonitorOpenTelemetryOptions } = require("@azure/monitor-opentelemetry");
const { Resource } = require("@opentelemetry/resources");
const { SemanticResourceAttributes } = require("@opentelemetry/semantic-conventions");
// Create a new Resource object with the following custom resource attributes:
//
// * service_name: my-service
// * service_namespace: my-namespace
// * service_instance_id: my-instance
const customResource = new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: "my-service",
[SemanticResourceAttributes.SERVICE_NAMESPACE]: "my-namespace",
[SemanticResourceAttributes.SERVICE_INSTANCE_ID]: "my-instance",
});
// Create a new AzureMonitorOpenTelemetryOptions object and set the resource property to the customResource object.
const options: AzureMonitorOpenTelemetryOptions = {
resource: customResource
};
// Enable Azure Monitor integration using the useAzureMonitor function and the AzureMonitorOpenTelemetryOptions object.
useAzureMonitor(options);
Ange namnet på molnrollen och molnrollinstansen via resursattribut . Namnet på molnrollen använder service.namespace och service.name attribut, även om det återgår till service.name om service.namespace det inte har angetts. Cloud Role Instance använder service.instance.id attributvärdet. Information om standardattribut för resurser finns i OpenTelemetry Semantiska konventioner.
Ange resursattribut med hjälp av variablerna OTEL_RESOURCE_ATTRIBUTES och/eller OTEL_SERVICE_NAME miljön. OTEL_RESOURCE_ATTRIBUTES tar serie med kommaavgränsade nyckel/värde-par. Om du till exempel vill ange namnet på molnrollen till my-namespace.my-helloworld-service och ange Cloud Role Instance till my-instancekan du ange OTEL_RESOURCE_ATTRIBUTES och OTEL_SERVICE_NAME så här:
Om du inte anger resursattributet service.namespace kan du också ange namnet på molnrollen med endast miljövariabeln OTEL_SERVICE_NAME eller resursattributet service.name . Om du till exempel vill ange namnet på molnrollen till my-helloworld-service och ange Cloud Role Instance till my-instancekan du ange OTEL_RESOURCE_ATTRIBUTES och OTEL_SERVICE_NAME så här:
Du kanske vill aktivera sampling för att minska datainmatningsvolymen, vilket minskar kostnaden. Azure Monitor tillhandahåller ett anpassat exempel på fast hastighet som fyller händelser med ett samplingsförhållande som Application Insights konverterar till ItemCount. Sampeln med fast hastighet säkerställer korrekta upplevelser och händelseantal. Exempelprogrammet är utformat för att bevara dina spårningar mellan tjänster, och det kan användas tillsammans med äldre Application Insights-SDK:er. Mer information finns i Läs mer om sampling.
Provtagaren förväntar sig en exempelfrekvens på mellan 0 och 1 inklusive. En hastighet på 0,1 innebär att cirka 10 % av dina spårningar skickas.
// Create a new ASP.NET Core web application builder.
var builder = WebApplication.CreateBuilder(args);
// Add the OpenTelemetry telemetry service to the application.
// This service will collect and send telemetry data to Azure Monitor.
builder.Services.AddOpenTelemetry().UseAzureMonitor(o =>
{
// Set the sampling ratio to 10%. This means that 10% of all traces will be sampled and sent to Azure Monitor.
o.SamplingRatio = 0.1F;
});
// Build the ASP.NET Core web application.
var app = builder.Build();
// Start the ASP.NET Core web application.
app.Run();
Provtagaren förväntar sig en exempelfrekvens på mellan 0 och 1 inklusive. En hastighet på 0,1 innebär att cirka 10 % av dina spårningar skickas.
// Create a new OpenTelemetry tracer provider.
// It is important to keep the TracerProvider instance active throughout the process lifetime.
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAzureMonitorTraceExporter(options =>
{
// Set the sampling ratio to 10%. This means that 10% of all traces will be sampled and sent to Azure Monitor.
options.SamplingRatio = 0.1F;
});
Från och med 3.4.0 är hastighetsbegränsad sampling tillgänglig och är nu standard. Mer information om sampling finns i Java-sampling.
Provtagaren förväntar sig en exempelfrekvens på mellan 0 och 1 inklusive. En hastighet på 0,1 innebär att cirka 10 % av dina spårningar skickas.
// Import the useAzureMonitor function and the AzureMonitorOpenTelemetryOptions class from the @azure/monitor-opentelemetry package.
const { useAzureMonitor, AzureMonitorOpenTelemetryOptions } = require("@azure/monitor-opentelemetry");
// Create a new AzureMonitorOpenTelemetryOptions object and set the samplingRatio property to 0.1.
const options: AzureMonitorOpenTelemetryOptions = {
samplingRatio: 0.1
};
// Enable Azure Monitor integration using the useAzureMonitor function and the AzureMonitorOpenTelemetryOptions object.
useAzureMonitor(options);
Funktionen configure_azure_monitor() använder automatiskt ApplicationInsightsSampler för kompatibilitet med Application Insights SDK:er och för att prova din telemetri. Miljövariabeln OTEL_TRACES_SAMPLER_ARG kan användas för att ange samplingsfrekvensen, med ett giltigt intervall på 0 till 1, där 0 är 0 % och 1 är 100 %.
Till exempel innebär värdet 0,1 att 10 % av dina spårningar skickas.
export OTEL_TRACES_SAMPLER_ARG=0.1
Dricks
När du använder sampling med fast hastighet/procent och du inte är säker på vad du ska ange samplingsfrekvensen som börjar du på 5 % (dvs. 0,05 samplingsförhållande) och justerar frekvensen baserat på noggrannheten i de åtgärder som visas i fel- och prestandabladen. En högre frekvens resulterar vanligtvis i högre noggrannhet. All sampling påverkar dock noggrannheten, så vi rekommenderar aviseringar om OpenTelemetry-mått, som inte påverkas av sampling.
Aktivera autentisering med Microsoft Entra-ID (tidigare Azure AD)
Du kanske vill aktivera Microsoft Entra-autentisering för en säkrare anslutning till Azure, vilket förhindrar att obehörig telemetri matas in i din prenumeration.
// Create a new ASP.NET Core web application builder.
var builder = WebApplication.CreateBuilder(args);
// Add the OpenTelemetry telemetry service to the application.
// This service will collect and send telemetry data to Azure Monitor.
builder.Services.AddOpenTelemetry().UseAzureMonitor(options => {
// Set the Azure Monitor credential to the DefaultAzureCredential.
// This credential will use the Azure identity of the current user or
// the service principal that the application is running as to authenticate
// to Azure Monitor.
options.Credential = new DefaultAzureCredential();
});
// Build the ASP.NET Core web application.
var app = builder.Build();
// Start the ASP.NET Core web application.
app.Run();
Vi stöder de autentiseringsklasser som tillhandahålls av Azure Identity.
Vi rekommenderar DefaultAzureCredential för lokal utveckling.
Vi rekommenderar ManagedIdentityCredential systemtilldelade och användartilldelade hanterade identiteter.
För systemtilldelade använder du standardkonstruktorn utan parametrar.
För användartilldelad anger du klient-ID:t till konstruktorn.
Vi rekommenderar ClientSecretCredential för tjänstens huvudnamn.
Ange klient-ID, klient-ID och klienthemlighet till konstruktorn.
// Create a DefaultAzureCredential.
var credential = new DefaultAzureCredential();
// Create a new OpenTelemetry tracer provider and set the credential.
// It is important to keep the TracerProvider instance active throughout the process lifetime.
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAzureMonitorTraceExporter(options =>
{
options.Credential = credential;
});
// Create a new OpenTelemetry meter provider and set the credential.
// It is important to keep the MetricsProvider instance active throughout the process lifetime.
var metricsProvider = Sdk.CreateMeterProviderBuilder()
.AddAzureMonitorMetricExporter(options =>
{
options.Credential = credential;
});
// Create a new logger factory and add the OpenTelemetry logger provider with the credential.
// It is important to keep the LoggerFactory instance active throughout the process lifetime.
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.AddAzureMonitorLogExporter(options =>
{
options.Credential = credential;
});
});
});
Mer information om Java finns i tilläggsdokumentationen för Java.
Vi stöder de autentiseringsklasser som tillhandahålls av Azure Identity.
// Import the useAzureMonitor function, the AzureMonitorOpenTelemetryOptions class, and the ManagedIdentityCredential class from the @azure/monitor-opentelemetry and @azure/identity packages, respectively.
const { useAzureMonitor, AzureMonitorOpenTelemetryOptions } = require("@azure/monitor-opentelemetry");
const { ManagedIdentityCredential } = require("@azure/identity");
// Create a new ManagedIdentityCredential object.
const credential = new ManagedIdentityCredential();
// Create a new AzureMonitorOpenTelemetryOptions object and set the credential property to the credential object.
const options: AzureMonitorOpenTelemetryOptions = {
azureMonitorExporterOptions: {
credential: credential
}
};
// Enable Azure Monitor integration using the useAzureMonitor function and the AzureMonitorOpenTelemetryOptions object.
useAzureMonitor(options);
# Import the `ManagedIdentityCredential` class from the `azure.identity` package.
from azure.identity import ManagedIdentityCredential
# 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 a managed identity credential.
# This will allow OpenTelemetry to authenticate to Azure Monitor without requiring you to provide a connection string.
configure_azure_monitor(
credential=ManagedIdentityCredential(),
)
Offlinelagring och automatiska återförsök
För att förbättra tillförlitligheten och motståndskraften skriver Azure Monitor OpenTelemetry-baserade erbjudanden som standard till offline/lokal lagring när ett program förlorar sin anslutning till Application Insights. Programmets telemetri sparas på disken och försöker regelbundet skicka den igen i upp till 48 timmar. I program med hög belastning tas telemetri ibland bort av två skäl. Först när den tillåtna tiden överskrids, och för det andra, när den maximala filstorleken överskrids eller SDK:t inte har möjlighet att rensa filen. Om vi behöver välja sparar produkten nyare händelser jämfört med gamla. Läs mer
Distributionspaketet innehåller AzureMonitorExporter, som som standard använder någon av följande platser för offlinelagring (listad i prioritetsordning):
Windows
%LOCALAPPDATA%\Microsoft\AzureMonitor
%TEMP%\Microsoft\AzureMonitor
Icke-Windows
%TMPDIR%/Microsoft/AzureMonitor
/var/tmp/Microsoft/AzureMonitor
/tmp/Microsoft/AzureMonitor
Om du vill åsidosätta standardkatalogen bör du ange AzureMonitorOptions.StorageDirectory.
// Create a new ASP.NET Core web application builder.
var builder = WebApplication.CreateBuilder(args);
// Add the OpenTelemetry telemetry service to the application.
// This service will collect and send telemetry data to Azure Monitor.
builder.Services.AddOpenTelemetry().UseAzureMonitor(options =>
{
// Set the Azure Monitor storage directory to "C:\\SomeDirectory".
// This is the directory where the OpenTelemetry SDK will store any telemetry data that cannot be sent to Azure Monitor immediately.
options.StorageDirectory = "C:\\SomeDirectory";
});
// Build the ASP.NET Core web application.
var app = builder.Build();
// Start the ASP.NET Core web application.
app.Run();
Om du vill inaktivera den här funktionen bör du ange AzureMonitorOptions.DisableOfflineStorage = true.
Som standard använder AzureMonitorExporter någon av följande platser för offlinelagring (listad i prioritetsordning):
Windows
%LOCALAPPDATA%\Microsoft\AzureMonitor
%TEMP%\Microsoft\AzureMonitor
Icke-Windows
%TMPDIR%/Microsoft/AzureMonitor
/var/tmp/Microsoft/AzureMonitor
/tmp/Microsoft/AzureMonitor
Om du vill åsidosätta standardkatalogen bör du ange AzureMonitorExporterOptions.StorageDirectory.
// Create a new OpenTelemetry tracer provider and set the storage directory.
// It is important to keep the TracerProvider instance active throughout the process lifetime.
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAzureMonitorTraceExporter(options =>
{
// Set the Azure Monitor storage directory to "C:\\SomeDirectory".
// This is the directory where the OpenTelemetry SDK will store any trace data that cannot be sent to Azure Monitor immediately.
options.StorageDirectory = "C:\\SomeDirectory";
});
// Create a new OpenTelemetry meter provider and set the storage directory.
// It is important to keep the MetricsProvider instance active throughout the process lifetime.
var metricsProvider = Sdk.CreateMeterProviderBuilder()
.AddAzureMonitorMetricExporter(options =>
{
// Set the Azure Monitor storage directory to "C:\\SomeDirectory".
// This is the directory where the OpenTelemetry SDK will store any metric data that cannot be sent to Azure Monitor immediately.
options.StorageDirectory = "C:\\SomeDirectory";
});
// Create a new logger factory and add the OpenTelemetry logger provider with the storage directory.
// It is important to keep the LoggerFactory instance active throughout the process lifetime.
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.AddAzureMonitorLogExporter(options =>
{
// Set the Azure Monitor storage directory to "C:\\SomeDirectory".
// This is the directory where the OpenTelemetry SDK will store any log data that cannot be sent to Azure Monitor immediately.
options.StorageDirectory = "C:\\SomeDirectory";
});
});
});
Om du vill inaktivera den här funktionen bör du ange AzureMonitorExporterOptions.DisableOfflineStorage = true.
Det går inte att konfigurera offlinelagring och automatiska återförsök i Java.
Som standard använder AzureMonitorExporter någon av följande platser för offlinelagring.
Windows
%TEMP%\Microsoft\AzureMonitor
Icke-Windows
%TMPDIR%/Microsoft/AzureMonitor
/var/tmp/Microsoft/AzureMonitor
Om du vill åsidosätta standardkatalogen bör du ange storageDirectory.
Till exempel:
// Import the useAzureMonitor function and the AzureMonitorOpenTelemetryOptions class from the @azure/monitor-opentelemetry package.
const { useAzureMonitor, AzureMonitorOpenTelemetryOptions } = require("@azure/monitor-opentelemetry");
// Create a new AzureMonitorOpenTelemetryOptions object and set the azureMonitorExporterOptions property to an object with the following properties:
//
// * connectionString: The connection string for your Azure Monitor Application Insights resource.
// * storageDirectory: The directory where the Azure Monitor OpenTelemetry exporter will store telemetry data when it is offline.
// * disableOfflineStorage: A boolean value that specifies whether to disable offline storage.
const options: AzureMonitorOpenTelemetryOptions = {
azureMonitorExporterOptions: {
connectionString: "<Your Connection String>",
storageDirectory: "C:\\SomeDirectory",
disableOfflineStorage: false
}
};
// Enable Azure Monitor integration using the useAzureMonitor function and the AzureMonitorOpenTelemetryOptions object.
useAzureMonitor(options);
Om du vill inaktivera den här funktionen bör du ange disableOfflineStorage = true.
Som standard använder Azure Monitor-exportörer följande sökväg:
Om du vill åsidosätta standardkatalogen bör du ange storage_directory den katalog som du vill använda.
Till exempel:
...
# Configure OpenTelemetry to use Azure Monitor with the specified connection string and storage directory.
# Replace `your-connection-string` with the connection string to your Azure Monitor Application Insights resource.
# Replace `C:\\SomeDirectory` with the directory where you want to store the telemetry data before it is sent to Azure Monitor.
configure_azure_monitor(
connection_string="your-connection-string",
storage_directory="C:\\SomeDirectory",
)
...
Om du vill inaktivera den här funktionen bör du ange disable_offline_storage till True. Standardvärdet är False.
Till exempel:
...
# Configure OpenTelemetry to use Azure Monitor with the specified connection string and disable offline storage.
# Replace `your-connection-string` with the connection string to your Azure Monitor Application Insights resource.
configure_azure_monitor(
connection_string="your-connection-string",
disable_offline_storage=True,
)
...
Aktivera OTLP-exportören
Du kanske vill aktivera exportör av OpenTelemetry Protocol (OTLP) tillsammans med Azure Monitor Exporter för att skicka telemetrin till två platser.
Kommentar
OTLP-exportören visas endast för enkelhetens skull. Vi har inte officiellt stöd för OTLP-exportören eller komponenter eller tredjepartsupplevelser nedströms.
Lägg till följande kodfragment. Det här exemplet förutsätter att du har en OpenTelemetry Collector med en OTLP-mottagare som körs. Mer information finns i exemplet på GitHub.
// Create a new ASP.NET Core web application builder.
var builder = WebApplication.CreateBuilder(args);
// Add the OpenTelemetry telemetry service to the application.
// This service will collect and send telemetry data to Azure Monitor.
builder.Services.AddOpenTelemetry().UseAzureMonitor();
// Add the OpenTelemetry OTLP exporter to the application.
// This exporter will send telemetry data to an OTLP receiver, such as Prometheus
builder.Services.AddOpenTelemetry().WithTracing(builder => builder.AddOtlpExporter());
builder.Services.AddOpenTelemetry().WithMetrics(builder => builder.AddOtlpExporter());
// Build the ASP.NET Core web application.
var app = builder.Build();
// Start the ASP.NET Core web application.
app.Run();
Lägg till följande kodfragment. Det här exemplet förutsätter att du har en OpenTelemetry Collector med en OTLP-mottagare som körs. Mer information finns i exemplet på GitHub.
// Create a new OpenTelemetry tracer provider and add the Azure Monitor trace exporter and the OTLP trace exporter.
// It is important to keep the TracerProvider instance active throughout the process lifetime.
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAzureMonitorTraceExporter()
.AddOtlpExporter();
// Create a new OpenTelemetry meter provider and add the Azure Monitor metric exporter and the OTLP metric exporter.
// It is important to keep the MetricsProvider instance active throughout the process lifetime.
var metricsProvider = Sdk.CreateMeterProviderBuilder()
.AddAzureMonitorMetricExporter()
.AddOtlpExporter();
Mer information om Java finns i tilläggsdokumentationen för Java.
Lägg till följande kodfragment. Det här exemplet förutsätter att du har en OpenTelemetry Collector med en OTLP-mottagare som körs. Mer information finns i exemplet på GitHub.
// Import the useAzureMonitor function, the AzureMonitorOpenTelemetryOptions class, the trace module, the ProxyTracerProvider class, the BatchSpanProcessor class, the NodeTracerProvider class, and the OTLPTraceExporter class from the @azure/monitor-opentelemetry, @opentelemetry/api, @opentelemetry/sdk-trace-base, @opentelemetry/sdk-trace-node, and @opentelemetry/exporter-trace-otlp-http packages, respectively.
const { useAzureMonitor, AzureMonitorOpenTelemetryOptions } = require("@azure/monitor-opentelemetry");
const { BatchSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
// Create a new OTLPTraceExporter object.
const otlpExporter = new OTLPTraceExporter();
// Enable Azure Monitor integration.
const options: AzureMonitorOpenTelemetryOptions = {
// Add the SpanEnrichingProcessor
spanProcessors: [new BatchSpanProcessor(otlpExporter)]
}
useAzureMonitor(options);
Lägg till följande kodfragment. Det här exemplet förutsätter att du har en OpenTelemetry Collector med en OTLP-mottagare som körs. Mer information finns i denna README.
# Import the `configure_azure_monitor()`, `trace`, `OTLPSpanExporter`, and `BatchSpanProcessor` classes from the appropriate packages.
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import BatchSpanProcessor
# Configure OpenTelemetry to use Azure Monitor with the specified connection string.
# Replace `<your-connection-string>` with the connection string to your Azure Monitor Application Insights resource.
configure_azure_monitor(
connection_string="<your-connection-string>",
)
# Get the tracer for the current module.
tracer = trace.get_tracer(__name__)
# Create an OTLP span exporter that sends spans to the specified endpoint.
# Replace `http://localhost:4317` with the endpoint of your OTLP collector.
otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317")
# Create a batch span processor that uses the OTLP span exporter.
span_processor = BatchSpanProcessor(otlp_exporter)
# Add the batch span processor to the tracer provider.
trace.get_tracer_provider().add_span_processor(span_processor)
# Start a new span with the name "test".
with tracer.start_as_current_span("test"):
print("Hello world!")
OpenTelemetry-konfigurationer
Följande OpenTelemetry-konfigurationer kan nås via miljövariabler när du använder Azure Monitor OpenTelemetry Distros.
Det här avsnittet innehåller svar på vanliga frågor.
Vad är OpenTelemetry?
Det är en ny standard med öppen källkod för observerbarhet. Läs mer på OpenTelemetry.
Varför investerar Microsoft Azure Monitor i OpenTelemetry?
Microsoft är bland de största bidragsgivarna till OpenTelemetry.
De viktigaste värdeförslagen för OpenTelemetry är att det är leverantörsneutralt och ger konsekventa API:er/SDK:er mellan olika språk.
Med tiden tror vi att OpenTelemetry gör det möjligt för Azure Monitor-kunder att observera program som skrivits på språk utöver våra språk som stöds. Den utökar också de typer av data som du kan samla in via en omfattande uppsättning instrumentationsbibliotek. Dessutom tenderar OpenTelemetry Software Development Kits (SDK:er) att vara mer högpresterande i stor skala än sina föregångare, Application Insights SDK:er.
Slutligen överensstämmer OpenTelemetry med Microsofts strategi att ta till sig öppen källkod.
Du kan se det som en tunn omslutning som buntar ihop alla OpenTelemetry-komponenter för en förstklassig upplevelse i Azure. Den här omslutningen kallas även för en distribution i OpenTelemetry.
Varför ska jag använda "Azure Monitor OpenTelemetry Distro"?
Det finns flera fördelar med att använda Azure Monitor OpenTelemetry Distro över inbyggd OpenTelemetry från communityn:
Minskar aktiveringsarbetet
Stöds av Microsoft
Innehåller Azure-specifika funktioner som:
Sampling som är kompatibel med klassiska Application Insights-SDK:er
I OpenTelemetrys anda utformade vi distributionen så att den var öppen och utökningsbar. Du kan till exempel lägga till:
En OpenTelemetry Protocol-exportör (OTLP) och skicka till ett andra mål samtidigt
Andra instrumentationsbibliotek som inte ingår i distributionen
Eftersom Distro tillhandahåller en OpenTelemetry-distribution stöder distributionen allt som stöds av OpenTelemetry. Du kan till exempel lägga till fler telemetriprocessorer, exportörer eller instrumentationsbibliotek om OpenTelemetry stöder dem.
Kommentar
Distributionen anger sampler till en anpassad, fast frekvensexempel för Application Insights. Du kan ändra detta till en annan provtagare, men om du gör det kan du inaktivera några av distributionsfunktionerna.
Mer information om exempelprogrammet som stöds finns i avsnittet Aktivera sampling i Konfigurera Azure Monitor OpenTelemetry.
För språk utan en fristående OpenTelemetry-exportör som stöds är Azure Monitor OpenTelemetry Distro det enda sättet att använda OpenTelemetry med Azure Monitor. För språk med en fristående OpenTelemetry-exportör som stöds kan du använda antingen Azure Monitor OpenTelemetry Distro eller lämplig fristående OpenTelemetry-exportör beroende på ditt telemetriscenario. Mer information finns i När ska jag använda Azure Monitor OpenTelemetry-exportören?.
Hur kan jag testa Azure Monitor OpenTelemetry Distro?
Att använda OpenTelemetry förhindrar nu att du behöver migrera vid ett senare tillfälle.
När ska jag använda Azure Monitor OpenTelemetry-exportören?
För ASP.NET Core, Java, Node.js och Python rekommenderar vi att du använder Azure Monitor OpenTelemetry Distro. Det är en kodrad för att komma igång.
För alla andra .NET-scenarier, inklusive klassiska ASP.NET, konsolappar, Windows Forms (WinForms) osv. rekommenderar vi att du använder .NET Azure Monitor OpenTelemetry-exportören: Azure.Monitor.OpenTelemetry.Exporter.
För mer komplexa Python-telemetriscenarier som kräver avancerad konfiguration rekommenderar vi att du använder Python Azure Monitor OpenTelemetry Exporter.
Vad är det aktuella versionstillståndet för funktioner i Azure Monitor OpenTelemetry Distro?
Följande diagram delar upp funktionsstöd för OpenTelemetry för varje språk.
❌ Den här funktionen är inte tillgänglig eller är inte tillämplig.
Kan OpenTelemetry användas för webbläsare?
Ja, men vi rekommenderar det inte och Azure stöder det inte. OpenTelemetry JavaScript är mycket optimerat för Node.js. I stället rekommenderar vi att du använder Application Insights JavaScript SDK.
När kan vi förvänta oss att OpenTelemetry SDK ska vara tillgängligt för användning i webbläsare?
OpenTelemetry-webb-SDK:t har ingen fastställd tillgänglighetstidslinje. Vi är förmodligen flera år från en webbläsar-SDK som är ett genomförbart alternativ till Application Insights JavaScript SDK.
Kan jag testa OpenTelemetry i en webbläsare idag?
OpenTelemetry-webbsandlådan är en förgrening som utformats för att få OpenTelemetry att fungera i en webbläsare. Det går ännu inte att skicka telemetri till Application Insights. SDK:t definierar inte allmänna klienthändelser.
Stöds application insights tillsammans med konkurrerande agenter som AppDynamics, DataDog och NewRelic?
Den här metoden är inte något som vi planerar att testa eller stödja, även om våra distributioner gör att du kan exportera till en OTLP-slutpunkt tillsammans med Azure Monitor samtidigt.
Kan jag använda förhandsversionsfunktioner i produktionsmiljöer?
Vad är skillnaden mellan manuell och automatisk instrumentering?
Se Översikt över OpenTelemetry.
Kan jag använda OpenTelemetry Collector?
Vissa kunder använder OpenTelemetry Collector som ett agentalternativ, även om Microsoft inte officiellt stöder en agentbaserad metod för programövervakning ännu. Under tiden bidrog communityn med öppen källkod med en OpenTelemetry Collector Azure Monitor-exportör som vissa kunder använder för att skicka data till Azure Monitor Application Insights. Detta stöds inte av Microsoft.
Vad är skillnaden mellan OpenCensus och OpenTelemetry?
OpenCensus är föregångaren till OpenTelemetry. Microsoft hjälpte till att samla OpenTracing och OpenCensus för att skapa OpenTelemetry, en enda observerbarhetsstandard för världen. Den aktuella produktionsrekommenderade Python SDK :t för Azure Monitor baseras på OpenCensus. Microsoft har åtagit sig att göra Azure Monitor baserat på OpenTelemetry.