<commit_before>%%%------------------------------------------------------------------------
%% Copyright 2019, 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 The types defined here, and referencing records in opentelemetry.hrl
%% are used to store trace information while being collected on the
%% Erlang node.
%%
%% Thus, while the types are based on protos found in the opentelemetry-proto
%% repo: src/opentelemetry/proto/trace/v1/trace.proto,
%% they are not exact translations because further processing is done after
%% the span has finished and can be vendor specific. For example, there is
%% no count of the number of dropped attributes in the span record. And
%% an attribute's value can be a function to only evaluate the value if it
%% is actually used (at the time of exporting). And the stacktrace is a
%% regular Erlang stack trace.
%% @end
%%%-------------------------------------------------------------------------
-module(opentelemetry).

-export([set_default_tracer/1,
         set_tracer/2,
         register_tracer/2,
         register_application_tracer/1,
         get_tracer/0,
         get_tracer/1,
         set_text_map_extractors/1,
         get_text_map_extractors/0,
         set_text_map_injectors/1,
         get_text_map_injectors/0,
         timestamp/0,
         timestamp_to_nano/1,
         convert_timestamp/2,
         links/1,
         link/1,
         link/2,
         link/4,
         event/2,
         event/3,
         events/1,
         status/2,
         verify_and_set_term/3,
         verify_and_set_term/4,
         generate_trace_id/0,
         generate_span_id/0]).

-include("opentelemetry.hrl").
-include_lib("kernel/include/logger.hrl").

-export_type([tracer/0,
              trace_id/0,
              span_id/0,
              trace_flags/0,
              timestamp/0,
              span_name/0,
              span_ctx/0,
              span/0,
              span_kind/0,
              link/0,
              links/0,
              attribute_key/0,
              attribute_value/0,
              attributes/0,
              event/0,
              events/0,
              event_name/0,
              tracestate/0,
              status/0,
              status_code/0,
              resource/0,
              text_map/0]).

-type tracer()             :: {module(), term()}.

-type trace_id()           :: non_neg_integer().
-type span_id()            :: non_neg_integer().

-type trace_flags()        :: non_neg_integer().

-type timestamp() :: integer().

-type span_ctx()           :: #span_ctx{}.
-type span()               :: term().
-type span_name()          :: unicode:unicode_binary() | atom().

-type attribute_key()      :: unicode:unicode_binary() | atom().
-type attribute_value()    :: any().
-type attribute()          :: {attribute_key(), attribute_value()}.
-type attributes()         :: [attribute()].

-type span_kind()          :: ?SPAN_KIND_INTERNAL    |
                              ?SPAN_KIND_SERVER      |
                              ?SPAN_KIND_CLIENT      |
                              ?SPAN_KIND_PRODUCER    |
                              ?SPAN_KIND_CONSUMER.
-type event()              :: #event{}.
-type events()             :: [#event{}].
-type event_name()         :: unicode:unicode_binary() | atom().
-type link()               :: #link{}.
-type links()              :: [#link{}].
-type status()             :: #status{}.
-type status_code()        :: ?OTEL_STATUS_UNSET | ?OTEL_STATUS_OK | ?OTEL_STATUS_ERROR.

%% The key must begin with a lowercase letter, and can only contain
%% lowercase letters 'a'-'z', digits '0'-'9', underscores '_', dashes
%% '-', asterisks '*', and forward slashes '/'.
%% The value is opaque string up to 256 characters printable ASCII
%% RFC0020 characters (i.e., the range 0x20 to 0x7E) except ',' and '='.
%% Note that this also excludes tabs, newlines, carriage returns, etc.
-type tracestate()         :: [{unicode:latin1_chardata(), unicode:latin1_chardata()}].

-type resource()           :: #{unicode:unicode_binary() => unicode:unicode_binary()}.

-type text_map()       :: [{unicode:unicode_binary(), unicode:unicode_binary()}].

-spec set_default_tracer(tracer()) -> boolean().
set_default_tracer(Tracer) ->
    verify_and_set_term(Tracer, default_tracer, otel_tracer).

-spec set_tracer(atom(), tracer()) -> boolean().
set_tracer(Name, Tracer) ->
    verify_and_set_term(Tracer, Name, otel_tracer).

-spec register_tracer(atom(), string()) -> boolean().
register_tracer(Name, Vsn) ->
    otel_tracer_provider:register_tracer(Name, Vsn).

-spec register_application_tracer(atom()) -> boolean().
register_application_tracer(Name) ->
    otel_tracer_provider:register_application_tracer(Name).

-spec get_tracer() -> tracer().
get_tracer() ->
    persistent_term:get({?MODULE, default_tracer}, {otel_tracer_noop, []}).

-spec get_tracer(atom()) -> tracer().
get_tracer(Name) ->
    persistent_term:get({?MODULE, Name}, get_tracer()).

set_text_map_extractors(List) when is_list(List) ->
    persistent_term:put({?MODULE, text_map_extractors}, List);
set_text_map_extractors(_) ->
    ok.

set_text_map_injectors(List) when is_list(List) ->
    persistent_term:put({?MODULE, text_map_injectors}, List);
set_text_map_injectors(_) ->
    ok.
<commit_msg>Add guard to register_tracer<commit_after>%%%------------------------------------------------------------------------
%% Copyright 2019, 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 The types defined here, and referencing records in opentelemetry.hrl
%% are used to store trace information while being collected on the
%% Erlang node.
%%
%% Thus, while the types are based on protos found in the opentelemetry-proto
%% repo: src/opentelemetry/proto/trace/v1/trace.proto,
%% they are not exact translations because further processing is done after
%% the span has finished and can be vendor specific. For example, there is
%% no count of the number of dropped attributes in the span record. And
%% an attribute's value can be a function to only evaluate the value if it
%% is actually used (at the time of exporting). And the stacktrace is a
%% regular Erlang stack trace.
%% @end
%%%-------------------------------------------------------------------------
-module(opentelemetry).

-export([set_default_tracer/1,
         set_tracer/2,
         register_tracer/2,
         register_application_tracer/1,
         get_tracer/0,
         get_tracer/1,
         set_text_map_extractors/1,
         get_text_map_extractors/0,
         set_text_map_injectors/1,
         get_text_map_injectors/0,
         timestamp/0,
         timestamp_to_nano/1,
         convert_timestamp/2,
         links/1,
         link/1,
         link/2,
         link/4,
         event/2,
         event/3,
         events/1,
         status/2,
         verify_and_set_term/3,
         verify_and_set_term/4,
         generate_trace_id/0,
         generate_span_id/0]).

-include("opentelemetry.hrl").
-include_lib("kernel/include/logger.hrl").

-export_type([tracer/0,
              trace_id/0,
              span_id/0,
              trace_flags/0,
              timestamp/0,
              span_name/0,
              span_ctx/0,
              span/0,
              span_kind/0,
              link/0,
              links/0,
              attribute_key/0,
              attribute_value/0,
              attributes/0,
              event/0,
              events/0,
              event_name/0,
              tracestate/0,
              status/0,
              status_code/0,
              resource/0,
              text_map/0]).

-type tracer()             :: {module(), term()}.

-type trace_id()           :: non_neg_integer().
-type span_id()            :: non_neg_integer().

-type trace_flags()        :: non_neg_integer().

-type timestamp() :: integer().

-type span_ctx()           :: #span_ctx{}.
-type span()               :: term().
-type span_name()          :: unicode:unicode_binary() | atom().

-type attribute_key()      :: unicode:unicode_binary() | atom().
-type attribute_value()    :: any().
-type attribute()          :: {attribute_key(), attribute_value()}.
-type attributes()         :: [attribute()].

-type span_kind()          :: ?SPAN_KIND_INTERNAL    |
                              ?SPAN_KIND_SERVER      |
                              ?SPAN_KIND_CLIENT      |
                              ?SPAN_KIND_PRODUCER    |
                              ?SPAN_KIND_CONSUMER.
-type event()              :: #event{}.
-type events()             :: [#event{}].
-type event_name()         :: unicode:unicode_binary() | atom().
-type link()               :: #link{}.
-type links()              :: [#link{}].
-type status()             :: #status{}.
-type status_code()        :: ?OTEL_STATUS_UNSET | ?OTEL_STATUS_OK | ?OTEL_STATUS_ERROR.

%% The key must begin with a lowercase letter, and can only contain
%% lowercase letters 'a'-'z', digits '0'-'9', underscores '_', dashes
%% '-', asterisks '*', and forward slashes '/'.
%% The value is opaque string up to 256 characters printable ASCII
%% RFC0020 characters (i.e., the range 0x20 to 0x7E) except ',' and '='.
%% Note that this also excludes tabs, newlines, carriage returns, etc.
-type tracestate()         :: [{unicode:latin1_chardata(), unicode:latin1_chardata()}].

-type resource()           :: #{unicode:unicode_binary() => unicode:unicode_binary()}.

-type text_map()       :: [{unicode:unicode_binary(), unicode:unicode_binary()}].

-spec set_default_tracer(tracer()) -> boolean().
set_default_tracer(Tracer) ->
    verify_and_set_term(Tracer, default_tracer, otel_tracer).

-spec set_tracer(atom(), tracer()) -> boolean().
set_tracer(Name, Tracer) ->
    verify_and_set_term(Tracer, Name, otel_tracer).

-spec register_tracer(atom(), string()) -> boolean().
register_tracer(Name, Vsn) when is_atom(Name) and is_binary(Vsn) ->
    otel_tracer_provider:register_tracer(Name, Vsn).

-spec register_application_tracer(atom()) -> boolean().
register_application_tracer(Name) ->
    otel_tracer_provider:register_application_tracer(Name).

-spec get_tracer() -> tracer().
get_tracer() ->
    persistent_term:get({?MODULE, default_tracer}, {otel_tracer_noop, []}).

-spec get_tracer(atom()) -> tracer().
get_tracer(Name) ->
    persistent_term:get({?MODULE, Name}, get_tracer()).

set_text_map_extractors(List) when is_list(List) ->
    persistent_term:put({?MODULE, text_map_extractors}, List);
set_text_map_extractors(_) ->
    ok.

set_text_map_injectors(List) when is_list(List) ->
    persistent_term:put({?MODULE, text_map_injectors}, List);
set_text_map_injectors(_) ->
    ok.

{'fraction_non_alphanumeric': 0.14812912064389075, 'fraction_numerical': 0.012329822758797843, 'mean_word_length': 2.3050367855121676, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5757686', 'n_tokens_mistral': 3544, 'n_tokens_neox': 3441, 'n_words': 877}