Skip to content

Comments

Implement telemetry:persist/0#144

Merged
josevalim merged 2 commits intobeam-telemetry:mainfrom
hauleth:push-pmkvkvwtvvnn
Feb 22, 2026
Merged

Implement telemetry:persist/0#144
josevalim merged 2 commits intobeam-telemetry:mainfrom
hauleth:push-pmkvkvwtvvnn

Conversation

@hauleth
Copy link
Contributor

@hauleth hauleth commented Feb 18, 2026

This new function adds support to "lock" the handlers. Locking handlers will improve performance of execution at the cost of attaching/detaching handlers.

Locked performance shows about 7% improvement in my Peep benchmark


============ PRE  LOCK ============

****** Process <0.285.0>  --  100.00% of total *** 
FUNCTION                                               CALLS  TIME (us)  PER CALL  [    %]
'Elixir.Enum':each/2                                       1          0      0.00  [ 0.00]
'Elixir.Metrics':metrics/0                                 1          2      2.00  [ 0.00]
'Elixir.Metrics':'-metrics/0-fun-0-'/1               1000000      31971      0.03  [ 1.80]
persistent_term:get/1                                1000000      43049      0.04  [ 2.42]
'Elixir.Enum':reduce_range/5                          500001      46116      0.09  [ 2.60]
persistent_term:get/2                                1000000      47030      0.05  [ 2.65]
telemetry:execute/3                                  1000000      62464      0.06  [ 3.52]
lists:foreach/2                                      2000000      63204      0.03  [ 3.56]
'Elixir.Enum':'-each/2-fun-0-'/3                     1000000      64639      0.06  [ 3.64]
telemetry_ets:list_for_event/2                       1000000      65600      0.07  [ 3.69]
telemetry:'-execute/3-fun-0-'/4                      1000000      65926      0.07  [ 3.71]
'Peep.Codegen.benchmark':meta/3                      2000000      66029      0.03  [ 3.72]
'Peep.Codegen.benchmark':fetch_measurement/3         2000000      66988      0.03  [ 3.77]
telemetry_handler_table:list_for_event/1             1000000      68770      0.07  [ 3.87]
'Peep.Codegen.benchmark':handle_event/4              1000000      69141      0.07  [ 3.89]
erlang:system_info/1                                 2000000      72961      0.04  [ 4.11]
ets:lookup/2                                         1000000      86598      0.09  [ 4.87]
ets:update_counter/4                                 2000000     163369      0.08  [ 9.19]
'Elixir.Peep.Storage.ETS':insert_metric/5            2000000     200463      0.10  [11.28]
'Peep.Codegen.benchmark':'-handle_event/4-fun-0-'/5  2000000     241976      0.12  [13.62]
lists:foreach_1/2                                    5000000     250623      0.05  [14.10]
                                                                1776919            [100.0]

============ POST LOCK ============

****** Process <0.287.0>  --  100.00% of total *** 
FUNCTION                                               CALLS  TIME (us)  PER CALL  [    %]
'Elixir.Enum':each/2                                       1          0      0.00  [ 0.00]
'Elixir.Metrics':metrics/0                                 1          1      1.00  [ 0.00]
'Elixir.Metrics':'-metrics/0-fun-0-'/1               1000000      32117      0.03  [ 1.94]
telemetry_pt:list_for_event/2                        1000000      36781      0.04  [ 2.23]
persistent_term:get/1                                1000000      43494      0.04  [ 2.63]
'Elixir.Enum':reduce_range/5                          500001      46552      0.09  [ 2.82]
persistent_term:get/2                                1000000      47869      0.05  [ 2.90]
telemetry:execute/3                                  1000000      62991      0.06  [ 3.81]
lists:foreach/2                                      2000000      63469      0.03  [ 3.84]
'Elixir.Enum':'-each/2-fun-0-'/3                     1000000      65129      0.07  [ 3.94]
telemetry:'-execute/3-fun-0-'/4                      1000000      66356      0.07  [ 4.02]
'Peep.Codegen.benchmark':meta/3                      2000000      66431      0.03  [ 4.02]
'Peep.Codegen.benchmark':fetch_measurement/3         2000000      68042      0.03  [ 4.12]
'Peep.Codegen.benchmark':handle_event/4              1000000      69050      0.07  [ 4.18]
telemetry_handler_table:list_for_event/1             1000000      70515      0.07  [ 4.27]
erlang:system_info/1                                 2000000      71940      0.04  [ 4.35]
ets:update_counter/4                                 2000000     168688      0.08  [10.21]
'Elixir.Peep.Storage.ETS':insert_metric/5            2000000     201402      0.10  [12.19]
'Peep.Codegen.benchmark':'-handle_event/4-fun-0-'/5  2000000     217614      0.11  [13.17]
lists:foreach_1/2                                    5000000     253943      0.05  [15.37]
                                                                1652384            [100.0]

This is just an initial implementation as PoC.

Close #142

This is preliminary step towards implementing beam-telemetry#142. This allows using
unnamed table and provide a way to swap implementation for more
performant one.
@hauleth hauleth force-pushed the push-pmkvkvwtvvnn branch 2 times, most recently from 21c2f13 to 3246db5 Compare February 18, 2026 17:17
@hauleth hauleth marked this pull request as draft February 18, 2026 17:31
@josevalim
Copy link
Contributor

@hauleth another approach would be to keep the persistent term and ets and dynamic registrations after locking still goes to ets. But maybe the double lookup makes everything worse.

@hauleth
Copy link
Contributor Author

hauleth commented Feb 18, 2026

Yeah, that would mean that we still need to pay ETS tax.

@hauleth
Copy link
Contributor Author

hauleth commented Feb 18, 2026

I am not sure if there should also be "unlock" function to go back to ETS-based implementation.

@josevalim
Copy link
Contributor

I don’t think there is a need right now. But we should probably add a flag to tests so we run the suite in both modes.

I am also not very happy with the name “lock”. We could try “persist”, “consolidate”, or something of sorts.

@hauleth
Copy link
Contributor Author

hauleth commented Feb 19, 2026

@josevalim I do not like the name lock either, but it was the first thing that come to me when designing interface. I am 100% open to name change to something clearer with intention.

It is tested in both modes. Only thing not tested for now is what happens when mode switches from one to another.

This adds new `telemetry:lock/0` function that allows locking telemetry
handlers to improve execution performance at cost of slowing down
attaching and detaching handlerrs.
@hauleth hauleth marked this pull request as ready for review February 21, 2026 13:04
@hauleth hauleth changed the title Implement telemetry:lock/0 Implement telemetry:persist/0 Feb 21, 2026
@josevalim josevalim merged commit c0aff75 into beam-telemetry:main Feb 22, 2026
5 checks passed
@josevalim
Copy link
Contributor

Please give it a try and let us know once you have some data. Once we release a new data, we probably want to enable this by default in Phoenix!

@hauleth hauleth deleted the push-pmkvkvwtvvnn branch February 23, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Use persistent_term for storing handlers for execution

2 participants