Skip to content

Commit

Permalink
Add streaming responses to Sinatra Puma app
Browse files Browse the repository at this point in the history
Help test the streaming responses in the future.
  • Loading branch information
tombruijn committed Aug 23, 2024
1 parent 1e9a0a9 commit 5a8126d
Showing 1 changed file with 48 additions and 15 deletions.
63 changes: 48 additions & 15 deletions ruby/sinatra-puma/app/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@
Appsignal.start

get "/" do
time = Time.now.strftime("%H:%M")
<<~HTML
<h1>Sinatra + Puma example app</h1>
<p>Run the <code>console</code> command for this app to "phased restart" this app.</p>
<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>
<li><a href="/errors?time=#{time}">Multiple errors with custom instrumentation</a></li>
<li><a href="/cron?time=#{time}">Custom cron check-in</a></li>
</ul>
HTML
time =
Appsignal.instrument "fetch.time" do
Time.now.strftime("%H:%M")
end
Appsignal.instrument "render.view" do
<<~HTML
<h1>Sinatra + Puma example app</h1>
<p>Run the <code>console</code> command for this app to "phased restart" this app.</p>
<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>
<li><a href="/heartbeat?time=#{time}">Custom heartbeat</a></li>
<li><a href="/errors?time=#{time}">Multiple errors with custom instrumentation</a></li>
<li><a href="/array?time=#{time}">Array response body</a></li>
<li><a href="/cron?time=#{time}">Custom cron check-in</a></li>
</ul>
HTML
end
end

get "/slow" do
Expand Down Expand Up @@ -57,6 +64,7 @@ class AnotherCustomError < StandardError
end

get "/stream/slow" do
sleep 0.5
stream do |out|
sleep 1
out << "1"
Expand All @@ -83,3 +91,28 @@ class AnotherCustomError < StandardError

"Cron check-in sent!"
end

class MyResponseBody
def initialize
@body = []
end

def <<(value)
@body << value
end

def to_ary
@body
end
end

get "/array" do
body = MyResponseBody.new
Appsignal.instrument "do.stuff" do
body << "abc"
Appsignal.instrument "do_more.stuff" do
body << "def"
end
end
[200, body]
end

0 comments on commit 5a8126d

Please sign in to comment.