MultiSpanProcessor caches method values of delegates

Abierto
#5,924 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
java

Línea de trabajo

Comienza con MultiSpanProcessor y la interfaz SpanProcessor; después, reproduce el comportamiento con la prueba respectNeeds mostrada en el issue. Comprueba cómo se usan isStartRequired() e isEndRequired() durante la construcción y el dispatch. Se considera terminado cuando el comportamiento documentado o acordado queda recogido en una prueba de regresión que pasa, y se han resuelto todas las implicaciones de compatibilidad para una futura versión major.

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

Descripción

Bug

The MultiSpanProcessor is designed to wrap a bunch of other SpanProcessors that it delegates to. SpanProcessor is an interface, and it has two methods isStartRequired() and isEndRequired(). These methods presumably tell the user of the SpanProcessor instance whether or not the corresponding onStart() or onEnd() method should be invoked.

As of this writing, there is only one caller of the isStartRequired() and isEndRequired() methods, and that is the MultiSpanProcessor. One could make the argument that simply allowing implementers to build a no-op onStart() or onEnd() would have been sufficient, but the original authors chose to guard this with another method call instead. Baffling.

In any case, the MultiSpanProcessor seems to have been crafted with premature optimization in mind. In the constructor, the delegates are grouped into two possibly overlapping sets -- those that require start and those that require end. This set creation is done in the constructor by invoking isStartRequired() and isEndRequired() at construction time, and never again. The creation of these sets effectively caches the method call return value for the life of the MultiSpanProcessor.

This might have been the original design, but it feels like a bug. There is nothing in the javadoc discouraging implementers from returning different values from is{Start/End}Required(), and forcing users to implement a method whose return value is subsequently ignored seems like bad design.

Because the SpanProcessor (and all of its friends) are "stable", this will be difficult to change. I suggest that we keep this in scope when looking at a future major version bump. There are definitely some interesting design discussions to be had around this.

Here is an example unit test that uses a SpanProcessor that only wants its onStart() and onEnd() to be called once. The MutiSpanProcessor is called 100 times, and sure enough, the delegate is also called 100 times. This could be a harsh surprise to the implementer!

This test fails today:

  @Test
  void respectNeeds() {
    AtomicInteger startCt = new AtomicInteger();
    AtomicInteger endCt = new AtomicInteger();
    SpanProcessor oncePlease = new SpanProcessor() {

      @Override
      public void onStart(Context parentContext, ReadWriteSpan span) {
        startCt.incrementAndGet();
      }

      @Override
      public boolean isStartRequired() {
        return startCt.get() == 0;
      }

      @Override
      public void onEnd(ReadableSpan span) {
        endCt.incrementAndGet();
      }

      @Override
      public boolean isEndRequired() {
        return endCt.get() == 0;
      }
    };
    SpanProcessor multi = MultiSpanProcessor.create(singletonList(oncePlease));
    for(int i=0; i < 100; i++){
      multi.onStart(null, null);
      multi.onEnd(null);
    }
    assertThat(startCt.get()).isEqualTo(1);
    assertThat(endCt.get()).isEqualTo(1);
  }
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.