You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从OpenTelemetry追踪信息中排除Hangfire请求?

排除OpenTelemetry中Hangfire相关追踪数据发送至AWS X-Ray的方案

问题背景

使用OpenTelemetry收集追踪数据并发送至AWS X-Ray时,Hangfire会生成独立的Client -> DB追踪序列,每分钟产生80-100条请求,干扰了正常的Client -> App -> DB追踪链路,需要排除Hangfire相关的追踪信息。尝试通过EntityFrameworkInstrumentationOptions和HangfireInstrumentationOptions配置过滤器未生效。

解决方案

方法1:直接移除Hangfire Instrumentation

如果不需要对Hangfire任务本身进行追踪,最简单的方式是从TracerProviderBuilder中移除Hangfire的Instrumentation配置:

Sdk.CreateTracerProviderBuilder()
    .AddXRayTraceId()
    .AddAWSInstrumentation()
    .AddAspNetCoreInstrumentation()
    // 移除该行即可停止收集Hangfire相关追踪
    // .AddHangfireInstrumentation()
    .AddHttpClientInstrumentation()
    .AddEntityFrameworkCoreInstrumentation()
    .AddNpgsql()
    .SetResourceBuilder(ResourceBuilder
        .CreateDefault()
        .AddService(otelOptions.ServiceName)
        .AddDetector(new AWSEC2ResourceDetector())
        .AddTelemetrySdk())
    .AddSource(otelOptions.ServiceName)
    .AddOtlpExporter(opts =>
    {
        opts.Endpoint = new Uri(otelOptions.Endpoint);
    })
    .Build();

方法2:配置Hangfire Instrumentation过滤所有Span

如果需要保留Hangfire Instrumentation但过滤所有生成的Span,可通过HangfireInstrumentationOptions的Filter方法实现:

.AddHangfireInstrumentation(options =>
{
    // 返回false表示不记录该Span
    options.Filter = (commandName, methodInfo, arguments) => false;
})

方法3:使用自定义SpanProcessor全局过滤

如果上述方法仍未生效,可通过自定义SpanProcessor全局过滤Hangfire相关Span。Hangfire生成的Span通常带有特定属性(如hangfire.job.id)或来源名称包含Hangfire,可据此判断并阻止导出:

  1. 实现自定义SpanProcessor:
public class HangfireSpanFilterProcessor : BaseProcessor<Activity>
{
    public override void OnEnd(Activity activity)
    {
        // 检查是否为Hangfire相关Span
        bool isHangfireSpan = activity.Source.Name.Contains("Hangfire") 
                              || activity.Tags.Any(t => t.Key.StartsWith("hangfire."));
        
        if (isHangfireSpan)
        {
            // 标记为不记录,阻止导出
            activity.Recorded = false;
        }
        
        base.OnEnd(activity);
    }
}
  1. 将自定义Processor添加到TracerProviderBuilder:
Sdk.CreateTracerProviderBuilder()
    .AddXRayTraceId()
    .AddAWSInstrumentation()
    .AddAspNetCoreInstrumentation()
    .AddHangfireInstrumentation()
    .AddHttpClientInstrumentation()
    .AddEntityFrameworkCoreInstrumentation()
    .AddNpgsql()
    .SetResourceBuilder(ResourceBuilder
        .CreateDefault()
        .AddService(otelOptions.ServiceName)
        .AddDetector(new AWSEC2ResourceDetector())
        .AddTelemetrySdk())
    .AddSource(otelOptions.ServiceName)
    // 添加自定义Span处理器
    .AddProcessor(new HangfireSpanFilterProcessor())
    .AddOtlpExporter(opts =>
    {
        opts.Endpoint = new Uri(otelOptions.Endpoint);
    })
    .Build();

说明

  • 方法1最直接彻底,适合不需要Hangfire追踪的场景;
  • 方法2针对Hangfire Instrumentation本身过滤,粒度更细,可根据commandName、methodInfo等参数调整过滤规则;
  • 方法3是全局过滤方案,适用于其他Instrumentation生成的关联Hangfire Span也需要排除的场景。

内容的提问来源于stack exchange,提问作者Vika Bobr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 10:28:30