Skip to content

Commit c5ca033

Browse files
authored
Merge pull request #727 from whatyouhide/al/resource-docs
Documentation for `otel_resource*` modules
2 parents f587610 + 62553dd commit c5ca033

File tree

4 files changed

+81
-15
lines changed

4 files changed

+81
-15
lines changed

apps/opentelemetry/src/otel_resource.erl

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@
1414
%%
1515
%% @doc A Resource is attributes representing the entity producing
1616
%% telemetry. For example, by default the language (Erlang), name of this
17-
%% library (opentelemetry) and version of this library are included in
17+
%% library (`opentelemetry'), and version of this library are included in
1818
%% the Resource.
1919
%%
2020
%% This module provides the functional interface for working with the
2121
%% resource record.
22+
%%
23+
%% The `opentelemetry' library supports <i>resource detectors</i> to
24+
%% detect attributes to include in the Resource. See {@link otel_resource_detector}
25+
%% for the behaviour to detect resources, and the {@link otel_resource_app_env}
26+
%% and {@link otel_resource_env_var} modules for built-in implementations.
27+
%%
28+
%% See the <a href="https://opentelemetry.io/docs/concepts/resources/">
29+
%% OpenTelemetry Resource documentation</a> for more information on Resources.
2230
%% @end
2331
%%%-----------------------------------------------------------------------
2432
-module(otel_resource).
@@ -32,22 +40,33 @@
3240

3341
-type key() :: unicode:latin1_binary() | atom().
3442
%% values allowed in attributes of a resource are limited
43+
3544
-type value() :: unicode:latin1_binary() | integer() | float() | boolean().
45+
%% A resource value.
46+
3647
-type schema_url() :: uri_string:uri_string().
48+
%% A schema URL for the resource.
3749

3850
-define(MAX_LENGTH, 255).
3951

4052
-record(resource, {schema_url :: schema_url() | undefined,
4153
attributes :: otel_attributes:t()}).
54+
4255
-type t() :: #resource{} | undefined.
56+
%% The type that represents a resource.
4357

4458
-export_type([t/0]).
4559

60+
%% @equiv create(Attributes, undefined)
4661
-spec create(#{key() => value()} | [{key(), value()}]) -> t().
4762
create(Attributes) ->
4863
create(Attributes, undefined).
4964

50-
%% verifies each key and value and drops any that don't pass verification
65+
%% @doc Creates a new resources from the given map or list of `Attributes' and
66+
%% with the given `SchemaUrl'.
67+
%%
68+
%%
69+
%% This function verifies each key and value, and drops any that don't pass verification.
5170
-spec create(#{key() => value()} | [{key(), value()}], schema_url() | undefined) -> t().
5271
create(Map, SchemaUrl) when is_map(Map) ->
5372
create(maps:to_list(Map), SchemaUrl);
@@ -69,18 +88,24 @@ create(List, SchemaUrl) when is_list(List) ->
6988
#resource{schema_url=SchemaUrl,
7089
attributes=otel_attributes:new(List1, 128, 255)}.
7190

91+
%% @doc Returns the schema URL of the resource.
7292
-spec schema_url(t()) -> schema_url() | undefined.
7393
schema_url(#resource{schema_url=Schema}) ->
7494
Schema;
7595
schema_url(_) ->
7696
undefined.
7797

98+
%% @doc Returns the attributes of the given `Resource'.
99+
%%
100+
%% This function returns `undefined' only in case `Resource' is an invalid argument
101+
%% (not a resource record).
78102
-spec attributes(t()) -> otel_attributes:t() | undefined.
79103
attributes(#resource{attributes=Attributes}) ->
80104
Attributes;
81105
attributes(_) ->
82106
undefined.
83107

108+
%% @doc Returns `true' if `Key' is valid and part of the given resource.
84109
-spec is_key(key(), t()) -> boolean().
85110
is_key(K, #resource{attributes=Attributes}) ->
86111
case try_check_key(K, false) of
@@ -92,14 +117,16 @@ is_key(K, #resource{attributes=Attributes}) ->
92117
is_key(_, _) ->
93118
false.
94119

95-
%% in case of collision the updating, first argument, resource takes precedence.
120+
%% @doc Merges the two given resources.
121+
%%
122+
%% In case of collision, the first argument (`Resource') takes precedence.
96123
-spec merge(t(), t()) -> t().
97124
merge(#resource{schema_url=NewSchemaUrl,
98-
attributes=NewAttributes}, Current=#resource{schema_url=CurrentSchemaUrl,
125+
attributes=NewAttributes}, CurrentResource=#resource{schema_url=CurrentSchemaUrl,
99126
attributes=CurrentAttributes}) ->
100127
SchameUrl = merge_schema_url(NewSchemaUrl, CurrentSchemaUrl),
101128
NewMap = otel_attributes:map(NewAttributes),
102-
Current#resource{schema_url=SchameUrl,
129+
CurrentResource#resource{schema_url=SchameUrl,
103130
attributes=otel_attributes:set(NewMap, CurrentAttributes)}.
104131

105132
%%

apps/opentelemetry/src/otel_resource_app_env.erl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,23 @@
1212
%% See the License for the specific language governing permissions and
1313
%% limitations under the License.
1414
%%
15-
%% @doc Adds attributes to the `Resource' based on the value of `resource'
15+
%% @doc Resource detector ({@link otel_resource_detector}) which adds attributes
16+
%% to the `Resource' based on the value of `resource'
1617
%% in the `opentelemetry' application's environment.
1718
%%
18-
%% [{service, #{name => "service-name",
19-
%% namespace => "service-namespace"}]
19+
%% For example, if the `opentelemetry' application environment has the following
20+
%% configuration under the `resource' key:
2021
%%
21-
%% Results in the `Resource' attributes `service.name' and `service.namespace'.
22+
%% ```
23+
%% [{service, #{name => "myservice",
24+
%% namespace => "mynamespace"}}]
25+
%% '''
26+
%%
27+
%% then it results in the `Resource' attributes `service.name' and `service.namespace'
28+
%% set to `myservice' and `mynamespace"}}]' respectively.
29+
%%
30+
%% This detector is on by default (see the default configuration for `resource_detectors' in the
31+
%% `opentelemetry' application environment).
2232
%% @end
2333
%%%-----------------------------------------------------------------------
2434
-module(otel_resource_app_env).
@@ -28,12 +38,14 @@
2838
-export([get_resource/1,
2939
parse/1]).
3040

41+
%% @private
3142
get_resource(_Config) ->
3243
Attributes = parse(application:get_env(opentelemetry, resource, #{})),
3344
otel_resource:create(Attributes).
3445

3546
%%
3647

48+
%% @private
3749
parse(Attributes) when is_map(Attributes) ->
3850
parse(maps:to_list(Attributes));
3951
parse(Attributes) when is_list(Attributes) ->

apps/opentelemetry/src/otel_resource_detector.erl

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
%% @doc Resource detectors are responsible for reading in attributes about
1616
%% the runtime environment of a node (such as an environment variable or
1717
%% some metadata endpoint provided by a cloud host) and returning a
18-
%% `otel_resource:t()' made from those attributes.
18+
%% {@link otel_resource:t()} made from those attributes.
1919
%%
20-
%% The state machine will spawn a process for each detector and collect the
21-
%% results of running each and merge in the order they are defined. Once in
22-
%% the `ready' state it will reply to `get_resource' calls with the final
23-
%% `otel_resource:t()'.
20+
%% This module is meant for users who intend to write their own resource
21+
%% detectors.
22+
%%
23+
%% This behaviour is a state machine (started by the `opentelemetry' application)
24+
%% which spawns a process for each detector, collects the
25+
%% results of running each, and merges them in the order they are defined. Once the
26+
%% state machine process is ready, it will reply to {@link get_resource/1}
27+
%% calls with the final {@link otel_resource:t()}.
2428
%% @end
2529
%%%-------------------------------------------------------------------------
2630
-module(otel_resource_detector).
@@ -36,6 +40,8 @@
3640
handle_event/4]).
3741

3842
-callback get_resource(term()) -> otel_resource:t().
43+
%% Function that takes the configuration of a resource detector
44+
%% and returns a resource.
3945

4046
-type detector() :: module() | {module(), term()}.
4147

@@ -46,15 +52,24 @@
4652
detectors :: [detector()],
4753
detector_timeout :: integer()}).
4854

55+
%% @private
4956
-spec start_link(Config) -> {ok, pid()} | ignore | {error, term()} when
5057
Config :: #{resource_detectors := [module()],
5158
resource_detector_timeout := integer()}.
5259
start_link(Config) ->
5360
gen_statem:start_link({local, ?MODULE}, ?MODULE, [Config], []).
5461

62+
%% @equiv get_resource(6000)
63+
-spec get_resource() -> otel_resource:t().
5564
get_resource() ->
5665
get_resource(6000).
5766

67+
%% @doc Gets the resource formed by detecting attributes through resource
68+
%% detectors.
69+
%%
70+
%% If the call doesn't complete within the given `Timeout' then an empty
71+
%% resource is returned.
72+
-spec get_resource(timeout()) -> otel_resource:t().
5873
get_resource(Timeout) ->
5974
try gen_statem:call(?MODULE, get_resource, Timeout)
6075
catch
@@ -67,6 +82,7 @@ get_resource(Timeout) ->
6782
otel_resource:create([])
6883
end.
6984

85+
%% @private
7086
init([#{resource_detectors := Detectors,
7187
resource_detector_timeout := DetectorTimeout}]) ->
7288
process_flag(trap_exit, true),
@@ -76,9 +92,11 @@ init([#{resource_detectors := Detectors,
7692
detector_timeout=DetectorTimeout},
7793
[{next_event, internal, spawn_detectors}]}.
7894

95+
%% @private
7996
callback_mode() ->
8097
[handle_event_function, state_enter].
8198

99+
%% @private
82100
handle_event(enter, _, ready, Data=#data{resource=Resource}) ->
83101
NewResource = default_resource_attributes(Resource),
84102
{keep_state, Data#data{resource=NewResource}};

apps/opentelemetry/src/otel_resource_env_var.erl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@
1212
%% See the License for the specific language governing permissions and
1313
%% limitations under the License.
1414
%%
15-
%% @doc
15+
%% @doc Resource detector ({@link otel_resource_detector}) which adds attributes
16+
%% to the `Resource' based on environment variables.
1617
%%
18+
%% This resource detector reads the `OTEL_RESOURCE_ATTRIBUTES' environment
19+
%% variable and parses it as a comma-separated list of key-value pairs. For
20+
% example, `key1=val1,key2=val2'.
21+
%%
22+
%% This detector is on by default (see the default configuration for `resource_detectors' in the
23+
%% `opentelemetry' application environment).
1724
%% @end
1825
%%%-----------------------------------------------------------------------
1926
-module(otel_resource_env_var).
@@ -27,11 +34,13 @@
2734
-define(LABEL_LIST_SPLITTER, ",").
2835
-define(LABEL_KEY_VALUE_SPLITTER, "=").
2936

37+
%% @private
3038
get_resource(_Config) ->
3139
otel_resource:create(parse(os:getenv(?OS_ENV))).
3240

3341
%%
3442

43+
%% @private
3544
-spec parse(false | string()) -> list().
3645
parse(false) ->
3746
[];

0 commit comments

Comments
 (0)