Skip to content

fix(grpc): Allow non-list interceptors #3520

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 7 commits into from
May 23, 2025
Merged

Conversation

sentrivana
Copy link
Contributor

@sentrivana sentrivana commented May 21, 2025

Description

The OpenTelemetry GRPC instrumentation assumes that server interceptors will be provided as a list -- it's using the .insert() method both in the sync and async server wrapper to add an interceptor of its own.

GRPC however states interceptors is just a Sequence(sync, async) and not necessarily a list, so we shouldn't assume that whatever gets passed in has an insert() method.

This originally surfaced in getsentry/sentry-python#4389. In case both Sentry and OTel are instrumenting GRPC, Sentry makes interceptors a tuple, which then throws an exception in OTel since tuples don't have an insert() method.

For good measure, I also applied this fix to channel interceptors (in the _add_interceptors wrapper).

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Added two test cases:

  • test_non_list_interceptors in instrumentation/opentelemetry-instrumentation-grpc/tests/test_aio_server_interceptor.py (async server interceptors)
  • test_non_list_interceptors in instrumentation/opentelemetry-instrumentation-grpc/tests/test_server_interceptor.py (sync server interceptors)

Undoing the changes in instrumentation/grpc/__init__.py and running the tests makes the new test cases fail.

The secure_channel and insecure_channel fix doesn't have a corresponding test case. I can add one if needed.

I also changed some of the existing tests to call uninstrument() in a finally if they call instrument() at the start. Not all of them would uninstrument if there was an exception, which was causing weird issues for me when sometimes things in other tests wouldn't be patched correctly.

Does This PR Require a Core Repo Change?

  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

The GRPC integration assumes that if interceptors are provided, they will be
a list, when GRPC itself types them as Sequence. With this change, we're
making the codepaths using interceptors more robust by explicitly turning them
into lists before manipulating them.
Copy link

linux-foundation-easycla bot commented May 21, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@sentrivana sentrivana marked this pull request as ready for review May 21, 2025 11:59
@sentrivana sentrivana requested a review from a team as a code owner May 21, 2025 11:59
@xrmx xrmx enabled auto-merge (squash) May 23, 2025 07:32
@xrmx xrmx merged commit f9d9f19 into open-telemetry:main May 23, 2025
720 checks passed
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