Background
Currently, in Instrumenter.doEnd(), the execution order is:
- AttributesExtractor.onEnd() - sets attributes (e.g., rpc.grpc.status_code, http.response.status_code)
- OperationListener.onEnd() - records metrics
- SpanStatusExtractor.extract() - sets span status
- span.end()
|
long endNanos = getNanos(endTime); |
|
for (int i = operationListeners.length - 1; i >= 0; i--) { |
|
operationListeners[i].onEnd(context, attributes, endNanos); |
|
} |
|
} |
|
|
|
SpanStatusBuilder spanStatusBuilder = new SpanStatusBuilderImpl(span); |
|
spanStatusExtractor.extract(spanStatusBuilder, request, response, error); |
This means when OperationListener.onEnd() is invoked, the span's status is still UNSET, even though the SpanStatusExtractor will set it to ERROR shortly after.
We want to record error count metrics based on span status.
When implementing custom OperationListener to record such metrics, developers cannot rely on span.getStatus() because it hasn't been set yet at that point.
Describe the solution you'd like
Move SpanStatusExtractor.extract() to execute before OperationListener.onEnd():
// Set span status FIRST
SpanStatusBuilder spanStatusBuilder = new SpanStatusBuilderImpl(span);
spanStatusExtractor.extract(spanStatusBuilder, request, response, error);
// THEN call OperationListener.onEnd()
long endNanos = getNanos(endTime);
for (int i = operationListeners.length - 1; i >= 0; i--) {
operationListeners[i].onEnd(context, attributes, endNanos);
}
Background
Currently, in
Instrumenter.doEnd(), the execution order is:opentelemetry-java-instrumentation/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/Instrumenter.java
Lines 286 to 293 in 5ee4eac
This means when
OperationListener.onEnd()is invoked, the span's status is still UNSET, even though theSpanStatusExtractorwill set it to ERROR shortly after.We want to record error count metrics based on span status.
When implementing custom
OperationListenerto record such metrics, developers cannot rely onspan.getStatus()because it hasn't been set yet at that point.Describe the solution you'd like
Move
SpanStatusExtractor.extract()to execute beforeOperationListener.onEnd():