Skip to content

Feature/expand sentry boilerplate#19

Closed
StianHanssen wants to merge 5 commits into
mbdevpl:mainfrom
StianHanssen:feature/expand_sentry_boilerplate
Closed

Feature/expand sentry boilerplate#19
StianHanssen wants to merge 5 commits into
mbdevpl:mainfrom
StianHanssen:feature/expand_sentry_boilerplate

Conversation

@StianHanssen

@StianHanssen StianHanssen commented Apr 8, 2025

Copy link
Copy Markdown
Contributor

Changes made:

  • Expose some more parameters for versatility.
    • I used the default parameters expected by the underlying sentry init method.
    • The following parameters were added:
      • debug
      • attach_stacktrace
      • send_default_pii
      • default_integrations
  • Add ability to set tags in global scope.
    • This allows for an easy way to set scope tags on initialisation.

@jenkins-mbdev

jenkins-mbdev Bot commented Apr 8, 2025

Copy link
Copy Markdown

Tools report for GitHub-mbdevpl/python-boilerplates/PR-19 [build #1]:

  • Pylint: ran 🆗

  • Mypy: ran 🆗

  • Flake518: ran 🆗

  • Pydocstyle: ran 🆗

  • Coverage: 99% 🆗 (click for details)
    Name                              Stmts   Miss Branch BrPart  Cover   Missing
    -----------------------------------------------------------------------------
    boilerplates/__init__.py              2      0      0      0   100%
    boilerplates/cli.py                  52      0     10      0   100%
    boilerplates/config.py               17      0      4      0   100%
    boilerplates/git_repo_tests.py       59      0     10      0   100%
    boilerplates/logging.py             124      0     20      0   100%
    boilerplates/packaging_tests.py     104      0      8      0   100%
    boilerplates/sentry.py               50      3      4      0    91%   83-85
    boilerplates/setup.py               190      0     58      0   100%
    setup.py                             32      9      6      1    68%   18-26
    test/__init__.py                      4      0      0      0   100%
    test/test_cli.py                     68      0     12      0   100%
    test/test_config.py                  31      0      0      0   100%
    test/test_git_repo.py                21      0      0      0   100%
    test/test_logging.py                 89      0      6      0   100%
    test/test_packaging.py                5      0      0      0   100%
    test/test_sentry.py                  26      0      0      0   100%
    test/test_setup.py                  147      0     16      0   100%
    -----------------------------------------------------------------------------
    TOTAL                              1021     12    154      1    99%
    

@codecov

codecov Bot commented Apr 8, 2025

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 81.25000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 98.7%. Comparing base (2de1826) to head (208b2b0).

Files with missing lines Patch % Lines
boilerplates/sentry.py 81.2% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main     #19     +/-   ##
========================================
- Coverage   100.0%   98.7%   -1.3%     
========================================
  Files          17      17             
  Lines        1005    1021     +16     
  Branches       76      77      +1     
========================================
+ Hits         1005    1008      +3     
- Misses          0      12     +12     
- Partials        0       1      +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread boilerplates/sentry.py

debug: bool = False
attach_stacktrace: bool = False
shutdown_timeout: float = 2.0

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

https://docs.sentry.io/platforms/python/configuration/options/#shutdown_timeout

according to Sentry docs, the timeout variable type is int -- so not sure if specifying decimal places would even work.

@StianHanssen StianHanssen Apr 8, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the documentation is not in sync with the code on this one. It is actually float if you check the code type definition: https://github.com/getsentry/sentry-python/blob/adcfa0f6abf8850f3b007bde609d0f943f621786/sentry_sdk/consts.py#L506

Comment thread boilerplates/sentry.py Outdated
Comment on lines +80 to +83
@classmethod
def remove_tags(cls, tags: t.List[str]):
"""Remove tags from the current scope."""
with sentry_sdk.configure_scope() as scope:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Maybe this method can be removed as it's not used or tested.

Is there any use case when one would like to remove the tags once they are set?

@StianHanssen StianHanssen Apr 8, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think there was justification for this method initially, but with the comment below, the wrapper class function does nothing more than calling the sentry function, at which point it is pretty redundant. So I have removed it.

Comment thread boilerplates/sentry.py Outdated
Comment on lines +74 to +78
@classmethod
def set_tags(cls, tags: t.Dict[str, str]):
"""Set tags for the current scope."""
with sentry_sdk.configure_scope() as scope:
scope.set_tags(tags)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

https://docs.sentry.io/platforms/python/migration/1.x-to-2.x#scope-configuring

According to Sentry docs, the sentry_sdk.configure_scope is deprecated -- also, I think if there's no specific scope here which we're creating, and Sentry docs suggest in such case to use global-level API methods, see here: https://docs.sentry.io/platforms/python/enriching-events/scopes/

I wonder if the code for setting tags could be added to the init method directly, in something like (in init() method directly) would suffice. Or even just set_tags without the if as the default value is empty dict.

if cls.tags:
    sentry_sdk.set_tags(cls.tags)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I removed the configure scope option, and seeing as the function just becomes a wrapper of another function, I removed the tag handling class functions.

I still set tags in init. I check that it would be safe to just feed the cls.tags variable with default {}, that will result in no-op.

@StianHanssen StianHanssen requested a review from mbdevpl April 8, 2025 05:42
@mbdevpl

mbdevpl commented Apr 22, 2025

Copy link
Copy Markdown
Owner

@StianHanssen Unfortunately, seems that CI is not cooperating on this contributed PR and I can't easily fix it. So, I've made a refactored version of this PR that includes all changes from here, plus some extras like adding you as a contributor. I'm closing this, please see the refactored PR:

@mbdevpl mbdevpl closed this Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants