11package io .dapr .it .actors ;
22
3+ import com .jayway .jsonpath .DocumentContext ;
4+ import com .jayway .jsonpath .JsonPath ;
35import io .dapr .actors .ActorId ;
46import io .dapr .actors .client .ActorProxyBuilder ;
57import io .dapr .client .DaprClient ;
68import io .dapr .it .BaseIT ;
79import io .dapr .it .actors .app .MyActor ;
810import io .dapr .it .actors .app .MyActorService ;
9- import io .dapr .it .tracing .Validation ;
1011import io .dapr .it .tracing .http .OpenTelemetryConfig ;
1112import io .opentelemetry .api .OpenTelemetry ;
1213import io .opentelemetry .api .trace .Span ;
1314import io .opentelemetry .api .trace .SpanKind ;
1415import io .opentelemetry .api .trace .Tracer ;
1516import io .opentelemetry .context .Scope ;
17+ import net .minidev .json .JSONArray ;
18+ import okhttp3 .HttpUrl ;
19+ import okhttp3 .OkHttpClient ;
20+ import okhttp3 .Request ;
21+ import okhttp3 .Response ;
1622import org .junit .jupiter .api .Test ;
1723
1824import java .util .UUID ;
1925
2026import static io .dapr .it .tracing .OpenTelemetry .createOpenTelemetry ;
2127import static io .dapr .it .tracing .OpenTelemetry .getReactorContext ;
28+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
29+ import static org .junit .jupiter .api .Assertions .assertTrue ;
2230
2331public class ActorTracingIT extends BaseIT {
2432
2533 @ Test
2634 public void testInvoke () throws Exception {
2735 var run = startDaprApp (
28- ActorTracingIT .class .getSimpleName (),
36+ ActorTracingIT .class .getSimpleName ()+ "Server" ,
2937 MyActorService .SUCCESS_MESSAGE ,
3038 MyActorService .class ,
3139 true ,
@@ -34,26 +42,83 @@ public void testInvoke() throws Exception {
3442 ActorTracingIT .class .getSimpleName ()+"Client" ,
3543 60000 );
3644
37- OpenTelemetry openTelemetry = createOpenTelemetry (OpenTelemetryConfig . SERVICE_NAME );
45+ OpenTelemetry openTelemetry = createOpenTelemetry ();
3846 Tracer tracer = openTelemetry .getTracer (OpenTelemetryConfig .TRACER_NAME );
3947 String spanName = UUID .randomUUID ().toString ();
4048 Span span = tracer .spanBuilder (spanName ).setSpanKind (SpanKind .CLIENT ).startSpan ();
4149
4250 try (DaprClient client = run .newDaprClientBuilder ().build ()) {
43- client .waitForSidecar (10000 ).block ();
4451 MyActor myActor =
4552 new ActorProxyBuilder <>(
4653 "MyActorTest" ,
4754 MyActor .class ,
4855 clientRun .newActorClient ()).build (new ActorId ("123456" ));
4956 try (Scope scope = span .makeCurrent ()) {
50- myActor .say ("hello world" ).contextWrite (getReactorContext (openTelemetry )).block ();
57+ client .waitForSidecar (10000 ).block ();
58+ myActor .say ("hello world" )
59+ .contextWrite (getReactorContext (openTelemetry ))
60+ .block ();
5161 }
62+ } finally {
63+ span .end ();
5264 }
5365
54- span .end ();
55-
56- Validation .validate (spanName , "calllocal/tracingithttp-service/say" );
66+ Validation .validateGrandChild (
67+ spanName ,
68+ "/dapr.proto.runtime.v1.dapr/invokeactor" ,
69+ "callactor/myactortest/say" );
5770 }
5871
72+ private static final class Validation {
73+
74+ private static final OkHttpClient HTTP_CLIENT = new OkHttpClient ();
75+
76+ /**
77+ * JSON Path for main span Id.
78+ */
79+ public static final String JSONPATH_MAIN_SPAN_ID = "$..[?(@.name == \" %s\" )]['id']" ;
80+
81+ /**
82+ * JSON Path for child span.
83+ */
84+ public static final String JSONPATH_PARENT_SPAN_ID =
85+ "$..[?(@.parentId=='%s' && @.name=='%s')]['id']" ;
86+
87+ public static void validateGrandChild (String grandParentSpanName , String parentSpanName , String grandChildSpanName ) throws Exception {
88+ // Must wait for some time to make sure Zipkin receives all spans.
89+ Thread .sleep (10000 );
90+ HttpUrl .Builder urlBuilder = new HttpUrl .Builder ();
91+ urlBuilder .scheme ("http" )
92+ .host ("localhost" )
93+ .port (9411 )
94+ .addPathSegments ("api/v2/traces" )
95+ .addQueryParameter ("limit" , "100" );
96+ Request .Builder requestBuilder = new Request .Builder ()
97+ .url (urlBuilder .build ());
98+ requestBuilder .method ("GET" , null );
99+
100+ Request request = requestBuilder .build ();
101+
102+ Response response = HTTP_CLIENT .newCall (request ).execute ();
103+ DocumentContext documentContext = JsonPath .parse (response .body ().string ());
104+ String grandParentSpanId = readOne (documentContext , String .format (JSONPATH_MAIN_SPAN_ID , grandParentSpanName )).toString ();
105+ assertNotNull (grandParentSpanId );
106+
107+ String parentSpanId = readOne (documentContext , String .format (JSONPATH_PARENT_SPAN_ID , grandParentSpanId , parentSpanName ))
108+ .toString ();
109+ assertNotNull (parentSpanId );
110+
111+ String grandChildSpanId = readOne (documentContext , String .format (JSONPATH_PARENT_SPAN_ID , parentSpanId , grandChildSpanName ))
112+ .toString ();
113+ assertNotNull (grandChildSpanId );
114+ }
115+
116+ private static Object readOne (DocumentContext documentContext , String path ) {
117+ JSONArray arr = documentContext .read (path );
118+ assertTrue (arr .size () > 0 );
119+
120+ return arr .get (0 );
121+ }
122+
123+ }
59124}
0 commit comments