/
RestClientSpanDisabledTest.java
246 lines (209 loc) · 7.68 KB
/
RestClientSpanDisabledTest.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* 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.INTERNAL;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_OK;
import java.net.URISyntaxException;
import java.net.URL;
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.RestClientDefinitionException;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
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.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Response;
class RestClientSpanDisabledTest extends Arquillian {
@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(InMemorySpanExporter.class, InMemorySpanExporterProvider.class)
.addAsLibrary(TestLibraries.AWAITILITY_LIB)
.addAsServiceProvider(ConfigurableSpanExporterProvider.class, InMemorySpanExporterProvider.class)
.addAsResource(new StringAsset("otel.experimental.sdk.enabled=false\notel.traces.exporter=in-memory"),
"META-INF/microprofile-config.properties");
}
@ArquillianResource
URL url;
@Inject
InMemorySpanExporter spanExporter;
SpanResourceClient client;
@BeforeMethod
void setUp() {
// Only want to run on server
if (spanExporter != null) {
spanExporter.reset();
try {
// Create client manually so we can pass in URL from arquillian
client = RestClientBuilder.newBuilder().baseUri(url.toURI()).build(SpanResourceClient.class);
} catch (IllegalStateException | RestClientDefinitionException | URISyntaxException e) {
Assert.fail("Failed to create rest client", e);
}
}
}
@Test
void span() {
Response response = client.span();
Assert.assertEquals(response.getStatus(), HTTP_OK);
spanExporter.getFinishedSpanItems(0);
}
@Test
void spanName() {
Response response = client.spanName("1");
Assert.assertEquals(response.getStatus(), HTTP_OK);
spanExporter.getFinishedSpanItems(0);
}
@Test
void spanNameQuery() {
Response response = client.spanNameQuery("1", "query");
Assert.assertEquals(response.getStatus(), HTTP_OK);
spanExporter.getFinishedSpanItems(0);
}
@Test
void spanError() {
// Can't use REST Client here due to org.jboss.resteasy.microprofile.client.DefaultResponseExceptionMapper
WebTarget target = ClientBuilder.newClient().target(url.toString() + "span/error");
Response response = target.request().get();
Assert.assertEquals(response.getStatus(), HTTP_INTERNAL_ERROR);
spanExporter.getFinishedSpanItems(0);
}
@Test
void spanChild() {
Response response = client.spanChild();
Assert.assertEquals(response.getStatus(), HTTP_OK);
spanExporter.getFinishedSpanItems(0);
}
@Test
void spanCurrent() {
Response response = client.spanCurrent();
Assert.assertEquals(response.getStatus(), HTTP_OK);
spanExporter.getFinishedSpanItems(0);
}
@Test
void spanNew() {
Response response = client.spanNew();
Assert.assertEquals(response.getStatus(), HTTP_OK);
spanExporter.getFinishedSpanItems(0);
}
@RequestScoped
@Path("/")
public static class SpanResource {
@Inject
SpanBean spanBean;
@Inject
Span span;
@Inject
Tracer tracer;
@GET
@Path("/span")
public Response span() {
return Response.ok().build();
}
@GET
@Path("/span/{name}")
public Response spanName(@PathParam(value = "name") String name, @QueryParam("query") String query) {
return Response.ok().build();
}
@GET
@Path("/span/error")
public Response spanError() {
return Response.serverError().build();
}
@GET
@Path("/span/child")
public Response spanChild() {
spanBean.spanChild();
return Response.ok().build();
}
@GET
@Path("/span/current")
public Response spanCurrent() {
span.setAttribute("tck.current.key", "tck.current.value");
return Response.ok().build();
}
@GET
@Path("/span/new")
public Response spanNew() {
Span span = tracer.spanBuilder("span.new")
.setSpanKind(INTERNAL)
.setParent(Context.current().with(this.span))
.setAttribute("tck.new.key", "tck.new.value")
.startSpan();
span.end();
return Response.ok().build();
}
}
@ApplicationScoped
public static class SpanBean {
@WithSpan
void spanChild() {
}
}
@RegisterRestClient(configKey = "client")
@Path("/")
public interface SpanResourceClient {
@GET
@Path("/span")
Response span();
@GET
@Path("/span/{name}")
Response spanName(@PathParam(value = "name") String name);
@GET
@Path("/span/{name}")
Response spanNameQuery(@PathParam(value = "name") String name, @QueryParam("query") String query);
@GET
@Path("/span/child")
Response spanChild();
@GET
@Path("/span/current")
Response spanCurrent();
@GET
@Path("/span/new")
Response spanNew();
}
@ApplicationPath("/")
public static class RestApplication extends Application {
}
}