%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%

%% ----------------------------------------------------------------------------
%% RabbitMQ Prometheus Plugin
%%
%% See https://rabbitmq.com/prometheus.html for details
%% ----------------------------------------------------------------------------

%% Option to return metrics per-object, unaggregated
{mapping, "prometheus.return_per_object_metrics", "rabbitmq_prometheus.return_per_object_metrics",
    [{datatype, boolean}]}.

%% Endpoint path
{mapping, "prometheus.path", "rabbitmq_prometheus.path",
    [{datatype, string}]}.

%% HTTP (TCP) listener options ========================================================

%% HTTP listener consistent with the management plugin, Web STOMP and Web MQTT.
%%
%% {tcp_config, [{port,     15692},
%%               {ip,       "127.0.0.1"}]}

{mapping, "prometheus.tcp.listener", "rabbitmq_prometheus.tcp_config",
    [{datatype, [{enum, [none]}, ip]}]}.
{mapping, "prometheus.tcp.port", "rabbitmq_prometheus.tcp_config.port",
    [{datatype, integer}]}.
{mapping, "prometheus.tcp.ip", "rabbitmq_prometheus.tcp_config.ip",
    [{datatype, string},
     {validators, ["is_ip"]}]}.

{translation,
    "rabbitmq_prometheus.tcp_config",
    fun(Conf) ->
        Setting = cuttlefish:conf_get("prometheus.tcp.listener", Conf, undefined),
        case Setting of
            none      -> [];
            undefined -> [{port, 15692}];
            {Ip, Port} when is_list(Ip), is_integer(Port) ->
                [{ip, Ip}, {port, Port}]
        end
    end
}.

{mapping, "prometheus.tcp.max_connections", "rabbitmq_prometheus.tcp_config.max_connections",
    [{datatype, [{atom, infinity}, integer]},
     {validators, ["non_negative_integer"]}]}.

{translation, "rabbitmq_prometheus.tcp_config.max_connections",
fun(Conf) ->
    case cuttlefish:conf_get("prometheus.tcp.max_connections", Conf, undefined) of
        undefined                -> cuttlefish:unset();
        infinity                 -> infinity;
        Val when is_integer(Val) -> Val;
        _                        -> cuttlefish:invalid("should be a non-negative integer")
    end
end}.

{mapping, "prometheus.tcp.compress", "rabbitmq_prometheus.tcp_config.cowboy_opts.compress",
    [{datatype, boolean}]}.
{mapping, "prometheus.tcp.idle_timeout", "rabbitmq_prometheus.tcp_config.cowboy_opts.idle_timeout",
    [{datatype, {integer, non_negative}}]}.
{mapping, "prometheus.tcp.inactivity_timeout", "rabbitmq_prometheus.tcp_config.cowboy_opts.inactivity_timeout",
    [{datatype, {integer, non_negative}}]}.
{mapping, "prometheus.tcp.request_timeout", "rabbitmq_prometheus.tcp_config.cowboy_opts.request_timeout",
    [{datatype, {integer, non_negative}}]}.
{mapping, "prometheus.tcp.shutdown_timeout", "rabbitmq_prometheus.tcp_config.cowboy_opts.shutdown_timeout",
    [{datatype, {integer, non_negative}}]}.
{mapping, "prometheus.tcp.max_keepalive", "rabbitmq_prometheus.tcp_config.cowboy_opts.max_keepalive",
    [{datatype, {integer, non_negative}}]}.

%% HTTPS (TLS) listener options ========================================================

%% HTTPS listener consistent with the management plugin, Web STOMP and Web MQTT.
%%
%% {ssl_config, [{port,       15691},
%%               {ip,         "127.0.0.1"},
%%               {cacertfile, "/path/to/cacert.pem"},
%%               {certfile,   "/path/to/cert.pem"},
%%               {keyfile,    "/path/to/key.pem"}]}

{mapping, "prometheus.ssl.port", "rabbitmq_prometheus.ssl_config.port",
    [{datatype, integer}]}.
{mapping, "prometheus.ssl.backlog", "rabbitmq_prometheus.ssl_config.backlog",
    [{datatype, integer}]}.
{mapping, "prometheus.ssl.ip", "rabbitmq_prometheus.ssl_config.ip",
    [{datatype, string}, {validators, ["is_ip"]}]}.

{mapping, "prometheus.ssl.max_connections", "rabbitmq_prometheus.ssl_config.max_connections",
    [{datatype, [{atom, infinity}, integer]},
     {validators, ["non_negative_integer"]}]}.

{translation, "rabbitmq_prometheus.ssl_config.max_connections",
fun(Conf) ->
    case cuttlefish:conf_get("prometheus.ssl.max_connections", Conf, undefined) of
        undefined                -> cuttlefish:unset();
        infinity                 -> infinity;
        Val when is_integer(Val) -> Val;
        _                        -> cuttlefish:invalid("should be a non-negative integer")
    end
end}.

{mapping, "prometheus.ssl.certfile", "rabbitmq_prometheus.ssl_config.ssl_opts.certfile",
    [{datatype, string}, {validators, ["pem_file"]}]}.
{mapping, "prometheus.ssl.keyfile", "rabbitmq_prometheus.ssl_config.ssl_opts.keyfile",
    [{datatype, string}, {validators, ["pem_file"]}]}.
{mapping, "prometheus.ssl.cacertfile", "rabbitmq_prometheus.ssl_config.ssl_opts.cacertfile",
    [{datatype, string}, {validators, ["pem_file"]}]}.
{mapping, "prometheus.ssl.password", "rabbitmq_prometheus.ssl_config.ssl_opts.password",
    [{datatype, [tagged_binary, binary]}]}.

{translation, "rabbitmq_prometheus.ssl_config.ssl_opts.password",
fun(Conf) ->
    rabbit_cuttlefish:optionally_tagged_binary("prometheus.ssl.password", Conf)
end}.

{mapping, "prometheus.ssl.verify", "rabbitmq_prometheus.ssl_config.ssl_opts.verify", [
    {datatype, {enum, [verify_peer, verify_none]}}]}.

{mapping, "prometheus.ssl.fail_if_no_peer_cert", "rabbitmq_prometheus.ssl_config.ssl_opts.fail_if_no_peer_cert", [
    {datatype, boolean}]}.

{mapping, "prometheus.ssl.honor_cipher_order", "rabbitmq_prometheus.ssl_config.ssl_opts.honor_cipher_order",
    [{datatype, boolean}]}.

{mapping, "prometheus.ssl.honor_ecc_order", "rabbitmq_prometheus.ssl_config.ssl_opts.honor_ecc_order",
    [{datatype, boolean}]}.

{mapping, "prometheus.ssl.reuse_sessions", "rabbitmq_prometheus.ssl_config.ssl_opts.reuse_sessions",
    [{datatype, boolean}]}.

{mapping, "prometheus.ssl.secure_renegotiate", "rabbitmq_prometheus.ssl_config.ssl_opts.secure_renegotiate",
    [{datatype, boolean}]}.

{mapping, "prometheus.ssl.client_renegotiation", "rabbitmq_prometheus.ssl_config.ssl_opts.client_renegotiation",
    [{datatype, boolean}]}.

{mapping, "prometheus.ssl.depth", "rabbitmq_prometheus.ssl_config.ssl_opts.depth",
    [{datatype, byte}]}.

{mapping, "prometheus.ssl.versions.$version", "rabbitmq_prometheus.ssl_config.ssl_opts.versions",
    [{datatype, atom}]}.

{translation, "rabbitmq_prometheus.ssl_config.ssl_opts.versions",
fun(Conf) ->
    Settings = cuttlefish_variable:filter_by_prefix("prometheus.ssl.versions", Conf),
    [V || {_, V} <- Settings]
end}.

{mapping, "prometheus.ssl.ciphers.$cipher", "rabbitmq_prometheus.ssl_config.ssl_opts.ciphers",
    [{datatype, string}]}.

{translation, "rabbitmq_prometheus.ssl_config.ssl_opts.ciphers",
fun(Conf) ->
    Settings = cuttlefish_variable:filter_by_prefix("prometheus.ssl.ciphers", Conf),
    lists:reverse([V || {_, V} <- Settings])
end}.

{mapping, "prometheus.ssl.compress", "rabbitmq_prometheus.ssl_config.cowboy_opts.compress",
    [{datatype, boolean}]}.
{mapping, "prometheus.ssl.idle_timeout", "rabbitmq_prometheus.ssl_config.cowboy_opts.idle_timeout",
    [{datatype, {integer, non_negative}}]}.
{mapping, "prometheus.ssl.inactivity_timeout", "rabbitmq_prometheus.ssl_config.cowboy_opts.inactivity_timeout",
    [{datatype, {integer, non_negative}}]}.
{mapping, "prometheus.ssl.request_timeout", "rabbitmq_prometheus.ssl_config.cowboy_opts.request_timeout",
    [{datatype, {integer, non_negative}}]}.
{mapping, "prometheus.ssl.shutdown_timeout", "rabbitmq_prometheus.ssl_config.cowboy_opts.shutdown_timeout",
    [{datatype, {integer, non_negative}}]}.
{mapping, "prometheus.ssl.max_keepalive", "rabbitmq_prometheus.ssl_config.cowboy_opts.max_keepalive",
    [{datatype, {integer, non_negative}}]}.

%% Kept for compatibility reasons. This is a no-op.
{mapping, "prometheus.filter_aggregated_queue_metrics_pattern", "rabbitmq_prometheus.filter_aggregated_queue_metrics_pattern",
    [{datatype, string}]}.

%% Authentication options ========================================================
{mapping, "prometheus.authentication.enabled", "rabbitmq_prometheus.authentication.enabled",
    [{datatype, boolean}]}.
