Skip to content

Commit

Permalink
Merge pull request #187 from appsignal/streaming-examples
Browse files Browse the repository at this point in the history
Add a streaming endpoint to Rails and Sinatra apps
  • Loading branch information
tombruijn committed Feb 22, 2024
2 parents b2e85f6 + 9d4b452 commit 7df46c3
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 9 deletions.
3 changes: 0 additions & 3 deletions ruby/rails6-mysql/app/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 4.1.0'
# Display performance information such as SQL time and flame graphs for each request in your browser.
# Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
gem 'rack-mini-profiler', '~> 2.0'
gem 'listen', '~> 3.3'
end

Expand Down
6 changes: 3 additions & 3 deletions ruby/rails6-mysql/app/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: /integration
specs:
appsignal (3.4.0)
appsignal (3.5.5)
rack

GEM
Expand Down Expand Up @@ -231,7 +231,7 @@ DEPENDENCIES
webpacker (~> 5.0)

RUBY VERSION
ruby 3.2.1p31
ruby 3.2.2p53

BUNDLED WITH
2.4.11
2.5.6
35 changes: 35 additions & 0 deletions ruby/rails6-mysql/app/app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,39 @@ def error
def slow
sleep 3
end

class StreamingSlowBody
def each
sleep 1
yield "1"
sleep 1
yield "2"
sleep 1
yield "3"
sleep 1
yield "4"
end
end

def streaming_slow
headers["Content-Length"] = "4"
headers["Last-Modified"] = Time.now.httpdate
self.response_body = StreamingSlowBody.new
end

class StreamingErrorBody
def each
yield "1"
sleep 0.5
yield "2"
yield "3"
raise "Rails error in streaming body"
end
end

def streaming_error_in_body
headers["Content-Length"] = "4"
headers["Last-Modified"] = Time.now.httpdate
self.response_body = StreamingErrorBody.new
end
end
9 changes: 9 additions & 0 deletions ruby/rails6-mysql/app/app/views/tests/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Rails 6 mysql app</h1>

<ul>
<li><%= link_to "Slow request", slow_path %></li>
<li><%= link_to "Error request", error_path %></li>
<li><%= link_to "Slow streaming request", streaming_slow_path %></li>
<li><%= link_to "Streaming request with error", streaming_error_path %></li>
<li><%= link_to "Posts: resources with queies", posts_path %></li>
</ul>
4 changes: 4 additions & 0 deletions ruby/rails6-mysql/app/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Rails.application.routes.draw do
get "/slow", to: "tests#slow"
get "/error", to: "tests#error"
get "/streaming/slow", to: "tests#streaming_slow"
get "/streaming/error", to: "tests#streaming_error_in_body"

resources :posts, :except => %i[edit update destroy]

root to: "tests#index"
end
2 changes: 1 addition & 1 deletion ruby/rails7-postgres/app/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: /integration
specs:
appsignal (3.4.12)
appsignal (3.5.5)
rack

GEM
Expand Down
37 changes: 36 additions & 1 deletion ruby/rails7-postgres/app/app/controllers/examples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,43 @@ class ExamplesController < ApplicationController
def slow
sleep 3
end

def error
raise "This is a Rails error!"
end

class StreamingSlowBody
def each
sleep 1
yield "1"
sleep 1
yield "2"
sleep 1
yield "3"
sleep 1
yield "4"
end
end

def streaming_slow
headers["Content-Length"] = "4"
headers["Last-Modified"] = Time.now.httpdate
self.response_body = StreamingSlowBody.new
end

class StreamingErrorBody
def each
yield "1"
sleep 0.5
yield "2"
yield "3"
raise "Rails error in streaming body"
end
end

def streaming_error_in_body
headers["Content-Length"] = "4"
headers["Last-Modified"] = Time.now.httpdate
self.response_body = StreamingErrorBody.new
end
end
10 changes: 9 additions & 1 deletion ruby/rails7-postgres/app/app/views/examples/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
<h1>Go to <a href="/items">&sol;items</a></h1>
<h1>Rails 7 postgresql app</h1>

<ul>
<li><%= link_to "Slow request", slow_path %></li>
<li><%= link_to "Error request", error_path %></li>
<li><%= link_to "Slow streaming request", streaming_slow_path %></li>
<li><%= link_to "Streaming request with error", streaming_error_path %></li>
<li><%= link_to "Items: resources with queies", items_path %></li>
</ul>
2 changes: 2 additions & 0 deletions ruby/rails7-postgres/app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
root :to => "examples#index"
get "/slow", to: "examples#slow"
get "/error", to: "examples#error"
get "/streaming/slow", to: "examples#streaming_slow"
get "/streaming/error", to: "examples#streaming_error_in_body"
resources :items
end
48 changes: 48 additions & 0 deletions ruby/sinatra-puma/app/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
PATH
remote: /integration
specs:
appsignal (3.5.5)
rack

GEM
remote: https://rubygems.org/
specs:
base64 (0.2.0)
multi_json (1.15.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
nio4r (2.7.0)
puma (6.4.2)
nio4r (~> 2.0)
rack (3.0.9)
rack-protection (4.0.0)
base64 (>= 0.1.0)
rack (>= 3.0.0, < 4)
rack-session (2.0.0)
rack (>= 3.0.0)
ruby2_keywords (0.0.5)
sinatra (4.0.0)
mustermann (~> 3.0)
rack (>= 3.0.0, < 4)
rack-protection (= 4.0.0)
rack-session (>= 2.0.0, < 3)
tilt (~> 2.0)
sinatra-contrib (4.0.0)
multi_json (>= 0.0.2)
mustermann (~> 3.0)
rack-protection (= 4.0.0)
sinatra (= 4.0.0)
tilt (~> 2.0)
tilt (2.3.0)

PLATFORMS
aarch64-linux

DEPENDENCIES
appsignal!
puma
sinatra
sinatra-contrib

BUNDLED WITH
2.2.33
22 changes: 22 additions & 0 deletions ruby/sinatra-puma/app/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<ul>
<li><a href="/slow?time=#{time}">Slow request</a></li>
<li><a href="/error?time=#{time}">Error request</a></li>
<li><a href="/stream/slow?time=#{time}">Slow streaming request</a></li>
<li><a href="/stream/error?time=#{time}">Streaming request with error</a></li>
</ul>
HTML
end
Expand All @@ -24,3 +26,23 @@
get "/error" do
raise "error"
end

get "/stream/slow" do
stream do |out|
sleep 1
out << "1"
sleep 1
out << "2"
sleep 1
out << "3"
end
end

get "/stream/error" do
stream do |out|
out << "a"
sleep 0.5
out << "b"
raise "Sinatra error in streaming body"
end
end
1 change: 1 addition & 0 deletions ruby/sinatra-puma/commands/run
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ echo "Install appsignal"
cd /app

# Paths for Puma
rm -rf shared
mkdir -p shared/sockets shared/pids shared/log

echo "Install dependencies"
Expand Down

0 comments on commit 7df46c3

Please sign in to comment.