Skip to content

Commit 2e00cac

Browse files
authored
Merge pull request #720 from tsloughter/append-v1-metrics
move metrics and logs to separate exporter modules from traces
2 parents e7df425 + ec0bb47 commit 2e00cac

33 files changed

+1766
-804
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_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),

apps/opentelemetry_experimental/src/otel_aggregation_last_value.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
init(#stream{name=Name,
4646
reader=ReaderId,
4747
aggregation_options=_Options,
48-
forget=Forget}, Attributes) ->
48+
forget=Forget}, Attributes) when erlang:is_reference(ReaderId) ->
4949
Generation = case Forget of
5050
true ->
5151
otel_metric_reader:checkpoint_generation(ReaderId);
@@ -153,7 +153,7 @@ datapoint(ExemplarReservoir, ExemplarsTab, CollectionStartTime, #last_value_aggr
153153
start_time=StartTime,
154154
checkpoint=Checkpoint}) ->
155155
Exemplars = otel_metric_exemplar_reservoir:collect(ExemplarReservoir, ExemplarsTab, Key),
156-
#datapoint{attributes=Attributes,
156+
#datapoint{attributes=binary_to_term(Attributes),
157157
start_time=StartTime,
158158
time=CollectionStartTime,
159159
value=Checkpoint,

0 commit comments

Comments
 (0)