Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of empty and missing tag values with standard formatter #86

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading