Skip to content

Commit

Permalink
tck changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yasmin-aumeeruddy committed Aug 10, 2023
1 parent ac65f0b commit 648f1e9
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_STATUS_CODE;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_TARGET;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_URL;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_OK;

import java.net.URL;
Expand Down Expand Up @@ -77,6 +78,7 @@ public static WebArchive createDeployment() {
URL url;

public static final String TEST_PASSED = "Test Passed";
public static final String QUERY_VALUE = "bar";

@Inject
private InMemorySpanExporter spanExporter;
Expand All @@ -92,16 +94,63 @@ void setUp() {

@Test
public void testIntegrationWithJaxRsClient() throws Exception {
basicClient.get("/JaxRsClientAsyncTestEndpoint/jaxrsclient");
basicClient.get("/JaxRsClientAsyncTestEndpoint/jaxrsclient?baggageValue=" + QUERY_VALUE);
readSpans();
}

@Test
public void testIntegrationWithJaxRsClientAsync() throws Exception {
basicClient.get("/JaxRsClientAsyncTestEndpoint/jaxrsclientasync");
basicClient.get("/JaxRsClientAsyncTestEndpoint/jaxrsclientasync?baggageValue=" + QUERY_VALUE);
readSpans();
}

@Test
public void testIntegrationWithJaxRsClientError() throws Exception {
basicClient.get("/JaxRsClientAsyncTestEndpoint/jaxrsclienterror");
readErrorSpans();
}

public void readErrorSpans() {

List<SpanData> spanData = spanExporter.getFinishedSpanItems(3);

List<SpanData> serverSpans = spanExporter.getSpansWithKind(SpanKind.SERVER);

SpanData firstURL = null;
SpanData secondURL = null;
for (SpanData span : serverSpans) {
if (span.getAttributes().get(HTTP_TARGET).contains("JaxRsClientAsyncTestEndpoint/jaxrsclient")) {
firstURL = span;
} else {
secondURL = span;
}
}

Assert.assertNotNull(firstURL);
Assert.assertNotNull(secondURL);

SpanData httpGet = spanExporter.getFirst(SpanKind.CLIENT);

// Assert correct parent-child links
// Shows that propagation occurred
Assert.assertEquals(httpGet.getSpanId(), secondURL.getParentSpanId());
Assert.assertEquals(firstURL.getSpanId(), httpGet.getParentSpanId());

Assert.assertEquals(HttpMethod.GET, firstURL.getAttributes().get(HTTP_METHOD));
Assert.assertEquals("http", firstURL.getAttributes().get(HTTP_SCHEME));

// Assert error is in spans from method getError() called by JAX Clients
Assert.assertEquals(HTTP_INTERNAL_ERROR, firstURL.getAttributes().get(HTTP_STATUS_CODE).intValue());
Assert.assertEquals(HTTP_INTERNAL_ERROR, secondURL.getAttributes().get(HTTP_STATUS_CODE).intValue());
Assert.assertEquals(HTTP_INTERNAL_ERROR, httpGet.getAttributes().get(HTTP_STATUS_CODE).intValue());

// There are many different URLs that will end up here. But all should contain "JaxRsClientAsyncTestEndpoint"
Assert.assertTrue(httpGet.getAttributes().get(HTTP_URL).contains("JaxRsClientAsyncTestEndpoint"));

Assert.assertEquals(HttpMethod.GET, httpGet.getAttributes().get(HTTP_METHOD));
Assert.assertTrue(httpGet.getAttributes().get(HTTP_URL).contains("JaxRsClientAsyncTestEndpoint"));
}

public void readSpans() {

List<SpanData> spanData = spanExporter.getFinishedSpanItems(3);
Expand Down Expand Up @@ -131,6 +180,7 @@ public void readSpans() {
Assert.assertEquals(HTTP_OK, firstURL.getAttributes().get(HTTP_STATUS_CODE).intValue());
Assert.assertEquals(HttpMethod.GET, firstURL.getAttributes().get(HTTP_METHOD));
Assert.assertEquals("http", firstURL.getAttributes().get(HTTP_SCHEME));
Assert.assertTrue(firstURL.getAttributes().get(HTTP_TARGET).contains(QUERY_VALUE));

// There are many different URLs that will end up here. But all should contain "JaxRsClientAsyncTestEndpoint"
Assert.assertTrue(httpGet.getAttributes().get(HTTP_URL).contains("JaxRsClientAsyncTestEndpoint"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.eclipse.microprofile.telemetry.tracing.tck.async;

import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.util.concurrent.TimeUnit.SECONDS;

import java.util.concurrent.Future;
Expand All @@ -35,6 +36,7 @@
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.core.Application;
Expand Down Expand Up @@ -69,18 +71,21 @@ private void closeClient() {

@GET
@Path("/jaxrsclient")
public Response getJax(@Context UriInfo uriInfo) {
public Response getJax(@Context UriInfo uriInfo, @QueryParam(value = "baggageValue") String baggageValue) {
Assert.assertNotNull(Span.current());

try (Scope s = Baggage.builder().put("foo", "bar").build().makeCurrent()) {
try (Scope s = Baggage.builder().put("foo", baggageValue).build().makeCurrent()) {
Baggage baggage = Baggage.current();
Assert.assertEquals("bar", baggage.getEntryValue("foo"));
Assert.assertEquals(baggageValue, baggage.getEntryValue("foo"));

String url = new String(uriInfo.getAbsolutePath().toString());
url = url.replace("jaxrsclient", "jaxrstwo"); // The jaxrsclient will use the URL as given so it needs
// the final part to be provided.
url = url.replace("jaxrsclient", "jaxrstwo"); // The jaxrsclient will use
// the URL as given so it
// needs
// the final part to be provided.

String result = client.target(url)
.queryParam("baggageValue", baggageValue)
.request(MediaType.TEXT_PLAIN)
.get(String.class);
Assert.assertEquals(TEST_PASSED, result);
Expand All @@ -92,19 +97,22 @@ public Response getJax(@Context UriInfo uriInfo) {

@GET
@Path("/jaxrsclientasync")
public Response getJaxAsync(@Context UriInfo uriInfo) {
public Response getJaxAsync(@Context UriInfo uriInfo, @QueryParam(value = "baggageValue") String baggageValue) {
Assert.assertNotNull(Span.current());

try (Scope s = Baggage.builder().put("foo", "bar").build().makeCurrent()) {
try (Scope s = Baggage.builder().put("foo", baggageValue).build().makeCurrent()) {
Baggage baggage = Baggage.current();
Assert.assertEquals("bar", baggage.getEntryValue("foo"));
Assert.assertEquals(baggageValue, baggage.getEntryValue("foo"));

String url = new String(uriInfo.getAbsolutePath().toString());
url = url.replace("jaxrsclientasync", "jaxrstwo"); // The jaxrsclient will use the URL as given so it needs
// the final part to be provided.
url = url.replace("jaxrsclientasync", "jaxrstwo"); // The jaxrsclient will
// use the URL as given
// so it needs
// the final part to be provided.

Client client = ClientBuilder.newClient();
Future<String> result = client.target(url)
.queryParam("baggageValue", baggageValue)
.request(MediaType.TEXT_PLAIN)
.async()
.get(String.class);
Expand All @@ -120,16 +128,53 @@ public Response getJaxAsync(@Context UriInfo uriInfo) {
return Response.ok(Span.current().getSpanContext().getTraceId()).build();
}

@GET
@Path("/jaxrsclienterror")
public Response getJaxError(@Context UriInfo uriInfo, @QueryParam(value = "baggageValue") String baggageValue) {
Assert.assertNotNull(Span.current());

try (Scope s = Baggage.builder().put("foo", baggageValue).build().makeCurrent()) {
Baggage baggage = Baggage.current();
Assert.assertEquals(baggageValue, baggage.getEntryValue("foo"));

String url = new String(uriInfo.getAbsolutePath().toString());
url = url.replace("jaxrsclienterror", "error"); // The jaxrsclient will
// use the URL as given
// so it needs
// the final part to be provided.

Client client = ClientBuilder.newClient();
Future<String> result = client.target(url)
.request(MediaType.TEXT_PLAIN)
.async()
.get(String.class);
try {
Assert.assertEquals(result.get(10, SECONDS), HTTP_INTERNAL_ERROR);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
client.close();
}
}
return Response.ok(Span.current().getSpanContext().getTraceId()).build();
}

// A method to be called by JAX Clients
// This method triggers span creation and span propagation is automatic.
@GET
@Path("/jaxrstwo")
public Response getJaxRsTwo() {
public Response getJaxRsTwo(@QueryParam(value = "baggageValue") String baggageValue) {
Assert.assertNotNull(Span.current());
Baggage baggage = Baggage.current();
// Assert that Baggage is propagated from Jax Server to Jax Client
Assert.assertEquals("bar", baggage.getEntryValue("foo"));
Assert.assertEquals(baggageValue, baggage.getEntryValue("foo"));
return Response.ok(TEST_PASSED).build();
}

@GET
@Path("/error")
public Response getError() {
return Response.serverError().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

package org.eclipse.microprofile.telemetry.tracing.tck.async;

import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_STATUS_CODE;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_TARGET;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static org.eclipse.microprofile.telemetry.tracing.tck.async.JaxRsServerAsyncTestEndpoint.BAGGAGE_VALUE_ATTR;
import static org.testng.Assert.assertEquals;

Expand Down Expand Up @@ -70,6 +73,7 @@ public static WebArchive createDeployment() {
}

private static final String TEST_VALUE = "test.value";
public static final String QUERY_VALUE = "bar";

@Inject
private InMemorySpanExporter spanExporter;
Expand All @@ -87,12 +91,22 @@ void setUp() {

@Test(groups = "optional-jaxrs-tests")
public void testJaxRsServerAsyncCompletionStage() {
doAsyncTest(JaxRsServerAsyncTestEndpointClient::getCompletionStage);
doAsyncTest((client) -> client.getCompletionStage(QUERY_VALUE));
}

@Test(groups = "optional-jaxrs-tests") // , expectedExceptions = {jakarta.ws.rs.WebApplicationException.class})
public void testJaxRsServerAsyncCompletionStageError() {
doErrorAsyncTest((client) -> client.getCompletionStageError(QUERY_VALUE));
}

@Test(groups = "optional-jaxrs-tests")
public void testJaxRsServerAsyncSuspend() {
doAsyncTest(JaxRsServerAsyncTestEndpointClient::getSuspend);
doAsyncTest((client) -> client.getSuspend(QUERY_VALUE));
}

@Test(groups = "optional-jaxrs-tests") // , expectedExceptions = {jakarta.ws.rs.WebApplicationException.class})
public void testJaxRsServerAsyncSuspendError() {
doErrorAsyncTest((client) -> client.getSuspendError(QUERY_VALUE));
}

private void doAsyncTest(Function<JaxRsServerAsyncTestEndpointClient, String> requestFunction) {
Expand All @@ -106,7 +120,6 @@ private void doAsyncTest(Function<JaxRsServerAsyncTestEndpointClient, String> re
JaxRsServerAsyncTestEndpointClient client = RestClientBuilder.newBuilder()
.baseUri(url.toURI())
.build(JaxRsServerAsyncTestEndpointClient.class);

String response = requestFunction.apply(client);
Assert.assertEquals("OK", response);
} catch (URISyntaxException e) {
Expand All @@ -131,6 +144,61 @@ private void doAsyncTest(Function<JaxRsServerAsyncTestEndpointClient, String> re
// Assert baggage propagated on subtask span
Assert.assertTrue(subtaskSpan.getAttributes().get(BAGGAGE_VALUE_ATTR).contains(TEST_VALUE));

// Assert that query parameter was passed correctly
Assert.assertTrue(serverSpan.getAttributes().get(HTTP_TARGET).contains(QUERY_VALUE));

// Assert that the server span finished after the subtask span
// Even though the resource method returned quickly, the span should not end until the response is actually
// returned
Assert.assertTrue(serverSpan.getEndEpochNanos() >= subtaskSpan.getEndEpochNanos());
}

private void doErrorAsyncTest(Function<JaxRsServerAsyncTestEndpointClient, String> requestFunction) {
Baggage baggage = Baggage.builder()
.put(JaxRsServerAsyncTestEndpoint.BAGGAGE_KEY, TEST_VALUE)
.build();

try (Scope s = baggage.makeCurrent()) {
// Make the request to the test endpoint
try {
JaxRsServerAsyncTestEndpointClient client = RestClientBuilder.newBuilder()
.baseUri(url.toURI())
.build(JaxRsServerAsyncTestEndpointClient.class);
try {
requestFunction.apply(client);
} catch (WebApplicationException e) {
readErrorSpans();
}
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}

private void readErrorSpans() {
List<SpanData> spanData = spanExporter.getFinishedSpanItems(3);

SpanData subtaskSpan = spanExporter.getFirst(SpanKind.INTERNAL);
SpanData clientSpan = spanExporter.getFirst(SpanKind.CLIENT);
SpanData serverSpan = spanExporter.getFirst(SpanKind.SERVER);

// Assert correct parent-child links
// Shows that propagation occurred
assertEquals(serverSpan.getSpanId(), subtaskSpan.getParentSpanId());
assertEquals(clientSpan.getSpanId(), serverSpan.getParentSpanId());

// Assert error is in span from method subtaskError()
assertEquals(HTTP_INTERNAL_ERROR, clientSpan.getAttributes().get(HTTP_STATUS_CODE).intValue());

// Assert that the expected headers were used
Assert.assertTrue(serverSpan.getAttributes().get(BAGGAGE_VALUE_ATTR).contains(TEST_VALUE));

// Assert baggage propagated on subtask span
Assert.assertTrue(subtaskSpan.getAttributes().get(BAGGAGE_VALUE_ATTR).contains(TEST_VALUE));

// Assert that query parameter was passed correctly
Assert.assertTrue(serverSpan.getAttributes().get(HTTP_TARGET).contains(QUERY_VALUE));

// Assert that the server span finished after the subtask span
// Even though the resource method returned quickly, the span should not end until the response is actually
// returned
Expand Down

0 comments on commit 648f1e9

Please sign in to comment.