Skip to content

Commit 22f3417

Browse files
SergeTupchiybryannaegele
authored andcommitted
fix(otel_batch_processor): start check_table_size timeouts
1 parent c00cffe commit 22f3417

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

apps/opentelemetry/src/otel_batch_processor.erl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,17 @@ callback_mode() ->
190190
idle(enter, _OldState, Data=#data{exporter=undefined,
191191
exporter_config=ExporterConfig,
192192
scheduled_delay_ms=SendInterval,
193+
check_table_size_ms=CheckInterval,
193194
reg_name=RegName}) ->
194195
Exporter = init_exporter(RegName, ExporterConfig),
195-
{keep_state, Data#data{exporter=Exporter}, [{{timeout, export_spans}, SendInterval, export_spans}]};
196-
idle(enter, _OldState, #data{scheduled_delay_ms=SendInterval}) ->
197-
{keep_state_and_data, [{{timeout, export_spans}, SendInterval, export_spans}]};
196+
{keep_state, Data#data{exporter=Exporter},
197+
[{{timeout, export_spans}, SendInterval, export_spans},
198+
{{timeout, check_table_size}, CheckInterval, check_table_size}]};
199+
idle(enter, _OldState, #data{scheduled_delay_ms=SendInterval,
200+
check_table_size_ms=CheckInterval}) ->
201+
{keep_state_and_data,
202+
[{{timeout, export_spans}, SendInterval, export_spans},
203+
{{timeout, check_table_size}, CheckInterval, check_table_size}]};
198204
idle(_, export_spans, Data=#data{exporter=undefined,
199205
exporter_config=ExporterConfig,
200206
reg_name=RegName}) ->
@@ -271,15 +277,15 @@ handle_event_(_State, _, force_flush, Data) ->
271277
handle_event_(_State, {timeout, check_table_size}, check_table_size, #data{max_queue_size=infinity}) ->
272278
keep_state_and_data;
273279
handle_event_(_State, {timeout, check_table_size}, check_table_size, #data{max_queue_size=MaxQueueSize,
280+
check_table_size_ms=CheckInterval,
274281
reg_name=RegName}) ->
275282
case ets:info(?CURRENT_TABLE(RegName), size) of
276283
M when M >= MaxQueueSize ->
277-
disable(RegName),
278-
keep_state_and_data;
284+
disable(RegName);
279285
_ ->
280-
enable(RegName),
281-
keep_state_and_data
282-
end;
286+
enable(RegName)
287+
end,
288+
{keep_state_and_data, [{{timeout, check_table_size}, CheckInterval, check_table_size}]};
283289
handle_event_(_, {call, From}, {set_exporter, ExporterConfig}, Data=#data{exporter=OldExporter,
284290
reg_name=RegName}) ->
285291
otel_exporter:shutdown(OldExporter),

apps/opentelemetry/test/otel_batch_processor_SUITE.erl

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
-include_lib("stdlib/include/assert.hrl").
66
-include_lib("common_test/include/ct.hrl").
77

8+
-include("otel_span.hrl").
89
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
910

1011
all() ->
11-
[exporting_timeout_test].
12+
[exporting_timeout_test,
13+
check_table_size_test].
1214

1315
%% verifies that after the runner has to be killed for taking too long
1416
%% that everything is still functional and the exporter does not crash
1517
exporting_timeout_test(_Config) ->
1618
process_flag(trap_exit, true),
1719

18-
{ok, Pid, _} = otel_batch_processor:start_link(#{reg_name => test_processor,
20+
{ok, Pid, _} = otel_batch_processor:start_link(#{name => test_processor,
1921
resource => otel_resource:create([]),
2022
exporter => ?MODULE,
2123
exporting_timeout_ms => 1,
@@ -30,6 +32,34 @@ exporting_timeout_test(_Config) ->
3032
ok
3133
end.
3234

35+
check_table_size_test(_Config) ->
36+
MaxQueueSize = 10,
37+
CheckTableSizeMs = 1,
38+
{ok, _Pid, #{reg_name := RegName}} = otel_batch_processor:start_link(
39+
#{name => test_processor_check_size_test,
40+
resource => otel_resource:create([]),
41+
exporter => ?MODULE,
42+
exporting_timeout_ms => timer:minutes(10),
43+
%% long enough, so that it never happens during the test
44+
scheduled_delay_ms => timer:minutes(10),
45+
check_table_size_ms => CheckTableSizeMs,
46+
max_queue_size => MaxQueueSize}
47+
),
48+
%% max_queue_size limit is not reached
49+
true = otel_batch_processor:on_end(generate_span(), #{reg_name => RegName}),
50+
lists:foreach(fun(_) ->
51+
otel_batch_processor:on_end(generate_span(), #{reg_name => RegName})
52+
end,
53+
lists:seq(1, MaxQueueSize)),
54+
%% Wait for more than CheckTablesizeMS to be sure check timeout occurred
55+
timer:sleep(CheckTableSizeMs * 5),
56+
dropped = otel_batch_processor:on_end(generate_span(), #{reg_name => RegName}),
57+
58+
otel_batch_processor:force_flush(#{reg_name => RegName}),
59+
%% force_flush is async, have to wait for some long enough time again,
60+
timer:sleep(CheckTableSizeMs * 10),
61+
true = otel_batch_processor:on_end(generate_span(), #{reg_name => RegName}).
62+
3363
%% exporter behaviour
3464

3565
init(_) ->
@@ -40,3 +70,13 @@ export(_, _) ->
4070

4171
shutdown(_) ->
4272
ok.
73+
74+
%% helpers
75+
76+
generate_span() ->
77+
#span{trace_id = otel_id_generator:generate_trace_id(),
78+
span_id = otel_id_generator:generate_span_id(),
79+
name = "test_span",
80+
trace_flags = 1,
81+
is_recording = true,
82+
instrumentation_scope = #instrumentation_scope{name = "test"}}.

0 commit comments

Comments
 (0)