本指南提供有关在 Azure Monitor Application Insights 中筛选 OpenTelemetry (OTel) 数据的说明。 通过实施筛选器,开发人员可以排除不必要的遥测数据,并防止收集敏感信息,确保优化性能和符合性。
你可能想要筛出遥测的原因包括:
- 筛出运行状况检查遥测,以减少噪音。
- 确保不会收集到个人身份信息(PII)和凭据。
- 筛出低价值的遥测以优化性能。
若要详细了解 OpenTelemetry 概念,请参阅 OpenTelemetry 概述或 OpenTelemetry 常见问题解答。
如需 Azure Monitor OpenTelemetry 发行版中包含的所有检测库的列表,请参阅添加和修改适用于 .NET、Java、Node.js 和 Python 应用程序的 Azure Monitor OpenTelemetry。
许多检测库都提供筛选选项。 有关指南,请参阅对应的README文件:
1 在包中提供 SqlClient 检测,尽管它仍处于 beta 版。 当它成为稳定版时,我们会将其作为标准包引用而添加。 在此期间,若要自定义 SQLClient 仪表,请将 OpenTelemetry.Instrumentation.SqlClient
包引用添加到项目中并使用其公共 API。
dotnet add package --prerelease OpenTelemetry.Instrumentation.SqlClient
builder.Services.AddOpenTelemetry().UseAzureMonitor().WithTracing(builder =>
{
builder.AddSqlClientInstrumentation(options =>
{
options.SetDbStatementForStoredProcedure = false;
});
});
许多检测库都提供筛选选项。 如需指南,请参阅相应的说明文件:
Java 原生不支持抑制自动收集的遥测数据。 若要筛选遥测,请参阅相关的外部文档:
备注
此示例专用于 HTTP 检测。 对于其他信号类型,目前没有可用于筛出遥测的特定机制。 相反,需要自定义范围处理器。
以下示例演示如何使用 HTTP/HTTPS 监控库排除被跟踪的某个 URL:
// Import the useAzureMonitor function and the ApplicationInsightsOptions class from the @azure/monitor-opentelemetry package.
const { useAzureMonitor, ApplicationInsightsOptions } = require("@azure/monitor-opentelemetry");
// Import the HttpInstrumentationConfig class from the @opentelemetry/instrumentation-http package.
const { HttpInstrumentationConfig }= require("@opentelemetry/instrumentation-http");
// Import the IncomingMessage and RequestOptions classes from the http and https packages, respectively.
const { IncomingMessage } = require("http");
const { RequestOptions } = require("https");
// Create a new HttpInstrumentationConfig object.
const httpInstrumentationConfig: HttpInstrumentationConfig = {
enabled: true,
ignoreIncomingRequestHook: (request: IncomingMessage) => {
// Ignore OPTIONS incoming requests.
if (request.method === 'OPTIONS') {
return true;
}
return false;
},
ignoreOutgoingRequestHook: (options: RequestOptions) => {
// Ignore outgoing requests with the /test path.
if (options.path === '/test') {
return true;
}
return false;
}
};
// Create a new ApplicationInsightsOptions object.
const config: ApplicationInsightsOptions = {
instrumentationOptions: {
http: {
httpInstrumentationConfig
}
}
};
// Enable Azure Monitor integration using the useAzureMonitor function and the ApplicationInsightsOptions object.
useAzureMonitor(config);
备注
此示例专用于 HTTP 检测。 对于其他信号类型,目前没有可用于筛出遥测的特定机制。 相反,需要自定义范围处理器。
以下示例演示如何使用 OTEL_PYTHON_EXCLUDED_URLS
环境变量排除跟踪某个 URL:
export OTEL_PYTHON_EXCLUDED_URLS="http://localhost:8080/ignore"
这样做将排除以下 Flask 示例中所示的终结点:
...
# Import the Flask and Azure Monitor OpenTelemetry SDK libraries.
import flask
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 to your Azure Monitor Application Insights resource.
configure_azure_monitor(
connection_string="<your-connection-string>",
)
# Create a Flask application.
app = flask.Flask(__name__)
# Define a route. Requests sent to this endpoint will not be tracked due to
# flask_config configuration.
@app.route("/ignore")
def ignore():
return "Request received but not tracked."
...
使用自定义处理器:
提示
在添加 Azure Monitor 之前,添加此处显示的处理器。
// Create an ASP.NET Core application builder.
var builder = WebApplication.CreateBuilder(args);
// Configure the OpenTelemetry tracer provider to add a new processor named ActivityFilteringProcessor.
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.AddProcessor(new ActivityFilteringProcessor()));
// Configure the OpenTelemetry tracer provider to add a new source named "ActivitySourceName".
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.AddSource("ActivitySourceName"));
// Add the Azure Monitor telemetry service to the application. This service will collect and send telemetry data to Azure Monitor.
builder.Services.AddOpenTelemetry().UseAzureMonitor();
// Build the ASP.NET Core application.
var app = builder.Build();
// Start the ASP.NET Core application.
app.Run();
使用以下代码将 ActivityFilteringProcessor.cs
添加到你的项目:
public class ActivityFilteringProcessor : BaseProcessor<Activity>
{
// The OnStart method is called when an activity is started. This is the ideal place to filter activities.
public override void OnStart(Activity activity)
{
// prevents all exporters from exporting internal activities
if (activity.Kind == ActivityKind.Internal)
{
activity.IsAllDataRequested = false;
}
}
}
如果没有使用 AddSource("ActivitySourceName")
显式添加特定源,则不会导出使用该源创建的任何活动。
使用自定义处理器:
// Create an OpenTelemetry tracer provider builder.
// It is important to keep the TracerProvider instance active throughout the process lifetime.
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("OTel.AzureMonitor.Demo") // Add a source named "OTel.AzureMonitor.Demo".
.AddProcessor(new ActivityFilteringProcessor()) // Add a new processor named ActivityFilteringProcessor.
.AddAzureMonitorTraceExporter() // Add the Azure Monitor trace exporter.
.Build();
使用以下代码将 ActivityFilteringProcessor.cs
添加到你的项目:
public class ActivityFilteringProcessor : BaseProcessor<Activity>
{
// The OnStart method is called when an activity is started. This is the ideal place to filter activities.
public override void OnStart(Activity activity)
{
// prevents all exporters from exporting internal activities
if (activity.Kind == ActivityKind.Internal)
{
activity.IsAllDataRequested = false;
}
}
}
如果没有使用 AddSource("ActivitySourceName")
显式添加特定源,则不会导出使用该源创建的任何活动。
若要从 Java 应用程序筛选遥测数据,可以使用采样替代(推荐)或遥测处理器。 有关详细信息,请参阅以下文档:
Java 本机不支持采样替代和遥测处理器。 若要筛选遥测,请参阅相关的外部文档:
你可以使用自定义范围处理器来排除导出某些范围。 若要将范围标记为不导出,请将 TraceFlag
设置为 DEFAULT
。
使用自定义属性示例,但替换以下代码行:
// Import the necessary packages.
const { SpanKind, TraceFlags } = require("@opentelemetry/api");
const { ReadableSpan, Span, SpanProcessor } = require("@opentelemetry/sdk-trace-base");
// Create a new SpanEnrichingProcessor class.
class SpanEnrichingProcessor implements SpanProcessor {
forceFlush(): Promise<void> {
return Promise.resolve();
}
shutdown(): Promise<void> {
return Promise.resolve();
}
onStart(_span: Span): void {}
onEnd(span) {
// If the span is an internal span, set the trace flags to NONE.
if(span.kind == SpanKind.INTERNAL){
span.spanContext().traceFlags = TraceFlags.NONE;
}
}
}
你可以使用自定义范围处理器来排除导出某些范围。 若要将范围标记为不导出,请将 TraceFlag
设置为 DEFAULT
:
...
# Import the necessary libraries.
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
# 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>",
# Configure the custom span processors to include span filter processor.
span_processors=[span_filter_processor],
)
...
使用以下代码将 SpanFilteringProcessor
添加到你的项目:
# Import the necessary libraries.
from opentelemetry.trace import SpanContext, SpanKind, TraceFlags
from opentelemetry.sdk.trace import SpanProcessor
# Define a custom span processor called `SpanFilteringProcessor`.
class SpanFilteringProcessor(SpanProcessor):
# Prevents exporting spans from internal activities.
def on_start(self, span, parent_context):
# Check if the span is an internal activity.
if span._kind is SpanKind.INTERNAL:
# Create a new span context with the following properties:
# * The trace ID is the same as the trace ID of the original span.
# * The span ID is the same as the span ID of the original span.
# * The is_remote property is set to `False`.
# * The trace flags are set to `DEFAULT`.
# * The trace state is the same as the trace state of the original span.
span._context = SpanContext(
span.context.trace_id,
span.context.span_id,
span.context.is_remote,
TraceFlags(TraceFlags.DEFAULT),
span.context.trace_state,
)