All of our application monitoring is currently done via New Relic APM, so naturally we want to be able to monitor Caddy via New Relic APM as well. I’m in the process of trying to build a Caddy New Relic plugin, and one problem I’ve run into is how to know which request matcher/named route matched a given request.
For example, let’s say my Caddyfile has a route like this:
If you were instrumenting any other Go-based web app/API, you’d do something like this…
…so that all requests to this endpoint (for all users) get treated as a single transaction within New Relic, and not a transaction for each individual user (i.e. /v1/users/1, /v1/users/2, etc, which would cause the number of distinct transactions within New Relic to balloon based on the number of distinct users).
To do this within Caddy, my initial thought was to simply use the request matcher/route as the name of the transaction. Is there anything available to modules in the http.handlers namespace that tells the middleware handler which request matcher/route matched the current request? If not, could someone point me in the direction of where/how I would make that info available to caddyhttp.MiddlewareHandler middleware? Or, if I’m simply thinking about this wrong and there’s a better way to do this, I’m all ears.
Since it’s a middleware chain, if the newrelic directive is ordered higher than anything in that handle/route (you can ensure that by using the global option order newrelic first), then wrapping the handler function before calling the next one might have the same effect as your code snippet.
This is why I was wondering if there was a way to update Caddy so that whatever component is responsible for deciding which of those path matchers “wins” and matches the incoming request, it could set something on the request object so downstream middleware know which request matcher was selected for the incoming request. I just don’t know where in the Caddy codebase this route matching logic lives.
Request matchers are by-design “dumb”. They just return a boolean result. No side effects (except in some edge cases where they do have side effects like adding some placeholders to the request context).
There’s many kinds of matchers, and many of them don’t have an easy way to generate an identifier for the match. For path matching it’s pretty obvious, just use the match pattern string. But for things like header matchers where it’s a key-value match, or regexp matching, or file matching, or even expression matching… it gets a lot trickier. So I’m not sure there’s really a good option for generalizing this.
Most of the matchers are here in code (some others like the file matcher is in another package, look for MatchFile if you care):
Yup, great points. Ok, will think about it some more and try to work my way through the Caddy codebase to see if I can come up with a solution to this problem. Thanks again @francislavoie for your help.
Oh – not to mention some matches are AND, if you used a named matcher with two different kinds of matchers for example, so it’s not sufficient to say “it’s just this one path” because that would be a lie