Skip to content

Commit

Permalink
Merge pull request #246 from open-telemetry/status-spec-compliance
Browse files Browse the repository at this point in the history
Better compliance with status spec
  • Loading branch information
bryannaegele authored Jul 13, 2021
2 parents 9110035 + f4b7356 commit 3eff2f2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
12 changes: 6 additions & 6 deletions apps/opentelemetry_api/include/opentelemetry.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
-define(SPAN_KIND_PRODUCER, producer).
-define(SPAN_KIND_CONSUMER, consumer).

-define(OTEL_STATUS_UNSET, unset).
-define(OTEL_STATUS_OK, ok).
-define(OTEL_STATUS_ERROR, error).

-record(span_ctx, {
%% 128 bit int trace id
trace_id :: opentelemetry:trace_id(),
Expand Down Expand Up @@ -70,11 +74,7 @@
}).

-record(status, {
code :: atom() | integer(),
code = ?OTEL_STATUS_UNSET :: opentelemetry:status_code(),
%% developer-facing error message
message :: unicode:unicode_binary()
message = <<"">> :: unicode:unicode_binary()
}).

-define(OTEL_STATUS_UNSET, unset).
-define(OTEL_STATUS_OK, ok).
-define(OTEL_STATUS_ERROR, error).
7 changes: 5 additions & 2 deletions apps/opentelemetry_api/lib/open_telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ defmodule OpenTelemetry do

@typedoc """
An optional final status for this span. Semantically when Status
wasn't set it means span ended without errors and assume `ok`.
wasn't set it means span ended without errors and assume `unset`.
Application developers may set the status as `ok` when the operation
has been validated to have completed successfully.
"""
@type status() :: :opentelemetry.status()

Expand Down Expand Up @@ -212,6 +215,6 @@ defmodule OpenTelemetry do
@doc """
Creates a Status.
"""
@spec status(atom(), String.t()) :: status()
@spec status(:opentelemetry.status_code(), String.t()) :: status()
defdelegate status(code, message), to: :opentelemetry
end
2 changes: 1 addition & 1 deletion apps/opentelemetry_api/lib/open_telemetry/span.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ defmodule OpenTelemetry.Span do
@doc """
Sets the Status of the currently active Span.
If used, this will override the default Span Status, which is `ok`.
If used, this will override the default Span Status, which is `:unset`.
"""
@spec set_status(OpenTelemetry.span_ctx(), OpenTelemetry.status()) :: boolean()
defdelegate set_status(span_ctx, status), to: :otel_span
Expand Down
2 changes: 1 addition & 1 deletion apps/opentelemetry_api/lib/open_telemetry/tracer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ defmodule OpenTelemetry.Tracer do
@doc """
Sets the Status of the currently active Span.
If used, this will override the default Span Status, which is `ok`.
If used, this will override the default Span Status, which is `:unset`.
"""
@spec set_status(OpenTelemetry.status()) :: boolean()
def set_status(status) do
Expand Down
10 changes: 6 additions & 4 deletions apps/opentelemetry_api/src/opentelemetry.erl
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,12 @@ events(List) ->
-spec status(Code, Message) -> status() | undefined when
Code :: status_code(),
Message :: unicode:unicode_binary().
status(Code, Message) when is_atom(Code),
is_binary(Message) ->
#status{code=Code,
message=Message};
status(?OTEL_STATUS_ERROR, Message) when is_binary(Message) ->
#status{code=?OTEL_STATUS_ERROR, message=Message};
status(?OTEL_STATUS_OK, _Message) ->
#status{code=?OTEL_STATUS_OK};
status(?OTEL_STATUS_UNSET, _Message) ->
#status{code=?OTEL_STATUS_UNSET};
status(_, _) ->
undefined.

Expand Down
8 changes: 7 additions & 1 deletion apps/opentelemetry_api/test/opentelemetry_api_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ update_span_data(_Config) ->

Events = opentelemetry:events([{opentelemetry:timestamp(),
<<"timed-event-name">>, []}]),
ErrorStatus = opentelemetry:status(?OTEL_STATUS_ERROR, <<"This is an error!">>),
?assertMatch(#status{code = ?OTEL_STATUS_ERROR, message = <<"This is an error!">>}, ErrorStatus),

UnsetStatus = opentelemetry:status(?OTEL_STATUS_UNSET, <<"This is a message">>),
?assertMatch(#status{code = ?OTEL_STATUS_UNSET, message = <<"">>}, UnsetStatus),

Status = opentelemetry:status(?OTEL_STATUS_OK, <<"This is Ok">>),
?assertMatch(#status{code = ?OTEL_STATUS_OK, message = <<"This is Ok">>}, Status),
?assertMatch(#status{code = ?OTEL_STATUS_OK, message = <<"">>}, Status),

otel_span:set_status(SpanCtx1, Status),
otel_span:add_events(SpanCtx1, Events),
Expand Down

0 comments on commit 3eff2f2

Please sign in to comment.