Skip to content

Commit

Permalink
User route template as OpenTelemetry HTTP route (#182)
Browse files Browse the repository at this point in the history
* User route template as OpenTelemetry HTTP route

* Remove unused import

* Verify 404 not found still has route path returned

* Set status code in doOnNext

* Update tracing-opentelemetry-http/src/main/java/io/micronaut/tracing/opentelemetry/instrument/http/server/OpenTelemetryServerFilter.java

Co-authored-by: Nemanja Mikic <n0t_l3ss@outlook.com>
  • Loading branch information
Xiaochao Yang and n0tl3ss authored Oct 13, 2022
1 parent a7a7d77 commit 3b92936
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import javax.annotation.Nullable;
import java.util.List;
import java.util.Optional;

@Internal
enum MicronautHttpServerAttributesGetter implements HttpServerAttributesGetter<HttpRequest<Object>, HttpResponse<Object>> {
Expand Down Expand Up @@ -103,8 +102,9 @@ public String target(HttpRequest<Object> request) {
@Override
@Nullable
public String route(HttpRequest<Object> request) {
Optional<Object> routeInfo = request.getAttribute(HttpAttributes.ROUTE_INFO);
return routeInfo.map(Object::toString).orElse(null);
String route = request.getAttribute(HttpAttributes.URI_TEMPLATE).map(Object::toString)
.orElse(request.getUri().toString());
return request.getMethodName() + " - " + route;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import io.micronaut.tracing.opentelemetry.instrument.util.OpenTelemetryExclusionsConfiguration;
import io.micronaut.tracing.opentelemetry.instrument.util.OpenTelemetryObserver;
import io.micronaut.tracing.opentelemetry.instrument.util.OpenTelemetryPublisherUtils;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
Expand Down Expand Up @@ -82,6 +84,15 @@ public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, Server
Context newContext = instrumenter.start(parentContext, request);

return OpenTelemetryPublisherUtils.createOpenTelemetryPublisher(requestPublisher, instrumenter, newContext, request, new OpenTelemetryObserver<MutableHttpResponse<?>>() {

@Override
public void doOnNext(MutableHttpResponse<?> object, Context context) {
OpenTelemetryObserver.super.doOnNext(object, context);
if (object.status().getCode() >= 400) {
Span.current().setStatus(StatusCode.ERROR);
}
}

@Override
public void doOnError(@NonNull Throwable throwable, @NonNull Context openTelemetryContext) {
request.setAttribute(CONTINUE, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,23 @@ class OpenTelemetryHttpSpec extends Specification {
testExporter.reset()
}

void 'test error 404'() {
when:
def route = '/error/notFoundRoute'
HttpResponse<String> response = reactorHttpClient.toBlocking().exchange(route, String)

then:
def e = thrown(HttpClientResponseException)
e.message == "Not Found"
conditions.eventually {
exporter.finishedSpanItems.size() == 1
exporter.finishedSpanItems[0].name == "GET - " + route
exporter.finishedSpanItems[0].status.statusCode == StatusCode.ERROR
}
cleanup:
exporter.reset()
}

@Introspected
static class SomeBody {
}
Expand Down

0 comments on commit 3b92936

Please sign in to comment.