/
RestSpanTest.java
145 lines (124 loc) · 5.65 KB
/
RestSpanTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (c) 2016-2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.eclipse.microprofile.telemetry.tracing.tck.rest;
import static io.opentelemetry.api.trace.SpanKind.SERVER;
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME;
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_VERSION;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_METHOD;
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_OK;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.net.URL;
import java.util.List;
import javax.inject.Inject;
import org.eclipse.microprofile.telemetry.tracing.tck.exporter.InMemorySpanExporter;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit5.ArquillianExtension;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.restassured.RestAssured;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Response;
@ExtendWith(ArquillianExtension.class)
class RestSpanTest {
@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addAsResource(new StringAsset("otel.experimental.sdk.enabled=true"),
"META-INF/microprofile-config.properties");
}
@ArquillianResource
URL url;
@Inject
InMemorySpanExporter spanExporter;
@BeforeEach
void setUp() {
spanExporter.reset();
}
@Test
void span() {
RestAssured.given().get("/span").then().statusCode(HTTP_OK);
List<SpanData> spanItems = spanExporter.getFinishedSpanItems(1);
assertEquals(1, spanItems.size());
assertEquals(SERVER, spanItems.get(0).getKind());
assertEquals(url.getPath() + "span", spanItems.get(0).getName());
assertEquals(HTTP_OK, spanItems.get(0).getAttributes().get(HTTP_STATUS_CODE));
assertEquals(HttpMethod.GET, spanItems.get(0).getAttributes().get(HTTP_METHOD));
assertEquals("org/eclipse/microprofile/telemetry/tracing/tck",
spanItems.get(0).getResource().getAttribute(SERVICE_NAME));
assertEquals("0.1.0-SNAPSHOT", spanItems.get(0).getResource().getAttribute(SERVICE_VERSION));
// FIXME drop deprecated lib use
InstrumentationLibraryInfo libraryInfo = spanItems.get(0).getInstrumentationLibraryInfo();
// Was decided at the MP Call on 13/06/2022 that lib name and version are responsibility of lib implementations
assertNotNull(libraryInfo.getName());
assertNotNull(libraryInfo.getVersion());
}
@Test
void spanName() {
RestAssured.given().get("/span/1").then().statusCode(HTTP_OK);
List<SpanData> spanItems = spanExporter.getFinishedSpanItems(1);
assertEquals(1, spanItems.size());
assertEquals(SERVER, spanItems.get(0).getKind());
assertEquals(url.getPath() + "span/{name}", spanItems.get(0).getName());
assertEquals(HTTP_OK, spanItems.get(0).getAttributes().get(HTTP_STATUS_CODE));
assertEquals(HttpMethod.GET, spanItems.get(0).getAttributes().get(HTTP_METHOD));
}
@Test
void spanNameWithoutQueryString() {
RestAssured.given().get("/span/1?id=1").then().statusCode(HTTP_OK);
List<SpanData> spanItems = spanExporter.getFinishedSpanItems(1);
assertEquals(1, spanItems.size());
assertEquals(SERVER, spanItems.get(0).getKind());
assertEquals(url.getPath() + "span/{name}", spanItems.get(0).getName());
assertEquals(HTTP_OK, spanItems.get(0).getAttributes().get(HTTP_STATUS_CODE));
assertEquals(HttpMethod.GET, spanItems.get(0).getAttributes().get(HTTP_METHOD));
assertEquals(url.getPath() + "span/1?id=1", spanItems.get(0).getAttributes().get(HTTP_TARGET));
}
@Path("/")
public static class SpanResource {
@GET
@Path("/span")
public Response span() {
return Response.ok().build();
}
@GET
@Path("/span/{name}")
public Response spanName(@PathParam(value = "name") String name) {
return Response.ok().build();
}
}
@ApplicationPath("/")
public static class RestApplication extends Application {
}
}