Skip to content
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

make sure _span_holder is reset #3716

Merged

Conversation

conglei
Copy link
Contributor

@conglei conglei commented Dec 3, 2024

Description

This PR addresses a bug where the _span_holder dictionary in the OpenTelemetryExtension class was shared across all instances due to being defined as a class-level attribute with a mutable default value. The fix is to reset the value of it during init, ensuring that each instance of the class has its own independent dictionary.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

The previous implementation led to unintended consequences:

  • Shared State: Modifications to _span_holder in one instance were visible to all other instances, causing data contamination, which messed up the traces.

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

Summary by Sourcery

Reset _span_holder in OpenTelemetryExtension during initialization to prevent shared state across instances and add tests to ensure correct initialization.

Bug Fixes:

  • Fix shared state issue in OpenTelemetryExtension by resetting _span_holder during initialization to ensure each instance has its own independent dictionary.

Tests:

  • Add tests to verify that _span_holder is correctly initialized as an empty dictionary for each instance of OpenTelemetryExtension and OpenTelemetryExtensionSync.

Copy link
Contributor

sourcery-ai bot commented Dec 3, 2024

Reviewer's Guide by Sourcery

This PR fixes a bug where the _span_holder dictionary was incorrectly defined as a class-level attribute with a mutable default value. The implementation moves the dictionary initialization to the __init__ method, ensuring each instance gets its own independent dictionary. New tests were added to verify the fix.

Updated class diagram for OpenTelemetryExtension

classDiagram
    class OpenTelemetryExtension {
        - Optional~ArgFilter~ _arg_filter
        - Dict~LifecycleStep, Span~ _span_holder
        - Tracer _tracer
        + OpenTelemetryExtension(arg_filter: Optional~ArgFilter~, execution_context: Optional~ExecutionContext~)
    }
    note for OpenTelemetryExtension "_span_holder is now initialized in the constructor to ensure instance-level separation."
Loading

File-Level Changes

Change Details Files
Changed _span_holder initialization from class-level to instance-level
  • Removed mutable default value from class-level _span_holder declaration
  • Added _span_holder initialization in init method
  • Declared _span_holder type annotation without default value
strawberry/extensions/tracing/opentelemetry.py
Added tests to verify _span_holder initialization behavior
  • Created test to verify empty initialization of _span_holder
  • Added test to ensure separate instances have independent _span_holder dictionaries
  • Included tests for both OpenTelemetryExtension and OpenTelemetryExtensionSync classes
tests/extensions/tracing/test_opentelemetry.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @conglei - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@botberry
Copy link
Member

botberry commented Dec 3, 2024

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


Description:
Fixed a bug in the OpenTelemetryExtension class where the _span_holder dictionary was incorrectly shared across all instances. This was caused by defining _span_holder as a class-level attribute with a mutable default value (dict()).

Here's the tweet text:

🆕 Release (next) is out! Thanks to Conglei for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

@botberry
Copy link
Member

botberry commented Dec 3, 2024

Hi, thanks for contributing to Strawberry 🍓!

We noticed that this PR is missing a RELEASE.md file. We use that to automatically do releases here on GitHub and, most importantly, to PyPI!

So as soon as this PR is merged, a release will be made 🚀.

Here's an example of RELEASE.md:

Release type: patch

Description of the changes, ideally with some examples, if adding a new feature.

Release type can be one of patch, minor or major. We use semver, so make sure to pick the appropriate type. If in doubt feel free to ask :)

Here's the tweet text:

🆕 Release (next) is out! Thanks to Conglei for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

Copy link

codspeed-hq bot commented Dec 3, 2024

CodSpeed Performance Report

Merging #3716 will not alter performance

Comparing conglei:conglei/fix-opentelemetry-bug (b1c01be) with main (2e4e3cc)

Summary

✅ 15 untouched benchmarks

@patrick91 patrick91 merged commit e5ee497 into strawberry-graphql:main Dec 3, 2024
110 of 111 checks passed
@botberry
Copy link
Member

botberry commented Dec 3, 2024

Thanks for contributing to Strawberry! 🎉 You've been invited to join
the Strawberry GraphQL organisation 😊

You can also request a free sticker by filling this form: https://forms.gle/dmnfQUPoY5gZbVT67

And don't forget to join our discord server: https://strawberry.rocks/discord 🔥

Copy link

codecov bot commented Dec 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.00%. Comparing base (2e4e3cc) to head (b1c01be).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3716      +/-   ##
==========================================
- Coverage   97.03%   97.00%   -0.03%     
==========================================
  Files         500      501       +1     
  Lines       33473    33490      +17     
  Branches     5592     5592              
==========================================
+ Hits        32480    32488       +8     
- Misses        789      791       +2     
- Partials      204      211       +7     

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants