From 1de8ccb240f3af25f9a08da9f4dc2b4c3856982f Mon Sep 17 00:00:00 2001 From: Andrey Fadeev Date: Tue, 11 Dec 2018 16:30:20 +0300 Subject: [PATCH] MG-137 Add key prefix for statsd publisher (#6) --- src/hay_statsd_protocol.erl | 8 ++++---- src/hay_statsd_publisher.erl | 7 +++++-- sys.config.example | 1 + 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/hay_statsd_protocol.erl b/src/hay_statsd_protocol.erl index 63e23b4..e7876aa 100644 --- a/src/hay_statsd_protocol.erl +++ b/src/hay_statsd_protocol.erl @@ -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), - <>. + <>. %% Internals diff --git a/src/hay_statsd_publisher.erl b/src/hay_statsd_publisher.erl index 0d7c9cf..993239a 100644 --- a/src/hay_statsd_publisher.erl +++ b/src/hay_statsd_publisher.erl @@ -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 -> diff --git a/sys.config.example b/sys.config.example index f880f7f..32f957f 100644 --- a/sys.config.example +++ b/sys.config.example @@ -7,6 +7,7 @@ ]}, {metrics_publishers, [ {hay_statsd_publisher, #{ + key_prefix => <<"">>, interval => 1000, host => "localhost", port => 8125,