Skip to content

Commit 4551efa

Browse files
authored
Align documentation/tests with typespecs for :keep and :drop (#121)
The documentation and tests for the `:keep` and `:drop` metric options incorrectly suggested that the optional 2nd argument for both options (introduced in 3fde830) were a single `measurement` value instead of a `measurements` map. See #118
1 parent 8130949 commit 4551efa

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

lib/telemetry_metrics.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ defmodule Telemetry.Metrics do
6868
* `:tag_values` - a function that receives the metadata and returns a map with
6969
the tags as keys and their respective values. Defaults to returning the
7070
metadata itself.
71-
* `:keep` - a predicate function that evaluates the metadata and measurement
72-
to conditionally record a given event. `:keep` and `:drop` cannot be
73-
combined. Defaults to `nil`.
74-
* `:drop` - a predicate function that evaluates the metadata and measurement
75-
to conditionally skip recording a given event. `:keep` and `:drop` cannot
76-
be combined. Defaults to `nil`.
71+
* `:keep` - a predicate function that evaluates the metadata and
72+
measurements to conditionally record a given event. `:keep` and `:drop`
73+
cannot be combined. Defaults to `nil`.
74+
* `:drop` - a predicate function that evaluates the metadata and
75+
measurements to conditionally skip recording a given event. `:keep` and
76+
`:drop` cannot be combined. Defaults to `nil`.
7777
* `:description` - human-readable description of the metric. Might be used by
7878
reporters for documentation purposes. Defaults to `nil`.
7979
* `:unit` - an atom describing the unit of selected measurement, typically in

test/console_reporter_test.exs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ defmodule Telemetry.Metrics.ConsoleReporterTest do
2525
metadata[:boom] == :pow
2626
end
2727
),
28+
summary("parser.result.size",
29+
keep: fn _metadata, %{size: size} -> size > 1024 end
30+
),
2831
sum("telemetry.event_size.metadata",
2932
measurement: &__MODULE__.metadata_measurement/2
3033
),
@@ -110,7 +113,7 @@ defmodule Telemetry.Metrics.ConsoleReporterTest do
110113
"""
111114
end
112115

113-
test "filters events", %{device: device} do
116+
test "filters events on metadata", %{device: device} do
114117
:telemetry.execute([:http, :request], %{response_time: 1000}, %{foo: :bar, boom: :pow})
115118
{_in, out} = StringIO.contents(device)
116119

@@ -126,6 +129,32 @@ defmodule Telemetry.Metrics.ConsoleReporterTest do
126129
"""
127130
end
128131

132+
test "filters events on measurement", %{device: device} do
133+
:telemetry.execute([:parser, :result], %{size: 512})
134+
:telemetry.execute([:parser, :result], %{size: 2048})
135+
{_in, out} = StringIO.contents(device)
136+
137+
assert out == """
138+
[Telemetry.Metrics.ConsoleReporter] Got new event!
139+
Event name: parser.result
140+
All measurements: %{size: 512}
141+
All metadata: %{}
142+
143+
Metric measurement: :size (summary)
144+
Event dropped
145+
146+
[Telemetry.Metrics.ConsoleReporter] Got new event!
147+
Event name: parser.result
148+
All measurements: %{size: 2048}
149+
All metadata: %{}
150+
151+
Metric measurement: :size (summary)
152+
With value: 2048
153+
Tag values: %{}
154+
155+
"""
156+
end
157+
129158
test "logs bad metrics", %{device: device} do
130159
log =
131160
capture_log(fn ->

test/telemetry_metrics_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ defmodule Telemetry.MetricsTest do
166166
metric =
167167
apply(Metrics, unquote(metric_type), [
168168
"my.repo.query",
169-
[keep: &(match?(%{repo: :my_app_read_only_repo}, &1) and &2 > 100)]
169+
[keep: &(match?(%{repo: :my_app_read_only_repo}, &1) and &2.duration > 100)]
170170
])
171171

172-
assert metric.keep.(%{repo: :my_app_read_only_repo}, 200)
173-
refute metric.keep.(%{repo: :my_app_read_only_repo}, 50)
172+
assert metric.keep.(%{repo: :my_app_read_only_repo}, %{duration: 200})
173+
refute metric.keep.(%{repo: :my_app_read_only_repo}, %{duration: 50})
174174
end
175175

176176
test "setting both keep and drop options raises" do

0 commit comments

Comments
 (0)