Unplug: Conditionally execute any plug at run-time

On more than one occasion I’ve needed the ability to conditionally execute plugs depending on the incoming request. I have also needed the ability to change this conditional behavior at run-time depending on some configuration. For example, as not to pollute my log system, I want to only run Plug.Telemetry if the request route is not something that Prometheus or Kubernetes hit frequently:

plug Unplug,
    if: {Unplug.Predicates.RequestPathNotIn, ["/metrics", "/healthcheck"]},
    do: Plug.Telemetry

Another example would be to add some sort of custom authentication around an arbitrary plug (like in this issue https://github.com/deadtrickster/prometheus-plugs/issues/30). Using Unplug, you can write you own Unplug predicate and only execute the plug if your request meets your particular criteria.

This removes the burden from the library maintainer from having to have some sort of configuration that meets your particular needs, and also still preserves Plug’s init/1 compile-time init behavior (https://hexdocs.pm/plug/Plug.html).

Hex: https://hex.pm/packages/unplug
Github: https://github.com/akoutmos/unplug

6 Likes