`B3PropagatorExtractorMultipleHeaders` allocates when headers are missing

Ouverte Adaptée aux débutants
#9,065 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 3 jours

@Eliot5566 y travaille déjà.

Depuis le 26/11/2026.

  • #1 par @Eliot5566 — ouverte

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
82/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
java
Domaine
backend

Piste de recherche

Localisez B3PropagatorExtractorMultipleHeaders et comparez la gestion d’un ID de trace manquant avec celle de B3PropagatorExtractorSingleHeader. Commencez par trouver les tests existants de l’extracteur et ajoutez ou adaptez un cas pour un carrier sans en-têtes B3, qui vérifie qu’aucune allocation n’a lieu lorsque la journalisation FINE est désactivée. Le travail est terminé lorsque l’absence d’en-têtes entraîne un retour sans allocation et que les messages concernant un ID de trace invalide ne sont formatés que lorsque la journalisation FINE est activée.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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

Langage dominant
Java
Étoiles
2.5k
Forks
1.1k
Merge moyen
5 j 11 h
PR mergées (30 j)
41

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de open-telemetry/opentelemetry-java

Toutes les issues de open-telemetry/opentelemetry-java

Issues similaires

Plus d'issues Java

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.