27
27
is_recording /1 ,
28
28
is_valid /1 ,
29
29
is_valid_name /1 ,
30
- process_attributes /1 ,
31
30
validate_start_opts /1 ,
32
31
set_attribute /3 ,
33
32
set_attributes /2 ,
45
44
-include_lib (" opentelemetry_semantic_conventions/include/trace.hrl" ).
46
45
47
46
-define (is_recording (SpanCtx ), SpanCtx =/= undefined andalso SpanCtx # span_ctx .is_recording =:= true ).
48
- -define (is_allowed_key (Key ), is_atom (Key ) orelse (is_binary (Key ) andalso Key =/= <<" " >>)).
49
- -define (is_allowed_value (Value ), is_atom (Value ) orelse
50
- is_boolean (Value ) orelse
51
- is_number (Value ) orelse
52
- is_binary (Value ) orelse
53
- is_list (Value )).
54
47
55
48
-type start_opts () :: #{attributes := opentelemetry :attributes_map (),
56
49
links := [opentelemetry :link ()],
@@ -68,7 +61,7 @@ validate_start_opts(Opts) when is_map(Opts) ->
68
61
StartTime = maps :get (start_time , Opts , opentelemetry :timestamp ()),
69
62
IsRecording = maps :get (is_recording , Opts , true ),
70
63
#{
71
- attributes => process_attributes (Attributes ),
64
+ attributes => otel_attributes : process_attributes (Attributes ),
72
65
links => Links ,
73
66
kind => Kind ,
74
67
start_time => StartTime ,
@@ -89,60 +82,6 @@ is_valid(#span_ctx{trace_id=TraceId,
89
82
is_valid (_ ) ->
90
83
false .
91
84
92
- -spec is_valid_attribute (opentelemetry :attribute_key (), opentelemetry :attribute_value ()) -> boolean ().
93
- is_valid_attribute (Key , Value ) when is_tuple (Value ) , ? is_allowed_key (Key ) ->
94
- is_valid_attribute (Key , tuple_to_list (Value ));
95
- % % lists as attribute values must be primitive types and homogeneous
96
- is_valid_attribute (Key , [Value1 | _Rest ] = Values ) when is_binary (Value1 ) , ? is_allowed_key (Key ) ->
97
- lists :all (fun is_binary /1 , Values );
98
- is_valid_attribute (Key , [Value1 | _Rest ] = Values ) when is_boolean (Value1 ) , ? is_allowed_key (Key ) ->
99
- lists :all (fun is_boolean /1 , Values );
100
- is_valid_attribute (Key , [Value1 | _Rest ] = Values ) when is_atom (Value1 ) , ? is_allowed_key (Key ) ->
101
- lists :all (fun is_valid_atom_value /1 , Values );
102
- is_valid_attribute (Key , [Value1 | _Rest ] = Values ) when is_integer (Value1 ) , ? is_allowed_key (Key ) ->
103
- lists :all (fun is_integer /1 , Values );
104
- is_valid_attribute (Key , [Value1 | _Rest ] = Values ) when is_float (Value1 ) , ? is_allowed_key (Key ) ->
105
- lists :all (fun is_float /1 , Values );
106
- is_valid_attribute (_Key , Value ) when is_list (Value ) ->
107
- false ;
108
- is_valid_attribute (Key , []) when ? is_allowed_key (Key ) ->
109
- true ;
110
- is_valid_attribute (Key , Value ) when ? is_allowed_key (Key ) , ? is_allowed_value (Value ) ->
111
- true ;
112
- is_valid_attribute (_ , _ ) ->
113
- false .
114
-
115
- is_valid_atom_value (undefined ) ->
116
- false ;
117
- is_valid_atom_value (nil ) ->
118
- false ;
119
- is_valid_atom_value (Value ) ->
120
- is_atom (Value ) andalso (is_boolean (Value ) == false ).
121
-
122
- -spec process_attributes (eqwalizer :dynamic ()) -> opentelemetry :attributes_map ().
123
- process_attributes (Attributes ) when is_map (Attributes ) ->
124
- maps :fold (fun process_attribute /3 , #{}, Attributes );
125
- process_attributes ([]) -> #{};
126
- process_attributes (Attributes ) when is_list (Attributes ) ->
127
- process_attributes (maps :from_list (Attributes ));
128
- process_attributes (_ ) ->
129
- #{}.
130
-
131
- process_attribute (Key , Value , Map ) when is_tuple (Value ) ->
132
- List = tuple_to_list (Value ),
133
- case is_valid_attribute (Key , List ) of
134
- true ->
135
- maps :put (Key , Value , Map );
136
- false ->
137
- Map
138
- end ;
139
- process_attribute (Key , Value , Map ) ->
140
- case is_valid_attribute (Key , Value ) of
141
- true ->
142
- maps :put (Key , Value , Map );
143
- false ->
144
- Map
145
- end .
146
85
-spec is_valid_name (any ()) -> boolean ().
147
86
is_valid_name (undefined ) ->
148
87
false ;
@@ -203,14 +142,14 @@ tracestate(_) ->
203
142
SpanCtx :: opentelemetry :span_ctx ().
204
143
set_attribute (SpanCtx = # span_ctx {span_sdk = {Module , _ }}, Key , Value ) when ? is_recording (SpanCtx ) , is_tuple (Value ) ->
205
144
List = tuple_to_list (Value ),
206
- case is_valid_attribute (Key , List ) of
145
+ case otel_attributes : is_valid_attribute (Key , List ) of
207
146
true ->
208
147
Module :set_attribute (SpanCtx , Key , List );
209
148
false ->
210
149
false
211
150
end ;
212
151
set_attribute (SpanCtx = # span_ctx {span_sdk = {Module , _ }}, Key , Value ) when ? is_recording (SpanCtx ) ->
213
- case is_valid_attribute (Key , Value ) of
152
+ case otel_attributes : is_valid_attribute (Key , Value ) of
214
153
true ->
215
154
Module :set_attribute (SpanCtx , Key , Value );
216
155
false ->
@@ -224,7 +163,7 @@ set_attribute(_, _, _) ->
224
163
SpanCtx :: opentelemetry :span_ctx ().
225
164
set_attributes (SpanCtx = # span_ctx {span_sdk = {Module , _ }}, Attributes ) when ? is_recording (SpanCtx ),
226
165
(is_list (Attributes ) orelse is_map (Attributes )) ->
227
- Module :set_attributes (SpanCtx , process_attributes (Attributes ));
166
+ Module :set_attributes (SpanCtx , otel_attributes : process_attributes (Attributes ));
228
167
set_attributes (_ , _ ) ->
229
168
false .
230
169
@@ -237,7 +176,7 @@ add_event(SpanCtx=#span_ctx{span_sdk={Module, _}}, Name, Attributes)
237
176
(is_list (Attributes ) orelse is_map (Attributes )) ->
238
177
case is_valid_name (Name ) of
239
178
true ->
240
- Module :add_event (SpanCtx , Name , process_attributes (Attributes ));
179
+ Module :add_event (SpanCtx , Name , otel_attributes : process_attributes (Attributes ));
241
180
false ->
242
181
false
243
182
end ;
0 commit comments