2023-08-03 15:30:19 +02:00
|
|
|
# Copyright 2023 Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2023-09-05 14:54:33 +02:00
|
|
|
from cliff import lister
|
|
|
|
|
2023-11-08 02:22:17 -05:00
|
|
|
from observabilityclient.i18n import _
|
2023-09-05 14:54:33 +02:00
|
|
|
from observabilityclient.utils import metric_utils
|
|
|
|
from observabilityclient.v1 import base
|
2023-08-03 15:30:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
class List(base.ObservabilityBaseCommand, lister.Lister):
|
2023-09-05 14:54:33 +02:00
|
|
|
"""Query prometheus for list of all metrics."""
|
2023-08-03 15:30:19 +02:00
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
client = metric_utils.get_client(self)
|
2023-10-31 05:31:58 -04:00
|
|
|
metrics = client.query.list(disable_rbac=parsed_args.disable_rbac)
|
2023-08-03 15:30:19 +02:00
|
|
|
return ["metric_name"], [[m] for m in metrics]
|
|
|
|
|
|
|
|
|
|
|
|
class Show(base.ObservabilityBaseCommand, lister.Lister):
|
2023-09-05 14:54:33 +02:00
|
|
|
"""Query prometheus for the current value of metric."""
|
2023-08-03 15:30:19 +02:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super().get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
2023-09-05 14:54:33 +02:00
|
|
|
'name',
|
|
|
|
help=_("Name of the metric to show"))
|
2023-08-03 15:30:19 +02:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
client = metric_utils.get_client(self)
|
2023-10-31 05:31:58 -04:00
|
|
|
metric = client.query.show(parsed_args.name,
|
|
|
|
disable_rbac=parsed_args.disable_rbac)
|
2023-09-05 14:54:33 +02:00
|
|
|
ret = metric_utils.metrics2cols(metric)
|
|
|
|
return ret
|
2023-08-03 15:30:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Query(base.ObservabilityBaseCommand, lister.Lister):
|
2023-09-05 14:54:33 +02:00
|
|
|
"""Query prometheus with a custom query string."""
|
2023-08-03 15:30:19 +02:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super().get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
2023-09-05 14:54:33 +02:00
|
|
|
'query',
|
|
|
|
help=_("Custom PromQL query"))
|
2023-08-03 15:30:19 +02:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
client = metric_utils.get_client(self)
|
2023-10-31 05:31:58 -04:00
|
|
|
metric = client.query.query(parsed_args.query,
|
|
|
|
disable_rbac=parsed_args.disable_rbac)
|
2023-08-03 15:30:19 +02:00
|
|
|
ret = metric_utils.metrics2cols(metric)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
class Delete(base.ObservabilityBaseCommand):
|
2023-09-05 14:54:33 +02:00
|
|
|
"""Delete data for a selected series and time range."""
|
|
|
|
|
2023-08-03 15:30:19 +02:00
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super().get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
2023-09-05 14:54:33 +02:00
|
|
|
'matches',
|
|
|
|
action="append",
|
|
|
|
nargs='+',
|
|
|
|
help=_("Series selector, that selects the series to delete. "
|
|
|
|
"Specify multiple selectors delimited by space to "
|
|
|
|
"delete multiple series."))
|
2023-08-03 15:30:19 +02:00
|
|
|
parser.add_argument(
|
2023-09-05 14:54:33 +02:00
|
|
|
'--start',
|
|
|
|
help=_("Start timestamp in rfc3339 or unix timestamp. "
|
|
|
|
"Defaults to minimum possible timestamp."))
|
2023-08-03 15:30:19 +02:00
|
|
|
parser.add_argument(
|
2023-09-05 14:54:33 +02:00
|
|
|
'--end',
|
|
|
|
help=_("End timestamp in rfc3339 or unix timestamp. "
|
|
|
|
"Defaults to maximum possible timestamp."))
|
2023-08-03 15:30:19 +02:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
client = metric_utils.get_client(self)
|
2023-10-31 05:31:58 -04:00
|
|
|
return client.query.delete(parsed_args.matches,
|
|
|
|
parsed_args.start,
|
|
|
|
parsed_args.end)
|
2023-08-03 15:30:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CleanTombstones(base.ObservabilityBaseCommand):
|
2023-09-05 14:54:33 +02:00
|
|
|
"""Remove deleted data from disk and clean up the existing tombstones."""
|
|
|
|
|
2023-08-03 15:30:19 +02:00
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super().get_parser(prog_name)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
client = metric_utils.get_client(self)
|
|
|
|
return client.query.clean_tombstones()
|
|
|
|
|
|
|
|
|
|
|
|
class Snapshot(base.ObservabilityBaseCommand, lister.Lister):
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
client = metric_utils.get_client(self)
|
|
|
|
ret = client.query.snapshot()
|
|
|
|
return ["Snapshot file name"], [[ret]]
|