Skip to content

Commit e28ca6a

Browse files
authored
Adding disable_http_logs to add_parameter (#35)
* Adding disable_http_logs to add_parameter * Added changes * Fixing linters
1 parent 24cafab commit e28ca6a

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ All notable changes to this project will be documented in this file. This projec
99
- Added the option to deploy from a local docker image with the CLI.
1010
- Added the option to keep the docker image when killing services
1111
- Added links to service paths to the dashboard
12+
- Improved redirecting of urls after logging in
13+
- Added `disable_http_logs` argument to `service.add_parameter`
1214

1315
### Bugfixes
1416

daeploy/_service/service.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@
3636
logger = logging.getLogger(__name__)
3737

3838

39+
def _disable_http_logs(path: str):
40+
logging.getLogger("uvicorn.access").addFilter(
41+
# Add a space to the path to make sure that we
42+
# only filter out this entrypoints HTTP logs.
43+
lambda record: f"{path} "
44+
not in record.getMessage()
45+
)
46+
47+
3948
class _Service:
4049
def __init__(self):
4150
self.app = FastAPI(
@@ -180,12 +189,7 @@ async def wrapper(_request: Request, *args, **kwargs):
180189
)
181190

182191
if disable_http_logs:
183-
logging.getLogger("uvicorn.access").addFilter(
184-
# Add a space to the path to make sure that we
185-
# only filter out this entrypoints HTTP logs.
186-
lambda record: f"{path} "
187-
not in record.getMessage()
188-
)
192+
_disable_http_logs(path)
189193

190194
# Wrap the original func in a pydantic validation wrapper and return that
191195
return validate_arguments(deco_func)
@@ -319,6 +323,7 @@ def add_parameter(
319323
value: Any,
320324
expose: bool = True,
321325
monitor: bool = False,
326+
disable_http_logs: bool = False,
322327
):
323328
"""Adds a parameter to the parameter endpoints.
324329
@@ -329,8 +334,12 @@ def add_parameter(
329334
Defaults to True.
330335
monitor (bool): Stores updates to this parameter in the monitoring
331336
database if True. Will try to coerce non-numeric types to
332-
string Defaults to False.
337+
string. Defaults to False.
338+
disable_http_logs (bool): Disable logs when getting and
339+
setting paramter with API. Defaults to False
333340
"""
341+
path = f"/~parameters/{parameter}"
342+
334343
if isinstance(value, Number):
335344
value = float(value)
336345

@@ -342,6 +351,9 @@ def update_parameter(value: value.__class__) -> Any:
342351
self.store(**{parameter: value})
343352
return value
344353

354+
if disable_http_logs:
355+
_disable_http_logs(path)
356+
345357
# Set initial value
346358
self.parameters[parameter] = {
347359
"value": None,
@@ -353,7 +365,6 @@ def update_parameter(value: value.__class__) -> Any:
353365
def get_parameter():
354366
return self.parameters[parameter]["value"]
355367

356-
path = f"/~parameters/{parameter}"
357368
self.app.get(path, tags=["Parameters"])(get_parameter)
358369

359370
# Register POST endpoint for the new parameter

tests/e2e_test/downstream/downstream.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
logger = logging.getLogger(__name__)
1313

1414
service.add_parameter("greeting_phrase", "Hello")
15+
service.add_parameter("silent_log", True, disable_http_logs=True)
1516

1617

1718
@service.entrypoint

tests/e2e_test/e2e_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,18 @@ def test_call_service_multiple_cases(
374374
assert resp.status_code == 200
375375
assert resp.json() == "Get - Got - Gotten"
376376

377+
# Test disable_http_logs
378+
resp = requests.get(
379+
url="http://localhost/services/downstream/~parameters/silent_log",
380+
headers=headers,
381+
)
382+
assert resp.json() == True
383+
logs = requests.get(
384+
url="http://localhost/services/~logs?name=downstream&version=0.1.0",
385+
headers=headers,
386+
)
387+
assert "GET /services/downstream_0.1.0/~parameters/silent_log" not in logs
388+
377389

378390
def test_raised_notification_from_service_ends_up_at_manager(
379391
dummy_manager, cli_auth_login, services, headers

0 commit comments

Comments
 (0)