spring-projects-experimental / spring-cloud-sleuth-otel

Spring Cloud Sleuth support for OpenTelemetry
https://spring-projects-experimental.github.io/spring-cloud-sleuth-otel/docs/current/reference/html/
Apache License 2.0
111 stars 33 forks source link

Different behaviour, hard to control between implementations #156

Closed maciej-gromul closed 1 year ago

maciej-gromul commented 1 year ago

https://github.com/spring-projects-experimental/spring-cloud-sleuth-otel/blob/a449cd64898d2898c2d95a27735d3f50a5ea245b/spring-cloud-sleuth-otel/src/main/java/org/springframework/cloud/sleuth/otel/bridge/OtelSpanInScope.java#L47

Current otel implementation is behaving differently than default one with brave tracer. Brave tracer when it get's extracted parent context through propagator creates a new span with parent context connected. When you open scope for that span, any new span will be connected with that new span.

Because in otel the OtelSpanInScope changes context to parent context when creating new span it's like having a child span but the scope is always to it's parent. It's even different with the builder where BraveBuilder simply creates new span with parent context, while otel returns after calling start a new span with method return of

if (this.parentContext != null) {
  return OtelSpan.fromOtel(new SpanFromSpanContext(span, span.getSpanContext(), (OtelTraceContext) this.parentContext));
}

So for brave if we get kafka record with b3 headers of 000000000000000a-000000000000000b-1-000000000000000a, method extract gives us a child span of random spanId connected with trace id 000000000000000a, and if we use tracer.withSpan(child) we will have that child as a scope and current trace context.

In case of otel calling tracer.withSpan(child) will have parent span and trace context as current span and trace context.