Skip to content

Commit 98ab326

Browse files
authored
Merge pull request #2572 from ViewComponent/copilot/add-event-emission-for-view-file
Instrumentation: include view template path in `render.view_component` payload
2 parents 192ccfb + 55c2cac commit 98ab326

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 6
1010

1111
## main
1212

13+
* Add `view_identifier` to the `render.view_component` instrumentation event payload, containing the path to the component's template file (e.g. `app/components/my_component.html.erb`). For components using inline render methods, `view_identifier` will be `nil`.
14+
15+
*GitHub Copilot*
16+
1317
* Replace deprecated `require_dependency` with `require` in preview loading.
1418

1519
*GitHub Copilot*

docs/guide/instrumentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Subscribe to the event:
2222
```ruby
2323
ActiveSupport::Notifications.subscribe("render.view_component") do |event| # or !render.view_component
2424
event.name # => "render.view_component"
25-
event.payload # => { name: "MyComponent", identifier: "/Users/mona/project/app/components/my_component.rb" }
25+
event.payload # => { name: "MyComponent", identifier: "/Users/mona/project/app/components/my_component.rb", view_identifier: "/Users/mona/project/app/components/my_component.html.erb" }
2626
end
2727
```
2828

lib/view_component/instrumentation.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,28 @@ def self.included(mod)
1111
def render_in(view_context, &block)
1212
return super if !Rails.application.config.view_component.instrumentation_enabled.present?
1313

14+
payload = {
15+
name: self.class.name,
16+
identifier: self.class.identifier,
17+
view_identifier: nil
18+
}
19+
1420
ActiveSupport::Notifications.instrument(
1521
"render.view_component",
16-
{
17-
name: self.class.name,
18-
identifier: self.class.identifier
19-
}
22+
payload
2023
) do
21-
super
24+
result = super
25+
payload[:view_identifier] = @__vc_instrumentation_view_identifier
26+
result
2227
end
28+
ensure
29+
@__vc_instrumentation_view_identifier = nil
30+
end
31+
32+
def around_render
33+
result = super
34+
@__vc_instrumentation_view_identifier = current_template&.path
35+
result
2336
end
2437
end
2538
end

test/sandbox/test/instrumentation_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def test_instrumentation_for_include
1616
assert_equal("render.view_component", events[0].name)
1717
assert_equal(events[0].payload[:name], "InstrumentationComponent")
1818
assert_match("app/components/instrumentation_component.rb", events[0].payload[:identifier])
19+
assert_match("app/components/instrumentation_component.html.erb", events[0].payload[:view_identifier])
1920
end
2021

2122
def test_instrumentation_disabled

0 commit comments

Comments
 (0)