Skip to content

Commit

Permalink
Showcase @ServerResponseFilter not being called for excepion case
Browse files Browse the repository at this point in the history
  • Loading branch information
famod committed Nov 8, 2023
1 parent c92e5a3 commit f3af2d5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Created on 07.11.2023
*
* Copyright(c) 1995 - 2022 T-Systems Multimedia Solutions GmbH
* Riesaer Str. 5, 01129 Dresden
* All rights reserved.
*/
package org.acme.opentelemetry;

import jakarta.ws.rs.container.ContainerResponseContext;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.ResponseBuilder;
import jakarta.ws.rs.core.Response.Status;

import org.jboss.resteasy.reactive.server.ServerResponseFilter;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;

public class TraceHeaderServerResponseFilter {

private final Span span;

TraceHeaderServerResponseFilter(final Span span) {
this.span = span;
}

@ServerResponseFilter
public void filter(final ContainerResponseContext responseContext, final Throwable t) {
System.err.println("!!! filter called, t: " + t);
final SpanContext spanContext = span.getSpanContext();
if (spanContext.isValid()) {
// header name taken from https://w3c.github.io/trace-context/#traceresponse-header
responseContext.getHeaders().add("trace-id", spanContext.getTraceId());
}
}

// this will work:
// @ServerExceptionMapper
public Response mapExc(final Exception exc) {
System.err.println("!!! mapExc called, exc: " + exc);
final SpanContext spanContext = span.getSpanContext();
final ResponseBuilder builder = Response.status(Status.INTERNAL_SERVER_ERROR);
if (spanContext.isValid()) {
// header name taken from https://w3c.github.io/trace-context/#traceresponse-header
builder.header("trace-id", spanContext.getTraceId());
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ public String hello() {
return "hello";
}

@GET
@Path("/fail")
@Produces(MediaType.TEXT_PLAIN)
public String fail() {
LOG.info("fail");
throw new RuntimeException("test");
}

@GET
@Path("/chain")
@Produces(MediaType.TEXT_PLAIN)
public String chain() {
ResourceClient resourceClient = RestClientBuilder.newBuilder()
final ResourceClient resourceClient = RestClientBuilder.newBuilder()
.baseUri(uriInfo.getBaseUri())
.build(ResourceClient.class);
return "chain -> " + resourceClient.hello();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;

import org.junit.jupiter.api.Test;

Expand All @@ -16,7 +17,18 @@ public void testHelloEndpoint() {
.when().get("/hello")
.then()
.statusCode(200)
.body(is("hello"));
.body(is("hello"))
.header("trace-id", notNullValue());
}

@Test
public void testFailEndpoint() {
given()
.when().get("/fail")
.then()
.statusCode(500)
// .body(containsString("RuntimeException: test"))
.header("trace-id", notNullValue());
}

@Test
Expand Down

0 comments on commit f3af2d5

Please sign in to comment.