|
5 | 5 |
|
6 | 6 | package io.opentelemetry.javaagent.instrumentation.finatra
|
7 | 7 |
|
8 |
| -import com.twitter.finagle.http.{Request, Response} |
| 8 | +import com.twitter.finagle.http.Request |
9 | 9 | import com.twitter.finatra.http.Controller
|
10 |
| -import com.twitter.util.Future |
11 |
| -import groovy.lang.Closure |
12 |
| -import io.opentelemetry.instrumentation.test.base.HttpServerTest.controller |
| 10 | +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest.controller |
13 | 11 | import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint._
|
| 12 | +import io.opentelemetry.instrumentation.testing.util.ThrowingSupplier |
14 | 13 |
|
15 | 14 | class FinatraController extends Controller {
|
16 | 15 | any(SUCCESS.getPath) { request: Request =>
|
17 | 16 | controller(
|
18 | 17 | SUCCESS,
|
19 |
| - new Closure[Response](null) { |
20 |
| - override def call(): Response = { |
21 |
| - response.ok(SUCCESS.getBody) |
22 |
| - } |
23 |
| - } |
| 18 | + supplier(() => response.ok(SUCCESS.getBody)) |
24 | 19 | )
|
25 | 20 | }
|
26 | 21 |
|
27 | 22 | any(ERROR.getPath) { request: Request =>
|
28 | 23 | controller(
|
29 | 24 | ERROR,
|
30 |
| - new Closure[Response](null) { |
31 |
| - override def call(): Response = { |
32 |
| - response.internalServerError(ERROR.getBody) |
33 |
| - } |
34 |
| - } |
| 25 | + supplier(() => response.internalServerError(ERROR.getBody)) |
35 | 26 | )
|
36 | 27 | }
|
37 | 28 |
|
38 | 29 | any(QUERY_PARAM.getPath) { request: Request =>
|
39 | 30 | controller(
|
40 | 31 | QUERY_PARAM,
|
41 |
| - new Closure[Response](null) { |
42 |
| - override def call(): Response = { |
43 |
| - response.ok(QUERY_PARAM.getBody) |
44 |
| - } |
45 |
| - } |
| 32 | + supplier(() => response.ok(QUERY_PARAM.getBody)) |
46 | 33 | )
|
47 | 34 | }
|
48 | 35 |
|
49 | 36 | any(EXCEPTION.getPath) { request: Request =>
|
50 | 37 | controller(
|
51 | 38 | EXCEPTION,
|
52 |
| - new Closure[Future[Response]](null) { |
53 |
| - override def call(): Future[Response] = { |
54 |
| - throw new Exception(EXCEPTION.getBody) |
55 |
| - } |
56 |
| - } |
| 39 | + supplier(() => throw new Exception(EXCEPTION.getBody)) |
57 | 40 | )
|
58 | 41 | }
|
59 | 42 |
|
60 | 43 | any(REDIRECT.getPath) { request: Request =>
|
61 | 44 | controller(
|
62 | 45 | REDIRECT,
|
63 |
| - new Closure[Response](null) { |
64 |
| - override def call(): Response = { |
65 |
| - response.found.location(REDIRECT.getBody) |
66 |
| - } |
67 |
| - } |
| 46 | + supplier(() => response.found.location(REDIRECT.getBody)) |
68 | 47 | )
|
69 | 48 | }
|
70 | 49 |
|
71 | 50 | any(CAPTURE_HEADERS.getPath) { request: Request =>
|
72 | 51 | controller(
|
73 | 52 | CAPTURE_HEADERS,
|
74 |
| - new Closure[Response](null) { |
75 |
| - override def call(): Response = { |
76 |
| - response |
77 |
| - .ok(CAPTURE_HEADERS.getBody) |
78 |
| - .header( |
79 |
| - "X-Test-Response", |
80 |
| - request.headerMap.get("X-Test-Request").get |
81 |
| - ) |
82 |
| - } |
83 |
| - } |
| 53 | + supplier(() => |
| 54 | + response |
| 55 | + .ok(CAPTURE_HEADERS.getBody) |
| 56 | + .header( |
| 57 | + "X-Test-Response", |
| 58 | + request.headerMap.get("X-Test-Request").get |
| 59 | + ) |
| 60 | + ) |
84 | 61 | )
|
85 | 62 | }
|
86 | 63 |
|
87 | 64 | any(INDEXED_CHILD.getPath) { request: Request =>
|
88 | 65 | controller(
|
89 | 66 | INDEXED_CHILD,
|
90 |
| - new Closure[Response](null) { |
91 |
| - override def call(): Response = { |
92 |
| - INDEXED_CHILD.collectSpanAttributes(new UrlParameterProvider { |
93 |
| - override def getParameter(name: String): String = |
94 |
| - request.getParam(name) |
95 |
| - }) |
96 |
| - response.ok(INDEXED_CHILD.getBody) |
97 |
| - } |
98 |
| - } |
| 67 | + supplier(() => { |
| 68 | + INDEXED_CHILD.collectSpanAttributes(new UrlParameterProvider { |
| 69 | + override def getParameter(name: String): String = |
| 70 | + request.getParam(name) |
| 71 | + }) |
| 72 | + response.ok(INDEXED_CHILD.getBody) |
| 73 | + }) |
99 | 74 | )
|
100 | 75 | }
|
101 | 76 |
|
102 | 77 | any("/path/:id/param") { request: Request =>
|
103 | 78 | controller(
|
104 | 79 | PATH_PARAM,
|
105 |
| - new Closure[Response](null) { |
106 |
| - override def call(): Response = { |
107 |
| - response.ok(request.params("id")) |
108 |
| - } |
109 |
| - } |
| 80 | + supplier(() => response.ok(request.params("id"))) |
110 | 81 | )
|
111 | 82 | }
|
| 83 | + |
| 84 | + def supplier[A](action: () => A): ThrowingSupplier[A, Exception] = { |
| 85 | + new ThrowingSupplier[A, Exception] { |
| 86 | + def get(): A = { |
| 87 | + action.apply() |
| 88 | + } |
| 89 | + } |
| 90 | + } |
112 | 91 | }
|
0 commit comments