Skip to content

Commit afda6b6

Browse files
committed
Feedback
1 parent baf12d6 commit afda6b6

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

apps/opentelemetry/src/otel_attributes.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ update_attribute(Key, [Value1 | _Rest] = Value, Attributes=#attributes{count_lim
8282
Attributes#attributes{map=Map#{Key => [maybe_truncate_binary(V, ValueLengthLimit) || V <- Value]}};
8383
%% already in the map and not a binary so update
8484
update_attribute(Key, Value, Attributes=#attributes{map=Map}) when is_map_key(Key, Map) ->
85-
Attributes#attributes{map=Map#{Key => Value}};
85+
Attributes#attributes{map=Map#{Key := Value}};
8686
%% we've already started dropping, so just increment
8787
update_attribute(_Key, _Value, Attributes=#attributes{dropped=Dropped})
8888
when Dropped > 0 ->

apps/opentelemetry/test/opentelemetry_SUITE.erl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,8 @@ too_many_attributes(Config) ->
728728

729729
?set_current_span(SpanCtx),
730730

731-
?set_attribute(<<"attr-1">>, <<"attr-value-1">>),
731+
%% tuple tests cover lists, as well.
732+
?set_attribute(attr1, {homogenous, tuple}),
732733

733734
%% dropped because of non-homogenous
734735
?set_attribute(<<"attr-2-dropped">>, {non_homogenous, <<"attributes">>}),
@@ -748,17 +749,17 @@ too_many_attributes(Config) ->
748749
otel_span:end_span(SpanCtx),
749750
[Span] = assert_exported(Tid, SpanCtx),
750751

751-
?assertEqual(#{<<"attr-1">> => <<"attr-value-5">>,
752+
?assertEqual(#{attr1 => [homogenous, tuple],
752753
<<"attr-3">> => 4}, otel_attributes:map(Span#span.attributes)),
753-
?assertEqual(2, otel_attributes:dropped(Span#span.attributes)),
754+
?assertEqual(3, otel_attributes:dropped(Span#span.attributes)),
754755

755756
%% test again using the `set_attributes' macro
756757
SpanCtx2 = ?start_span(<<"span-2">>),
757758

758759
?set_current_span(SpanCtx2),
759760

760761
?set_attributes(#{<<"attr-1">> => <<"attr-value-1">>,
761-
<<"attr-2">> => {non_homogenous, <<"attributes">>},
762+
<<"attr-2">> => {homogenous, attribute},
762763
<<"attr-3">> => attr_3_value,
763764
<<"attr-4">> => <<"attr-value-4">>}),
764765

@@ -770,7 +771,7 @@ too_many_attributes(Config) ->
770771

771772

772773
%% order isn't guaranteed so just verify the number dropped is right
773-
?assertEqual(1, otel_attributes:dropped(Span2#span.attributes)),
774+
?assertEqual(2, otel_attributes:dropped(Span2#span.attributes)),
774775

775776
ok.
776777

apps/opentelemetry_api/src/otel_span.erl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ is_valid(_) ->
8989
-spec is_valid_attribute(opentelemetry:attribute_key(), opentelemetry:attribute_value()) -> boolean().
9090
is_valid_attribute(Key, Value) when is_tuple(Value) , ?is_allowed_key(Key) ->
9191
is_valid_attribute(Key, tuple_to_list(Value));
92+
%% lists as attribute values must be primitive types and homogeneous
9293
is_valid_attribute(Key, [Value1 | _Rest] = Values) when is_binary(Value1) , ?is_allowed_key(Key) ->
9394
lists:all(fun is_binary/1, Values);
9495
is_valid_attribute(Key, [Value1 | _Rest] = Values) when is_boolean(Value1) , ?is_allowed_key(Key) ->
@@ -174,6 +175,14 @@ tracestate(_) ->
174175
Key :: opentelemetry:attribute_key(),
175176
Value :: opentelemetry:attribute_value(),
176177
SpanCtx :: opentelemetry:span_ctx().
178+
set_attribute(SpanCtx=#span_ctx{span_sdk={Module, _}}, Key, Value) when ?is_recording(SpanCtx) , is_tuple(Value) ->
179+
List = tuple_to_list(Value),
180+
case is_valid_attribute(Key, List) of
181+
true ->
182+
Module:set_attribute(SpanCtx, Key, List);
183+
false ->
184+
false
185+
end;
177186
set_attribute(SpanCtx=#span_ctx{span_sdk={Module, _}}, Key, Value) when ?is_recording(SpanCtx) ->
178187
case is_valid_attribute(Key, Value) of
179188
true ->

apps/opentelemetry_api/src/otel_tracer.erl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,46 +43,46 @@
4343
-callback start_span(otel_ctx:t(),
4444
opentelemetry:tracer(),
4545
opentelemetry:span_name(),
46-
otel_span:start_opts()) -> opentelemetry:span_ctx() | undefined.
46+
otel_span:start_opts()) -> opentelemetry:span_ctx().
4747
-callback with_span(otel_ctx:t(), opentelemetry:tracer(),
48-
opentelemetry:span_name(), otel_span:start_opts(), traced_fun(T)) -> T | any().
48+
opentelemetry:span_name(), otel_span:start_opts(), traced_fun(T)) -> T.
4949

5050
-spec start_span(opentelemetry:tracer(), opentelemetry:span_name(), otel_span:start_opts())
51-
-> opentelemetry:span_ctx() | undefined.
51+
-> opentelemetry:span_ctx().
5252
start_span(Tracer={Module, _}, SpanName, Opts) ->
5353
case otel_span:is_valid_name(SpanName) of
5454
true ->
5555
Module:start_span(otel_ctx:get_current(), Tracer, SpanName, otel_span:validate_start_opts(Opts));
5656
false ->
57-
undefind
57+
otel_tracer_noop:noop_span_ctx()
5858
end.
5959

6060
-spec start_span(otel_ctx:t(), opentelemetry:tracer(), opentelemetry:span_name(), otel_span:start_opts())
61-
-> opentelemetry:span_ctx() | undefined.
61+
-> opentelemetry:span_ctx().
6262
start_span(Ctx, Tracer={Module, _}, SpanName, Opts) ->
6363
case otel_span:is_valid_name(SpanName) of
6464
true ->
6565
Module:start_span(Ctx, Tracer, SpanName, otel_span:validate_start_opts(Opts));
6666
false ->
67-
undefined
67+
otel_tracer_noop:noop_span_ctx()
6868
end.
6969

70-
-spec with_span(opentelemetry:tracer(), opentelemetry:span_name(), otel_span:start_opts(), traced_fun(T)) -> T | any().
70+
-spec with_span(opentelemetry:tracer(), opentelemetry:span_name(), otel_span:start_opts(), traced_fun(T)) -> T.
7171
with_span(Tracer={Module, _}, SpanName, Opts, Fun) when is_atom(Module) ->
7272
case otel_span:is_valid_name(SpanName) of
7373
true ->
7474
Module:with_span(otel_ctx:get_current(), Tracer, SpanName, otel_span:validate_start_opts(Opts), Fun);
7575
false ->
76-
Fun()
76+
Fun(otel_tracer_noop:noop_span_ctx())
7777
end.
7878

79-
-spec with_span(otel_ctx:t(), opentelemetry:tracer(), opentelemetry:span_name(), otel_span:start_opts(), traced_fun(T)) -> T | any().
79+
-spec with_span(otel_ctx:t(), opentelemetry:tracer(), opentelemetry:span_name(), otel_span:start_opts(), traced_fun(T)) -> T.
8080
with_span(Ctx, Tracer={Module, _}, SpanName, Opts, Fun) when is_atom(Module) ->
8181
case otel_span:is_valid_name(SpanName) of
8282
true ->
8383
Module:with_span(Ctx, Tracer, SpanName, otel_span:validate_start_opts(Opts), Fun);
8484
false ->
85-
Fun()
85+
Fun(otel_tracer_noop:noop_span_ctx())
8686
end.
8787

8888
%% @doc Returns a `span_ctx' record with `is_recording' set to `false'. This is mainly

apps/opentelemetry_api/src/otel_tracer_noop.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
-export([start_span/4,
2323
with_span/5,
24-
end_span/2]).
24+
end_span/2,
25+
noop_span_ctx/0]).
2526

2627
-include("opentelemetry.hrl").
2728
-include("otel_tracer.hrl").
@@ -64,3 +65,7 @@ with_span(Ctx, Tracer, SpanName, Opts, Fun) ->
6465
-> boolean() | {error, term()}.
6566
end_span(_, _) ->
6667
true.
68+
69+
-spec noop_span_ctx() -> opentelemetry:span_ctx().
70+
noop_span_ctx() ->
71+
?NOOP_SPAN_CTX.

0 commit comments

Comments
 (0)