with_span
is better for regular code
with_span
does for you
def child do
OpenTelemetry.Tracer.with_span "parent" do
# start the child and set it to current in an unattached context
child = Tracer.start_span("child")
Tracer.set_current_span(child)
task =
Task.async(fn ->
# attach the context with the child span active to this process
Ctx.attach(Ctx.get_current())
Process.sleep(2_000)
return = :ok
Span.end_span(child)
return
end)
return = Task.await(task)
Process.sleep(1_000)
return
end
end
Works better this way, thank you :slight_smile:
with_span
sub child to test the context propagation and it seems to work, at least in the collector output
I put it one last time, hopping it is the working one:
def child do
OpenTelemetry.Tracer.with_span "parent" do
# start the child and set it to current in an unattached context
child = Tracer.start_span("child")
ctx = Tracer.set_current_span(Ctx.get_current(), child)
task =
Task.async(fn ->
# attach the context with the child span active to this process
Ctx.attach(ctx)
OpenTelemetry.Tracer.with_span "sub child" do
Process.sleep(1_000)
end
Process.sleep(2_000)
return = :ok
Span.end_span(child)
return
end)
return = Task.await(task)
Process.sleep(1_000)
return
end
end
And the collector ouput:
2021-02-01T14:15:56.013Z INFO loggingexporter/logging_exporter.go:313 TracesExporter {"#spans": 1}
2021-02-01T14:15:56.013Z DEBUG loggingexporter/logging_exporter.go:352 ResourceSpans #0
InstrumentationLibrarySpans #0
InstrumentationLibrary
Span #0
Trace ID : cb92a6334f872a7f0627252a84343726
Parent ID : e9eb3a93a4b906e7
ID : 48538a2a12443b3b
Name : sub child
Kind : SPAN_KIND_INTERNAL
Start time : 2021-02-01 14:15:54.09668847 +0000 UTC
End time : 2021-02-01 14:15:55.09773665 +0000 UTC
Status code : STATUS_CODE_UNSET
Status message :
2021-02-01T14:16:01.018Z INFO loggingexporter/logging_exporter.go:313 TracesExporter {"#spans": 2}
2021-02-01T14:16:01.018Z DEBUG loggingexporter/logging_exporter.go:352 ResourceSpans #0
InstrumentationLibrarySpans #0
InstrumentationLibrary
Span #0
Trace ID : cb92a6334f872a7f0627252a84343726
Parent ID :
ID : 7230abc39174d66e
Name : parent
Kind : SPAN_KIND_INTERNAL
Start time : 2021-02-01 14:15:54.096561326 +0000 UTC
End time : 2021-02-01 14:15:58.099794929 +0000 UTC
Status code : STATUS_CODE_UNSET
Status message :
Span #1
Trace ID : cb92a6334f872a7f0627252a84343726
Parent ID : 7230abc39174d66e
ID : e9eb3a93a4b906e7
Name : child
Kind : SPAN_KIND_INTERNAL
Start time : 2021-02-01 14:15:54.096620217 +0000 UTC
End time : 2021-02-01 14:15:57.098743035 +0000 UTC
Status code : STATUS_CODE_UNSET
Status message :
with_span
macro
with_span
can take a context as the first argument. so in some cases you don't need to attach the context, just do Tracer.with_span ctx, name, %{}, do ...
link
attribute will be more clean :thumbsup:
:undefined
when calling OpenTelemetry.link/1
, I still get an error during the span creationwith_span
macro: def link do
Tracer.with_span "parent" do
parent = Tracer.current_span_ctx()
task =
Task.async(fn ->
Tracer.with_span "child", %{links: [OpenTelemetry.link(parent)]} do
:hello
end
end)
Task.await(task)
end
end
{:opentelemetry_exporter_trace_service_pb, :is_empty_string, [:undefined],
[
file: '/test/deps/opentelemetry_exporter/apps/opentelemetry_exporter/src/opentelemetry_exporter_trace_service_pb.erl',
line: 928
]},