Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.
Open
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
12 changes: 12 additions & 0 deletions src/oc_span.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

message_event/4,

set_kind/2,

set_status/3]).

-include("opencensus.hrl").
Expand Down Expand Up @@ -138,6 +140,16 @@ set_status(Code, Message, Span=#span{}) ->
set_status(_, _, undefined) ->
undefined.

%%--------------------------------------------------------------------
%% @doc
%% Set Kind.
%% @end
%%--------------------------------------------------------------------
-spec set_kind(Kind, Span) -> Span when Kind :: opencensus:span_kind(), Span :: maybe(opencensus:span()).
set_kind(Kind, Span=#span{}) ->
Span#span{kind=Kind};
set_kind(_, undefined) ->
undefined.

%%--------------------------------------------------------------------
%% @doc
Expand Down
14 changes: 13 additions & 1 deletion src/oc_trace.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@

message_event/4,

set_status/3]).
set_status/3,

set_kind/2]).

-dialyzer({nowarn_function, update_trace_options/2}).

Expand Down Expand Up @@ -302,6 +304,16 @@ set_status(Code, Message, #span_ctx{span_id=SpanId,
set_status(_Code, _Message, _Span) ->
true.

%%--------------------------------------------------------------------
%% @doc
%% Set Kind. Returns true if the data was successfully updated.
%% @end
%%--------------------------------------------------------------------
-spec set_kind(opencensus:span_kind(), maybe(opencensus:span_ctx()))-> boolean().
set_kind(Kind, SpanCtx) ->
lookup_and_replace(SpanCtx, fun(SpanData) ->
oc_span:set_kind(Kind, SpanData)
end).

%%--------------------------------------------------------------------
%% @doc
Expand Down
30 changes: 29 additions & 1 deletion src/opencensus.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

-export([generate_trace_id/0,
generate_span_id/0,
http_status_to_trace_status/1]).
http_status_to_trace_status/1,
span_kind_client/0,
span_kind_server/0,
span_kind_unspecified/0]).

-include("opencensus.hrl").

Expand Down Expand Up @@ -108,6 +111,31 @@ http_status_to_trace_status(503) ->
http_status_to_trace_status(S) ->
S.

%%--------------------------------------------------------------------
%% @doc
%% Returns atom for span kind client to set on trace.
%% @end
%%--------------------------------------------------------------------
-spec span_kind_client() -> span_kind().
span_kind_client() -> ?SPAN_KIND_CLIENT.

%%--------------------------------------------------------------------
%% @doc
%% Returns atom for span kind server to set on trace.
%% @end
%%--------------------------------------------------------------------
-spec span_kind_server() -> span_kind().
span_kind_server() -> ?SPAN_KIND_SERVER.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need this for elixir lib

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, can you add it for all the kinds and then i'll merge.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

%%--------------------------------------------------------------------
%% @doc
%% Returns atom for span kind unspecified to set on trace.
%% @end
%%--------------------------------------------------------------------
-spec span_kind_unspecified() -> span_kind().
span_kind_unspecified() -> ?SPAN_KIND_UNSPECIFIED.


%%

%% Before OTP-20 rand:uniform could not give precision higher than 2^56.
Expand Down
7 changes: 5 additions & 2 deletions test/oc_span_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ modifications(_Config) ->
Span6 = oc_span:add_link(Link, Span5),
?assertEqual(undefined, oc_span:add_link(Link, undefined)),

?assertEqual({error, no_report_buffer}, oc_span:finish_span(#span_ctx{}, Span6)),
Span7 = oc_span:set_kind(?SPAN_KIND_SERVER, Span6),
?assertEqual(undefined, oc_span:set_kind(?SPAN_KIND_SERVER, undefined)),

?assertEqual({error, no_report_buffer}, oc_span:finish_span(#span_ctx{}, Span7)),

{ok, _} = application:ensure_all_started(opencensus),
?assertEqual(true, oc_span:finish_span(#span_ctx{}, Span6)),
?assertEqual(true, oc_span:finish_span(#span_ctx{}, Span7)),
?assertEqual(true, oc_span:finish_span(#span_ctx{}, undefined)),

application:stop(opencensus).
15 changes: 15 additions & 0 deletions test/opencensus_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,21 @@ status(Config) ->
?assertMatch(#status{code=Code,
message=Message}, SpanData#span.status).

kind(Config) ->
Tab = ?config(tid, Config),
SpanName1 = <<"kind-span-1">>,
SpanCtx = oc_trace:start_span(SpanName1, undefined),
SpanKind = ?SPAN_KIND_SERVER,

oc_trace:set_kind(SpanKind, SpanCtx),

?FINISH(Tab, SpanCtx),

[SpanData] = ets:lookup(Tab, SpanCtx#span_ctx.span_id),
?assertEqual(SpanName1, SpanData#span.name),
?assertEqual(SpanKind, SpanData#span.kind),
?assert(SpanData#span.end_time > SpanData#span.start_time).

ctx_with_span(Config) ->
Tab = ?config(tid, Config),

Expand Down