/
RestSpanTest.java
188 lines (162 loc) · 7.91 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* 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_ROUTE;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_SCHEME;
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.NET_HOST_NAME;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_PORT;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import org.eclipse.microprofile.telemetry.tracing.tck.BasicHttpClient;
import org.eclipse.microprofile.telemetry.tracing.tck.ConfigAsset;
import org.eclipse.microprofile.telemetry.tracing.tck.TestLibraries;
import org.eclipse.microprofile.telemetry.tracing.tck.exporter.InMemorySpanExporter;
import org.eclipse.microprofile.telemetry.tracing.tck.exporter.InMemorySpanExporterProvider;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.trace.data.SpanData;
import jakarta.inject.Inject;
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;
class RestSpanTest extends Arquillian {
private static final String TEST_SERVICE_NAME = "org/eclipse/microprofile/telemetry/tracing/tck";
private static final String TEST_SERVICE_VERSION = "0.1.0-TEST";
@Deployment
public static WebArchive createDeployment() {
ConfigAsset config = new ConfigAsset()
.add("otel.service.name", TEST_SERVICE_NAME)
.add("otel.resource.attributes", SERVICE_VERSION.getKey() + "=" + TEST_SERVICE_VERSION)
.add("otel.sdk.disabled", "false")
.add("otel.traces.exporter", "in-memory");
return ShrinkWrap.create(WebArchive.class)
.addClasses(InMemorySpanExporter.class, InMemorySpanExporterProvider.class, BasicHttpClient.class)
.addAsLibrary(TestLibraries.AWAITILITY_LIB)
.addAsServiceProvider(ConfigurableSpanExporterProvider.class, InMemorySpanExporterProvider.class)
.addAsResource(config, "META-INF/microprofile-config.properties");
}
@ArquillianResource
URL url;
@Inject
InMemorySpanExporter spanExporter;
private BasicHttpClient basicClient;
@BeforeMethod
void setUp() {
// Only want to run on server
if (spanExporter != null) {
spanExporter.reset();
basicClient = new BasicHttpClient(url);
}
}
private void assertServerSpan(SpanData server, String path) {
assertServerSpan(server, path, HTTP_OK);
}
private void assertServerSpan(SpanData server, String path, int statusCode) {
Assert.assertEquals(server.getKind(), SERVER);
Assert.assertEquals(server.getAttributes().get(HTTP_STATUS_CODE).intValue(), statusCode);
Assert.assertEquals(server.getAttributes().get(HTTP_METHOD), HttpMethod.GET);
Assert.assertEquals(server.getAttributes().get(HTTP_SCHEME), url.getProtocol());
Assert.assertEquals(server.getAttributes().get(HTTP_TARGET), url.getPath() + path);
// route is required when available, definitely available for REST endpoints
Assert.assertNotNull(server.getAttributes().get(HTTP_ROUTE));
// not asserting specific value as it is only recommended, and should contain application prefix
Assert.assertEquals(server.getAttributes().get(NET_HOST_NAME), url.getHost());
if (url.getPort() != url.getDefaultPort()) {
Assert.assertEquals(server.getAttributes().get(NET_HOST_PORT).intValue(), url.getPort());
}
}
@Test
void span() throws URISyntaxException, IOException {
assertEquals(basicClient.get("/span"), HTTP_OK);
List<SpanData> spanItems = spanExporter.getFinishedSpanItems(1);
assertEquals(spanItems.size(), 1);
SpanData span = spanItems.get(0);
assertServerSpan(span, "span");
assertEquals(
span.getResource().getAttribute(SERVICE_NAME),
TEST_SERVICE_NAME);
assertEquals(span.getResource().getAttribute(SERVICE_VERSION), TEST_SERVICE_VERSION);
InstrumentationScopeInfo libraryInfo = span.getInstrumentationScopeInfo();
// Was decided at the MP Call on 13/06/2022 that lib name and version are responsibility of lib implementations
assertNotNull(libraryInfo.getName());
}
@Test
void spanName() {
assertEquals(basicClient.get("/span/1"), HTTP_OK);
List<SpanData> spanItems = spanExporter.getFinishedSpanItems(1);
assertEquals(spanItems.size(), 1);
SpanData span = spanItems.get(0);
assertServerSpan(span, "span/1");
Assert.assertFalse(span.getName().contains("span/1"),
"Span name should not contain full path when using @PathParam");
Assert.assertTrue(span.getAttributes().get(HTTP_ROUTE).contains("span/{name}"),
"Route should contain path template");
}
@Test
void spanNameWithoutQueryString() {
assertEquals(basicClient.get("/span/1?id=1"), HTTP_OK);
List<SpanData> spanItems = spanExporter.getFinishedSpanItems(1);
assertEquals(spanItems.size(), 1);
SpanData span = spanItems.get(0);
assertServerSpan(span, "span/1?id=1");
Assert.assertFalse(span.getName().contains("=1"), "Span name should not contain query when using @QueryParam");
Assert.assertFalse(span.getAttributes().get(HTTP_ROUTE).contains("=1"),
"Route should not contain query when using @QueryParam");
}
@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 {
}
}