cowboy icon indicating copy to clipboard operation
cowboy copied to clipboard

Adding metadata/labels to a route?

Open rlipscombe opened this issue 2 months ago • 2 comments

One question that occasionally arises at work is "how do we attach telemetry labels to a cowboy route?". We'd like to be able to attach arbitrary metadata to a route, and have it available in the cowboy_metrics_h callback.

Ideally, it would be an extra element in the route tuple, something like this:

    Dispatch = cowboy_router:compile([
        {'_', [
            {"/", cowboy_static, {priv_file, ?APPLICATION, "index.html"}},
            {"/users/:id", [{id, int}], the_user_h, [], #{labels => #{route => "/users"}}}
        ]}
    ]),

...and would be picked up by the cowboy_router middleware and put in the env along with handler and handler_opts.

(to avoid ambiguity, if you wanted this, you'd have to explicitly specify constraints, possibly empty -- or maybe {Handler, Opts} would need to be a tuple in this case).

Does this seem like a good idea? Is there another way to do it right now?

rlipscombe avatar Apr 18 '24 17:04 rlipscombe