Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

otel_tracer_noop:with_span/5 doesn't restore context properly #753

Open
DenysGonchar opened this issue Jun 30, 2024 · 1 comment
Open

otel_tracer_noop:with_span/5 doesn't restore context properly #753

DenysGonchar opened this issue Jun 30, 2024 · 1 comment

Comments

@DenysGonchar
Copy link

description:

otel_tracer_noop:with_span/5 doesn't restore context properly

here is a code snippet that demonstrates the problem:

require OpenTelemetry.Tracer, as: Tracer

ctx = Tracer.with_span :parent_span1 do
  OpenTelemetry.Ctx.get_current()
end |> IO.inspect(label: "parent_span1 context"); :ok

OpenTelemetry.set_default_tracer({:otel_tracer_noop, []})

Tracer.with_span :parent_span2 do
  OpenTelemetry.Ctx.get_current() |> IO.inspect(label: "parent_span2 context")
  Tracer.with_span ctx, :nested_span, %{} do
    OpenTelemetry.Ctx.get_current() |> IO.inspect(label: "nested_span context")
  end
  OpenTelemetry.Ctx.get_current() |> IO.inspect(label: "restored parent_span2 context")
end; :ok

and the output is:

parent_span1 context: %{
  {:otel_tracer, :span_ctx} => {:span_ctx,
   226093672235243343240248130524572830431, 14684213315684633963, 1,
   {:tracestate, []}, true, false, true,
   {:otel_span_ets, #Function<2.55784961/1 in :otel_tracer_server.on_end/1>}}
}

parent_span2 context: %{
  {:otel_tracer, :span_ctx} => {:span_ctx, 0, 0, 0, {:tracestate, []}, false,
   false, false, :undefined}
}
nested_span context: %{
  {:otel_tracer, :span_ctx} => {:span_ctx,
   226093672235243343240248130524572830431, 14684213315684633963, 1,
   {:tracestate, []}, true, false, true,
   {:otel_span_ets, #Function<2.55784961/1 in :otel_tracer_server.on_end/1>}}
}
restored parent_span2 context: %{
  {:otel_tracer, :span_ctx} => {:span_ctx,
   226093672235243343240248130524572830431, 14684213315684633963, 1,
   {:tracestate, []}, true, false, true,
   {:otel_span_ets, #Function<2.55784961/1 in :otel_tracer_server.on_end/1>}}
}

expected behaviour:

the context is properly restored after execution of otel_trace:with_span/5

proposed changes:

with_span(Ctx, Tracer, SpanName, Opts, Fun) ->
    SpanCtx = start_span(Ctx, Tracer, SpanName, Opts),
    Ctx1 = otel_tracer:set_current_span(Ctx, SpanCtx),
    Token = otel_ctx:attach(Ctx1),
    try
        Fun(SpanCtx)
    after
        otel_ctx:detach(Token)
    end.
@tsloughter
Copy link
Member

@DenysGonchar did you try this change? I'm actually not seeing what is different from the current code in your suggestion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants