استخدم إحدى الطرق الثلاث التالية لتكوين سلسلة الاتصال:
إضافة UseAzureMonitor() إلى الملف:program.cs
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>";
});
var app = builder.Build();
app.Run();
إذا قمت بتعيين سلسلة الاتصال في أكثر من مكان واحد، فإننا نلتزم بالأسبقية التالية:
رمز
متغير بيئة
ملف التكوين
استخدم إحدى الطريقتين التاليتين لتكوين سلسلة الاتصال:
أضف مصدر Azure Monitor إلى كل إشارة OpenTelemetry في بدء تشغيل التطبيق.
// 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>";
})
.Build();
// 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>";
})
.Build();
// 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(logging =>
{
logging.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);
استخدم إحدى الطريقتين التاليتين لتكوين سلسلة الاتصال:
# 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>",
)
تعيين اسم دور مجموعة النظراء ومثيل دور مجموعة النظراء
بالنسبة للغات المدعومة، يكتشف Azure Monitor OpenTelemetry Distro تلقائيا سياق المورد ويوفر القيم الافتراضية لاسم دور السحابة وخصائص مثيل دور السحابة للمكون الخاص بك. ومع ذلك، قد ترغب في تجاوز القيم الافتراضية إلى شيء منطقي لفريقك. تظهر قيمة اسم دور السحابة على خريطة التطبيق كاسم أسفل عقدة.
قم بتعيين اسم دور السحابة ومثيل دور السحابة عبر سمات المورد . يستخدم اسم دور مجموعة النظراء السمات service.namespace وservice.name، على الرغم من أنه يقع مرة أخرى إلى service.name إذا لم يتم تعيين service.namespace. يستخدم مثيل دور مجموعة النظراء service.instance.id قيمة السمة. للحصول على معلومات حول السمات القياسية للموارد، راجع اصطلاحات القياس الدلالي OpenTelemetry.
// 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 ResourceBuilder to add the custom resource attributes to all signals.
// Custom resource attributes should be added AFTER AzureMonitor to override the default ResourceDetectors.
.ConfigureResource(resourceBuilder => resourceBuilder.AddAttributes(_testResourceAttributes));
// Build the ASP.NET Core web application.
var app = builder.Build();
// Start the ASP.NET Core web application.
app.Run();
قم بتعيين اسم دور السحابة ومثيل دور السحابة عبر سمات المورد . يستخدم اسم دور مجموعة النظراء السمات service.namespace وservice.name، على الرغم من أنه يقع مرة أخرى إلى service.name إذا لم يتم تعيين service.namespace. يستخدم مثيل دور مجموعة النظراء service.instance.id قيمة السمة. للحصول على معلومات حول السمات القياسية للموارد، راجع اصطلاحات القياس الدلالي OpenTelemetry.
// 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()
.Build();
// 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()
.Build();
// 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(logging =>
{
// Set ResourceBuilder on the Logging config.
logging.SetResourceBuilder(resourceBuilder);
logging.AddAzureMonitorLogExporter();
});
});
استخدم لتطبيقات الصور الأصلية spring.application.name ل Spring Boot
استخدم لتطبيقات الصور الأصلية quarkus.application.name في Quarkus
قم بتعيين اسم دور السحابة ومثيل دور السحابة عبر سمات المورد . يستخدم اسم دور مجموعة النظراء السمات service.namespace وservice.name، على الرغم من أنه يقع مرة أخرى إلى service.name إذا لم يتم تعيين service.namespace. يستخدم مثيل دور مجموعة النظراء service.instance.id قيمة السمة. للحصول على معلومات حول السمات القياسية للموارد، راجع اصطلاحات القياس الدلالي OpenTelemetry.
// 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);
قم بتعيين اسم دور السحابة ومثيل دور السحابة عبر سمات المورد . يستخدم اسم دور مجموعة النظراء السمات service.namespace وservice.name، على الرغم من أنه يقع مرة أخرى إلى service.name إذا لم يتم تعيين service.namespace. يستخدم مثيل دور مجموعة النظراء service.instance.id قيمة السمة. للحصول على معلومات حول السمات القياسية للموارد، راجع اصطلاحات القياس الدلالي OpenTelemetry.
تعيين سمات الموارد باستخدام OTEL_RESOURCE_ATTRIBUTES و/أو OTEL_SERVICE_NAME متغيرات البيئة. OTEL_RESOURCE_ATTRIBUTES يأخذ سلسلة من أزواج قيم المفاتيح المفصولة بفواصل. على سبيل المثال، لتعيين اسم دور السحابة إلى my-namespace.my-helloworld-service وتعيين مثيل دور السحابة إلى my-instance، يمكنك تعيين OTEL_RESOURCE_ATTRIBUTES وعلى OTEL_SERVICE_NAME هذا النحو:
إذا لم تقم بتعيين سمة service.namespace المورد، يمكنك بدلا من ذلك تعيين اسم دور السحابة مع متغير البيئة OTEL_SERVICE_NAME فقط أو سمة service.name المورد. على سبيل المثال، لتعيين اسم دور السحابة إلى my-helloworld-service وتعيين مثيل دور السحابة إلى my-instance، يمكنك تعيين OTEL_RESOURCE_ATTRIBUTES وعلى OTEL_SERVICE_NAME هذا النحو:
قد ترغب في تمكين أخذ العينات لتقليل حجم استيعاب البيانات، ما يقلل من التكلفة. يوفر Azure Monitor أداة عينة مخصصة ذات معدل ثابت تملأ الأحداث بنسبة أخذ العينات، والتي يحولها Application Insights إلى ItemCount. يضمن أداة أخذ العينات ذات المعدل الثابت تجارب دقيقة وعدد الأحداث. تم تصميم أداة أخذ العينات للحفاظ على آثارك عبر الخدمات، وهي قابلة للتشغيل المتداخل مع مجموعات تطوير برامج Application Insights القديمة (SDKs). لمزيد من المعلومات، راجع معرفة المزيد حول أخذ العينات.
يتوقع العينة معدل عينة يتراوح بين 0 و1 ضمنا. معدل 0.1 يعني أن حوالي 10٪ من آثارك يتم إرسالها.
// 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 sampling ratio to 10%. This means that 10% of all traces will be sampled and sent to Azure Monitor.
options.SamplingRatio = 0.1F;
});
// Build the ASP.NET Core web application.
var app = builder.Build();
// Start the ASP.NET Core web application.
app.Run();
يتوقع العينة معدل عينة يتراوح بين 0 و1 ضمنا. معدل 0.1 يعني أن حوالي 10٪ من آثارك يتم إرسالها.
// 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;
})
.Build();
بدءا من 3.4.0، يتوفر أخذ العينات محدودة المعدل وهو الآن الافتراضي. لمزيد من المعلومات حول أخذ العينات، راجع أخذ عينات Java.
بالنسبة لتطبيقات Spring Boot الأصلية، تنطبق تكوينات أخذ العينات ل OpenTelemetry Java SDK.
بالنسبة لتطبيقات Quarkus الأصلية، راجع وثائق Quarkus OpenTelemetry.
يتوقع العينة معدل عينة يتراوح بين 0 و1 ضمنا. معدل 0.1 يعني أن حوالي 10٪ من آثارك يتم إرسالها.
// 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);
configure_azure_monitor() تستخدم الدالة تلقائيا ApplicationInsightsSampler للتوافق مع Application Insights SDKs ولعينة بيانات تتبع الاستخدام الخاصة بك. OTEL_TRACES_SAMPLER_ARG يمكن استخدام متغير البيئة لتحديد معدل أخذ العينات، مع نطاق صالح من 0 إلى 1، حيث 0 هو 0٪ و1 هو 100٪.
على سبيل المثال، تعني قيمة 0.1 أن 10٪ من آثارك يتم إرسالها.
export OTEL_TRACES_SAMPLER_ARG=0.1
تلميح
عند استخدام أخذ العينات بمعدل ثابت/النسبة المئوية ولست متأكدا مما يجب تعيين معدل أخذ العينات عليه، ابدأ من 5٪. (نسبة أخذ العينات 0.05) اضبط المعدل استنادا إلى دقة العمليات المعروضة في أجزاء الفشل والأداء. ينتج عن ارتفاع المعدل بشكل عام دقة أعلى. ومع ذلك، يؤثر أي أخذ عينات على الدقة لذلك نوصي بالتنبيه على مقاييس القياس المفتوح، والتي لا تتأثر بأخذ العينات.
المقاييس الحية
توفر المقاييس المباشرة لوحة معلومات تحليلات في الوقت الحقيقي للحصول على نظرة ثاقبة حول نشاط التطبيق وأدائه.
للحصول على الشروط القانونية التي تنطبق على ميزات Azure الموجودة في الإصدار التجريبي، أو المعاينة، أو التي لم يتم إصدارها بعد في التوفر العام، راجع شروط الاستخدام التكميلية لمعاينات Microsoft Azure.
يتم تمكين هذه الميزة بشكل افتراضي.
يمكن للمستخدمين تعطيل Live Metrics عند تكوين Distro.
builder.Services.AddOpenTelemetry().UseAzureMonitor(options => {
// Disable the Live Metrics feature.
options.EnableLiveMetrics = false;
});
هذه الميزة غير متوفرة في Azure Monitor .NET Exporter.
لا تتوفر المقاييس المباشرة اليوم لتطبيقات GraalVM الأصلية.
هام
للحصول على الشروط القانونية التي تنطبق على ميزات Azure الموجودة في الإصدار التجريبي، أو المعاينة، أو التي لم يتم إصدارها بعد في التوفر العام، راجع شروط الاستخدام التكميلية لمعاينات Microsoft Azure.
يمكن للمستخدمين تمكين/تعطيل Live Metrics عند تكوين Distro باستخدام الخاصية enableLiveMetrics .
للحصول على الشروط القانونية التي تنطبق على ميزات Azure الموجودة في الإصدار التجريبي، أو المعاينة، أو التي لم يتم إصدارها بعد في التوفر العام، راجع شروط الاستخدام التكميلية لمعاينات Microsoft Azure.
يمكنك تمكين المقاييس المباشرة باستخدام Azure monitor OpenTelemetry Distro ل Python كما يلي:
لتحسين الموثوقية والمرونة، تكتب العروض المستندة إلى Azure Monitor OpenTelemetry إلى التخزين غير المتصل/المحلي بشكل افتراضي عندما يفقد أحد التطبيقات اتصاله ب Application Insights. يقوم بحفظ بيانات تتبع الاستخدام للتطبيق على القرص ويحاول إرساله مرة أخرى بشكل دوري لمدة تصل إلى 48 ساعة. في التطبيقات عالية التحميل، يتم إسقاط بيانات تتبع الاستخدام أحيانا لسببين. أولا، عند تجاوز الوقت المسموح به، وثانيا، عند تجاوز الحد الأقصى لحجم الملف أو عندما لا يكون لدى SDK فرصة لمسح الملف. إذا كنا بحاجة إلى الاختيار، فإن المنتج يحفظ الأحداث الأحدث على الأحداث القديمة. معرفة المزيد
تتضمن حزمة Distro AzureMonitorExporter، الذي يستخدم بشكل افتراضي أحد المواقع التالية للتخزين دون اتصال (مدرج بترتيب الأسبقية):
Windows
٪LOCALAPPDATA٪\Microsoft\AzureMonitor
٪TEMP٪\Microsoft\AzureMonitor
غير Windows
٪TMPDIR٪/Microsoft/AzureMonitor
/var/tmp/Microsoft/AzureMonitor
/tmp/Microsoft/AzureMonitor
لتجاوز الدليل الافتراضي، يجب عليك تعيين 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();
لتعطيل هذه الميزة، يجب تعيين AzureMonitorOptions.DisableOfflineStorage = true.
بشكل افتراضي، يستخدم AzureMonitorExporter أحد المواقع التالية للتخزين دون اتصال (مدرج بترتيب الأسبقية):
Windows
٪LOCALAPPDATA٪\Microsoft\AzureMonitor
٪TEMP٪\Microsoft\AzureMonitor
غير Windows
٪TMPDIR٪/Microsoft/AzureMonitor
/var/tmp/Microsoft/AzureMonitor
/tmp/Microsoft/AzureMonitor
لتجاوز الدليل الافتراضي، يجب عليك تعيين 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";
})
.Build();
// 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";
})
.Build();
// 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(logging =>
{
logging.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";
});
});
});
لتعطيل هذه الميزة، يجب تعيين AzureMonitorExporterOptions.DisableOfflineStorage = true.
لا يتوفر تكوين التخزين دون اتصال وإعادة المحاولة التلقائية في Java.
للحصول على قائمة كاملة بالتكوينات المتوفرة، راجع خيارات التكوين.
لا يتوفر تكوين التخزين دون اتصال وإعادة المحاولة التلقائية في تطبيقات الصور الأصلية ل Java.
بشكل افتراضي، يستخدم AzureMonitorExporter أحد المواقع التالية للتخزين دون اتصال.
Windows
٪TEMP٪\Microsoft\AzureMonitor
غير Windows
٪TMPDIR٪/Microsoft/AzureMonitor
/var/tmp/Microsoft/AzureMonitor
لتجاوز الدليل الافتراضي، يجب عليك تعيين storageDirectory.
على سبيل المثال:
// 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);
لتعطيل هذه الميزة، يجب تعيين disableOfflineStorage = true.
بشكل افتراضي، يستخدم مصدرو Azure Monitor المسار التالي:
لتجاوز الدليل الافتراضي، يجب تعيين storage_directory إلى الدليل الذي تريده.
على سبيل المثال:
...
# 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",
)
...
لتعطيل هذه الميزة، يجب تعيين disable_offline_storage إلى True. الإعدادات الافتراضية لـ False.
على سبيل المثال:
...
# 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,
)
...
تمكين مصدر OTLP
قد ترغب في تمكين مصدر بروتوكول القياس المفتوح (OTLP) جنبا إلى جنب مع مصدر Azure Monitor لإرسال بيانات تتبع الاستخدام إلى موقعين.
إشعار
يتم عرض مصدر OTLP للراحة فقط. نحن لا ندعم رسميًا المصدر OTLP أو أي مكونات أو تجارب طرف ثالث في المصب منه.
أضف جزء التعليمة البرمجية المكررة التالي. يفترض هذا المثال أن لديك جامع OpenTelemetry مع جهاز استقبال OTLP قيد التشغيل. للحصول على التفاصيل، راجع المثال على 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();
أضف جزء التعليمة البرمجية المكررة التالي. يفترض هذا المثال أن لديك جامع OpenTelemetry مع جهاز استقبال OTLP قيد التشغيل. للحصول على التفاصيل، راجع المثال على 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()
.Build();
// 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()
.Build();
أضف جزء التعليمة البرمجية المكررة التالي. يفترض هذا المثال أن لديك جامع OpenTelemetry مع جهاز استقبال OTLP قيد التشغيل. للحصول على التفاصيل، راجع المثال على 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);
أضف جزء التعليمة البرمجية المكررة التالي. يفترض هذا المثال أن لديك جامع OpenTelemetry مع جهاز استقبال OTLP قيد التشغيل. للحصول على التفاصيل، راجع 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
يمكن الوصول إلى تكوينات OpenTelemetry التالية من خلال متغيرات البيئة أثناء استخدام Azure Monitor OpenTelemetry Distros.
لتغيير هذا السلوك، يجب تعيين متغير بيئة إلى أو truefalse.
ASP.NET Core Instrumentation: OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_DISABLE_URL_QUERY_REDACTION يتم تعطيل Query String Redaction بشكل افتراضي. للتمكين، قم بتعيين متغير البيئة هذا إلى false.
Http Client Instrumentation: OTEL_DOTNET_EXPERIMENTAL_HTTPCLIENT_DISABLE_URL_QUERY_REDACTION يتم تعطيل Query String Redaction بشكل افتراضي. للتمكين، قم بتعيين متغير البيئة هذا إلى false.
لتغيير هذا السلوك، يجب تعيين متغير بيئة إلى أو truefalse.
ASP.NET Core Instrumentation: OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_DISABLE_URL_QUERY_REDACTION يتم تمكين Query String Redaction بشكل افتراضي. لتعطيل، قم بتعيين متغير البيئة هذا إلى true.
Http Client Instrumentation: OTEL_DOTNET_EXPERIMENTAL_HTTPCLIENT_DISABLE_URL_QUERY_REDACTION يتم تمكين Query String Redaction بشكل افتراضي. لتعطيل، قم بتعيين متغير البيئة هذا إلى true.
أضف ما يلي إلى applicationinsights.json ملف التكوين:
نحن نعمل بنشاط في مجتمع OpenTelemetry لدعم Redaction.
عند استخدام حزمة توزيع بيانات تتبع استخدام Azure Monitor OpenTelemetry، يمكن تنقيح سلاسل الاستعلام عن طريق إنشاء معالج امتداد وتطبيقه على تكوين distro.