diff --git a/src/telemetry_poller.erl b/src/telemetry_poller.erl index a8c4723..74bfc5f 100644 --- a/src/telemetry_poller.erl +++ b/src/telemetry_poller.erl @@ -48,12 +48,13 @@ The following measurements are supported: * `memory` (default) * `total_run_queue_lengths` (default) * `system_counts` (default) + * `persistent_term` (default) * `{process_info, Proplist}` * `{Module, Function, Args}` We will discuss each measurement in detail. Also note that the `telemetry_poller` application ships with a built-in poller that -measures `memory`, `total_run_queue_lengths` and `system_counts`. This takes +measures `memory`, `total_run_queue_lengths`, `system_counts`, and `persistent_term`. This takes the VM measurement out of the way so your application can focus on what is specific to its behaviour. @@ -83,6 +84,14 @@ The measurement includes: * `atom_count` - the number of atoms currently existing at the local node * `port_count` - the number of ports currently existing at the local node +### Persistent term (since 1.2.0) + +An event emitted as `[vm, persistent_term]`. The measurement includes information +about persistent terms in the system, as returned by `persistent_term:info/0`: + + * `count` - The number of persistent terms + * `memory` - The total amount of memory (measured in bytes) used by all persistent terms + ### Process info A measurement with information about a given process. It must be specified @@ -315,6 +324,7 @@ A measurement for the poller. memory | total_run_queue_lengths | system_counts + | persistent_term | {process_info, [{name, atom()} | {event, [atom()]} | {keys, [atom()]}]} | {module(), atom(), list()}. @@ -425,6 +435,8 @@ parse_measurement(total_run_queue_lengths) -> {telemetry_poller_builtin, total_run_queue_lengths, []}; parse_measurement(system_counts) -> {telemetry_poller_builtin, system_counts, []}; +parse_measurement(persistent_term) -> + {telemetry_poller_builtin, persistent_term, []}; parse_measurement({process_info, List}) when is_list(List) -> Name = case proplists:get_value(name, List) of undefined -> erlang:error({badarg, "Expected `name' key to be given under process_info measurement"}); diff --git a/src/telemetry_poller_app.erl b/src/telemetry_poller_app.erl index 1795dd4..2ca1942 100644 --- a/src/telemetry_poller_app.erl +++ b/src/telemetry_poller_app.erl @@ -13,7 +13,12 @@ start(_StartType, _StartArgs) -> PollerOpts -> Default = #{ name => telemetry_poller_default, - measurements => [memory, total_run_queue_lengths, system_counts] + measurements => [ + memory, + total_run_queue_lengths, + system_counts, + persistent_term + ] }, FinalOpts = maps:to_list(maps:merge(Default, maps:from_list(PollerOpts))), [telemetry_poller:child_spec(FinalOpts)] diff --git a/src/telemetry_poller_builtin.erl b/src/telemetry_poller_builtin.erl index 379c864..34fbec7 100644 --- a/src/telemetry_poller_builtin.erl +++ b/src/telemetry_poller_builtin.erl @@ -5,6 +5,7 @@ memory/0, total_run_queue_lengths/0, system_counts/0, + persistent_term/0, process_info/3 ]). @@ -65,3 +66,8 @@ total_run_queue_lengths() -> port_count => PortCount }). -endif. + +-spec persistent_term() -> ok. +persistent_term() -> + Info = persistent_term:info(), + telemetry:execute([vm, persistent_term], Info, #{}). diff --git a/test/telemetry_poller_SUITE.erl b/test/telemetry_poller_SUITE.erl index cb70b04..cbae9ca 100644 --- a/test/telemetry_poller_SUITE.erl +++ b/test/telemetry_poller_SUITE.erl @@ -12,6 +12,7 @@ all() -> [ can_configure_sampling_period, dispatches_custom_mfa, dispatches_memory, + dispatches_persistent_term, dispatches_process_info, dispatches_system_counts, dispatches_total_run_queue_lengths, @@ -154,6 +155,22 @@ dispatches_process_info(_Config) -> ct:fail(timeout_receive_echo) end. +dispatches_persistent_term(_Config) -> + {ok, _Poller} = telemetry_poller:start_link([{measurements, [persistent_term]}, {period, 100}]), + HandlerId = attach_to([vm, persistent_term]), + receive + {event, [vm, persistent_term], #{count := Count, memory := Memory}, _} -> + ?assert(is_integer(Count)), + ?assert(Count >= 0), + ?assert(is_integer(Memory)), + ?assert(Memory >= 0), + telemetry:detach(HandlerId), + ?assert(true) + after + 1000 -> + ct:fail(timeout_receive_echo) + end. + attach_to(Event) -> HandlerId = make_ref(), telemetry:attach(HandlerId, Event, fun test_handler:echo_event/4, #{caller => erlang:self()}),