`B3PropagatorExtractorMultipleHeaders` allocates when headers are missing

未关闭 适合新手
#9,065 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

@Eliot5566 已经在做这个了。

开始于 2026年11月26日。

  • #1 来自 @Eliot5566 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java
领域
backend

调研方向

找到 B3PropagatorExtractorMultipleHeaders,并将其对缺失 trace ID 的处理与 B3PropagatorExtractorSingleHeader 进行比较。先找到现有的 extractor 测试,再添加或调整一个针对不带 B3 header 的 carrier 的用例,验证在禁用 FINE 日志时不会发生分配。缺失 header 时不经分配即返回,且仅在启用 FINE 日志时才格式化无效 trace ID 消息,即为完成。

由索引模型根据 Issue 内容生成。

描述

Bug
Describe the bug

B3PropagatorExtractorMultipleHeaders builds a log message string on every extract() call where the X-B3-TraceId header is absent, even when FINE logging is disabled.

A missing X-B3-TraceId header goes through the "invalid trace ID" branch, which eagerly concatenates the (null) value into the message when calling logger.fine(...):

String traceId = getter.get(carrier, B3Propagator.TRACE_ID_HEADER);
if (!Common.isTraceIdValid(traceId)) {
  logger.fine(
      "Invalid TraceId in B3 header: " + traceId + "'. Returning INVALID span context.");
  return Optional.empty();
}

A missing header is the common case for any request that isn't part of a trace. B3PropagatorExtractorSingleHeader already returns early without logging when its b3 header is absent. The multi-header extractor doesn't, so every untraced request allocates a string that is then discarded.

Steps to reproduce

Call B3Propagator.injectingMultiHeaders().extract(Context.root(), carrier, getter) (or injectingSingleHeader(), since both extract both formats) on a carrier with no B3 headers, and measure allocation.

  private static final TextMapGetter<Map<String, String>> GETTER =
      (carrier, key) -> carrier == null ? null : carrier.get(key);

  @Test
  void extractWithoutB3HeadersDoesNotAllocate() {
    B3Propagator propagator = B3Propagator.injectingMultiHeaders();
    Map<String, String> carrier = Collections.emptyMap();
    ThreadMXBean threads = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    int iterations = 20_000;
    // warm-up
    for (int i = 0; i < iterations; i++) {
      propagator.extract(Context.root(), carrier, GETTER);
    }
    long before = threads.getCurrentThreadAllocatedBytes();
    for (int i = 0; i < iterations; i++) {
      propagator.extract(Context.root(), carrier, GETTER);
    }
    long bytesPerExtract = (threads.getCurrentThreadAllocatedBytes() - before) / iterations;
    assertThat(bytesPerExtract).isZero();
  }
What did you expect to see?

No allocation when the B3 headers are absent (and FINE logging is disabled).

What did you see instead?

bytes allocated per extract() call with a carrier not containing the trace ID header.

What version and what artifacts are you using?

opentelemetry-extension-trace-propagators 1.68.0.

Suggested fix

Return early without logging when the trace ID header is absent (traceId is null), matching the single-header extractor, and use a parameterized log call (logger.log(Level.FINE, "... {0} ...", traceId)) for the remaining invalid-ID messages, so they are only formatted when FINE is enabled. The existing messages also contain a stray ' after the value, which should be dropped (with MessageFormat it would otherwise be interpreted as a quote). Alternatively, the log messages could be put in an if block that checks for logger.isLoggable(Level.FINE).

主要语言
Java
星标
2.5k
派生
1.1k
平均合并
5 天 11 小时
30 天内合并 PR
41

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

open-telemetry/opentelemetry-java 的其他 Issue

查看 open-telemetry/opentelemetry-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。