From e16a9464f1558a1a31d5e3bdda00e6ee99676741 Mon Sep 17 00:00:00 2001 From: chbndrhnns Date: Tue, 2 Mar 2021 14:15:07 +0100 Subject: [PATCH] feat: Allow truthy values for env variable --- prometheus_fastapi_instrumentator/instrumentation.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/prometheus_fastapi_instrumentator/instrumentation.py b/prometheus_fastapi_instrumentator/instrumentation.py index a76fa08..ae083fb 100644 --- a/prometheus_fastapi_instrumentator/instrumentation.py +++ b/prometheus_fastapi_instrumentator/instrumentation.py @@ -119,7 +119,7 @@ def instrument(self, app: FastAPI): if ( self.should_respect_env_var - and os.environ.get(self.env_var_name, "false") != "true" + and not self._should_instrumentate() ): return self @@ -242,7 +242,7 @@ def expose( if ( self.should_respect_env_var - and os.environ.get(self.env_var_name, "false") != "true" + and not self._should_instrumentate() ): return self @@ -324,6 +324,10 @@ def _get_handler(self, request: Request) -> Tuple[str, bool]: # ========================================================================== + def _should_instrumentate(self): + """Check if the environment variable is set to a truthy value""" + return os.getenv(self.env_var, 'False').lower() in ['true', '1'] + def _is_handler_excluded(self, handler: str, is_templated: bool) -> bool: """Determines if the handler should be ignored.