Skip to content

Commit 9a917f3

Browse files
committed
Fix handling of empty and missing tag values with standard formatter
1 parent f9229e1 commit 9a917f3

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

lib/telemetry_metrics_statsd/formatter/standard.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ defmodule TelemetryMetricsStatsd.Formatter.Standard do
5858
end
5959

6060
defp do_format_metric_tags([{_, nil} | tags]) do
61-
[?., "nil" | format_metric_tags(tags)]
61+
[?., "nil" | do_format_metric_tags(tags)]
6262
end
6363

6464
defp do_format_metric_tags([{_, tag_value} | tags]) do
65-
[?., to_string(tag_value) | format_metric_tags(tags)]
65+
[?., to_string(tag_value) | do_format_metric_tags(tags)]
6666
end
6767

6868
defp format_metric_value(%Metrics.Counter{}, _value), do: "1|c"

test/telemetry_metrics_statsd/formatter/standard_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ defmodule TelemetryMetricsStatsd.Formatter.StandardTest do
6464
test "nil tags are included in the formatted metric" do
6565
m = given_last_value("my.awesome.metric", tags: [:method, :status])
6666

67-
assert format(m, 131, method: nil, status: 200) ==
68-
"my.awesome.metric.nil.200:131|g"
67+
assert format(m, 131, method: "GET", status: nil) ==
68+
"my.awesome.metric.GET.nil:131|g"
6969
end
7070

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

74-
assert format(m, 131, method: "", status: 200) == ""
74+
assert format(m, 131, method: "GET", status: "") == ""
7575
end
7676

7777
test "tags passed as explicit argument are used for the formatted metric" do

test/telemetry_metrics_statsd_test.exs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,47 @@ defmodule TelemetryMetricsStatsdTest do
144144
"invalid value for :formatter option: expected :formatter be either :standard or :datadog, got :my_formatter"
145145
end
146146

147-
test "it doesn't crash when tag values are missing" do
147+
148+
test "it doesn't crash when tag values are missing with standard formatter" do
149+
{socket, port} = given_udp_port_opened()
150+
151+
counter = given_counter("http.request.count", tags: [:method, :status])
152+
153+
start_reporter(
154+
metrics: [counter],
155+
port: port,
156+
)
157+
158+
handlers_before = :telemetry.list_handlers([])
159+
160+
:telemetry.execute([:http, :request], %{latency: 172}, %{method: "GET"})
161+
assert_reported(socket, "")
162+
163+
handlers_after = :telemetry.list_handlers([])
164+
assert handlers_after == handlers_before
165+
end
166+
167+
168+
test "it doesn't crash when tag values are nil with standard formatter" do
169+
{socket, port} = given_udp_port_opened()
170+
171+
counter = given_counter("http.request.count", tags: [:method, :status])
172+
173+
start_reporter(
174+
metrics: [counter],
175+
port: port,
176+
)
177+
178+
handlers_before = :telemetry.list_handlers([])
179+
180+
:telemetry.execute([:http, :request], %{latency: 172}, %{method: "GET", status: nil})
181+
assert_reported(socket, "http.request.count.GET.nil:1|c")
182+
183+
handlers_after = :telemetry.list_handlers([])
184+
assert handlers_after == handlers_before
185+
end
186+
187+
test "it doesn't crash when tag values are missing with DataDog formatter" do
148188
{socket, port} = given_udp_port_opened()
149189

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

0 commit comments

Comments
 (0)