Skip to content

Commit

Permalink
use attributes:set for resource merging
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan Sloughter committed Dec 17, 2021
1 parent f2733f3 commit a7a579f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
11 changes: 3 additions & 8 deletions apps/opentelemetry/src/otel_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,9 @@ attributes({otel_resource, Attributes}) ->

%% In case of collision the updating, first argument, resource takes precedence.
-spec merge(t(), t()) -> t().
merge({otel_resource, CurrentAttributes}, {otel_resource, UpdatingAttributes}) ->
CurrentMap = otel_attributes:map(CurrentAttributes),
UpdatingMap = otel_attributes:map(UpdatingAttributes),
%% TODO: fix, the dropped field for the new attributes will be wrong
%% we can't just use `otel_attributes:set' since that will drop instead
%% of override a key/value pair when the limit is reached even if the
%% new attribute matches a key in the current attributes
{otel_resource, otel_attributes:new(maps:merge(CurrentMap, UpdatingMap), 128, 255)}.
merge({otel_resource, NewAttributes}, {otel_resource, CurrentAttributes}) ->
NewMap = otel_attributes:map(NewAttributes),
{otel_resource, otel_attributes:set(NewMap, CurrentAttributes)}.

%%

Expand Down
8 changes: 4 additions & 4 deletions apps/opentelemetry/src/otel_resource_detector.erl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ handle_event(info, {'EXIT', Pid, _}, {collecting, [{_, Pid, Detector} | Rest]},

handle_event(info, {resource, Ref, Resource}, {collecting, [{Ref, _, _} | Rest]},
Data=#data{resource=CurrentResource}) ->
NewResource = otel_resource:merge(CurrentResource, Resource),
NewResource = otel_resource:merge(Resource, CurrentResource),
{next_state, next_state(Rest), Data#data{resource=NewResource}, state_timeout(Data)};
handle_event(state_timeout, resource_detector_timeout, {collecting, [{_, Pid, Detector} | Rest]}, Data) ->
?LOG_WARNING("detector ~p timed out while executing", [Detector]),
Expand Down Expand Up @@ -160,7 +160,7 @@ spawn_detector(Module, Ref) ->
required_attributes(Resource) ->
ProgName = prog_name(),
ProcessResource = otel_resource:create([{?PROCESS_EXECUTABLE_NAME, ProgName} | process_attributes()]),
Resource1 = otel_resource:merge(Resource, ProcessResource),
Resource1 = otel_resource:merge(ProcessResource, Resource),
add_service_name(Resource1, ProgName).

process_attributes() ->
Expand Down Expand Up @@ -225,14 +225,14 @@ add_service_name(Resource, ProgName) ->
case maps:is_key(?SERVICE_NAME, otel_attributes:map(Attributes)) of
false ->
ServiceResource = service_release_name(ProgName),
otel_resource:merge(Resource, ServiceResource);
otel_resource:merge(ServiceResource, Resource);
true ->
Resource
end;
ServiceName ->
%% service.name resource first to override any other service.name
%% attribute that could be set in the resource
otel_resource:merge(Resource, otel_resource:create([{?SERVICE_NAME, ServiceName}]))
otel_resource:merge(otel_resource:create([{?SERVICE_NAME, ServiceName}]), Resource)
end.

service_release_name(ProgName) ->
Expand Down
10 changes: 4 additions & 6 deletions apps/opentelemetry/test/opentelemetry_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -614,9 +614,8 @@ record_but_not_sample(Config) ->
record_exception_works(Config) ->
Tid = ?config(tid, Config),
SpanCtx = ?start_span(<<"span-1">>),
try throw(my_error) of
_ ->
ok
try
throw(my_error)
catch
Class:Term:Stacktrace ->
otel_span:record_exception(SpanCtx, Class, Term, Stacktrace, [{<<"some-attribute">>, <<"value">>}]),
Expand All @@ -634,9 +633,8 @@ record_exception_works(Config) ->
record_exception_with_message_works(Config) ->
Tid = ?config(tid, Config),
SpanCtx = ?start_span(<<"span-1">>),
try throw(my_error) of
_ ->
ok
try
throw(my_error)
catch
Class:Term:Stacktrace ->
otel_span:record_exception(SpanCtx, Class, Term, <<"My message">>, Stacktrace, [{<<"some-attribute">>, <<"value">>}]),
Expand Down
2 changes: 1 addition & 1 deletion apps/opentelemetry/test/otel_resource_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ combining(_Config) ->
{version, "1.1.1"}]}])),
Resource2 = otel_resource:create(otel_resource_env_var:parse("service.name=cttest,service.version=1.1.1")),

Merged = otel_resource:merge(Resource2, Resource1),
Merged = otel_resource:merge(Resource1, Resource2),

Expected = {otel_resource, otel_attributes:new([{<<"service.name">>, <<"other-name">>},
{<<"service.version">>, <<"1.1.1">>}], 128, 255)},
Expand Down

0 comments on commit a7a579f

Please sign in to comment.