Creating a plugin that detects and records the matched params #324
Replies: 2 comments 10 replies
-
You probably want to use the match_hook plugin: https://roda.jeremyevans.net/rdoc/classes/Roda/RodaPlugins/MatchHook.html |
Beta Was this translation helpful? Give feedback.
-
Hi there, def if_match(args, &block)
@otel_matched_path ||= []
segment = args.first
next_token = remaining_path.split("/").reject { |val| val.empty? }.first
if !@otel_matched_path.include?(segment)
# When there is a parameter in the matcher. Ex: sessions/:session_id
if !segment.is_a?(String) && block.arity > 0
@otel_matched_path << ":#{block.parameters.flatten[1]}"
# when its a symbol match. r.on :session_id
elsif segment.is_a?(Symbol)
@otel_matched_path << segment
# when its a normal match. r.on "form"
elsif segment == next_token
@otel_matched_path << segment
end
end
env["http.route"] = "#{env["SCRIPT_NAME"]}/#{@otel_matched_path.join("/")}"
super(args, &block)
end I had to follow a similar approach for the Having the
Likewise, I was able to instrument exception within a trace monkeypatching the module InstanceMethods
# Override the internal handle_error method
# https://github.com/jeremyevans/roda/blob/master/lib/roda/plugins/error_handler.rb#L93
# so the exception could be captured before sending it to the defined handler in the roda apps
def _handle_error(e)
span = OpenTelemetry::Instrumentation::Rack.current_span
return unless span.recording?
span.record_exception(e)
super(e)
end
end I wanted to have a hook before dispatching the exception to the error_handler from nested roda apps. Is there a better way of doing it? Nonetheless, I am bit worried that we are modifying the internal api of the framework, and I don't feel really comfortable doing so. Hence, I was thinking on adding the information of the args and the parameters of the block to the match hook so that a custom plugin like mine could use it. What's your take on it? Regards, |
Beta Was this translation helpful? Give feedback.
-
Hi @jeremyevans , I am trying to create a plugin to instrument an application following the Open Telemetry spec. The plugin must be automatic, like every other instrumentation, requiring should start generating telemetry data according to the spec. My initial approach was to have a middleware like this one at the rack app level that could read the env and obtain the information needed to build the attributes described in the otel spec. That middleware would be autoloaded using an approach similar like the following:
Just by requiring this file we could start using our roda instrumentation. Most of the basic http attributes from the spec are covered by the rack instrumentation however there is one that I haven't figured out how it could possibly be implemented: the http.route. That attribute allows to group traces (units of work in your system) using the parameter described in an on/is block, for example: /users/:userID?. This attribute is extremely useful as you could group every request of fetching a specific user and find anomalies very quickly.
My first approach was to fetch that information from the env hash in rack, however, after digging in the roda source code I've realized that there's no track of those matched params, they are just consumed like any other part of the path. So my question is: is there a way from a plugin perspective to identify them and if not, would a PR with the proposed changed by considered? Thanks for the time considering this.
Regards,
Eduardo
Beta Was this translation helpful? Give feedback.
All reactions