`B3PropagatorExtractorMultipleHeaders` allocates when headers are missing

Đang mở Phù hợp với người mới
#9,065 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

@Eliot5566 đang làm issue này rồi.

Từ ngày 26/11/2026.

  • #1 của @Eliot5566 — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
java
Lĩnh vực
backend

Hướng nghiên cứu

Tìm B3PropagatorExtractorMultipleHeaders và so sánh cách xử lý khi thiếu trace ID với B3PropagatorExtractorSingleHeader. Bắt đầu bằng cách tìm các bài kiểm tra extractor hiện có và thêm hoặc điều chỉnh một trường hợp cho carrier không có header B3, xác minh không có cấp phát nào khi ghi log FINE bị tắt. Hoàn tất khi thiếu header thì hàm trả về mà không cấp phát, và các thông báo về trace ID không hợp lệ chỉ được định dạng khi ghi log FINE được bật.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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).

Ngôn ngữ chính
Java
Star
2.5k
Fork
1.1k
Merge trung bình
5 ngày 11 giờ
Pull request đã merge (30 ngày)
41

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của open-telemetry/opentelemetry-java

Tất cả issue của open-telemetry/opentelemetry-java

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.