Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper instrumentation #2072

Closed
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 @@ -30,6 +30,10 @@ nav_order: 5

*Reegan Viljoen*

* Add helper instrumentation.

*Reegan Viljoen*

## 3.13.0

* Add ruby head and YJIT to CI.
Expand Down
18 changes: 18 additions & 0 deletions docs/guide/instrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ ActiveSupport::Notifications.subscribe("render.view_component") do |event| # or
end
```

## Instrument helpers

To enable helpers instrumentation, use the `instrument_helpers` option wich will instrument all helpers used in the rendering of a ViewComponent:

```ruby
# config/application.rb
# Enable ActiveSupport notifications for all ViewComponents
config.view_component.instrument_helpers = true
config.view_component.use_deprecated_instrumentation_name = false
```

```ruby
ActiveSupport::Notifications.subscribe("render.view_component.helpers") do |event| # or !render.view_component
event.name # => "render.view_component"
event.payload # => { name: "MyComponent", identifier: "/Users/mona/project/app/components/my_component.rb" }
end
```

## Viewing instrumentation sums in the browser developer tools

When using `render.view_component` with `config.server_timing = true` (default in development) in Rails 7, the browser developer tools display the sum total timing information in Network > Timing under the key `render.view_component`.
Expand Down
6 changes: 6 additions & 0 deletions lib/view_component/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def defaults
preview_route: "/rails/view_components",
show_previews_source: false,
instrumentation_enabled: false,
instrument_helpers: false,
use_deprecated_instrumentation_name: true,
render_monkey_patch_enabled: true,
view_component_path: "app/components",
Expand Down Expand Up @@ -113,6 +114,11 @@ def defaults
# Whether ActiveSupport notifications are enabled.
# Defaults to `false`.

# @!attribute instrument_helpers
# @return [Boolean]
# Whether ActiveSupport notifications are enabled for helpers.
# Defaults to `false`.

# @!attribute use_deprecated_instrumentation_name
# @return [Boolean]
# Whether ActiveSupport Notifications use the private name `"!render.view_component"`
Expand Down
13 changes: 13 additions & 0 deletions lib/view_component/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ def render_in(view_context, &block)
end
end

def helpers
super unless ViewComponent::Base.config.instrumentation_helpers
ActiveSupport::Notifications.instrument(
"#{notification_name}.helpers",
{
name: self.class.name,
identifier: self.class.identifier
}
) do
super
end
end

private

def notification_name
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<div>hello,world!</div>
<%= helpers.message %>
15 changes: 15 additions & 0 deletions test/sandbox/test/instrumentation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@ def test_instrumentation_with_deprecated_name
assert_equal(events.size, 1)
assert_equal("!render.view_component", events[0].name)
end

def test_helpers_instrumentation
with_config_option(:use_deprecated_instrumentation_name, false) do
with_config_option(:instrument_helpers, true) do
events = []
ActiveSupport::Notifications.subscribe("render.view_component.helpers") do |*args|
events << ActiveSupport::Notifications::Event.new(*args)
end
render_inline(InstrumentationComponent.new)

assert_equal(events.size, 1)
assert_equal("render.view_component.helpers", events[0].name)
end
end
end
end
Loading