I'm trying to test some opentelemetry spans are correctly build and linked in parent child relations.

clientSpan = Span.wrap(
                SpanContext.createFromRemoteParent(
                  "12345678123456781234567812345678",
                  "1234567812345678",
                  TraceFlags.getDefault,
                  TraceState.getDefault
                )
              )
              .updateName("clientSpan") 

And the code which I want to test creates a new span which is the child span of the client span which it receives

tracer.spanBuilder("my-endpoint")
.setSpanKind(SpanKind.SERVER)
.setParent(Context.current().`with`(clientSpan))
.startSpan()

My problem is that whenever I pass the clientSpan the InMemorySpanExporter.getFinishedSpanItems() returns empty. It returns non-empty and as expected when no clientSpan is used.

In the unit test i tried both ending the parent span or leaving it open: clientSpan.end() but getFinishedSpanItems is always empty.

Maybe I'm not building correctly the clientSpan in my unit test ? (in prod code it gets extracted from the carrier-propagator-getter combo)


Solution 1:

The wrap returns a non-recording span that contains the provided span context but has no functionality. All the operations are no-op. So whatever you do clientSpan has no effect.

Please change the TraceFlags.getDefault() to TraceFlags.getSampled(). The default returns a flag with all bit off which means the no child spans will be considered