Skip to content

Commit

Permalink
Fix handling of empty and missing tag values with standard formatter (#…
Browse files Browse the repository at this point in the history
…86)

Fixes #81
  • Loading branch information
arkgil authored Sep 17, 2023
1 parent 29e0a20 commit 8d07cd8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/telemetry_metrics_statsd/formatter/standard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ defmodule TelemetryMetricsStatsd.Formatter.Standard do
end

defp do_format_metric_tags([{_, nil} | tags]) do
[?., "nil" | format_metric_tags(tags)]
[?., "nil" | do_format_metric_tags(tags)]
end

defp do_format_metric_tags([{_, tag_value} | tags]) do
[?., to_string(tag_value) | format_metric_tags(tags)]
[?., to_string(tag_value) | do_format_metric_tags(tags)]
end

defp format_metric_value(%Metrics.Counter{}, _value), do: "1|c"
Expand Down
6 changes: 3 additions & 3 deletions test/telemetry_metrics_statsd/formatter/standard_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ defmodule TelemetryMetricsStatsd.Formatter.StandardTest do
test "nil tags are included in the formatted metric" do
m = given_last_value("my.awesome.metric", tags: [:method, :status])

assert format(m, 131, method: nil, status: 200) ==
"my.awesome.metric.nil.200:131|g"
assert format(m, 131, method: "GET", status: nil) ==
"my.awesome.metric.GET.nil:131|g"
end

test "empty string tag values are dropped" do
m = given_last_value("my.awesome.metric", tags: [:method, :status])

assert format(m, 131, method: "", status: 200) == ""
assert format(m, 131, method: "GET", status: "") == ""
end

test "tags passed as explicit argument are used for the formatted metric" do
Expand Down
40 changes: 39 additions & 1 deletion test/telemetry_metrics_statsd_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,45 @@ defmodule TelemetryMetricsStatsdTest do
"invalid value for :formatter option: expected :formatter be either :standard or :datadog, got :my_formatter"
end

test "it doesn't crash when tag values are missing" do
test "it doesn't crash when tag values are missing with standard formatter" do
{socket, port} = given_udp_port_opened()

counter = given_counter("http.request.count", tags: [:method, :status])

start_reporter(
metrics: [counter],
port: port
)

handlers_before = :telemetry.list_handlers([])

:telemetry.execute([:http, :request], %{latency: 172}, %{method: "GET"})
assert_reported(socket, "")

handlers_after = :telemetry.list_handlers([])
assert handlers_after == handlers_before
end

test "it doesn't crash when tag values are nil with standard formatter" do
{socket, port} = given_udp_port_opened()

counter = given_counter("http.request.count", tags: [:method, :status])

start_reporter(
metrics: [counter],
port: port
)

handlers_before = :telemetry.list_handlers([])

:telemetry.execute([:http, :request], %{latency: 172}, %{method: "GET", status: nil})
assert_reported(socket, "http.request.count.GET.nil:1|c")

handlers_after = :telemetry.list_handlers([])
assert handlers_after == handlers_before
end

test "it doesn't crash when tag values are missing with DataDog formatter" do
{socket, port} = given_udp_port_opened()

counter = given_counter("http.request.count", tags: [:method, :status])
Expand Down

0 comments on commit 8d07cd8

Please sign in to comment.