Skip to content

Commit 8278626

Browse files
authored
Merge pull request rails#49297 from hannahramadan/main
Add instrumentation for ActionController::Live#send_stream
2 parents d4bf9f3 + 6488bd9 commit 8278626

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

actionpack/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Add instrumentation for ActionController::Live#send_stream
2+
3+
Allows subscribing to `send_stream` events. The event payload contains the filename, disposition, and type.
4+
5+
*Hannah Ramadan*
6+
17
* Add support for `with_routing` test helper in `ActionDispatch::IntegrationTest`
28

39
*Gannon McGibbon*

actionpack/lib/action_controller/metal/live.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,18 @@ def response_body=(body)
343343
# end
344344
# end
345345
def send_stream(filename:, disposition: "attachment", type: nil)
346-
response.headers["Content-Type"] =
347-
(type.is_a?(Symbol) ? Mime[type].to_s : type) ||
348-
Mime::Type.lookup_by_extension(File.extname(filename).downcase.delete("."))&.to_s ||
349-
"application/octet-stream"
346+
payload = { filename: filename, disposition: disposition, type: type }
347+
ActiveSupport::Notifications.instrument("send_stream.action_controller", payload) do
348+
response.headers["Content-Type"] =
349+
(type.is_a?(Symbol) ? Mime[type].to_s : type) ||
350+
Mime::Type.lookup_by_extension(File.extname(filename).downcase.delete("."))&.to_s ||
351+
"application/octet-stream"
350352

351-
response.headers["Content-Disposition"] =
352-
ActionDispatch::Http::ContentDisposition.format(disposition: disposition, filename: filename)
353+
response.headers["Content-Disposition"] =
354+
ActionDispatch::Http::ContentDisposition.format(disposition: disposition, filename: filename)
353355

354-
yield response.stream
356+
yield response.stream
357+
end
355358
ensure
356359
response.stream.close
357360
end

actionpack/test/controller/live_stream_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,19 @@ def test_send_stream
373373
assert_match "my.csv", @response.headers["Content-Disposition"]
374374
end
375375

376+
def test_send_stream_instrumentation
377+
payload = nil
378+
subscriber = proc { |event| payload = event.payload }
379+
380+
ActiveSupport::Notifications.subscribed(subscriber, "send_stream.action_controller") do
381+
get :send_stream_with_explicit_content_type
382+
end
383+
384+
assert_equal "sample.csv", payload[:filename]
385+
assert_equal "attachment", payload[:disposition]
386+
assert_equal "text/csv", payload[:type]
387+
end
388+
376389
def test_send_stream_with_options
377390
get :send_stream_with_options
378391
assert_equal %[{ name: "David", age: 41 }], @response.body

guides/source/active_support_instrumentation.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,22 @@ Additional keys may be added by the caller.
197197
| `:keys` | The unpermitted keys |
198198
| `:context` | Hash with the following keys: `:controller`, `:action`, `:params`, `:request` |
199199

200+
#### `send_stream.action_controller`
201+
202+
| Key | Value |
203+
| -------------- | ---------------------------------------- |
204+
| `:filename` | The filename |
205+
| `:type` | HTTP content type |
206+
| `:disposition` | HTTP content disposition |
207+
208+
```ruby
209+
{
210+
filename: "subscribers.csv",
211+
type: "text/csv",
212+
disposition: "attachment"
213+
}
214+
```
215+
200216
### Action Controller — Caching
201217

202218
#### `write_fragment.action_controller`

0 commit comments

Comments
 (0)