Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pass HttpResponse from HttpClientResponseException to OpenTelemetry client instrumenter #710

Open
wants to merge 2 commits into
base: 6.10.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.propagation.PropagatedContext;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpResponseProvider;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.annotation.Filter;
import io.micronaut.http.filter.ClientFilterChain;
Expand All @@ -38,6 +39,8 @@
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;

import java.util.Optional;

import static io.micronaut.http.HttpAttributes.INVOCATION_CONTEXT;
import static io.micronaut.tracing.opentelemetry.instrument.http.client.OpenTelemetryClientFilter.CLIENT_PATH;

Expand Down Expand Up @@ -93,13 +96,21 @@ public Publisher<? extends HttpResponse<?>> doFilter(MutableHttpRequest<?> reque
Span span = Span.fromContext(context);
span.recordException(throwable);
span.setStatus(StatusCode.ERROR);
instrumenter.end(context, request, null, throwable);
HttpResponse<?> response = findResponseInThrowable(throwable).orElse(null);
instrumenter.end(context, request, response, throwable);
});

}
}
}

private Optional<HttpResponse<?>> findResponseInThrowable(@Nullable Throwable throwable) {
if (throwable instanceof HttpResponseProvider httpResponseProvider) {
return Optional.ofNullable(httpResponseProvider.getResponse());
}
return Optional.empty();
}

private void handleContinueSpan(MutableHttpRequest<?> request) {
Object invocationContext = request.getAttribute(INVOCATION_CONTEXT).orElse(null);
if (invocationContext instanceof MethodInvocationContext<?, ?> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class OpenTelemetryHttpSpec extends Specification {
assert serverSpans.every {it.attributes.stream().any { it.get(SemanticAttributes.HTTP_METHOD) }}
assert !hasRoute || serverSpans.every {it.attributes.stream().any { it.get(SemanticAttributes.HTTP_ROUTE) }}
assert serverSpans.every {it.attributes.stream().any {x -> Optional.ofNullable(x.get(SemanticAttributes.HTTP_STATUS_CODE)).map { it.intValue() == httpStatus.code }.orElse(false) }}
def clientSpans = exporter.finishedSpanItems.findAll { it -> it.kind == SpanKind.CLIENT }
assert clientSpans.every {it.attributes.stream().any { it.get(SemanticAttributes.HTTP_METHOD) }}
assert clientSpans.every {it.attributes.stream().any {x -> Optional.ofNullable(x.get(SemanticAttributes.HTTP_STATUS_CODE)).map { it.intValue() == httpStatus.code }.orElse(false) }}
}

void 'test map WithSpan annotation'() {
Expand Down Expand Up @@ -230,6 +233,34 @@ class OpenTelemetryHttpSpec extends Specification {
'/error/completionStageErrorContinueSpan' | 1 | 'inside normal method continueSpan'
}

void 'test client error #desc, path=/error/#variant'() {
def errorClient = context.getBean(ErrorClient)

when:
String responseBody = errorClient.get(variant)

then:
def e = thrown(HttpClientResponseException)
e.message == "Internal Server Error"
conditions.eventually {
hasSpans(hasInternalSpan ? 1 : 0, 1, 1)
exporter.finishedSpanItems.events.any { it.size() > 0 && it.get(0).name == "exception" }
exporter.finishedSpanItems.stream().allMatch(span -> span.status.statusCode == StatusCode.ERROR)
hasHttpSemanticAttributes(e.status)
}
cleanup:
exporter.reset()
where:
variant | hasInternalSpan | desc
'publisher' | true | 'inside publisher'
'publisherErrorContinueSpan' | false | 'inside continueSpan publisher'
'mono' | true | 'propagated through publisher'
'sync' | true | 'inside normal function'
'completionStage' | true | 'inside completionStage'
'completionStagePropagation' | true | 'propagated through completionStage'
'completionStageErrorContinueSpan' | false | 'inside normal method continueSpan'
}

void 'client with tracing annotations'() {
def warehouseClient = embeddedServer.applicationContext.getBean(WarehouseClient)
def serverSpanCount = 2
Expand Down Expand Up @@ -639,6 +670,13 @@ class OpenTelemetryHttpSpec extends Specification {
}
}

@Client("/error")
static interface ErrorClient {

@Get("/{variant}")
String get(@PathVariable String variant)
}

@Controller('/exclude')
static class ExcludeController {

Expand Down
Loading