Skip to content

cowboy 2

Cowboy2 Plugin

Description

The Cowboy2 plugin collects metrics related to requests served by the application.

Applications it depends on

cowboy version 2.4 and over

Modules

wombat_plugin_cowboy2

Reports

The Cowboy 2 plugin reports hit counts and average response time for pathsth, it also reports hit counts for different HTTP statuses grouped by their type.

For the plugin to work the application has to be prepared with the following changes:

The handler configuration needs to be extended to include the cowboy metrics handler and a metrics callback has to be added:

1
2
3
4
5
6
7
{ok, _} = cowboy:start_clear(my_http_listener,
                 [{port, 8081}],
                 #{env => #{dispatch => Dispatch},
                   metrics_callback =>
                       fun do_metrics_callback/1,
                   stream_handlers =>
                       [cowboy_metrics_h, cowboy_stream_h]}),

The cowboy_metrics_h module in the stream handlers will prepare Cowboy to call the metrics_callback function with the metrics related to a request.

The implementation of the do_metrics_callback is as follows:

1
2
3
4
5
6
do_metrics_callback(Metric) ->
    try wombat_plugin_cowboy2 ! {cowboy_metric, Metric} of
      _ -> ok
    catch
      _:_ -> ok
    end.

If the cowboy_metrics_h is missing or there is no metrics callback, you will see this warning in Wombat:

1
2
cowboy_metrics_h is not installed for handler HANDLER, metrics will not be reported.
Please check the Wombat docs for setting up Cowboy2 for metrics collecting.

The metrics will appear on the GUI soon after the plugin has been started and the application started serving requests.

Configure the default Email

Elixir Configuration

Add metrics_callback and stream_handlers to the cowboy2 application start function, and define the do_metric_callback function as below:

1
2
3
4
5
6
7
8
def start(_type, _args) do
    dispatch_config = build_dispatch_config
    { :ok, _ } = :cowboy.start_clear(:http,[{:port, 8081}],
        {:env => %{:dispatch => dispatch_config},
        :metrics_callback => &do_metrics_callback/1,
        :stream_handlers => [:cowboy_metrics_h, :cowboy_stream_h],
        :middlewares => [:cowboy_router, :directory_lister, :cowboy_handler]})
end
1
2
3
4
5
6
7
8
def do_metrics_callback(metric) do
    try do
        send :wombat_plugin_cowboy2, {:cowboy_metric, metric}
        :ok
    rescue
        _ -> :ok
    end
end