Skip to content

Instantly share code, notes, and snippets.

@garthk
Created February 17, 2021 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garthk/e06284c737ff07e2b5a7c324d081f356 to your computer and use it in GitHub Desktop.
Save garthk/e06284c737ff07e2b5a7c324d081f356 to your computer and use it in GitHub Desktop.
Forging span links using OpenCensus and Honeycomb
@doc """
Forge links between spans.
Preferred, but not supported by our reporter:
:PARENT_LINKED_SPAN
|> :oc_span.link(link_trace_id, link_span_id, %{})
|> :oc_trace.add_link(span_ctx)
... but that's ok, [Honeycomb can link using the attributes of extra spans][send-links].
[send-links]: https://docs.honeycomb.io/getting-data-in/tracing/send-trace-data/#links
"""
@spec link_span_ctx(
:undefined | span_ctx(),
:undefined | span_ctx(),
:CHILD_LINKED_SPAN | :PARENT_LINKED_SPAN | :TYPE_UNSPECIFIED
) :: :ok
def link_span_ctx(our_span_ctx, their_span_ctx, link_type \\ :PARENT_LINKED_SPAN)
def link_span_ctx(:undefined, _, _), do: false
def link_span_ctx(_, :undefined, _), do: false
def link_span_ctx(our_span_ctx, their_span_ctx, link_type) do
{:span_ctx, link_trace_id, link_span_id, _, _} = their_span_ctx
attributes = %{
"trace.link.span_id" => to_string(:io_lib.format("~16.16.0b", [link_span_id])),
"trace.link.trace_id" => to_string(:io_lib.format("~32.16.0b", [link_trace_id])),
"meta.span_type" => "link"
}
link_type
|> Atom.to_string()
|> start_span(our_span_ctx, attributes: attributes)
|> finish_span()
:ok
end
@garthk
Copy link
Author

garthk commented Feb 17, 2021

start_span/3 is a wrapper around :oc_trace.start_span/3; finish_span/1 is a wrapper around :oc_trace.finish_span/1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment