2020
2121from __future__ import annotations
2222
23- import argparse
2423import asyncio
2524import contextlib
2625import datetime
26+ from typing import Literal
2727
2828import fastapi
29- import garf_exporter
3029import prometheus_client
3130import requests
31+ import typer
3232import uvicorn
3333from garf_executors .entrypoints import utils as garf_utils
34+ from typing_extensions import Annotated
35+
36+ import garf_exporter
3437from garf_exporter import exporter_service
3538
39+ typer_app = typer .Typer ()
40+
3641
3742class GarfExporterError (Exception ):
3843 """Base class for GarfExporter errors."""
@@ -80,12 +85,6 @@ def healthcheck(host: str, port: int) -> bool:
8085metrics_app = prometheus_client .make_asgi_app (registry = exporter .registry )
8186app .mount ('/metrics' , metrics_app )
8287
83- logger = garf_utils .init_logging (
84- loglevel = 'INFO' ,
85- logger_type = 'rich' ,
86- name = 'garf-exporter' ,
87- )
88-
8988
9089async def start_metric_generation (
9190 request : exporter_service .GarfExporterRequest ,
@@ -129,44 +128,82 @@ def health(request: fastapi.Request):
129128 raise fastapi .HTTPException (status_code = 404 , detail = 'Not updated properly' )
130129
131130
132- def main () -> None : # noqa: D103
133- parser = argparse .ArgumentParser ()
134- parser .add_argument ('-s' , '--source' , dest = 'source' )
135- parser .add_argument ('-c' , '--config' , dest = 'config' , default = None )
136- parser .add_argument ('--log' , '--loglevel' , dest = 'loglevel' , default = 'info' )
137- parser .add_argument (
138- '--expose-type' ,
139- dest = 'expose_type' ,
140- choices = ['http' , 'pushgateway' ],
141- default = 'http' ,
142- )
143- parser .add_argument ('--host' , dest = 'host' , default = '0.0.0.0' )
144- parser .add_argument ('--port' , dest = 'port' , type = int , default = 8000 )
145- parser .add_argument ('--logger' , dest = 'logger' , default = 'local' )
146- parser .add_argument ('--iterations' , dest = 'iterations' , default = None , type = int )
147- parser .add_argument ('--delay-minutes' , dest = 'delay' , type = int , default = 15 )
148- parser .add_argument ('--namespace' , dest = 'namespace' , default = 'garf' )
149- parser .add_argument ('--max-parallel' , dest = 'parallel' , default = None )
150- parser .add_argument (
151- '--fetching-timeout-seconds' , dest = 'fetching_timeout' , default = 120 , type = int
131+ @typer_app .command (
132+ context_settings = {'allow_extra_args' : True , 'ignore_unknown_options' : True }
133+ )
134+ def main (
135+ ctx : typer .Context ,
136+ source : Annotated [
137+ str ,
138+ typer .Option (
139+ '-s' , '--source' , help = 'API alias' , prompt = 'Please add API alias'
140+ ),
141+ ],
142+ config : Annotated [
143+ str ,
144+ typer .Option (
145+ '-c' ,
146+ '--config' ,
147+ help = 'Path to configuration file' ,
148+ prompt = 'Please add path to config file.' ,
149+ ),
150+ ],
151+ expose_type : Annotated [
152+ str ,
153+ typer .Option (help = 'Type of metric expose' ),
154+ ] = 'http' ,
155+ loglevel : Annotated [
156+ str ,
157+ typer .Option (help = 'Level of logging' ),
158+ ] = 'INFO' ,
159+ logger : Annotated [
160+ str ,
161+ typer .Option (help = 'Type of logging' ),
162+ ] = 'rich' ,
163+ namespace : Annotated [
164+ str ,
165+ typer .Option (help = 'Namespace prefix for Prometheus' ),
166+ ] = 'garf' ,
167+ host : Annotated [
168+ str ,
169+ typer .Option (help = 'Host for exposing metrics' ),
170+ ] = '0.0.0.0' ,
171+ port : Annotated [
172+ int ,
173+ typer .Option (help = 'Port for exposing metrics' ),
174+ ] = 8000 ,
175+ delay_minutes : Annotated [
176+ int ,
177+ typer .Option (help = 'Delay in minutes between exports' ),
178+ ] = 15 ,
179+ fetching_timeout : Annotated [
180+ int ,
181+ typer .Option (help = 'Timeout in second for restarting stale exports' ),
182+ ] = 120 ,
183+ iterations : Annotated [
184+ int ,
185+ typer .Option (help = 'Stop export after N iterations' ),
186+ ] = 0 ,
187+ ) -> None :
188+ garf_utils .init_logging (
189+ loglevel = loglevel ,
190+ logger_type = logger ,
191+ name = 'garf-exporter' ,
152192 )
153- parser .add_argument ('--collectors' , dest = 'collectors' , default = 'default' )
154- parser .add_argument ('-v' , '--version' , dest = 'version' , action = 'store_true' )
155- args , kwargs = parser .parse_known_args ()
156- cli_parameters = garf_utils .ParamsParser (['macro' , 'source' ]).parse (kwargs )
193+ cli_parameters = garf_utils .ParamsParser (['macro' , 'source' ]).parse (ctx .args )
157194 runtime_options = exporter_service .GarfExporterRuntimeOptions (
158- expose_type = args . expose_type ,
159- host = args . host ,
160- port = args . port ,
161- namespace = args . namespace ,
162- fetching_timeout = args . fetching_timeout ,
163- iterations = args . iterations ,
164- delay_minutes = args . delay ,
195+ expose_type = expose_type ,
196+ host = host ,
197+ port = port ,
198+ namespace = namespace ,
199+ fetching_timeout = fetching_timeout ,
200+ iterations = iterations ,
201+ delay_minutes = delay_minutes ,
165202 )
166203 request = exporter_service .GarfExporterRequest (
167- source = args . source ,
204+ source = source ,
168205 source_parameters = cli_parameters .get ('source' ),
169- collectors_config = args . config ,
206+ collectors_config = config ,
170207 macros = cli_parameters .get ('macro' ),
171208 runtime_options = runtime_options ,
172209 )
@@ -187,4 +224,4 @@ async def start_uvicorn():
187224
188225
189226if __name__ == '__main__' :
190- main ()
227+ typer_app ()
0 commit comments