Skip to content

Instantly share code, notes, and snippets.

@tsloughter
Last active Jan 29, 2021
Embed
What would you like to do?
test "create child Span in Task" do
:otel_batch_processor.set_exporter(:otel_exporter_pid, self())
OpenTelemetry.register_tracer(:test_tracer, "0.1.0")
# create the parent span
parent = Tracer.start_span("parent")
# make a new context with it as the active span
ctx = Tracer.set_current_span(Ctx.new(), parent)
# attach this context (put it in the process dictionary)
Ctx.attach(ctx)
# start the child and set it to current in an unattached context
child = Tracer.start_span("child")
ctx = Tracer.set_current_span(ctx, SpanCtx)
task = Task.async(
fn ->
# attach the context with the child span active to this process
Ctx.attach(ctx)
Span.end_span(child)
:hello
end)
ret = Task.await(task)
assert :hello = ret
span_ctx(span_id: parent_span_id) = Span.end_span(parent)
assert_receive {:span,
span(
name: "child",
parent_span_id: ^parent_span_id
)}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment