Skip to content

Commit

Permalink
Merge pull request #327 from tsloughter/span-limits
Browse files Browse the repository at this point in the history
Configuration improvements and span limit settings
  • Loading branch information
tsloughter authored Dec 13, 2021
2 parents 20c89cf + 68f0341 commit 70681ed
Show file tree
Hide file tree
Showing 13 changed files with 485 additions and 159 deletions.
9 changes: 9 additions & 0 deletions apps/opentelemetry/include/otel_span.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@

instrumentation_library :: opentelemetry:instrumentation_library() | undefined
}).

-record(span_limits, {
attribute_count_limit = 128 :: integer(), %% Maximum allowed attribute count per span;
attribute_value_length_limit = infinity :: integer() | infinity, %% Maximum allowed attribute value length
event_count_limit = 128 :: integer(), %% Maximum allowed span event count
link_count_limit = 128 :: integer(), %% Maximum allowed span link count
attribute_per_event_limit = 128 :: integer(), %% Maximum allowed attribute per span event count
attribute_per_link_limit = 128 :: integer() %% Maximum allowed attribute per span link count
}).
22 changes: 10 additions & 12 deletions apps/opentelemetry/src/opentelemetry_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@
stop/1]).

start(_StartType, _StartArgs) ->
Opts = otel_configuration:merge_with_os(
Config = otel_configuration:merge_with_os(
application:get_all_env(opentelemetry)),

%% set global span limits record based on configuration
otel_span_limits:set(Config),

%% set the global propagators for HTTP based on the application env
setup_text_map_propagators(Opts),
setup_text_map_propagators(Config),

SupResult = opentelemetry_sup:start_link(Opts),
SupResult = opentelemetry_sup:start_link(Config),

%% must be done after the supervisor starts so that otel_tracer_server is running
%% TODO: make this work with release upgrades. Currently if an application's version
%% changes the version in the tracer will not be updated.
register_loaded_application_tracers(Opts),
register_loaded_application_tracers(Config),

SupResult.

Expand All @@ -52,19 +55,14 @@ stop(_State) ->

%% internal functions

setup_text_map_propagators(Opts) ->
List = proplists:get_value(text_map_propagators, Opts, []),
setup_text_map_propagators(#{text_map_propagators := List}) ->
CompositePropagator = otel_propagator_text_map_composite:create(List),
opentelemetry:set_text_map_propagator(CompositePropagator).

register_loaded_application_tracers(Opts) ->
RegisterLoadedApplications = proplists:get_value(register_loaded_applications, Opts, true),
register_loaded_applications_(RegisterLoadedApplications).

register_loaded_applications_(true) ->
register_loaded_application_tracers(#{register_loaded_applications := true}) ->
%% TODO: filter out OTP apps that will not have any instrumentation
LoadedApplications = application:loaded_applications(),
opentelemetry:register_applications(LoadedApplications),
ok;
register_loaded_applications_(_) ->
register_loaded_application_tracers(_) ->
ok.
2 changes: 1 addition & 1 deletion apps/opentelemetry/src/opentelemetry_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ init([Opts]) ->
type => worker,
modules => [otel_tracer_provider, otel_tracer_server]},

Processors = proplists:get_value(processors, Opts, []),
Processors = maps:get(processors, Opts),
BatchProcessorOpts = proplists:get_value(otel_batch_processor, Processors, #{}),
BatchProcessor = #{id => otel_batch_processor,
start => {otel_batch_processor, start_link, [BatchProcessorOpts]},
Expand Down
335 changes: 254 additions & 81 deletions apps/opentelemetry/src/otel_configuration.erl

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions apps/opentelemetry/src/otel_resource_detector.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ get_resource(Timeout) ->
otel_resource:create([])
end.

init([Opts]) ->
init([_Opts]) ->
process_flag(trap_exit, true),

Detectors = proplists:get_value(resource_detectors, Opts, []),
DetectorTimeout = proplists:get_value(resource_detectors_timeout, Opts, 5000),
Detectors = application:get_env(opentelemetry, resource_detectors, []),
DetectorTimeout = application:get_env(opentelemetry, resource_detectors_timeout, 5000),

{ok, collecting, #data{resource=otel_resource:create([]),
detectors=Detectors,
Expand Down
55 changes: 55 additions & 0 deletions apps/opentelemetry/src/otel_span_limits.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
%%%------------------------------------------------------------------------
%% Copyright 2021, OpenTelemetry Authors
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @doc Module for setting the global limits for the number of attributes,
%% events and links on a Span.
%% @end
%%%-------------------------------------------------------------------------
-module(otel_span_limits).

-export([get/0,
set/1]).

-include("otel_span.hrl").

-define(SPAN_LIMITS_KEY, {?MODULE, span_limits}).

-spec get() -> #span_limits{}.
get() ->
persistent_term:get(?SPAN_LIMITS_KEY).

%% -spec set(#{attribute_count_limit => integer(),
%% attribute_value_length_limit => integer() | infinity,
%% event_count_limit => integer(),
%% link_count_limit => integer(),
%% attribute_per_event_limit => integer(),
%% attribute_per_link_limit => integer()}) -> ok.
-spec set(otel_configuration:t()) -> ok.
set(Opts) ->
SpanLimits = maps:fold(fun(attribute_count_limit, AttributeCountLimit, Acc) ->
Acc#span_limits{attribute_count_limit=AttributeCountLimit};
(attribute_value_length_limit, AttributeValueLengthLimit, Acc) ->
Acc#span_limits{attribute_value_length_limit=AttributeValueLengthLimit};
(event_count_limit, EventCountLimit, Acc) ->
Acc#span_limits{event_count_limit=EventCountLimit};
(link_count_limit, LinkCountLimit, Acc) ->
Acc#span_limits{link_count_limit=LinkCountLimit};
(attribute_per_event_limit, AttributePerEventLimit, Acc) ->
Acc#span_limits{attribute_per_event_limit=AttributePerEventLimit};
(attribute_per_link_limit, AttributePerLinkLimit, Acc) ->
Acc#span_limits{attribute_per_link_limit=AttributePerLinkLimit};
(_, _, Acc) ->
Acc
end, #span_limits{}, Opts),
persistent_term:put(?SPAN_LIMITS_KEY, SpanLimits).
4 changes: 2 additions & 2 deletions apps/opentelemetry/src/otel_span_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ start_link(Opts) ->
start_child(ChildSpec) ->
supervisor:start_child(?SERVER, ChildSpec).

init([Opts]) ->
init([_Opts]) ->
SupFlags = #{strategy => one_for_one,
intensity => 1,
period => 5},

SweeperOpts = proplists:get_value(sweeper, Opts, #{}),
SweeperOpts = application:get_env(opentelemetry, sweeper, #{}),
Sweeper = #{id => otel_span_sweeper,
start => {otel_span_sweeper, start_link, [SweeperOpts]},
restart => permanent,
Expand Down
10 changes: 4 additions & 6 deletions apps/opentelemetry/src/otel_tracer_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ start_link(Opts) ->
init(Opts) ->
Resource = otel_resource_detector:get_resource(),

IdGeneratorModule = proplists:get_value(id_generator, Opts, otel_id_generator),
IdGeneratorModule = application:get_env(opentelemetry, id_generator, otel_id_generator),

SamplerSpec = proplists:get_value(
sampler, Opts, {parent_based, #{root => always_on}}
),
SamplerSpec = maps:get(sampler, Opts),
Sampler = otel_sampler:new(SamplerSpec),
Processors = proplists:get_value(processors, Opts, []),
DenyList = proplists:get_value(deny_list, Opts, []),
Processors = maps:get(processors, Opts),
DenyList = application:get_env(opentelemetry, deny_list, []),

{ok, LibraryVsn} = application:get_key(opentelemetry, vsn),
LibraryName = <<"opentelemetry">>,
Expand Down
9 changes: 6 additions & 3 deletions apps/opentelemetry/test/opentelemetry_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,18 @@ registered_tracers(_Config) ->

propagator_configuration(_Config) ->
?assertEqual({otel_propagator_text_map_composite,
[{otel_propagator_b3, b3multi}, otel_propagator_baggage]}, opentelemetry:get_text_map_extractor()),
[{otel_propagator_b3, b3multi}, otel_propagator_baggage]},
opentelemetry:get_text_map_extractor()),
?assertEqual({otel_propagator_text_map_composite,
[{otel_propagator_b3, b3multi}, otel_propagator_baggage]}, opentelemetry:get_text_map_injector()),
[{otel_propagator_b3, b3multi}, otel_propagator_baggage]},
opentelemetry:get_text_map_injector()),

opentelemetry:set_text_map_extractor({otel_propagator_baggage, []}),

?assertEqual({otel_propagator_baggage, []}, opentelemetry:get_text_map_extractor()),
?assertEqual({otel_propagator_text_map_composite,
[{otel_propagator_b3, b3multi}, otel_propagator_baggage]}, opentelemetry:get_text_map_injector()),
[{otel_propagator_b3, b3multi}, otel_propagator_baggage]},
opentelemetry:get_text_map_injector()),

opentelemetry:set_text_map_injector({{otel_propagator_b3, b3multi}, []}),

Expand Down
Loading

0 comments on commit 70681ed

Please sign in to comment.