`B3PropagatorExtractorMultipleHeaders` allocates when headers are missing
维护者通常 3 天内回复
评估
调研方向
找到 B3PropagatorExtractorMultipleHeaders,并将其对缺失 trace ID 的处理与 B3PropagatorExtractorSingleHeader 进行比较。先找到现有的 extractor 测试,再添加或调整一个针对不带 B3 header 的 carrier 的用例,验证在禁用 FINE 日志时不会发生分配。缺失 header 时不经分配即返回,且仅在启用 FINE 日志时才格式化无效 trace ID 消息,即为完成。
由索引模型根据 Issue 内容生成。
描述
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 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
open-telemetry/opentelemetry-java 的其他 Issue
-
[incubator] ComposableProbabilitySampler does not validate ratio range可能已有人在做 @eminfikret 于 4 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
open-telemetry/opentelemetry-java#9046 · 2 条评论 · 已指派 1 人 ·
维护者通常 3 天内回复
-
PrometheusComponentProvider promotes all resource attributes when resource_constant_labels is empty ({})可能已有人在做 @rooks-ns 于 19 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
open-telemetry/opentelemetry-java#9014 · 1 条评论 ·
维护者通常 3 天内回复
-
Fix `otel.sdk.log.created` counting semantics可能已有人在做 @anuraaga 于 112 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
open-telemetry/opentelemetry-java#8688 ·
维护者通常 3 天内回复
-
SdkSpanBuilder drops invalid parent TraceState when IdGenerator.generatesRandomTraceIds() is true可能已有人在做 @Eliot5566 今天认领。 未关闭Bug SDK
难度 4/5 3-5 天 新手友好度 48/100
open-telemetry/opentelemetry-java#9062 · 1 条评论 ·
维护者通常 3 天内回复
-
Feature Request
难度 5/5 一周以上 新手友好度 12/100
open-telemetry/opentelemetry-java#9061 ·
维护者通常 3 天内回复
查看 open-telemetry/opentelemetry-java 的全部 Issue
相似的 Issue
-
feature request needs triage
难度 1/5 1 小时以内 新手友好度 66/100
TeamNewPipe/NewPipe#14083 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 68/100
PCL-Community/PCL-CE#3907 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 73/100
维护者通常 2 天内回复
-
area/dependencies area/dependencies/quarkus backport/26.6 backport/26.8 kind/cve severity/critical source/scan-dependencies status/triage
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 2 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
stripe/stripe-java#2341 ·
维护者通常 3 天内回复