@@ -166,7 +166,57 @@ class W3CHttpInjectorTest extends DDCoreSpecification {
166166 tracer. close()
167167 }
168168
169+ def " update last parent id on child span" () {
170+ setup :
171+ def writer = new ListWriter ()
172+ def tracer = tracerBuilder(). writer(writer). build()
173+ final Map<String , String > carrier = [:]
174+
175+ when : ' injecting root span context'
176+ def rootSpan = tracer. startSpan(' test' , ' root' )
177+ def rootSpanId = rootSpan. spanId
178+ def rootScope = tracer. activateSpan(rootSpan)
179+
180+ injector. inject(rootSpan. context() as DDSpanContext , carrier, MapSetter . INSTANCE )
181+ def lastParentId = extractLastParentId(carrier)
182+
183+ then : ' trace state has root span id as last parent'
184+ lastParentId == rootSpanId
185+
186+ when : ' injecting child span context'
187+ def childSpan = tracer. startSpan(' test' , ' child' )
188+ def childSpanId = childSpan. spanId
189+ carrier. clear()
190+ injector. inject(childSpan. context() as DDSpanContext , carrier, MapSetter . INSTANCE )
191+ lastParentId = extractLastParentId(carrier)
192+
193+ then : ' trace state has child span id as last parent'
194+ lastParentId == childSpanId
195+
196+ when : ' injecting root span again'
197+ childSpan. finish()
198+ carrier. clear()
199+ injector. inject(rootSpan. context() as DDSpanContext , carrier, MapSetter . INSTANCE )
200+ lastParentId = extractLastParentId(carrier)
201+
202+ then : ' trace state has root span is as last parent again'
203+ lastParentId == rootSpanId
204+
205+ cleanup :
206+ rootScope. close()
207+ rootSpan. finish()
208+ }
209+
169210 static String buildTraceParent (String traceId , String spanId , int samplingPriority ) {
170211 return " 00-${ DDTraceId.from(traceId).toHexString()} -${ DDSpanId.toHexStringPadded(DDSpanId.from(spanId))} -${ samplingPriority > 0 ? '01': '00'} "
171212 }
213+
214+ static long extractLastParentId (Map<String , String > carrier ) {
215+ def traceState = carrier[TRACE_STATE_KEY ]
216+ def traceStateMembers = traceState. split(' ,' )
217+ def ddTraceStateMember = traceStateMembers. find { it. startsWith(" dd=" )}. substring(3 )
218+ def parts = ddTraceStateMember. split(' ;' )
219+ def spanIdHex = parts. find { it. startsWith(' p:' )}. substring(2 )
220+ DDSpanId . fromHex(spanIdHex)
221+ }
172222}
0 commit comments