Skip to content

Commit

Permalink
Merge pull request #574 from tsloughter/sdk-disabled
Browse files Browse the repository at this point in the history
add support for OTEL_SDK_DISABLED environment variable
  • Loading branch information
Tristan Sloughter authored Apr 19, 2023
2 parents 3bf392b + 64fecd2 commit eafe427
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## SDK

### Added

- [Add support for OTEL_SDK_DISABLED environment variable and sdk_disabled
application environment
variable](https://github.com/open-telemetry/opentelemetry-erlang/pull/574)

### Changes

- [Resource is now an argument to TracerProvider start, but still set
Expand Down
29 changes: 18 additions & 11 deletions apps/opentelemetry/src/opentelemetry_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,32 @@

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

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

%% set the global propagators for HTTP based on the application env
%% these get set even if the SDK is disabled
setup_text_map_propagators(Config),

SupResult = opentelemetry_sup:start_link(Config),

Resource = otel_resource_detector:get_resource(),
_ = otel_tracer_provider_sup:start(?GLOBAL_TRACER_PROVIDER_NAME, Resource, Config),
case Config of
#{sdk_disabled := true} ->
%% skip the rest if the SDK is disabled
SupResult;
_ ->
%% set global span limits record based on configuration
otel_span_limits:set(Config),

Resource = otel_resource_detector:get_resource(),
_ = otel_tracer_provider_sup:start(?GLOBAL_TRACER_PROVIDER_NAME, Resource, 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.
create_loaded_application_tracers(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.
create_loaded_application_tracers(Config),

SupResult.
SupResult
end.

stop(_State) ->
ok.
Expand Down
2 changes: 2 additions & 0 deletions apps/opentelemetry/src/opentelemetry_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
start_link(Opts) ->
supervisor:start_link({local, ?SERVER}, ?MODULE, [Opts]).

init([#{sdk_disabled := true}]) ->
{ok, {#{}, []}};
init([Opts]) ->
SupFlags = #{strategy => one_for_one,
intensity => 1,
Expand Down
9 changes: 6 additions & 3 deletions apps/opentelemetry/src/otel_configuration.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@

%% required configuration
%% using a map instead of a record because there can be more values
-type t() :: #{log_level := atom(),
-type t() :: #{sdk_disabled := boolean(),
log_level := atom(),
register_loaded_applications := boolean() | undefined,
create_application_tracers := boolean() | undefined,
id_generator := module(),
Expand Down Expand Up @@ -70,7 +71,8 @@

-spec new() -> t().
new() ->
?assert_type(#{log_level => info,
?assert_type(#{sdk_disabled => false,
log_level => info,
register_loaded_applications => undefined,
create_application_tracers => undefined,
id_generator => otel_id_generator,
Expand Down Expand Up @@ -295,7 +297,8 @@ report_cb(#{source := transform,
[OSVar, Key, Transform, Value, otel_utils:format_exception(Kind, Reason, StackTrace)]}.

config_mappings(general_sdk) ->
[{"OTEL_LOG_LEVEL", log_level, existing_atom},
[{"OTEL_SDK_DISABLED", sdk_disabled, boolean},
{"OTEL_LOG_LEVEL", log_level, existing_atom},

%% `register_loaded_applications' is kept for backwards compatibility
{"OTEL_REGISTER_LOADED_APPLICATIONS", register_loaded_applications, boolean},
Expand Down
2 changes: 1 addition & 1 deletion apps/opentelemetry/src/otel_resource_detector.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ get_resource() ->
get_resource(Timeout) ->
try gen_statem:call(?MODULE, get_resource, Timeout)
catch
exit:{timeout, _} ->
_:_ ->
%% TODO: should we return an error instead?
%% returning an empty resource ensures we continue on and
%% don't crash anything depending on the returned resource
Expand Down
8 changes: 7 additions & 1 deletion apps/opentelemetry/src/otel_tracer_provider_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ start(Name, Config) ->
start(Name, otel_resource:create([]), Config).

start(Name, Resource, Config) ->
supervisor:start_child(?MODULE, [Name, Resource, Config]).
try
supervisor:start_child(?MODULE, [Name, Resource, Config])
catch
exit:{noproc, _} ->
%% no tracer provider sup is started, the sdk is probably disabled
{error, no_tracer_provider_supervisor}
end.

init([]) ->
SupFlags = #{strategy => simple_one_for_one,
Expand Down
18 changes: 17 additions & 1 deletion apps/opentelemetry/test/opentelemetry_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ all() ->
{group, otel_batch_processor}].

all_cases() ->
[with_span, macros, child_spans,
[with_span, macros, child_spans, disabled_sdk,
update_span_data, tracer_instrumentation_scope, tracer_previous_ctx, stop_temporary_app,
reset_after, attach_ctx, default_sampler, non_recording_ets_table,
root_span_sampling_always_on, root_span_sampling_always_off,
Expand All @@ -59,6 +59,10 @@ init_per_group(Processor, Config) ->
end_per_group(_, _Config) ->
ok.

init_per_testcase(disabled_sdk, Config) ->
application:set_env(opentelemetry, sdk_disabled, true),
{ok, _} = application:ensure_all_started(opentelemetry),
Config;
init_per_testcase(no_exporter, Config) ->
application:set_env(opentelemetry, processors,
[{otel_batch_processor, #{scheduled_delay_ms => 1}}]),
Expand Down Expand Up @@ -139,6 +143,11 @@ init_per_testcase(_, Config) ->
{ok, _} = application:ensure_all_started(opentelemetry),
[{tid, Tid} | Config].

end_per_testcase(disabled_sdk, _Config) ->
application:set_env(opentelemetry, sdk_disabled, false),
_ = application:stop(opentelemetry),
_ = application:unload(opentelemetry),
ok;
end_per_testcase(disable_auto_creation, _Config) ->
_ = application:stop(opentelemetry),
_ = application:unload(opentelemetry),
Expand Down Expand Up @@ -1062,6 +1071,13 @@ too_many_attributes(Config) ->

ok.

disabled_sdk(_Config) ->
SpanCtx1 = ?start_span(<<"span-1">>),

?assertMatch(#span_ctx{trace_id=0,
span_id=0}, SpanCtx1),
ok.

no_exporter(_Config) ->
SpanCtx1 = ?start_span(<<"span-1">>),

Expand Down

0 comments on commit eafe427

Please sign in to comment.