Skip to content

Commit 1a37d5f

Browse files
committed
span filter
1 parent 915268e commit 1a37d5f

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

instrumentation/java-http-server/testing/src/main/java/io/opentelemetry/instrumentation/httpserver/AbstractJdkHttpServerTest.java

+31-23
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,17 @@ protected Filter customFilter() {
4040
return null;
4141
}
4242

43-
void sendResponse(HttpExchange exchange, int status, String response) throws IOException {
43+
static void sendResponse(HttpExchange exchange, int status, String response) throws IOException {
4444
sendResponse(exchange, status, Collections.emptyMap(), response);
4545
}
4646

47-
void sendResponse(HttpExchange exchange, int status, Map<String, String> headers)
47+
static void sendResponse(HttpExchange exchange, int status, Map<String, String> headers)
4848
throws IOException {
4949
sendResponse(exchange, status, headers, "");
5050
}
5151

52-
void sendResponse(HttpExchange exchange, int status, Map<String, String> headers, String response)
52+
static void sendResponse(
53+
HttpExchange exchange, int status, Map<String, String> headers, String response)
5354
throws IOException {
5455

5556
byte[] bytes = response.getBytes(Charset.defaultCharset());
@@ -178,27 +179,8 @@ protected HttpServer setupServer() throws IOException {
178179
}
179180

180181
// Make sure user decorators see spans.
181-
Filter spanFilter =
182-
new Filter() {
182+
Filter spanFilter = new SpanFilter();
183183

184-
@Override
185-
public void doFilter(HttpExchange exchange, Chain chain) throws IOException {
186-
187-
if (!Span.current().getSpanContext().isValid()) {
188-
// Return an invalid code to fail any assertion
189-
190-
exchange.sendResponseHeaders(601, -1);
191-
}
192-
exchange.getResponseHeaders().set("decoratingfunction", "ok");
193-
exchange.getResponseHeaders().set("decoratinghttpservicefunction", "ok");
194-
chain.doFilter(exchange);
195-
}
196-
197-
@Override
198-
public String description() {
199-
return "test";
200-
}
201-
};
202184
contexts.forEach(ctx -> ctx.getFilters().add(spanFilter));
203185
server.start();
204186

@@ -226,4 +208,30 @@ protected void configure(HttpServerTestOptions options) {
226208
options.setTestPathParam(false);
227209
options.setTestException(false);
228210
}
211+
212+
static class SpanFilter extends Filter {
213+
214+
@Override
215+
public void doFilter(HttpExchange exchange, Chain chain) throws IOException {
216+
217+
if (!Span.current().getSpanContext().isValid()) {
218+
// Return an invalid code to fail any assertion
219+
220+
exchange.sendResponseHeaders(601, -1);
221+
}
222+
exchange.getResponseHeaders().set("decoratingfunction", "ok");
223+
exchange.getResponseHeaders().set("decoratinghttpservicefunction", "ok");
224+
chain.doFilter(exchange);
225+
226+
// server will hang if nothing is sent
227+
if (exchange.getResponseCode() == -1) {
228+
sendResponse(exchange, 500, "nothing");
229+
}
230+
}
231+
232+
@Override
233+
public String description() {
234+
return "test";
235+
}
236+
}
229237
}

0 commit comments

Comments
 (0)