mirror of
https://github.com/valitydev/how_are_you.git
synced 2024-11-06 02:35:16 +00:00
MG-137 Add key prefix for statsd publisher (#6)
This commit is contained in:
parent
d56ec23c52
commit
1de8ccb240
@ -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
|
||||
|
||||
|
@ -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 ->
|
||||
|
@ -7,6 +7,7 @@
|
||||
]},
|
||||
{metrics_publishers, [
|
||||
{hay_statsd_publisher, #{
|
||||
key_prefix => <<"">>,
|
||||
interval => 1000,
|
||||
host => "localhost",
|
||||
port => 8125,
|
||||
|
Loading…
Reference in New Issue
Block a user