Skip to content

SS-1250 Remove ip addresses from logs #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 10, 2025
Merged

Conversation

churnikov
Copy link
Contributor

Description

Remove ip addresses from logs.

This should work according to https://betterstack.com/community/guides/logging/structlog#installing-and-configuring-django-structlog

Checklist

If you're unsure about any of the items below, don't hesitate to ask. We're here to help!
This is simply a reminder of what we are going to look for before merging your code.

  • This pull request is against develop branch (not applicable for hotfixes)
  • I have included a link to the issue on GitHub or JIRA (if any)
  • I have included migration files (if there are changes to the model classes)
  • I have included, reviewed and executed tests (unit and end2end) to complement my changes
  • I have updated the related documentation (if necessary)
  • I have added a reviewer for this pull request
  • I have added myself as an author for this pull request
  • In the case I have modified settings.py, then I have also updated the studio-settings-configmap.yaml file in serve-charts

Further comments

Anything else you think we should know before merging your code!

@churnikov churnikov requested a review from Copilot July 1, 2025 14:14
@churnikov churnikov self-assigned this Jul 1, 2025
@churnikov churnikov requested a review from a team as a code owner July 1, 2025 14:14
@churnikov churnikov added the enhancement Improvement of existing feature or request label Jul 1, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR removes IP addresses from structured logs by clearing the ip field in request metadata.

  • Adds a signal handler to unset the ip context variable.
  • Registers the new signal handler in the app’s ready() method.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
portal/signals.py Introduce remove_ip_address handler to clear ip.
portal/apps.py Import portal.signals in AppsConfig.ready().
Comments suppressed due to low confidence (1)

portal/signals.py:7

  • [nitpick] Consider adding a unit test to verify that the ip field is indeed cleared from structured logs when this handler runs.
@receiver(bind_extra_request_metadata)



@receiver(bind_extra_request_metadata)
def remove_ip_address(request, logger, **kwargs):
Copy link
Preview

Copilot AI Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signal handler signature is missing the sender parameter. It should be defined as def remove_ip_address(sender, request, logger, **kwargs): so arguments are aligned correctly.

Suggested change
def remove_ip_address(request, logger, **kwargs):
def remove_ip_address(sender, request, logger, **kwargs):

Copilot uses AI. Check for mistakes.


@receiver(bind_extra_request_metadata)
def remove_ip_address(request, logger, **kwargs):
structlog.contextvars.bind_contextvars(ip=None)
Copy link
Preview

Copilot AI Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signal handlers for bind_extra_request_metadata should return a dict of metadata (e.g., return {"ip": None}) instead of only binding contextvars, otherwise the ip field may not be removed as intended.

Suggested change
structlog.contextvars.bind_contextvars(ip=None)
return {"ip": None}

Copilot uses AI. Check for mistakes.

@j-awada
Copy link
Contributor

j-awada commented Jul 8, 2025

Hi @churnikov, I just wondered if this means that no IP addresses will show up in the Loki logs after this change?

@churnikov
Copy link
Contributor Author

Hi @churnikov, I just wondered if this means that no IP addresses will show up in the Loki logs after this change?

@j-awada
They won't be visible in the studio logs, but they will still be visible in the ingress controller logs, so I don't think that this change will affect app stats api route that you've implemented, if you were concerned about it:)

@j-awada
Copy link
Contributor

j-awada commented Jul 10, 2025

Hi @churnikov, I just wondered if this means that no IP addresses will show up in the Loki logs after this change?

@j-awada They won't be visible in the studio logs, but they will still be visible in the ingress controller logs, so I don't think that this change will affect app stats api route that you've implemented, if you were concerned about it:)

That makes sense, thanks!

churnikov and others added 2 commits July 10, 2025 11:33
Add explicit flag for development logs

Signed-off-by: Nikita Churikov <[email protected]>
@churnikov
Copy link
Contributor Author

Please also review this related PR
ScilifelabDataCentre/serve-charts#100

@churnikov churnikov merged commit dac258e into develop Jul 10, 2025
2 checks passed
@churnikov churnikov deleted the SS-1250-remove-ip-from-logs branch July 10, 2025 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement of existing feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants