14
14
% %
15
15
% % @doc A Resource is attributes representing the entity producing
16
16
% % 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
18
18
% % the Resource.
19
19
% %
20
20
% % This module provides the functional interface for working with the
21
21
% % 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.
22
30
% % @end
23
31
% %%-----------------------------------------------------------------------
24
32
-module (otel_resource ).
32
40
33
41
-type key () :: unicode :latin1_binary () | atom ().
34
42
% % values allowed in attributes of a resource are limited
43
+
35
44
-type value () :: unicode :latin1_binary () | integer () | float () | boolean ().
45
+ % % A resource value.
46
+
36
47
-type schema_url () :: uri_string :uri_string ().
48
+ % % A schema URL for the resource.
37
49
38
50
-define (MAX_LENGTH , 255 ).
39
51
40
52
-record (resource , {schema_url :: schema_url () | undefined ,
41
53
attributes :: otel_attributes :t ()}).
54
+
42
55
-type t () :: # resource {} | undefined .
56
+ % % The type that represents a resource.
43
57
44
58
-export_type ([t / 0 ]).
45
59
60
+ % % @equiv create(Attributes, undefined)
46
61
-spec create (#{key () => value ()} | [{key (), value ()}]) -> t ().
47
62
create (Attributes ) ->
48
63
create (Attributes , undefined ).
49
64
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.
51
70
-spec create (#{key () => value ()} | [{key (), value ()}], schema_url () | undefined ) -> t ().
52
71
create (Map , SchemaUrl ) when is_map (Map ) ->
53
72
create (maps :to_list (Map ), SchemaUrl );
@@ -69,18 +88,24 @@ create(List, SchemaUrl) when is_list(List) ->
69
88
# resource {schema_url = SchemaUrl ,
70
89
attributes = otel_attributes :new (List1 , 128 , 255 )}.
71
90
91
+ % % @doc Returns the schema URL of the resource.
72
92
-spec schema_url (t ()) -> schema_url () | undefined .
73
93
schema_url (# resource {schema_url = Schema }) ->
74
94
Schema ;
75
95
schema_url (_ ) ->
76
96
undefined .
77
97
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).
78
102
-spec attributes (t ()) -> otel_attributes :t () | undefined .
79
103
attributes (# resource {attributes = Attributes }) ->
80
104
Attributes ;
81
105
attributes (_ ) ->
82
106
undefined .
83
107
108
+ % % @doc Returns `true' if `Key' is valid and part of the given resource.
84
109
-spec is_key (key (), t ()) -> boolean ().
85
110
is_key (K , # resource {attributes = Attributes }) ->
86
111
case try_check_key (K , false ) of
@@ -92,14 +117,16 @@ is_key(K, #resource{attributes=Attributes}) ->
92
117
is_key (_ , _ ) ->
93
118
false .
94
119
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.
96
123
-spec merge (t (), t ()) -> t ().
97
124
merge (# resource {schema_url = NewSchemaUrl ,
98
- attributes = NewAttributes }, Current = # resource {schema_url = CurrentSchemaUrl ,
125
+ attributes = NewAttributes }, CurrentResource = # resource {schema_url = CurrentSchemaUrl ,
99
126
attributes = CurrentAttributes }) ->
100
127
SchameUrl = merge_schema_url (NewSchemaUrl , CurrentSchemaUrl ),
101
128
NewMap = otel_attributes :map (NewAttributes ),
102
- Current # resource {schema_url = SchameUrl ,
129
+ CurrentResource # resource {schema_url = SchameUrl ,
103
130
attributes = otel_attributes :set (NewMap , CurrentAttributes )}.
104
131
105
132
% %
0 commit comments