Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

## main

* 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`.

Check failure on line 13 in docs/CHANGELOG.md

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Microsoft.Foreign] Use 'for example' instead of 'e.g. '. Raw Output: {"message": "[Microsoft.Foreign] Use 'for example' instead of 'e.g. '.", "location": {"path": "docs/CHANGELOG.md", "range": {"start": {"line": 13, "column": 141}}}, "severity": "ERROR"}

*GitHub Copilot*

## 4.5.0

* Fix initialization ordering issue causing missing asset errors in Sprockets.
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/instrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Subscribe to the event:
```ruby
ActiveSupport::Notifications.subscribe("render.view_component") do |event| # or !render.view_component
event.name # => "render.view_component"
event.payload # => { name: "MyComponent", identifier: "/Users/mona/project/app/components/my_component.rb" }
event.payload # => { name: "MyComponent", identifier: "/Users/mona/project/app/components/my_component.rb", view_identifier: "/Users/mona/project/app/components/my_component.html.erb" }
end
```

Expand Down
23 changes: 18 additions & 5 deletions lib/view_component/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,28 @@ def self.included(mod)
def render_in(view_context, &block)
return super if !Rails.application.config.view_component.instrumentation_enabled.present?

payload = {
name: self.class.name,
identifier: self.class.identifier,
view_identifier: nil
}

ActiveSupport::Notifications.instrument(
"render.view_component",
{
name: self.class.name,
identifier: self.class.identifier
}
payload
) do
super
result = super
payload[:view_identifier] = @__vc_instrumentation_view_identifier
result
end
ensure
@__vc_instrumentation_view_identifier = nil
end

def around_render
result = super
@__vc_instrumentation_view_identifier = current_template&.path
result
end
end
end
1 change: 1 addition & 0 deletions test/sandbox/test/instrumentation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_instrumentation_for_include
assert_equal("render.view_component", events[0].name)
assert_equal(events[0].payload[:name], "InstrumentationComponent")
assert_match("app/components/instrumentation_component.rb", events[0].payload[:identifier])
assert_match("app/components/instrumentation_component.html.erb", events[0].payload[:view_identifier])
end

def test_instrumentation_disabled
Expand Down
Loading