Skip to content
This repository has been archived by the owner on Oct 26, 2020. It is now read-only.

Add accessors to deconstruct Span #54

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/open_telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ defmodule OpenTelemetry do
defdelegate link(trace_id, span_id, attributes, tracestate), to: :opentelemetry

@doc """
Creates a list of `t:link/0` from a list of 3-tuples.
Creates a list of `t:link/0` from a list of 4-tuples.
"""
@spec links([{integer(), integer(), list(), list()}]) :: [link()]
defdelegate links(link_list), to: :opentelemetry
Expand Down
19 changes: 19 additions & 0 deletions lib/open_telemetry/span.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ defmodule OpenTelemetry.Span do
- A Status (`t:OpenTelemetry.status/0`)
"""

@doc """
Get the SpanId of a Span.
"""
@spec span_id(OpenTelemetry.span_ctx()) :: OpenTelemetry.span_id()
defdelegate span_id(span), to: :ot_span

@doc """
Get the TraceId of a Span.
"""
@spec trace_id(OpenTelemetry.span_ctx()) :: OpenTelemetry.trace_id()
defdelegate trace_id(span), to: :ot_span

@doc """
Get the Tracestate of a Span.
"""
@spec tracestate(OpenTelemetry.span_ctx()) :: OpenTelemetry.tracestate()
defdelegate tracestate(span), to: :ot_span


@doc """
Set an attribute with key and value on the currently active Span.
"""
Expand Down
13 changes: 13 additions & 0 deletions src/ot_span.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
-module(ot_span).

-export([get_ctx/2,
trace_id/1,
span_id/1,
tracestate/1,
is_recording/2,
set_attribute/4,
set_attributes/3,
Expand Down Expand Up @@ -126,6 +129,16 @@ update_name(Tracer, SpanCtx, SpanName) when ?is_recording(SpanCtx) ->
update_name(_, _, _) ->
false.

%% accessors
-spec trace_id(opentelemetry:span_ctx()) -> opentelemetry:trace_id().
trace_id(#span_ctx{ trace_id = TraceId }) -> TraceId.

-spec span_id(opentelemetry:span_ctx()) -> opentelemetry:span_id().
span_id(#span_ctx{ span_id = SpanId }) -> SpanId.

-spec tracestate(opentelemetry:span_ctx()) -> opentelemetry:tracestate().
tracestate(#span_ctx{ tracestate = Tracestate }) -> Tracestate.

%% internal functions

do_span_function(Function, Tracer, SpanCtx, Args) ->
Expand Down
10 changes: 10 additions & 0 deletions test/open_telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ defmodule OpenTelemetryTest do
end
end
end

test "can deconstruct a span context" do
Tracer.with_span "span-1" do
span = Tracer.current_span_ctx()

assert nil != Span.trace_id(span)
assert nil != Span.span_id(span)
assert [] = Span.tracestate(span)
end
end
end
26 changes: 25 additions & 1 deletion test/opentelemetry_api_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
-include("tracer.hrl").

all() ->
[noop_tracer, update_span_data, noop_with_span, macros].
[noop_tracer, update_span_data, noop_with_span, macros, can_create_link_from_span].

init_per_suite(Config) ->
application:load(opentelemetry_api),
Expand All @@ -18,6 +18,30 @@ init_per_suite(Config) ->
end_per_suite(_Config) ->
ok.

can_create_link_from_span(_Config) ->
%% start a span to create a link to
SpanCtx = ?start_span(<<"span-1">>),

%% extract individual values from span context
TraceId = ot_span:trace_id(SpanCtx),
SpanId = ot_span:span_id(SpanCtx),
Tracestate = ot_span:tracestate(SpanCtx),

%% end span, so there's no current span set
?end_span(),

%% we don't need any attributes for this test
Attributes = [],

%% attempt to create a link
Link = opentelemetry:link(TraceId, SpanId, Attributes, Tracestate),
?assertMatch(#link{ trace_id = TraceId
, span_id = SpanId
, attributes = Attributes
, tracestate = Tracestate
}, Link).


noop_tracer(_Config) ->
%% start a span and 2 children
SpanCtx1 = ?start_span(<<"span-1">>),
Expand Down