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

Improve nested Sinatra app instrumentation #1097

Merged
merged 1 commit into from
Jun 19, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: patch
type: change
---

Improve instrumentation for mounted Sinatra apps in Rails apps. The sample reported for the Sinatra request will now include the time spent in Rails and its middleware.
39 changes: 29 additions & 10 deletions lib/appsignal/rack/sinatra_instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,26 @@ def call(env)
end
end

private

def call_with_appsignal_monitoring(env)
options = { :force => @options.include?(:force) && @options[:force] }
options.merge!(:params_method => @options[:params_method]) if @options[:params_method]
request = @options.fetch(:request_class, Sinatra::Request).new(env)
transaction = Appsignal::Transaction.create(
SecureRandom.uuid,
Appsignal::Transaction::HTTP_REQUEST,
request,
options
)
has_parent_instrumentation = env.key?(Appsignal::Rack::APPSIGNAL_TRANSACTION)
transaction =
if has_parent_instrumentation
env[Appsignal::Rack::APPSIGNAL_TRANSACTION]
else
Appsignal::Transaction.create(
SecureRandom.uuid,
Appsignal::Transaction::HTTP_REQUEST,
request
)
end

begin
params = fetch_params(request, @options.fetch(:params_method, :params))
transaction.params = params if params

Appsignal.instrument("process_action.sinatra") do
@app.call(env)
end
Expand All @@ -73,7 +82,9 @@ def call_with_appsignal_monitoring(env)
transaction.set_metadata("path", request.path)
transaction.set_metadata("method", request.request_method)
transaction.set_http_or_background_queue_start
Appsignal::Transaction.complete_current!

# Only close if this middleware created the instrumentation
Appsignal::Transaction.complete_current! unless has_parent_instrumentation
end
end

Expand All @@ -88,7 +99,15 @@ def action_name(env)
end
end

private
def fetch_params(request, params_method)
return unless request.respond_to?(params_method)

request.send(params_method)
rescue => error
# Getting params from the request has been know to fail.
Appsignal.internal_logger.debug "Exception while getting Sinatra params: #{error}"
nil
end

def raise_errors?(app)
app.respond_to?(:settings) &&
Expand Down
Loading