MG-137 Add key prefix for statsd publisher (#6)

This commit is contained in:
Andrey Fadeev 2018-12-11 16:30:20 +03:00 committed by GitHub
parent d56ec23c52
commit 1de8ccb240
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 6 deletions

View File

@ -1,15 +1,15 @@
-module(hay_statsd_protocol).
-export([encode_metric/1]).
-export([encode_metric/2]).
%% API
-spec encode_metric(hay_metrics:metric()) -> binary().
encode_metric(Metric) ->
-spec encode_metric(hay_metrics:metric(), binary()) -> binary().
encode_metric(Metric, Prefix) ->
Type = hay_metrics:type(Metric),
Key = hay_metrics:key(Metric),
Value = hay_metrics:value(Metric),
<<Key/binary, ":", (format_value(Value))/binary, "|", (encode_type(Type)):8, "\n">>.
<<Prefix/binary, Key/binary, ":", (format_value(Value))/binary, "|", (encode_type(Type)):8, "\n">>.
%% Internals

View File

@ -11,6 +11,7 @@
-type port_number() :: inet:port_number().
-type options() :: #{
key_prefix => binary(),
interval => timeout(),
host => host(),
port => port_number(),
@ -35,6 +36,7 @@
%% Internal types
-record(state, {
key_prefix :: binary(),
interval :: timeout(),
host :: host(),
port :: port_number(),
@ -50,6 +52,7 @@
-spec init(options()) -> {ok, state()}.
init(Options) ->
{ok, #state{
key_prefix = maps:get(key_prefix, Options, <<"">>),
interval = maps:get(interval, Options, 1000),
host = maps:get(host, Options, "localhost"),
port = maps:get(port, Options, 8125),
@ -87,8 +90,8 @@ encode_packets([], _State, Acc, [], _Size) ->
Acc;
encode_packets([], _State, Acc, PacketAcc, _Size) ->
[PacketAcc | Acc];
encode_packets([Metric | Rest], #state{mtu = MTU} = State, Acc, PacketAcc, Size) ->
MetricBin = hay_statsd_protocol:encode_metric(Metric),
encode_packets([Metric | Rest], #state{mtu = MTU, key_prefix = Prefix} = State, Acc, PacketAcc, Size) ->
MetricBin = hay_statsd_protocol:encode_metric(Metric, Prefix),
MetricSize = erlang:byte_size(MetricBin),
case Size + MetricSize of
TotalSize when TotalSize > MTU ->

View File

@ -7,6 +7,7 @@
]},
{metrics_publishers, [
{hay_statsd_publisher, #{
key_prefix => <<"">>,
interval => 1000,
host => "localhost",
port => 8125,