Skip to content

Commit 7a3628e

Browse files
authored
Add persistent_term built-in measurement (#75)
1 parent 43a0f4f commit 7a3628e

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/telemetry_poller.erl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ The following measurements are supported:
4848
* `memory` (default)
4949
* `total_run_queue_lengths` (default)
5050
* `system_counts` (default)
51+
* `persistent_term` (default)
5152
* `{process_info, Proplist}`
5253
* `{Module, Function, Args}`
5354
5455
We will discuss each measurement in detail. Also note that the
5556
`telemetry_poller` application ships with a built-in poller that
56-
measures `memory`, `total_run_queue_lengths` and `system_counts`. This takes
57+
measures `memory`, `total_run_queue_lengths`, `system_counts`, and `persistent_term`. This takes
5758
the VM measurement out of the way so your application can focus
5859
on what is specific to its behaviour.
5960
@@ -83,6 +84,14 @@ The measurement includes:
8384
* `atom_count` - the number of atoms currently existing at the local node
8485
* `port_count` - the number of ports currently existing at the local node
8586
87+
### Persistent term (since 1.2.0)
88+
89+
An event emitted as `[vm, persistent_term]`. The measurement includes information
90+
about persistent terms in the system, as returned by `persistent_term:info/0`:
91+
92+
* `count` - The number of persistent terms
93+
* `memory` - The total amount of memory (measured in bytes) used by all persistent terms
94+
8695
### Process info
8796
8897
A measurement with information about a given process. It must be specified
@@ -315,6 +324,7 @@ A measurement for the poller.
315324
memory
316325
| total_run_queue_lengths
317326
| system_counts
327+
| persistent_term
318328
| {process_info, [{name, atom()} | {event, [atom()]} | {keys, [atom()]}]}
319329
| {module(), atom(), list()}.
320330

@@ -425,6 +435,8 @@ parse_measurement(total_run_queue_lengths) ->
425435
{telemetry_poller_builtin, total_run_queue_lengths, []};
426436
parse_measurement(system_counts) ->
427437
{telemetry_poller_builtin, system_counts, []};
438+
parse_measurement(persistent_term) ->
439+
{telemetry_poller_builtin, persistent_term, []};
428440
parse_measurement({process_info, List}) when is_list(List) ->
429441
Name = case proplists:get_value(name, List) of
430442
undefined -> erlang:error({badarg, "Expected `name' key to be given under process_info measurement"});

src/telemetry_poller_app.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ start(_StartType, _StartArgs) ->
1313
PollerOpts ->
1414
Default = #{
1515
name => telemetry_poller_default,
16-
measurements => [memory, total_run_queue_lengths, system_counts]
16+
measurements => [
17+
memory,
18+
total_run_queue_lengths,
19+
system_counts,
20+
persistent_term
21+
]
1722
},
1823
FinalOpts = maps:to_list(maps:merge(Default, maps:from_list(PollerOpts))),
1924
[telemetry_poller:child_spec(FinalOpts)]

src/telemetry_poller_builtin.erl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
memory/0,
66
total_run_queue_lengths/0,
77
system_counts/0,
8+
persistent_term/0,
89
process_info/3
910
]).
1011

@@ -65,3 +66,8 @@ total_run_queue_lengths() ->
6566
port_count => PortCount
6667
}).
6768
-endif.
69+
70+
-spec persistent_term() -> ok.
71+
persistent_term() ->
72+
Info = persistent_term:info(),
73+
telemetry:execute([vm, persistent_term], Info, #{}).

test/telemetry_poller_SUITE.erl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ all() -> [
1212
can_configure_sampling_period,
1313
dispatches_custom_mfa,
1414
dispatches_memory,
15+
dispatches_persistent_term,
1516
dispatches_process_info,
1617
dispatches_system_counts,
1718
dispatches_total_run_queue_lengths,
@@ -154,6 +155,22 @@ dispatches_process_info(_Config) ->
154155
ct:fail(timeout_receive_echo)
155156
end.
156157

158+
dispatches_persistent_term(_Config) ->
159+
{ok, _Poller} = telemetry_poller:start_link([{measurements, [persistent_term]}, {period, 100}]),
160+
HandlerId = attach_to([vm, persistent_term]),
161+
receive
162+
{event, [vm, persistent_term], #{count := Count, memory := Memory}, _} ->
163+
?assert(is_integer(Count)),
164+
?assert(Count >= 0),
165+
?assert(is_integer(Memory)),
166+
?assert(Memory >= 0),
167+
telemetry:detach(HandlerId),
168+
?assert(true)
169+
after
170+
1000 ->
171+
ct:fail(timeout_receive_echo)
172+
end.
173+
157174
attach_to(Event) ->
158175
HandlerId = make_ref(),
159176
telemetry:attach(HandlerId, Event, fun test_handler:echo_event/4, #{caller => erlang:self()}),

0 commit comments

Comments
 (0)