Skip to content

Commit 87a5ffb

Browse files
authored
Event handler (#207)
This process is a single global process where the system register handlers. These handlers react on all metrics and log events, and therefore a single process becomes responsible for handling with virtually all metrics and logs of a quite big system, meaning, a bottleneck and a SPOF. Also, having this process deal with logs also means logs lose information about their calls, like stacktraces or file and line positions. Instead, at the points of log and metric generations, these can be incremented locally. Performance is not a problem, metrics usually just increment a counter in an ETS table, which is very likely faster than passing a message with a big payload to a bottlenecked process. Likewise logs, most logs will only be filtered out against a log-level, which in the case of OTP Logger is indeed faster than any message passing even.
1 parent d308435 commit 87a5ffb

14 files changed

+133
-249
lines changed

src/erldns_app.erl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ start(_Type, _Args) ->
3636
Ret.
3737

3838
start_phase(post_start, _StartType, _PhaseArgs) ->
39-
erldns_events:add_handler(erldns_event_handler),
40-
4139
case application:get_env(erldns, custom_zone_parsers) of
4240
{ok, Parsers} ->
4341
erldns_zone_parser:register_parsers(Parsers);
@@ -55,8 +53,18 @@ start_phase(post_start, _StartType, _PhaseArgs) ->
5553
?LOG_INFO("Loading zones from local file"),
5654
erldns_zone_loader:load_zones(),
5755

58-
?LOG_INFO("Notifying servers to start"),
59-
erldns_events:notify({?MODULE, start_servers}),
56+
% Start up the UDP and TCP servers
57+
?LOG_INFO("Starting the UDP and TCP supervisor"),
58+
supervisor:start_child(
59+
erldns_sup,
60+
#{
61+
id => erldns_server_sup,
62+
start => {erldns_server_sup, start_link, []},
63+
restart => permanent,
64+
shutdown => infinity,
65+
type => supervisor
66+
}
67+
),
6068

6169
ok.
6270

src/erldns_decoder.erl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
%% system crash
1717
-module(erldns_decoder).
1818

19+
-include_lib("kernel/include/logger.hrl").
1920
-include_lib("dns_erlang/include/dns_records.hrl").
2021

2122
-export([decode_message/1]).
@@ -36,7 +37,10 @@ decode_message(Bin) ->
3637
M
3738
catch
3839
Exception:Reason ->
39-
erldns_events:notify({?MODULE, decode_message_error, {Exception, Reason, Bin}}),
40+
?LOG_ERROR(
41+
"Error decoding message (module: ~p, event: ~p, data: ~p, exception: ~p, reason: ~p)",
42+
[?MODULE, decode_message_error, Bin, Exception, Reason]
43+
),
4044
{formerr, Reason, Bin}
4145
end
4246
end.

src/erldns_encoder.erl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
%% system crash.
1717
-module(erldns_encoder).
1818

19+
-include_lib("kernel/include/logger.hrl").
1920
-include_lib("dns_erlang/include/dns_records.hrl").
2021

2122
-export([
@@ -39,7 +40,9 @@ encode_message(Response) ->
3940
M
4041
catch
4142
Exception:Reason ->
42-
erldns_events:notify({?MODULE, encode_message_error, {Exception, Reason, Response}}),
43+
?LOG_ERROR("Error encoding message (module: ~p, event: ~p, response: ~p, exception: ~p, reason: ~p)", [
44+
?MODULE, encode_message_error, Response, Exception, Reason
45+
]),
4346
encode_message(build_error_response(Response))
4447
end
4548
end.
@@ -65,7 +68,10 @@ encode_message(Response, Opts) ->
6568
M
6669
catch
6770
Exception:Reason ->
68-
erldns_events:notify({?MODULE, encode_message_error, {Exception, Reason, Response, Opts}}),
71+
?LOG_ERROR(
72+
"Error encoding with opts (module: ~p, event: ~p, response: ~p, opts: ~p, exception: ~p, reason: ~p)",
73+
[?MODULE, encode_message_error, Response, Opts, Exception, Reason]
74+
),
6975
{false, encode_message(build_error_response(Response))}
7076
end
7177
end.

src/erldns_event_handler.erl

Lines changed: 0 additions & 147 deletions
This file was deleted.

src/erldns_events.erl

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/erldns_handler.erl

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ handle(Message, Host, {throttled, Host, _ReqCount}) ->
107107
%% append the SOA record if it is a zone transfer and complete the response
108108
%% by filling out count-related header fields.
109109
handle(Message, Host, _) ->
110-
erldns_events:notify({?MODULE, start_handle, [{host, Host}, {message, Message}]}),
111110
Response = folsom_metrics:histogram_timed_update(request_handled_histogram, ?MODULE, do_handle, [Message, Host]),
112-
erldns_events:notify({?MODULE, end_handle, [{host, Host}, {message, Message}, {response, Response}]}),
113111
Response.
114112

115113
do_handle(Message, Host) ->
@@ -123,10 +121,8 @@ do_handle(Message, Host) ->
123121
handle_message(Message, Host) ->
124122
case erldns_packet_cache:get({Message#dns_message.questions, Message#dns_message.additional}, Host) of
125123
{ok, CachedResponse} ->
126-
erldns_events:notify({?MODULE, packet_cache_hit, [{host, Host}, {message, Message}]}),
127124
CachedResponse#dns_message{id = Message#dns_message.id};
128125
{error, Reason} ->
129-
erldns_events:notify({?MODULE, packet_cache_miss, [{reason, Reason}, {host, Host}, {message, Message}]}),
130126
% SOA lookup
131127
handle_packet_cache_miss(Message, get_authority(Message), Host)
132128
end.
@@ -165,11 +161,10 @@ safe_handle_packet_cache_miss(Message, AuthorityRecords, Host) ->
165161
Response ->
166162
maybe_cache_packet(Response, Response#dns_message.aa)
167163
catch
168-
Exception:Reason:Stacktrace ->
169-
% ?LOG_ERROR("Error answering request (module: ~p, event: ~p, exception: ~p, reason: ~p, message: ~p, stacktrace: "
170-
% "~p)",
171-
% [?MODULE, resolve_error, Exception, Reason, Message, Stacktrace]),
172-
erldns_events:notify({?MODULE, resolve_error, {Exception, Reason, Message, Stacktrace}}),
164+
Class:Reason:Stacktrace ->
165+
?LOG_ERROR(#{what => resolve_error, dns_message => Message, class => Class, reason => Reason, stacktrace => Stacktrace}),
166+
folsom_metrics:notify({erldns_handler_error_counter, {inc, 1}}),
167+
folsom_metrics:notify({erldns_handler_error_meter, 1}),
173168
RCode =
174169
case Reason of
175170
{error, rcode, ?DNS_RCODE_SERVFAIL} -> ?DNS_RCODE_SERVFAIL;
@@ -210,11 +205,13 @@ complete_response(Message) ->
210205
notify_empty_response(Message) ->
211206
case {Message#dns_message.rc, Message#dns_message.anc + Message#dns_message.auc + Message#dns_message.adc} of
212207
{?DNS_RCODE_REFUSED, _} ->
213-
erldns_events:notify({?MODULE, refused_response, Message#dns_message.questions}),
208+
folsom_metrics:notify({refused_response_meter, 1}),
209+
folsom_metrics:notify({refused_response_counter, {inc, 1}}),
214210
Message;
215211
{_, 0} ->
216212
?LOG_INFO("Empty response (module: ~p, event: ~p, message: ~p)", [?MODULE, empty_response, Message]),
217-
erldns_events:notify({?MODULE, empty_response, Message}),
213+
folsom_metrics:notify({empty_response_meter, 1}),
214+
folsom_metrics:notify({empty_response_counter, {inc, 1}}),
218215
Message;
219216
_ ->
220217
Message

src/erldns_resolver.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,8 @@ requires_additional_processing([], Acc) ->
825825
check_dnssec(Message, Host, Question) ->
826826
case proplists:get_bool(dnssec, erldns_edns:get_opts(Message)) of
827827
true ->
828-
erldns_events:notify({?MODULE, dnssec_request, Host, Question#dns_query.name}),
828+
folsom_metrics:notify(dnssec_request_counter, {inc, 1}),
829+
folsom_metrics:notify(dnssec_request_meter, 1),
829830
true;
830831
false ->
831832
false

src/erldns_storage.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ load_zones(Filename, false) when is_list(Filename) ->
201201
?LOG_DEBUG("Loaded zones (count: ~p)", [length(JsonZones)]),
202202
{ok, length(JsonZones)};
203203
{error, Reason} ->
204-
erldns_events:notify({?MODULE, failed_zones_load, Reason}),
204+
?LOG_ERROR(
205+
"Failed to load zones (module: ~p, event: ~p, reason: ~p)",
206+
[?MODULE, failed_zones_load, Reason]
207+
),
205208
{err, Reason}
206209
end.
207210

src/erldns_sup.erl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ gc_registered(ProcessName) ->
5858
init(_Args) ->
5959
SysProcs =
6060
[
61-
?CHILD(erldns_events, worker, []),
6261
?CHILD(erldns_zone_cache, worker, []),
6362
?CHILD(erldns_zone_parser, worker, []),
6463
?CHILD(erldns_zone_encoder, worker, []),

0 commit comments

Comments
 (0)