Skip to content

Commit 0985972

Browse files
author
Fredrik Mile
authored
Merge pull request #14 from vikinganalytics/disable_logging_flag
Added flag for ignoring logs for entrypoint
2 parents c61b370 + 3570a15 commit 0985972

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

daeploy/_service/service.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def entrypoint(
7777
func: Callable = None,
7878
method: str = "POST",
7979
monitor: bool = False,
80+
disable_http_logs: bool = False,
8081
**fastapi_kwargs,
8182
) -> Callable:
8283
"""Registers a function as an entrypoint, which will make it reachable
@@ -96,6 +97,11 @@ def my_function(arg1:type1) -> type2:
9697
method (str): HTTP method for entrypoint. Defauts to "POST"
9798
monitor (bool): Set if the input and output to this entrypoint should
9899
be saved to the service's monitoring database. Defaults to False.
100+
disable_http_logs (bool): Set if the http entry logs should be disabled for
101+
this entrypoint.
102+
These logs are genereated from uvicorn. Defaults to False.
103+
Example of http entry log:
104+
"POST /services/service_1.0.0/entrypoint_name HTTP/1.1" 200 OK
99105
**fastapi_kwargs: Keyword arguments for the resulting API endpoint.
100106
See FastAPI for keyword arguments of the ``FastAPI.api_route()``
101107
function.
@@ -167,6 +173,14 @@ async def wrapper(_request: Request, *args, **kwargs):
167173
wrapper
168174
)
169175

176+
if disable_http_logs:
177+
logging.getLogger("uvicorn.access").addFilter(
178+
# Add a space to the path to make sure that we
179+
# only filter out this entrypoints HTTP logs.
180+
lambda record: f"{path} "
181+
not in record.getMessage()
182+
)
183+
170184
# Wrap the original func in a pydantic validation wrapper and return that
171185
return validate_arguments(deco_func)
172186

daeploy/utilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def get_service_root_path() -> str:
7171
if name == UNKNOWN_NAME and version == UNKNOWN_VERSION:
7272
return ""
7373

74-
return f"/services/{name}_{version}/"
74+
return f"/services/{name}_{version}"
7575

7676

7777
def get_headers() -> dict:

tests/e2e_test/downstream/downstream.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,15 @@ def store_variable_vz_10_times():
7373
service.store(vz=i)
7474

7575

76+
@service.entrypoint(disable_http_logs=True)
77+
def http_logs():
78+
logger.info("This is a correct log!")
79+
80+
81+
@service.entrypoint(disable_http_logs=False)
82+
def http_logs_2():
83+
pass
84+
85+
7686
if __name__ == "__main__":
7787
service.run()

tests/e2e_test/e2e_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,23 @@ def test_reaching_daeploy_entrypoint_with_basemodel_args(
250250
}
251251

252252

253+
def test_disable_http_logs_in_entrypoint(
254+
dummy_manager, cli_auth_login, services, logs, headers
255+
):
256+
http_logs_url = "http://localhost/services/downstream/http_logs"
257+
http_no_logs_url = "http://localhost/services/downstream/http_logs_2"
258+
259+
# Call the entrypoint that allows http logs.
260+
requests.request("POST", url=http_logs_url, headers=headers)
261+
# Call the entrypoint that does not allow http logs.
262+
requests.request("POST", url=http_no_logs_url, headers=headers)
263+
logs = logs("downstream")
264+
# Check that the logs from inside the entrypoint are logged.
265+
assert "This is a correct log!" in logs
266+
assert '"POST /services/downstream_0.1.0/http_logs_2 HTTP/1.1" 200 OK' in logs
267+
assert '"POST /services/downstream_0.1.0/http_logs HTTP/1.1" 200 OK' not in logs
268+
269+
253270
def test_call_service_multiple_cases(
254271
dummy_manager, cli_auth_login, services, logs, headers
255272
):

tests/sdk_test/daeploy_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pandas as pd
1111
import pydantic
1212
import pytest
13+
import logging
1314
from fastapi.exceptions import FastAPIError
1415
from fastapi.testclient import TestClient
1516
from daeploy._service import db

0 commit comments

Comments
 (0)