Skip to content

Commit ca61a11

Browse files
authored
Merge pull request #732 from whatyouhide/al/opentelemetry-docs
Document more functions in the "opentelemetry" module
2 parents 2e00cac + 2046e14 commit ca61a11

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

apps/opentelemetry_api/src/opentelemetry.erl

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,11 @@ convert_timestamp(Timestamp, Unit) ->
343343
Offset = erlang:time_offset(),
344344
erlang:convert_time_unit(Timestamp + Offset, native, Unit).
345345

346-
-spec links([{TraceId, SpanId, Attributes, TraceState} | span_ctx() | {span_ctx(), Attributes}]) -> [link()] when
347-
TraceId :: trace_id(),
348-
SpanId :: span_id(),
346+
%% @doc Creates a list of Span links from the given `List'.
347+
%%
348+
%% This is equivalent to calling {@link link/2} or {@link link/4} multiple times.
349+
-spec links([TraceIdAndSpanId | span_ctx() | {span_ctx(), Attributes}]) -> [link()] when
350+
TraceIdAndSpanId :: {trace_id(), span_id(), Attributes, TraceState},
349351
Attributes :: attributes_map(),
350352
TraceState :: otel_tracestate:t() | [{string(), string()}].
351353
links(List) when is_list(List) ->
@@ -368,18 +370,26 @@ links(List) when is_list(List) ->
368370
links(_) ->
369371
[].
370372

373+
374+
%% @equiv link(SpanCtx, [])
371375
-spec link(span_ctx() | undefined) -> link() | undefined.
372376
link(SpanCtx) ->
373377
link(SpanCtx, []).
374378

379+
%% @doc Creates a Span link to the Span represented by the given `SpanCtx'.
380+
%%
381+
%% The returned link can be used in the `links' field of a Span.
375382
-spec link(span_ctx() | undefined, attributes_map()) -> link() | undefined.
376-
link(#span_ctx{trace_id=TraceId,
377-
span_id=SpanId,
378-
tracestate=TraceState}, Attributes) ->
383+
link(_SpanCtx = #span_ctx{trace_id=TraceId,
384+
span_id=SpanId,
385+
tracestate=TraceState}, Attributes) ->
379386
?MODULE:link(TraceId, SpanId, otel_attributes:process_attributes(Attributes), TraceState);
380387
link(_, _) ->
381388
undefined.
382389

390+
%% @doc Creates a Span link to the Span represented by the given `TraceId' and `SpanId'.
391+
%%
392+
%% The returned link can be used in the `links' field of a Span.
383393
-spec link(TraceId, SpanId, Attributes, TraceState) -> link() | undefined when
384394
TraceId :: trace_id(),
385395
SpanId :: span_id(),
@@ -395,12 +405,18 @@ link(TraceId, SpanId, Attributes, TraceState) when is_integer(TraceId),
395405
link(_, _, _, _) ->
396406
undefined.
397407

408+
%% @equiv event(opentelemetry:timestamp(), Name, Attributes)
398409
-spec event(Name, Attributes) -> event() | undefined when
399410
Name :: event_name(),
400411
Attributes :: attributes_map().
401412
event(Name, Attributes) ->
402413
event(opentelemetry:timestamp(), Name, Attributes).
403414

415+
%% @doc Creates a Span event with the given `Name' and `Attributes'.
416+
%%
417+
%% The Span event is marked to have happened at `Timestamp'. The returned
418+
%% event can be used to add an event to a Span through {@link otel_span:add_events/2},
419+
%% for example.
404420
-spec event(Timestamp, Name, Attributes) -> event() | undefined when
405421
Timestamp :: integer(),
406422
Name :: event_name(),
@@ -419,7 +435,16 @@ event(Timestamp, Name, Attributes) when is_integer(Timestamp),
419435
event(_, _, _) ->
420436
undefined.
421437

422-
events(List) ->
438+
%% @doc Creates a list of Span events from the given `List'.
439+
%%
440+
%% This is a conveneince function to create a list of Span events from a list
441+
%% of `{Time, Name, Attributes}' or `{Name, Attributes}' tuples. It's equivalent
442+
%% to calling {@link event/2} or {@link event/3} multiple times. This function
443+
%% also automatically filters out any invalid tuple.
444+
-spec events([Event]) -> [event()] when
445+
Event :: {Timestamp :: integer(), event_name(), attributes_map()} |
446+
{event_name(), attributes_map()}.
447+
events(Events) ->
423448
Now = opentelemetry:timestamp(),
424449
lists:filtermap(fun({Time, Name, Attributes}) ->
425450
case event(Time, Name, Attributes) of
@@ -437,13 +462,21 @@ events(List) ->
437462
end;
438463
(_) ->
439464
false
440-
end, List).
465+
end, Events).
441466

467+
%% @doc Create a Span status from the given `Code'.
468+
%%
469+
%% The returned status can be used to set the status of a Span through
470+
%% {@link otel_span:set_status/2}, for example.
442471
-spec status(Code) -> status() | undefined when
443472
Code :: status_code().
444473
status(Code) ->
445474
status(Code, <<>>).
446475

476+
%% @doc Create a Span status from the given `Code' and with the given `Message'.
477+
%%
478+
%% The returned status can be used to set the status of a Span through
479+
%% {@link otel_span:set_status/2}, for example.
447480
-spec status(Code, Message) -> status() | undefined when
448481
Code :: status_code(),
449482
Message :: unicode:unicode_binary().
@@ -458,6 +491,7 @@ status(_, _) ->
458491

459492
%% internal functions
460493

494+
%% @private
461495
-spec verify_and_set_term(module() | {module(), term()}, term(), atom()) -> boolean().
462496
verify_and_set_term(Module, TermKey, Behaviour) ->
463497
case verify_module_exists(Module) of
@@ -471,6 +505,7 @@ verify_and_set_term(Module, TermKey, Behaviour) ->
471505
false
472506
end.
473507

508+
%% @private
474509
-spec verify_module_exists(module() | {module(), term()}) -> boolean().
475510
verify_module_exists({Module, _}) ->
476511
verify_module_exists(Module);
@@ -486,6 +521,7 @@ verify_module_exists(Module) ->
486521
%% for use in a filtermap
487522
%% return {true, Link} if a link is returned or return false
488523
%% a list is supported for tracestate for backwards compatibility
524+
%% @private
489525
link_or_false(TraceId, SpanId, Attributes, TraceState) when is_list(TraceState) ->
490526
link_or_false(TraceId, SpanId, Attributes, otel_tracestate:new(TraceState));
491527
link_or_false(TraceId, SpanId, Attributes, TraceState) ->
@@ -519,6 +555,7 @@ schema_url_to_binary(_) ->
519555

520556
%% Vsn can't be an atom or anything but a list or binary
521557
%% so return empty binary string if it isn't a list or binary.
558+
%% @private
522559
vsn_to_binary(Vsn) when is_binary(Vsn) ; is_list(Vsn) ->
523560
unicode:characters_to_binary(Vsn);
524561
vsn_to_binary(_) ->

0 commit comments

Comments
 (0)