`B3PropagatorExtractorMultipleHeaders` allocates when headers are missing

Abierto Apto para principiantes
#9,065 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

@Eliot5566 ya está trabajando en esto.

Desde el 26/11/2026.

  • #1 de @Eliot5566 — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
82/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
java
Área
backend

Línea de trabajo

Localiza B3PropagatorExtractorMultipleHeaders y compara cómo gestiona la ausencia del ID de traza con B3PropagatorExtractorSingleHeader. Empieza por encontrar las pruebas existentes del extractor y añade o adapta un caso para un carrier sin encabezados B3 que verifique que no se realiza ninguna asignación con el registro FINE desactivado. Se considera terminado cuando, si faltan encabezados, se retorna sin realizar asignaciones y los mensajes sobre ID de traza no válido solo se formatean cuando el registro FINE está activado.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
Java
Estrellas
2.5k
Forks
1.1k
Merge medio
5 d 11 h
PR fusionados (30 d)
41

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de open-telemetry/opentelemetry-java

Todos los issues de open-telemetry/opentelemetry-java

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.