Skip to content

Commit

Permalink
feat: Trace route_eval in Sinatra
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgns committed Sep 17, 2023
1 parent d77b700 commit eb7e797
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,29 @@ module Extensions
# Sinatra extension that installs TracerMiddleware and provides
# tracing for template rendering
module TracerExtension

# Sinatra hook after extension is registered
def self.registered(app)
# Create tracing `render` method

::Sinatra::Base.module_eval do
# Invoked when a matching route is found.
# This method yields directly to user code.
def route_eval
Sinatra::Instrumentation.instance.tracer.in_span(
'sinatra.route_eval',
attributes: {
'sinatra.route' => request.path,
'sinatra.request_method' => request.request_method,
'sinatra.resource' => "#{request.request_method} #{request.path}",
}
) do
throw :halt, yield
# Can't use super?
# super
end
end

# Create tracing `render` method
def render(_engine, data, *)
template_name = data.is_a?(Symbol) ? data : :literal

Expand All @@ -28,6 +47,7 @@ def render(_engine, data, *)
end
end
end

app.use(*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args)
app.use(Middlewares::TracerMiddleware)
end
Expand Down

0 comments on commit eb7e797

Please sign in to comment.