Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
DX Improvements from v1.8.1 release (#1421)
Browse files Browse the repository at this point in the history
* stop swallowing integrity errors on dataset update

* dont attach logging middleware if analytics opt_out is true

* handle diff correctly

* disable consider-using-f-string as we need this for logging statements to work effectively in fideslog
  • Loading branch information
Sean Preston authored Oct 4, 2022
1 parent 3c30339 commit da6fe5f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 33 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ src_paths = ["src", "tests"]
disable=[
"bad-option-value",
"broad-except",
"consider-using-f-string",
"dangerous-default-value",
"duplicate-code",
"fixme",
Expand Down
71 changes: 39 additions & 32 deletions src/fidesops/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,46 @@
)


@app.middleware("http")
async def dispatch_log_request(request: Request, call_next: Callable) -> Response:
"""
HTTP Middleware that logs analytics events for each call to Fidesops endpoints.
:param request: Request to fidesops api
:param call_next: Callable api endpoint
:return: Response
"""
fides_source: Optional[str] = request.headers.get("X-Fides-Source")
now: datetime = datetime.now(tz=timezone.utc)
endpoint = f"{request.method}: {request.url}"

try:
response = await call_next(request)
# HTTPExceptions are considered a handled err by default so are not thrown here.
# Accepted workaround is to inspect status code of response.
# More context- https://github.com/tiangolo/fastapi/issues/1840
response.background = BackgroundTask(
prepare_and_log_request,
endpoint,
request.url.hostname,
response.status_code,
now,
fides_source,
"HTTPException" if response.status_code >= 400 else None,
)
return response
if not config.root_user.analytics_opt_out:

@app.middleware("http")
async def dispatch_log_request(request: Request, call_next: Callable) -> Response:
"""
HTTP Middleware that logs analytics events for each call to Fidesops endpoints.
:param request: Request to fidesops api
:param call_next: Callable api endpoint
:return: Response
"""
fides_source: Optional[str] = request.headers.get("X-Fides-Source")
now: datetime = datetime.now(tz=timezone.utc)
endpoint = f"{request.method}: {request.url}"

except Exception as e:
await prepare_and_log_request(
endpoint, request.url.hostname, 500, now, fides_source, e.__class__.__name__
)
raise
try:
response = await call_next(request)
# HTTPExceptions are considered a handled err by default so are not thrown here.
# Accepted workaround is to inspect status code of response.
# More context- https://github.com/tiangolo/fastapi/issues/1840
response.background = BackgroundTask(
prepare_and_log_request,
endpoint,
request.url.hostname,
response.status_code,
now,
fides_source,
"HTTPException" if response.status_code >= 400 else None,
)
return response

except Exception as e:
await prepare_and_log_request(
endpoint,
request.url.hostname,
500,
now,
fides_source,
e.__class__.__name__,
)
raise


async def prepare_and_log_request(
Expand Down
15 changes: 14 additions & 1 deletion src/fidesops/ops/api/v1/endpoints/dataset_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from fastapi_pagination.bases import AbstractPage
from fastapi_pagination.ext.sqlalchemy import paginate
from pydantic import conlist
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
from starlette.status import (
HTTP_200_OK,
Expand Down Expand Up @@ -251,14 +252,26 @@ def create_or_update_dataset(
# Try to find an existing DatasetConfig matching the given connection & key
dataset_config = DatasetConfig.create_or_update(db, data=data)
created_or_updated.append(dataset_config.dataset)
except (SaaSConfigNotFoundException, ValidationError) as exception:
except (
SaaSConfigNotFoundException,
ValidationError,
) as exception:
logger.warning(exception.message)
failed.append(
BulkUpdateFailed(
message=exception.message,
data=data,
)
)
except IntegrityError:
message = "Dataset with key '%s' already exists." % data["fides_key"]
logger.warning(message)
failed.append(
BulkUpdateFailed(
message=message,
data=data,
)
)
except Exception:
logger.warning("Create/update failed for dataset '%s'.", data["fides_key"])
failed.append(
Expand Down

0 comments on commit da6fe5f

Please sign in to comment.