Skip to content

Commit 9bb72f6

Browse files
authored
Merge branch 'main' into al/document-samplers
2 parents e30700c + ca61a11 commit 9bb72f6

34 files changed

+1811
-812
lines changed

apps/opentelemetry/src/otel_batch_processor.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,18 +437,18 @@ completed(FromPid) ->
437437

438438
export(undefined, _, _) ->
439439
true;
440-
export({ExporterModule, Config}, Resource, SpansTid) ->
440+
export(Exporter, Resource, SpansTid) ->
441441
%% don't let a exporter exception crash us
442442
%% and return true if exporter failed
443443
try
444-
otel_exporter:export_traces(ExporterModule, SpansTid, Resource, Config) =:= failed_not_retryable
444+
otel_exporter_traces:export(Exporter, SpansTid, Resource) =:= failed_not_retryable
445445
catch
446446
Kind:Reason:StackTrace ->
447447
?LOG_INFO(#{source => exporter,
448448
during => export,
449449
kind => Kind,
450450
reason => Reason,
451-
exporter => ExporterModule,
451+
exporter => Exporter,
452452
stacktrace => StackTrace}, #{report_cb => fun ?MODULE:report_cb/1}),
453453
true
454454
end.
@@ -459,7 +459,7 @@ report_cb(#{source := exporter,
459459
during := export,
460460
kind := Kind,
461461
reason := Reason,
462-
exporter := ExporterModule,
462+
exporter := {ExporterModule, _},
463463
stacktrace := StackTrace}) ->
464464
{"span exporter threw exception: exporter=~p ~ts",
465465
[ExporterModule, otel_utils:format_exception(Kind, Reason, StackTrace)]}.

apps/opentelemetry/src/otel_exporter.erl

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,25 @@
2121
export_traces/4,
2222
export_metrics/4,
2323
export_logs/4,
24+
export_traces/3,
25+
export_metrics/3,
26+
export_logs/3,
2427
shutdown/1,
2528
report_cb/1]).
2629

27-
%% Do any initialization of the exporter here and return configuration
28-
%% that will be passed along with a list of spans to the `export' function.
30+
%% Kept only for backwards compatibility. Look at `otel_exporter_traces', `otel_exporter_metrics'
31+
%% and `otel_exporter_logs' instead.
2932
-callback init(term()) -> {ok, term()} | ignore.
3033

31-
%% This function is called when the configured interval expires with any
32-
%% spans that have been collected so far and the configuration returned in `init'.
33-
%% Do whatever needs to be done to export each span here, the caller will block
34-
%% until it returns.
35-
-callback export(traces | metrics, ets:tab(), otel_resource:t(), term()) -> ok |
36-
success |
37-
failed_not_retryable |
38-
failed_retryable.
34+
%% Kept only for backwards compatibility. Look at `otel_exporter_traces', `otel_exporter_metrics'
35+
%% and `otel_exporter_logs' instead.
36+
-callback export(traces | logs | metrics, ets:tab(), otel_resource:t(), term()) -> ok |
37+
success |
38+
failed_not_retryable |
39+
failed_retryable.
40+
41+
%% Kept only for backwards compatibility. Look at `otel_exporter_traces', `otel_exporter_metrics'
42+
%% and `otel_exporter_logs' instead.failed_retryable.
3943
-callback shutdown(term()) -> ok.
4044

4145
-include_lib("kernel/include/logger.hrl").
@@ -116,6 +120,17 @@ init(Exporter) when Exporter =:= none ; Exporter =:= undefined ->
116120
init(ExporterModule) when is_atom(ExporterModule) ->
117121
init({ExporterModule, []}).
118122

123+
export_traces({ExporterModule, Config}, SpansTid, Resource) ->
124+
ExporterModule:export(traces, SpansTid, Resource, Config).
125+
126+
export_metrics({ExporterModule, Config}, MetricsTid, Resource) ->
127+
ExporterModule:export(metrics, MetricsTid, Resource, Config).
128+
129+
export_logs({ExporterModule, Config}, Batch, Resource) ->
130+
ExporterModule:export(logs, Batch, Resource, Config).
131+
132+
%% below export_* functions are for backwards compatibility
133+
119134
export_traces(ExporterModule, SpansTid, Resource, Config) ->
120135
ExporterModule:export(traces, SpansTid, Resource, Config).
121136

apps/opentelemetry/src/otel_exporter_pid.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
%%%-----------------------------------------------------------------------
1818
-module(otel_exporter_pid).
1919

20-
-behaviour(otel_exporter).
20+
-behaviour(otel_exporter_traces).
2121

2222
-export([init/1,
23-
export/4,
23+
export/3,
2424
shutdown/1]).
2525

2626
init(Pid) ->
2727
{ok, Pid}.
2828

29-
export(traces, SpansTid, _Resource, Pid) ->
29+
export(SpansTid, _Resource, Pid) ->
3030
ets:foldl(fun(Span, _Acc) ->
3131
Pid ! {span, Span}
3232
end, [], SpansTid),

apps/opentelemetry/src/otel_exporter_stdout.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
%%%-----------------------------------------------------------------------
1818
-module(otel_exporter_stdout).
1919

20-
-behaviour(otel_exporter).
20+
-behaviour(otel_exporter_traces).
2121

2222
-export([init/1,
23-
export/4,
23+
export/3,
2424
shutdown/1]).
2525

2626
init(_) ->
2727
{ok, []}.
2828

29-
export(_, SpansTid, _Resource, _) ->
29+
export(SpansTid, _Resource, _) ->
3030
io:format("*SPANS FOR DEBUG*~n"),
3131
ets:foldl(fun(Span, _Acc) ->
3232
io:format("~p~n", [Span])

apps/opentelemetry/src/otel_exporter_tab.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
%%%-----------------------------------------------------------------------
1818
-module(otel_exporter_tab).
1919

20-
-behaviour(otel_exporter).
20+
-behaviour(otel_exporter_traces).
2121

2222
-export([init/1,
23-
export/4,
23+
export/3,
2424
shutdown/1]).
2525

2626
init(Tid) ->
2727
{ok, Tid}.
2828

29-
export(traces, SpansTid, _Resource, Tid) ->
29+
export(SpansTid, _Resource, Tid) ->
3030
ets:foldl(fun(Span, _Acc) ->
3131
ets:insert(Tid, Span)
3232
end, [], SpansTid),
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
%%%------------------------------------------------------------------------
2+
%% Copyright 2019, OpenTelemetry Authors
3+
%% Licensed under the Apache License, Version 2.0 (the "License");
4+
%% you may not use this file except in compliance with the License.
5+
%% You may obtain a copy of the License at
6+
%%
7+
%% http://www.apache.org/licenses/LICENSE-2.0
8+
%%
9+
%% Unless required by applicable law or agreed to in writing, software
10+
%% distributed under the License is distributed on an "AS IS" BASIS,
11+
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
%% See the License for the specific language governing permissions and
13+
%% limitations under the License.
14+
%%
15+
%% @doc
16+
%% @end
17+
%%%-----------------------------------------------------------------------
18+
-module(otel_exporter_traces).
19+
20+
-export([init/1,
21+
export/3,
22+
shutdown/1]).
23+
24+
%% Do any initialization of the exporter here and return configuration
25+
%% that will be passed along with a list of spans to the `export' function.
26+
-callback init(term()) -> {ok, term()} | ignore.
27+
28+
%% This function is called when the configured interval expires with any
29+
%% spans that have been collected so far and the configuration returned in `init'.
30+
%% Do whatever needs to be done to export each span here, the caller will block
31+
%% until it returns.
32+
-callback export(ets:tab(), otel_resource:t(), term()) -> ok |
33+
success |
34+
failed_not_retryable |
35+
failed_retryable.
36+
-callback shutdown(term()) -> ok.
37+
38+
init(Opts) ->
39+
otel_exporter:init(Opts).
40+
41+
export({ExporterModule, Config}, Metrics, Resource) ->
42+
ExporterModule:export(Metrics, Resource, Config).
43+
44+
shutdown(Exporter) ->
45+
otel_exporter:shutdown(Exporter).

apps/opentelemetry/src/otel_simple_processor.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,18 @@ completed(FromPid) ->
241241

242242
export(undefined, _, _) ->
243243
true;
244-
export({ExporterModule, Config}, Resource, SpansTid) ->
244+
export(Exporter, Resource, SpansTid) ->
245245
%% don't let a exporter exception crash us
246246
%% and return true if exporter failed
247247
try
248-
otel_exporter:export_traces(ExporterModule, SpansTid, Resource, Config) =:= failed_not_retryable
248+
otel_exporter_traces:export(Exporter, SpansTid, Resource) =:= failed_not_retryable
249249
catch
250250
Kind:Reason:StackTrace ->
251251
?LOG_INFO(#{source => exporter,
252252
during => export,
253253
kind => Kind,
254254
reason => Reason,
255-
exporter => ExporterModule,
255+
exporter => Exporter,
256256
stacktrace => StackTrace}, #{report_cb => fun ?MODULE:report_cb/1}),
257257
true
258258
end.
@@ -263,7 +263,7 @@ report_cb(#{source := exporter,
263263
during := export,
264264
kind := Kind,
265265
reason := Reason,
266-
exporter := ExporterModule,
266+
exporter := {ExporterModule, _},
267267
stacktrace := StackTrace}) ->
268268
{"exporter threw exception: exporter=~p ~ts",
269269
[ExporterModule, otel_utils:format_exception(Kind, Reason, StackTrace)]}.

apps/opentelemetry_api/src/opentelemetry.erl

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,11 @@ convert_timestamp(Timestamp, Unit) ->
343343
Offset = erlang:time_offset(),
344344
erlang:convert_time_unit(Timestamp + Offset, native, Unit).
345345

346-
-spec links([{TraceId, SpanId, Attributes, TraceState} | span_ctx() | {span_ctx(), Attributes}]) -> [link()] when
347-
TraceId :: trace_id(),
348-
SpanId :: span_id(),
346+
%% @doc Creates a list of Span links from the given `List'.
347+
%%
348+
%% This is equivalent to calling {@link link/2} or {@link link/4} multiple times.
349+
-spec links([TraceIdAndSpanId | span_ctx() | {span_ctx(), Attributes}]) -> [link()] when
350+
TraceIdAndSpanId :: {trace_id(), span_id(), Attributes, TraceState},
349351
Attributes :: attributes_map(),
350352
TraceState :: otel_tracestate:t() | [{string(), string()}].
351353
links(List) when is_list(List) ->
@@ -368,18 +370,26 @@ links(List) when is_list(List) ->
368370
links(_) ->
369371
[].
370372

373+
374+
%% @equiv link(SpanCtx, [])
371375
-spec link(span_ctx() | undefined) -> link() | undefined.
372376
link(SpanCtx) ->
373377
link(SpanCtx, []).
374378

379+
%% @doc Creates a Span link to the Span represented by the given `SpanCtx'.
380+
%%
381+
%% The returned link can be used in the `links' field of a Span.
375382
-spec link(span_ctx() | undefined, attributes_map()) -> link() | undefined.
376-
link(#span_ctx{trace_id=TraceId,
377-
span_id=SpanId,
378-
tracestate=TraceState}, Attributes) ->
383+
link(_SpanCtx = #span_ctx{trace_id=TraceId,
384+
span_id=SpanId,
385+
tracestate=TraceState}, Attributes) ->
379386
?MODULE:link(TraceId, SpanId, otel_attributes:process_attributes(Attributes), TraceState);
380387
link(_, _) ->
381388
undefined.
382389

390+
%% @doc Creates a Span link to the Span represented by the given `TraceId' and `SpanId'.
391+
%%
392+
%% The returned link can be used in the `links' field of a Span.
383393
-spec link(TraceId, SpanId, Attributes, TraceState) -> link() | undefined when
384394
TraceId :: trace_id(),
385395
SpanId :: span_id(),
@@ -395,12 +405,18 @@ link(TraceId, SpanId, Attributes, TraceState) when is_integer(TraceId),
395405
link(_, _, _, _) ->
396406
undefined.
397407

408+
%% @equiv event(opentelemetry:timestamp(), Name, Attributes)
398409
-spec event(Name, Attributes) -> event() | undefined when
399410
Name :: event_name(),
400411
Attributes :: attributes_map().
401412
event(Name, Attributes) ->
402413
event(opentelemetry:timestamp(), Name, Attributes).
403414

415+
%% @doc Creates a Span event with the given `Name' and `Attributes'.
416+
%%
417+
%% The Span event is marked to have happened at `Timestamp'. The returned
418+
%% event can be used to add an event to a Span through {@link otel_span:add_events/2},
419+
%% for example.
404420
-spec event(Timestamp, Name, Attributes) -> event() | undefined when
405421
Timestamp :: integer(),
406422
Name :: event_name(),
@@ -419,7 +435,16 @@ event(Timestamp, Name, Attributes) when is_integer(Timestamp),
419435
event(_, _, _) ->
420436
undefined.
421437

422-
events(List) ->
438+
%% @doc Creates a list of Span events from the given `List'.
439+
%%
440+
%% This is a conveneince function to create a list of Span events from a list
441+
%% of `{Time, Name, Attributes}' or `{Name, Attributes}' tuples. It's equivalent
442+
%% to calling {@link event/2} or {@link event/3} multiple times. This function
443+
%% also automatically filters out any invalid tuple.
444+
-spec events([Event]) -> [event()] when
445+
Event :: {Timestamp :: integer(), event_name(), attributes_map()} |
446+
{event_name(), attributes_map()}.
447+
events(Events) ->
423448
Now = opentelemetry:timestamp(),
424449
lists:filtermap(fun({Time, Name, Attributes}) ->
425450
case event(Time, Name, Attributes) of
@@ -437,13 +462,21 @@ events(List) ->
437462
end;
438463
(_) ->
439464
false
440-
end, List).
465+
end, Events).
441466

467+
%% @doc Create a Span status from the given `Code'.
468+
%%
469+
%% The returned status can be used to set the status of a Span through
470+
%% {@link otel_span:set_status/2}, for example.
442471
-spec status(Code) -> status() | undefined when
443472
Code :: status_code().
444473
status(Code) ->
445474
status(Code, <<>>).
446475

476+
%% @doc Create a Span status from the given `Code' and with the given `Message'.
477+
%%
478+
%% The returned status can be used to set the status of a Span through
479+
%% {@link otel_span:set_status/2}, for example.
447480
-spec status(Code, Message) -> status() | undefined when
448481
Code :: status_code(),
449482
Message :: unicode:unicode_binary().
@@ -458,6 +491,7 @@ status(_, _) ->
458491

459492
%% internal functions
460493

494+
%% @private
461495
-spec verify_and_set_term(module() | {module(), term()}, term(), atom()) -> boolean().
462496
verify_and_set_term(Module, TermKey, Behaviour) ->
463497
case verify_module_exists(Module) of
@@ -471,6 +505,7 @@ verify_and_set_term(Module, TermKey, Behaviour) ->
471505
false
472506
end.
473507

508+
%% @private
474509
-spec verify_module_exists(module() | {module(), term()}) -> boolean().
475510
verify_module_exists({Module, _}) ->
476511
verify_module_exists(Module);
@@ -486,6 +521,7 @@ verify_module_exists(Module) ->
486521
%% for use in a filtermap
487522
%% return {true, Link} if a link is returned or return false
488523
%% a list is supported for tracestate for backwards compatibility
524+
%% @private
489525
link_or_false(TraceId, SpanId, Attributes, TraceState) when is_list(TraceState) ->
490526
link_or_false(TraceId, SpanId, Attributes, otel_tracestate:new(TraceState));
491527
link_or_false(TraceId, SpanId, Attributes, TraceState) ->
@@ -519,6 +555,7 @@ schema_url_to_binary(_) ->
519555

520556
%% Vsn can't be an atom or anything but a list or binary
521557
%% so return empty binary string if it isn't a list or binary.
558+
%% @private
522559
vsn_to_binary(Vsn) when is_binary(Vsn) ; is_list(Vsn) ->
523560
unicode:characters_to_binary(Vsn);
524561
vsn_to_binary(_) ->

apps/opentelemetry_experimental/src/otel_aggregation.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ maybe_init_aggregate(Ctx, MetricsTab, ExemplarsTab, Stream=#stream{aggregation_m
4545
attribute_keys=AttributeKeys},
4646
Value, Attributes) ->
4747
{FilteredAttributes, DroppedAttributes} = filter_attributes(AttributeKeys, Attributes),
48-
case AggregationModule:aggregate(Ctx, MetricsTab, ExemplarsTab, Stream, Value, FilteredAttributes, DroppedAttributes) of
48+
case AggregationModule:aggregate(Ctx, MetricsTab, ExemplarsTab, Stream, Value, term_to_binary(FilteredAttributes), DroppedAttributes) of
4949
true ->
5050
true;
5151
false ->
5252
%% entry doesn't exist, create it and rerun the aggregate function
53-
Metric = AggregationModule:init(Stream, FilteredAttributes),
53+
Metric = AggregationModule:init(Stream, term_to_binary(FilteredAttributes)),
5454
%% don't overwrite a possible concurrent measurement doing the same
5555
_ = ets:insert_new(MetricsTab, Metric),
56-
AggregationModule:aggregate(Ctx, MetricsTab, ExemplarsTab, Stream, Value, FilteredAttributes, DroppedAttributes)
56+
AggregationModule:aggregate(Ctx, MetricsTab, ExemplarsTab, Stream, Value, term_to_binary(FilteredAttributes), DroppedAttributes)
5757
end.
5858

5959
filter_attributes(undefined, Attributes) ->

apps/opentelemetry_experimental/src/otel_aggregation_histogram_explicit.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ datapoint(ExemplarReservoir, ExemplarsTab, CollectionStartTime,
308308
Exemplars = otel_metric_exemplar_reservoir:collect(ExemplarReservoir, ExemplarsTab, Key),
309309
Buckets = get_buckets(BucketCounts, Boundaries),
310310
#histogram_datapoint{
311-
attributes=Attributes,
311+
attributes=binary_to_term(Attributes),
312312
start_time=StartTime,
313313
time=CollectionStartTime,
314314
count=lists:sum(Buckets),
@@ -333,7 +333,7 @@ datapoint(ExemplarReservoir, ExemplarsTab, CollectionStartTime,
333333
Exemplars = otel_metric_exemplar_reservoir:collect(ExemplarReservoir, ExemplarsTab, Key),
334334
Buckets = get_buckets(BucketCounts, Boundaries),
335335
#histogram_datapoint{
336-
attributes=Attributes,
336+
attributes=binary_to_term(Attributes),
337337
start_time=StartTime,
338338
time=CollectionStartTime,
339339
count=lists:sum(Buckets),

0 commit comments

Comments
 (0)