5
5
6
6
package io .opentelemetry .javaagent .instrumentation .jsonrpc4j .v1_3 ;
7
7
8
+ import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .assertThat ;
9
+ import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .equalTo ;
10
+ import static io .opentelemetry .semconv .incubating .RpcIncubatingAttributes .RPC_JSONRPC_VERSION ;
11
+ import static io .opentelemetry .semconv .incubating .RpcIncubatingAttributes .RPC_METHOD ;
12
+ import static io .opentelemetry .semconv .incubating .RpcIncubatingAttributes .RPC_SERVICE ;
13
+ import static io .opentelemetry .semconv .incubating .RpcIncubatingAttributes .RPC_SYSTEM ;
14
+
15
+ import com .fasterxml .jackson .databind .ObjectMapper ;
8
16
import com .googlecode .jsonrpc4j .JsonRpcBasicServer ;
17
+ import com .googlecode .jsonrpc4j .JsonRpcHttpClient ;
18
+ import com .googlecode .jsonrpc4j .ProxyUtil ;
19
+ import com .googlecode .jsonrpc4j .spring .rest .JsonRpcRestClient ;
20
+ import io .opentelemetry .api .trace .SpanKind ;
9
21
import io .opentelemetry .instrumentation .jsonrpc4j .v1_3 .AbstractJsonRpcTest ;
22
+ import io .opentelemetry .instrumentation .jsonrpc4j .v1_3 .CalculatorService ;
23
+ import io .opentelemetry .instrumentation .jsonrpc4j .v1_3 .CalculatorServiceImpl ;
10
24
import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
11
25
import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
26
+ import java .net .MalformedURLException ;
27
+ import java .net .URL ;
28
+ import java .util .HashMap ;
29
+ import java .util .Map ;
30
+ import org .junit .jupiter .api .AfterAll ;
31
+ import org .junit .jupiter .api .BeforeAll ;
32
+ import org .junit .jupiter .api .Test ;
12
33
import org .junit .jupiter .api .extension .RegisterExtension ;
13
34
14
35
class AgentJsonRpcTest extends AbstractJsonRpcTest {
@@ -25,4 +46,99 @@ protected InstrumentationExtension testing() {
25
46
protected JsonRpcBasicServer configureServer (JsonRpcBasicServer server ) {
26
47
return server ;
27
48
}
49
+
50
+ @ Test
51
+ void testClient () throws Throwable {
52
+ CalculatorService clientProxy =
53
+ ProxyUtil .createClientProxy (
54
+ this .getClass ().getClassLoader (), CalculatorService .class , getHttpClient ());
55
+ int res =
56
+ testing ()
57
+ .runWithSpan (
58
+ "parent" ,
59
+ () -> {
60
+ return clientProxy .add (1 , 2 );
61
+ });
62
+
63
+ assertThat (res ).isEqualTo (3 );
64
+
65
+ testing ()
66
+ .waitAndAssertTraces (
67
+ trace ->
68
+ trace .hasSpansSatisfyingExactly (
69
+ span -> span .hasName ("parent" ).hasKind (SpanKind .INTERNAL ).hasNoParent (),
70
+ span ->
71
+ span .hasName (
72
+ "io.opentelemetry.instrumentation.jsonrpc4j.v1_3.CalculatorService/add" )
73
+ .hasKind (SpanKind .CLIENT )
74
+ .hasParent (trace .getSpan (0 ))
75
+ .hasAttributesSatisfyingExactly (
76
+ equalTo (RPC_SYSTEM , "jsonrpc" ),
77
+ equalTo (RPC_JSONRPC_VERSION , "2.0" ),
78
+ equalTo (
79
+ RPC_SERVICE ,
80
+ "io.opentelemetry.instrumentation.jsonrpc4j.v1_3.CalculatorService" ),
81
+ equalTo (RPC_METHOD , "add" ))),
82
+ trace -> trace .hasSpansSatisfyingExactly (span -> span .hasKind (SpanKind .SERVER )));
83
+
84
+ testing ()
85
+ .waitAndAssertMetrics (
86
+ "io.opentelemetry.jsonrpc4j-1.3" ,
87
+ "rpc.client.duration" ,
88
+ metrics ->
89
+ metrics .anySatisfy (
90
+ metric ->
91
+ assertThat (metric )
92
+ .hasUnit ("ms" )
93
+ .hasHistogramSatisfying (
94
+ histogram ->
95
+ histogram .hasPointsSatisfying (
96
+ point ->
97
+ point .hasAttributesSatisfying (
98
+ equalTo (RPC_METHOD , "add" ),
99
+ equalTo (
100
+ RPC_SERVICE ,
101
+ "io.opentelemetry.instrumentation.jsonrpc4j.v1_3.CalculatorService" ),
102
+ equalTo (RPC_SYSTEM , "jsonrpc" ))))));
103
+ }
104
+
105
+ private JettyServer jettyServer ;
106
+
107
+ @ BeforeAll
108
+ public void setup () throws Exception {
109
+ this .jettyServer = createServer ();
110
+ }
111
+
112
+ private static JettyServer createServer () throws Exception {
113
+ JettyServer jettyServer = new JettyServer (CalculatorServiceImpl .class );
114
+ jettyServer .startup ();
115
+ return jettyServer ;
116
+ }
117
+
118
+ protected JsonRpcRestClient getClient () throws MalformedURLException {
119
+ return getClient (JettyServer .SERVLET );
120
+ }
121
+
122
+ protected JsonRpcRestClient getClient (String servlet ) throws MalformedURLException {
123
+ return new JsonRpcRestClient (new URL (jettyServer .getCustomServerUrlString (servlet )));
124
+ }
125
+
126
+ protected JsonRpcHttpClient getHttpClient () throws MalformedURLException {
127
+ Map <String , String > header = new HashMap <>();
128
+ return new JsonRpcHttpClient (
129
+ new ObjectMapper (),
130
+ new URL (jettyServer .getCustomServerUrlString (JettyServer .SERVLET )),
131
+ header );
132
+ }
133
+
134
+ protected JsonRpcHttpClient getHttpClient (String servlet ) throws MalformedURLException {
135
+ Map <String , String > header = new HashMap <>();
136
+ return new JsonRpcHttpClient (
137
+ new ObjectMapper (), new URL (jettyServer .getCustomServerUrlString (servlet )), header );
138
+ }
139
+
140
+ @ AfterAll
141
+ public void teardown () throws Exception {
142
+ jettyServer .stop ();
143
+ }
28
144
}
0 commit comments