`B3PropagatorExtractorMultipleHeaders` allocates when headers are missing

Aperta Adatta ai principianti
#9,065 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

@Eliot5566 ci sta già lavorando.

Dal 26/11/2026.

  • #1 di @Eliot5566 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java
Ambito
backend

Direzione di ricerca

Individua B3PropagatorExtractorMultipleHeaders e confronta la gestione dell’ID di traccia mancante con quella di B3PropagatorExtractorSingleHeader. Inizia cercando i test esistenti dell’estrattore e aggiungi o adatta un caso per un carrier senza intestazioni B3 che verifichi che non venga effettuata alcuna allocazione con la registrazione FINE disabilitata. Il lavoro è completato quando, in assenza di intestazioni, si ritorna senza effettuare allocazioni e i messaggi relativi a un ID di traccia non valido vengono formattati solo quando la registrazione FINE è abilitata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Java
Stelle
2.5k
Fork
1.1k
Merge medio
5g 11h
PR unite (30g)
41

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di open-telemetry/opentelemetry-java

Tutte le issue di open-telemetry/opentelemetry-java

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.