-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactive_support_instrumentation.html
1736 lines (1682 loc) · 55.6 KB
/
active_support_instrumentation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Active Support Instrumentation — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" data-turbolinks-track="reload">
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<script src="javascripts/syntaxhighlighter.js" data-turbolinks-track="reload"></script>
<script src="javascripts/turbolinks.js" data-turbolinks-track="reload"></script>
<script src="javascripts/guides.js" data-turbolinks-track="reload"></script>
<script src="javascripts/responsive-tables.js" data-turbolinks-track="reload"></script>
<meta property="og:title" content="Active Support Instrumentation — Ruby on Rails Guides" />
<meta name="description" content="Active Support InstrumentationActive Support is a part of core Rails that provides Ruby language extensions, utilities, and other things. One of the things it includes is an instrumentation API that can be used inside an application to measure certain actions that occur within Ruby code, such as that inside a Rails application or the framework itself. It is not limited to Rails, however. It can be used independently in other Ruby scripts if it is so desired.In this guide, you will learn how to use the instrumentation API inside of Active Support to measure events inside of Rails and other Ruby code.After reading this guide, you will know: What instrumentation can provide. The hooks inside the Rails framework for instrumentation. Adding a subscriber to a hook. Building a custom instrumentation implementation." />
<meta property="og:description" content="Active Support InstrumentationActive Support is a part of core Rails that provides Ruby language extensions, utilities, and other things. One of the things it includes is an instrumentation API that can be used inside an application to measure certain actions that occur within Ruby code, such as that inside a Rails application or the framework itself. It is not limited to Rails, however. It can be used independently in other Ruby scripts if it is so desired.In this guide, you will learn how to use the instrumentation API inside of Active Support to measure events inside of Rails and other Ruby code.After reading this guide, you will know: What instrumentation can provide. The hooks inside the Rails framework for instrumentation. Adding a subscriber to a hook. Building a custom instrumentation implementation." />
<meta property="og:locale" content="en_US" />
<meta property="og:site_name" content="Ruby on Rails Guides" />
<meta property="og:image" content="https://avatars.githubusercontent.com/u/4223" />
<meta property="og:type" content="website" />
</head>
<body class="guide">
<div id="topNav">
<div class="wrapper">
<strong class="more-info-label">공식 웹사이트 <a href="https://rubyonrails.org/">rubyonrails.org:</a> </strong>
<span class="red-button more-info-button">
루비온레일스 웹사이트
</span>
<ul class="more-info-links s-hidden">
<li class="more-info"><a href="https://weblog.rubyonrails.org/">블로그</a></li>
<li class="more-info"><a href="https://guides.rubyonrails.org/">영문가이드</a></li>
<li class="more-info"><a href="https://api.rubyonrails.org/">레일스API</a></li>
<li class="more-info"><a href="https://stackoverflow.com/questions/tagged/ruby-on-rails">질문하기</a></li>
<li class="more-info"><a href="https://github.com/rails/rails">GitHub에서 기여하기</a></li>
</ul>
</div>
</div>
<div id="header">
<div class="wrapper clearfix">
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
<ul class="nav">
<li><a class="nav-item" href="index.html">홈</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">가이드 인덱스</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<div class="guides-section-container">
<div class="guides-section">
<dt>시작하면서</dt>
<dd><a href="getting_started.html">레일스로 시작하기</a></dd>
</div>
<div class="guides-section">
<dt>모델</dt>
<dd><a href="active_record_basics.html">액티브 레코드 기본</a></dd>
<dd><a href="active_record_migrations.html">액티브 레코드 마이그레이션</a></dd>
<dd><a href="active_record_validations.html">액티브 레코드 유효성 검증</a></dd>
<dd><a href="active_record_callbacks.html">액티브 레코드 콜백</a></dd>
<dd><a href="association_basics.html">Active Record Associations</a></dd>
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
</div>
<div class="guides-section">
<dt>Views</dt>
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
</div>
<div class="guides-section">
<dt>Controllers</dt>
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
</div>
<div class="guides-section">
<dt>Other Components</dt>
<dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
<dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
<dd><a href="active_job_basics.html">Active Job Basics</a></dd>
<dd><a href="active_storage_overview.html">Active Storage Overview</a></dd>
<dd><a href="action_cable_overview.html">Action Cable Overview</a></dd>
</div>
<div class="guides-section">
<dt>Digging Deeper</dt>
<dd><a href="i18n.html">Rails Internationalization (I18n) API</a></dd>
<dd><a href="testing.html">Testing Rails Applications</a></dd>
<dd><a href="security.html">Securing Rails Applications</a></dd>
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
<dd><a href="command_line.html">The Rails Command Line</a></dd>
<dd><a href="asset_pipeline.html">The Asset Pipeline</a></dd>
<dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd>
<dd><a href="autoloading_and_reloading_constants.html">Autoloading and Reloading Constants (Zeitwerk Mode)</a></dd>
<dd><a href="autoloading_and_reloading_constants_classic_mode.html">Autoloading and Reloading Constants (Classic Mode)</a></dd>
<dd><a href="caching_with_rails.html">Caching with Rails: An Overview</a></dd>
<dd><a href="api_app.html">Using Rails for API-only Applications</a></dd>
</div>
<div class="guides-section">
<dt>Extending Rails</dt>
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
<dd><a href="generators.html">Creating and Customizing Rails Generators & Templates</a></dd>
</div>
<div class="guides-section">
<dt>Contributions</dt>
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
<dd><a href="ruby_on_rails_guides_guidelines.html">Guides Guidelines</a></dd>
</div>
<div class="guides-section">
<dt>Policies</dt>
<dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
</div>
<div class="guides-section">
<dt>Release Notes</dt>
<dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
<dd><a href="6_0_release_notes.html">Version 6.0 - August 2019</a></dd>
<dd><a href="5_2_release_notes.html">Version 5.2 - April 2018</a></dd>
<dd><a href="5_1_release_notes.html">Version 5.1 - April 2017</a></dd>
<dd><a href="5_0_release_notes.html">Version 5.0 - June 2016</a></dd>
<dd><a href="4_2_release_notes.html">Version 4.2 - December 2014</a></dd>
<dd><a href="4_1_release_notes.html">Version 4.1 - April 2014</a></dd>
<dd><a href="4_0_release_notes.html">Version 4.0 - June 2013</a></dd>
<dd><a href="3_2_release_notes.html">Version 3.2 - January 2012</a></dd>
<dd><a href="3_1_release_notes.html">Version 3.1 - August 2011</a></dd>
<dd><a href="3_0_release_notes.html">Version 3.0 - August 2010</a></dd>
<dd><a href="2_3_release_notes.html">Version 2.3 - March 2009</a></dd>
<dd><a href="2_2_release_notes.html">Version 2.2 - November 2008</a></dd>
</div>
</div>
</div>
</li>
<li><a class="nav-item" href="contributing_to_ruby_on_rails.html">기여하기</a></li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">가이드 인덱스</option>
<optgroup label="시작하면서">
<option value="getting_started.html">레일스로 시작하기</option>
</optgroup>
<optgroup label="모델">
<option value="active_record_basics.html">액티브 레코드 기본</option>
<option value="active_record_migrations.html">액티브 레코드 마이그레이션</option>
<option value="active_record_validations.html">액티브 레코드 유효성 검증</option>
<option value="active_record_callbacks.html">액티브 레코드 콜백</option>
<option value="association_basics.html">Active Record Associations</option>
<option value="active_record_querying.html">Active Record Query Interface</option>
</optgroup>
<optgroup label="Views">
<option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option>
<option value="form_helpers.html">Action View Form Helpers</option>
</optgroup>
<optgroup label="Controllers">
<option value="action_controller_overview.html">Action Controller Overview</option>
<option value="routing.html">Rails Routing from the Outside In</option>
</optgroup>
<optgroup label="Other Components">
<option value="active_support_core_extensions.html">Active Support Core Extensions</option>
<option value="action_mailer_basics.html">Action Mailer Basics</option>
<option value="active_job_basics.html">Active Job Basics</option>
<option value="active_storage_overview.html">Active Storage Overview</option>
<option value="action_cable_overview.html">Action Cable Overview</option>
</optgroup>
<optgroup label="Digging Deeper">
<option value="i18n.html">Rails Internationalization (I18n) API</option>
<option value="testing.html">Testing Rails Applications</option>
<option value="security.html">Securing Rails Applications</option>
<option value="debugging_rails_applications.html">Debugging Rails Applications</option>
<option value="configuring.html">Configuring Rails Applications</option>
<option value="command_line.html">The Rails Command Line</option>
<option value="asset_pipeline.html">The Asset Pipeline</option>
<option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option>
<option value="autoloading_and_reloading_constants.html">Autoloading and Reloading Constants (Zeitwerk Mode)</option>
<option value="autoloading_and_reloading_constants_classic_mode.html">Autoloading and Reloading Constants (Classic Mode)</option>
<option value="caching_with_rails.html">Caching with Rails: An Overview</option>
<option value="api_app.html">Using Rails for API-only Applications</option>
</optgroup>
<optgroup label="Extending Rails">
<option value="rails_on_rack.html">Rails on Rack</option>
<option value="generators.html">Creating and Customizing Rails Generators & Templates</option>
</optgroup>
<optgroup label="Contributions">
<option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
<option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
<option value="ruby_on_rails_guides_guidelines.html">Guides Guidelines</option>
</optgroup>
<optgroup label="Policies">
<option value="maintenance_policy.html">Maintenance Policy</option>
</optgroup>
<optgroup label="Release Notes">
<option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
<option value="6_0_release_notes.html">Version 6.0 - August 2019</option>
<option value="5_2_release_notes.html">Version 5.2 - April 2018</option>
<option value="5_1_release_notes.html">Version 5.1 - April 2017</option>
<option value="5_0_release_notes.html">Version 5.0 - June 2016</option>
<option value="4_2_release_notes.html">Version 4.2 - December 2014</option>
<option value="4_1_release_notes.html">Version 4.1 - April 2014</option>
<option value="4_0_release_notes.html">Version 4.0 - June 2013</option>
<option value="3_2_release_notes.html">Version 3.2 - January 2012</option>
<option value="3_1_release_notes.html">Version 3.1 - August 2011</option>
<option value="3_0_release_notes.html">Version 3.0 - August 2010</option>
<option value="2_3_release_notes.html">Version 2.3 - March 2009</option>
<option value="2_2_release_notes.html">Version 2.2 - November 2008</option>
</optgroup>
</select>
</li>
</ul>
</div>
</div>
<hr class="hide" />
<div id="feature">
<div class="wrapper">
<h2>Active Support Instrumentation</h2><p>Active Support is a part of core Rails that provides Ruby language extensions, utilities, and other things. One of the things it includes is an instrumentation API that can be used inside an application to measure certain actions that occur within Ruby code, such as that inside a Rails application or the framework itself. It is not limited to Rails, however. It can be used independently in other Ruby scripts if it is so desired.</p><p>In this guide, you will learn how to use the instrumentation API inside of Active Support to measure events inside of Rails and other Ruby code.</p><p>After reading this guide, you will know:</p>
<ul>
<li>What instrumentation can provide.</li>
<li>The hooks inside the Rails framework for instrumentation.</li>
<li>Adding a subscriber to a hook.</li>
<li>Building a custom instrumentation implementation.</li>
</ul>
<div id="subCol">
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
<ol class="chapters">
<li><a href="#introduction-to-instrumentation">Introduction to instrumentation</a></li>
<li><a href="#rails-framework-hooks">Rails framework hooks</a></li>
<li>
<a href="#action-controller">Action Controller</a>
<ul>
<li><a href="#write-fragment-action-controller">write_fragment.action_controller</a></li>
<li><a href="#read-fragment-action-controller">read_fragment.action_controller</a></li>
<li><a href="#expire-fragment-action-controller">expire_fragment.action_controller</a></li>
<li><a href="#exist-fragment-questionmark-action-controller">exist_fragment?.action_controller</a></li>
<li><a href="#write-page-action-controller">write_page.action_controller</a></li>
<li><a href="#expire-page-action-controller">expire_page.action_controller</a></li>
<li><a href="#start-processing-action-controller">start_processing.action_controller</a></li>
<li><a href="#process-action-action-controller">process_action.action_controller</a></li>
<li><a href="#send-file-action-controller">send_file.action_controller</a></li>
<li><a href="#send-data-action-controller">send_data.action_controller</a></li>
<li><a href="#redirect-to-action-controller">redirect_to.action_controller</a></li>
<li><a href="#halted-callback-action-controller">halted_callback.action_controller</a></li>
<li><a href="#unpermitted-parameters-action-controller">unpermitted_parameters.action_controller</a></li>
</ul>
</li>
<li>
<a href="#action-dispatch">Action Dispatch</a>
<ul>
<li><a href="#process-middleware-action-dispatch">process_middleware.action_dispatch</a></li>
</ul>
</li>
<li>
<a href="#action-view">Action View</a>
<ul>
<li><a href="#render-template-action-view">render_template.action_view</a></li>
<li><a href="#render-partial-action-view">render_partial.action_view</a></li>
<li><a href="#render-collection-action-view">render_collection.action_view</a></li>
</ul>
</li>
<li>
<a href="#active-record">Active Record</a>
<ul>
<li><a href="#sql-active-record">sql.active_record</a></li>
<li><a href="#instantiation-active-record">instantiation.active_record</a></li>
</ul>
</li>
<li>
<a href="#action-mailer">Action Mailer</a>
<ul>
<li><a href="#deliver-action-mailer">deliver.action_mailer</a></li>
<li><a href="#process-action-mailer">process.action_mailer</a></li>
</ul>
</li>
<li>
<a href="#active-support">Active Support</a>
<ul>
<li><a href="#cache-read-active-support">cache_read.active_support</a></li>
<li><a href="#cache-generate-active-support">cache_generate.active_support</a></li>
<li><a href="#cache-fetch-hit-active-support">cache_fetch_hit.active_support</a></li>
<li><a href="#cache-write-active-support">cache_write.active_support</a></li>
<li><a href="#cache-delete-active-support">cache_delete.active_support</a></li>
<li><a href="#cache-exist-questionmark-active-support">cache_exist?.active_support</a></li>
</ul>
</li>
<li>
<a href="#active-job">Active Job</a>
<ul>
<li><a href="#enqueue-at-active-job">enqueue_at.active_job</a></li>
<li><a href="#enqueue-active-job">enqueue.active_job</a></li>
<li><a href="#enqueue-retry-active-job">enqueue_retry.active_job</a></li>
<li><a href="#perform-start-active-job">perform_start.active_job</a></li>
<li><a href="#perform-active-job">perform.active_job</a></li>
<li><a href="#retry-stopped-active-job">retry_stopped.active_job</a></li>
<li><a href="#discard-active-job">discard.active_job</a></li>
</ul>
</li>
<li>
<a href="#action-cable">Action Cable</a>
<ul>
<li><a href="#perform-action-action-cable">perform_action.action_cable</a></li>
<li><a href="#transmit-action-cable">transmit.action_cable</a></li>
<li><a href="#transmit-subscription-confirmation-action-cable">transmit_subscription_confirmation.action_cable</a></li>
<li><a href="#transmit-subscription-rejection-action-cable">transmit_subscription_rejection.action_cable</a></li>
<li><a href="#broadcast-action-cable">broadcast.action_cable</a></li>
</ul>
</li>
<li>
<a href="#active-storage">Active Storage</a>
<ul>
<li><a href="#service-upload-active-storage">service_upload.active_storage</a></li>
<li><a href="#service-streaming-download-active-storage">service_streaming_download.active_storage</a></li>
<li><a href="#service-download-chunk-active-storage">service_download_chunk.active_storage</a></li>
<li><a href="#service-download-active-storage">service_download.active_storage</a></li>
<li><a href="#service-delete-active-storage">service_delete.active_storage</a></li>
<li><a href="#service-delete-prefixed-active-storage">service_delete_prefixed.active_storage</a></li>
<li><a href="#service-exist-active-storage">service_exist.active_storage</a></li>
<li><a href="#service-url-active-storage">service_url.active_storage</a></li>
<li><a href="#service-update-metadata-active-storage">service_update_metadata.active_storage</a></li>
<li><a href="#preview-active-storage">preview.active_storage</a></li>
</ul>
</li>
<li>
<a href="#railties">Railties</a>
<ul>
<li><a href="#load-config-initializer-railties">load_config_initializer.railties</a></li>
</ul>
</li>
<li>
<a href="#rails">Rails</a>
<ul>
<li><a href="#deprecation-rails">deprecation.rails</a></li>
</ul>
</li>
<li><a href="#subscribing-to-an-event">Subscribing to an event</a></li>
<li><a href="#creating-custom-events">Creating custom events</a></li>
</ol>
</div>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<h3 id="introduction-to-instrumentation"><a class="anchorlink" href="#introduction-to-instrumentation">1 Introduction to instrumentation</a></h3><p>The instrumentation API provided by Active Support allows developers to provide hooks which other developers may hook into. There are several of these within the <a href="#rails-framework-hooks">Rails framework</a>. With this API, developers can choose to be notified when certain events occur inside their application or another piece of Ruby code.</p><p>For example, there is a hook provided within Active Record that is called every time Active Record uses an SQL query on a database. This hook could be <strong>subscribed</strong> to, and used to track the number of queries during a certain action. There's another hook around the processing of an action of a controller. This could be used, for instance, to track how long a specific action has taken.</p><p>You are even able to create your own events inside your application which you can later subscribe to.</p><h3 id="rails-framework-hooks"><a class="anchorlink" href="#rails-framework-hooks">2 Rails framework hooks</a></h3><p>Within the Ruby on Rails framework, there are a number of hooks provided for common events. These are detailed below.</p><h3 id="action-controller"><a class="anchorlink" href="#action-controller">3 Action Controller</a></h3><h4 id="write-fragment-action-controller"><a class="anchorlink" href="#write-fragment-action-controller">3.1 write_fragment.action_controller</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>The complete key</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
key: 'posts/1-dashboard-view'
}
</pre>
</div>
<h4 id="read-fragment-action-controller"><a class="anchorlink" href="#read-fragment-action-controller">3.2 read_fragment.action_controller</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>The complete key</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
key: 'posts/1-dashboard-view'
}
</pre>
</div>
<h4 id="expire-fragment-action-controller"><a class="anchorlink" href="#expire-fragment-action-controller">3.3 expire_fragment.action_controller</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>The complete key</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
key: 'posts/1-dashboard-view'
}
</pre>
</div>
<h4 id="exist-fragment-questionmark-action-controller"><a class="anchorlink" href="#exist-fragment-questionmark-action-controller">3.4 exist_fragment?.action_controller</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>The complete key</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
key: 'posts/1-dashboard-view'
}
</pre>
</div>
<h4 id="write-page-action-controller"><a class="anchorlink" href="#write-page-action-controller">3.5 write_page.action_controller</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:path</code></td>
<td>The complete path</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
path: '/users/1'
}
</pre>
</div>
<h4 id="expire-page-action-controller"><a class="anchorlink" href="#expire-page-action-controller">3.6 expire_page.action_controller</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:path</code></td>
<td>The complete path</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
path: '/users/1'
}
</pre>
</div>
<h4 id="start-processing-action-controller"><a class="anchorlink" href="#start-processing-action-controller">3.7 start_processing.action_controller</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:controller</code></td>
<td>The controller name</td>
</tr>
<tr>
<td><code>:action</code></td>
<td>The action</td>
</tr>
<tr>
<td><code>:params</code></td>
<td>Hash of request parameters without any filtered parameter</td>
</tr>
<tr>
<td><code>:headers</code></td>
<td>Request headers</td>
</tr>
<tr>
<td><code>:format</code></td>
<td>html/js/json/xml etc</td>
</tr>
<tr>
<td><code>:method</code></td>
<td>HTTP request verb</td>
</tr>
<tr>
<td><code>:path</code></td>
<td>Request path</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
controller: "PostsController",
action: "new",
params: { "action" => "new", "controller" => "posts" },
headers: #<ActionDispatch::Http::Headers:0x0055a67a519b88>,
format: :html,
method: "GET",
path: "/posts/new"
}
</pre>
</div>
<h4 id="process-action-action-controller"><a class="anchorlink" href="#process-action-action-controller">3.8 process_action.action_controller</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:controller</code></td>
<td>The controller name</td>
</tr>
<tr>
<td><code>:action</code></td>
<td>The action</td>
</tr>
<tr>
<td><code>:params</code></td>
<td>Hash of request parameters without any filtered parameter</td>
</tr>
<tr>
<td><code>:headers</code></td>
<td>Request headers</td>
</tr>
<tr>
<td><code>:format</code></td>
<td>html/js/json/xml etc</td>
</tr>
<tr>
<td><code>:method</code></td>
<td>HTTP request verb</td>
</tr>
<tr>
<td><code>:path</code></td>
<td>Request path</td>
</tr>
<tr>
<td><code>:status</code></td>
<td>HTTP status code</td>
</tr>
<tr>
<td><code>:view_runtime</code></td>
<td>Amount spent in view in ms</td>
</tr>
<tr>
<td><code>:db_runtime</code></td>
<td>Amount spent executing database queries in ms</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
controller: "PostsController",
action: "index",
params: {"action" => "index", "controller" => "posts"},
headers: #<ActionDispatch::Http::Headers:0x0055a67a519b88>,
format: :html,
method: "GET",
path: "/posts",
status: 200,
view_runtime: 46.848,
db_runtime: 0.157
}
</pre>
</div>
<h4 id="send-file-action-controller"><a class="anchorlink" href="#send-file-action-controller">3.9 send_file.action_controller</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:path</code></td>
<td>Complete path to the file</td>
</tr>
</tbody>
</table>
<div class="info"><p>Additional keys may be added by the caller.</p></div><h4 id="send-data-action-controller"><a class="anchorlink" href="#send-data-action-controller">3.10 send_data.action_controller</a></h4><p><code>ActionController</code> does not add any specific information to the payload. All options are passed through to the payload.</p><h4 id="redirect-to-action-controller"><a class="anchorlink" href="#redirect-to-action-controller">3.11 redirect_to.action_controller</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:status</code></td>
<td>HTTP response code</td>
</tr>
<tr>
<td><code>:location</code></td>
<td>URL to redirect to</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
status: 302,
location: "http://localhost:3000/posts/new"
}
</pre>
</div>
<h4 id="halted-callback-action-controller"><a class="anchorlink" href="#halted-callback-action-controller">3.12 halted_callback.action_controller</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:filter</code></td>
<td>Filter that halted the action</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
filter: ":halting_filter"
}
</pre>
</div>
<h4 id="unpermitted-parameters-action-controller"><a class="anchorlink" href="#unpermitted-parameters-action-controller">3.13 unpermitted_parameters.action_controller</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:keys</code></td>
<td>Unpermitted keys</td>
</tr>
</tbody>
</table>
<h3 id="action-dispatch"><a class="anchorlink" href="#action-dispatch">4 Action Dispatch</a></h3><h4 id="process-middleware-action-dispatch"><a class="anchorlink" href="#process-middleware-action-dispatch">4.1 process_middleware.action_dispatch</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:middleware</code></td>
<td>Name of the middleware</td>
</tr>
</tbody>
</table>
<h3 id="action-view"><a class="anchorlink" href="#action-view">5 Action View</a></h3><h4 id="render-template-action-view"><a class="anchorlink" href="#render-template-action-view">5.1 render_template.action_view</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:identifier</code></td>
<td>Full path to template</td>
</tr>
<tr>
<td><code>:layout</code></td>
<td>Applicable layout</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
identifier: "/Users/adam/projects/notifications/app/views/posts/index.html.erb",
layout: "layouts/application"
}
</pre>
</div>
<h4 id="render-partial-action-view"><a class="anchorlink" href="#render-partial-action-view">5.2 render_partial.action_view</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:identifier</code></td>
<td>Full path to template</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
identifier: "/Users/adam/projects/notifications/app/views/posts/_form.html.erb"
}
</pre>
</div>
<h4 id="render-collection-action-view"><a class="anchorlink" href="#render-collection-action-view">5.3 render_collection.action_view</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:identifier</code></td>
<td>Full path to template</td>
</tr>
<tr>
<td><code>:count</code></td>
<td>Size of collection</td>
</tr>
<tr>
<td><code>:cache_hits</code></td>
<td>Number of partials fetched from cache</td>
</tr>
</tbody>
</table>
<p><code>:cache_hits</code> is only included if the collection is rendered with <code>cached: true</code>.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
identifier: "/Users/adam/projects/notifications/app/views/posts/_post.html.erb",
count: 3,
cache_hits: 0
}
</pre>
</div>
<h3 id="active-record"><a class="anchorlink" href="#active-record">6 Active Record</a></h3><h4 id="sql-active-record"><a class="anchorlink" href="#sql-active-record">6.1 sql.active_record</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:sql</code></td>
<td>SQL statement</td>
</tr>
<tr>
<td><code>:name</code></td>
<td>Name of the operation</td>
</tr>
<tr>
<td><code>:connection_id</code></td>
<td>Object ID of the connection object</td>
</tr>
<tr>
<td><code>:connection</code></td>
<td>Connection object</td>
</tr>
<tr>
<td><code>:binds</code></td>
<td>Bind parameters</td>
</tr>
<tr>
<td><code>:type_casted_binds</code></td>
<td>Typecasted bind parameters</td>
</tr>
<tr>
<td><code>:statement_name</code></td>
<td>SQL Statement name</td>
</tr>
<tr>
<td><code>:cached</code></td>
<td>
<code>true</code> is added when cached queries used</td>
</tr>
</tbody>
</table>
<div class="info"><p>The adapters will add their own data as well.</p></div><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
sql: "SELECT \"posts\".* FROM \"posts\" ",
name: "Post Load",
connection_id: 70307250813140,
connection: #<ActiveRecord::ConnectionAdapters::SQLite3Adapter:0x00007f9f7a838850>,
binds: [#<ActiveModel::Attribute::WithCastValue:0x00007fe19d15dc00>],
type_casted_binds: [11],
statement_name: nil
}
</pre>
</div>
<h4 id="instantiation-active-record"><a class="anchorlink" href="#instantiation-active-record">6.2 instantiation.active_record</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:record_count</code></td>
<td>Number of records that instantiated</td>
</tr>
<tr>
<td><code>:class_name</code></td>
<td>Record's class</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
record_count: 1,
class_name: "User"
}
</pre>
</div>
<h3 id="action-mailer"><a class="anchorlink" href="#action-mailer">7 Action Mailer</a></h3><h4 id="deliver-action-mailer"><a class="anchorlink" href="#deliver-action-mailer">7.1 deliver.action_mailer</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:mailer</code></td>
<td>Name of the mailer class</td>
</tr>
<tr>
<td><code>:message_id</code></td>
<td>ID of the message, generated by the Mail gem</td>
</tr>
<tr>
<td><code>:subject</code></td>
<td>Subject of the mail</td>
</tr>
<tr>
<td><code>:to</code></td>
<td>To address(es) of the mail</td>
</tr>
<tr>
<td><code>:from</code></td>
<td>From address of the mail</td>
</tr>
<tr>
<td><code>:bcc</code></td>
<td>BCC addresses of the mail</td>
</tr>
<tr>
<td><code>:cc</code></td>
<td>CC addresses of the mail</td>
</tr>
<tr>
<td><code>:date</code></td>
<td>Date of the mail</td>
</tr>
<tr>
<td><code>:mail</code></td>
<td>The encoded form of the mail</td>
</tr>
<tr>
<td><code>:perform_deliveries</code></td>
<td>Whether delivery of this message is performed or not</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
mailer: "Notification",
message_id: "[email protected]",
subject: "Rails Guides",
to: ["[email protected]", "[email protected]"],
from: ["[email protected]"],
date: Sat, 10 Mar 2012 14:18:09 +0100,
mail: "...", # omitted for brevity
perform_deliveries: true
}
</pre>
</div>
<h4 id="process-action-mailer"><a class="anchorlink" href="#process-action-mailer">7.2 process.action_mailer</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:mailer</code></td>
<td>Name of the mailer class</td>
</tr>
<tr>
<td><code>:action</code></td>
<td>The action</td>
</tr>
<tr>
<td><code>:args</code></td>
<td>The arguments</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
mailer: "Notification",
action: "welcome_email",
args: []
}
</pre>
</div>
<h3 id="active-support"><a class="anchorlink" href="#active-support">8 Active Support</a></h3><h4 id="cache-read-active-support"><a class="anchorlink" href="#cache-read-active-support">8.1 cache_read.active_support</a></h4>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>Key used in the store</td>
</tr>
<tr>
<td><code>:hit</code></td>
<td>If this read is a hit</td>
</tr>
<tr>
<td><code>:super_operation</code></td>
<td>:fetch is added when a read is used with <code>#fetch</code>
</td>
</tr>
</tbody>