Skip to content

Commit

Permalink
feat: metricq_metric_option now allows multiple metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
devmaxde committed Oct 29, 2024
1 parent c895f86 commit 0c12525
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
7 changes: 4 additions & 3 deletions examples/metricq_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import metricq
from metricq import Metric
from metricq.cli import metricq_command
from metricq.cli.wrapper import metricq_metric_option
from metricq.logging import get_logger

logger = get_logger()
Expand Down Expand Up @@ -70,11 +71,11 @@ async def on_data(


@metricq_command(default_token="sink-py-dummy")
@click.option("-m", "--metrics", multiple=True, required=True)
def source(server: str, token: str, metrics: list[Metric]) -> None:
@metricq_metric_option(multiple=True)
def source(server: str, token: str, metric: list[Metric]) -> None:
# Initialize the DummySink class with a list of metrics given on the
# command line.
sink = DummySink(metrics=metrics, token=token, url=server)
sink = DummySink(metrics=metric, token=token, url=server)

# Run the sink. This call will block until the connection is closed.
sink.run()
Expand Down
3 changes: 2 additions & 1 deletion examples/metricq_synchronous_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
from metricq.cli import metricq_command
from metricq.logging import get_logger

logger = get_logger()


@metricq_command(default_token="source-py-dummy")
def synchronous_source(server: str, token: str) -> None:
logger = get_logger()
ssource = SynchronousSource(token=token, url=server)
ssource.declare_metrics(
{
Expand Down
2 changes: 2 additions & 0 deletions metricq/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
ChoiceParam,
CommandLineChoice,
DurationParam,
MetricParam,
TemplateStringParam,
TimestampParam,
)
Expand All @@ -18,6 +19,7 @@
"DurationParam",
"TemplateStringParam",
"TimestampParam",
"MetricParam",
"metricq_command",
"metricq_metric_option",
"metricq_server_option",
Expand Down
18 changes: 13 additions & 5 deletions metricq/cli/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,27 @@ def metricq_token_option(default: str) -> Callable[[FC], FC]:
)


def metricq_metric_option(default: Optional[str] = None) -> Callable[[FC], FC]:
def metricq_metric_option(
default: Optional[str] = None, multiple: bool = False, required: bool = False
) -> Callable[[FC], FC]:
response_default = default if (default is None or not multiple) else [default]
help = "Use the -–metric / -m parameter to specify which metric the program should use."
if multiple:
help += " Can be used multiple times to specify several metrics."

return option(
"--metric",
"-m",
type=MetricParam(),
metavar="METRIC",
show_default=True,
required=default is None,
default=default,
help="Use the -–metric parameter to specify which metric the program should use",
required=required,
default=response_default,
multiple=multiple,
help=help,
)



def get_metric_command_logger() -> logging.Logger:
logger = get_logger()
logger.setLevel(logging.WARNING)
Expand Down

0 comments on commit 0c12525

Please sign in to comment.