.. option:: metrics
- Reads aggregated metrics data in Prometheus format directly from the running
- resolver.
+ Get aggregated metrics from the running resolver in JSON format (default) or optionally in Prometheus format.
+ The ``prometheus-client`` Python package needs to be installed if you wish to use the Prometheus format.
- Requires a connection to the management API.
+ Requires a connection to the management HTTP API.
+
+ .. option:: --prometheus
+
+ Get metrics in Prometheus format if dependencies are met in the resolver.
.. option:: [file]
- Optional. The file into which Prometheus metrics will be exported.
+ Optional. The file into which metrics will be exported.
If not specified, the metrics are printed into ``stdout``.
.. code-block:: bash
- $ kresctl metrics ./metrics/data.txt
+ $ kresctl metrics ./kres-metrics.json
+ $ kresctl metrics --prometheus
.. option:: cache clear
from typing import List, Optional, Tuple, Type
from knot_resolver_manager.cli.command import Command, CommandArgs, CompWords, register_command
+from knot_resolver_manager.utils.modeling.parsing import DataFormat, parse_json
from knot_resolver_manager.utils.requests import request
class MetricsCommand(Command):
def __init__(self, namespace: argparse.Namespace) -> None:
self.file: Optional[str] = namespace.file
+ self.prometheus: bool = namespace.prometheus
super().__init__(namespace)
def register_args_subparser(
subparser: "argparse._SubParsersAction[argparse.ArgumentParser]",
) -> Tuple[argparse.ArgumentParser, "Type[Command]"]:
- metrics = subparser.add_parser("metrics", help="get prometheus metrics data")
+ metrics = subparser.add_parser(
+ "metrics",
+ help="Get aggregated metrics from the running resolver in JSON format (default) or optionally in Prometheus format."
+ "\nThe 'prometheus-client' Python package needs to be installed if you wish to use the Prometheus format."
+ "\nRequires a connection to the management HTTP API.",
+ )
+
+ metrics.add_argument(
+ "--prometheus",
+ help="Get metrics in Prometheus format if dependencies are met in the resolver.",
+ action="store_true",
+ default=False,
+ )
+
metrics.add_argument(
"file",
- help="Optional, file where to export Prometheus metrics. If not specified, the metrics are printed.",
+ help="Optional. The file into which metrics will be exported."
+ "\nIf not specified, the metrics are printed into stdout.",
nargs="?",
default=None,
)
return {}
def run(self, args: CommandArgs) -> None:
- response = request(args.socket, "GET", "metrics")
+ response = request(args.socket, "GET", "metrics/prometheus" if self.prometheus else "metrics/json")
if response.status == 200:
+ if self.prometheus:
+ metrics = response.body
+ else:
+ metrics = DataFormat.JSON.dict_dump(parse_json(response.body), indent=4)
+
if self.file:
with open(self.file, "w") as f:
- f.write(response.body)
+ f.write(metrics)
else:
- print(response.body)
+ print(metrics)
else:
print(response, file=sys.stderr)
sys.exit(1)