open-telemetry / open-telemetry/opentelemetry-python-contrib

grpc instrument throw exception if a user set interceptor as tuple type.

Open Beginner friendly
#2,780 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
4d 15h
Merged PRs (30d)
16

Description

Describe your environment

OS: (e.g, Ubuntu)
Python version: (e.g., Python 3.8.10)
Package version: (e.g., 0.46.0)

What happened?

The following method expects an interceptor of the list type, but some libraries pass interceptors as tuples. (Example: https://pypi.org/project/py-grpc-prometheus/)

I think it would be better to check type of change type as list.

    def _instrument(self, **kwargs):
        self._original_func = grpc.server
        tracer_provider = kwargs.get("tracer_provider")

        def server(*args, **kwargs):
            if "interceptors" in kwargs:
                # add our interceptor as the first
                kwargs["interceptors"].insert(
                    0,
                    server_interceptor(
                        tracer_provider=tracer_provider, filter_=self._filter
                    ),
                )
            else:
                kwargs["interceptors"] = [
                    server_interceptor(
                        tracer_provider=tracer_provider, filter_=self._filter
                    )
                ]
            return self._original_func(*args, **kwargs)

        grpc.server = server
Steps to Reproduce

You can download https://pypi.org/project/py-grpc-prometheus/.
And setup like the following.

server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),
interceptors=(PromServerInterceptor(),))

/site-packages/opentelemetry/instrumentation/grpc/__init__.py", line 338, in server
   kwargs["interceptors"].insert(
 AttributeError: 'tuple' object has no attribute 'insert'
Expected Result

Even though the user sends an interceptor as tuple, grpc instrument should work as expected.

Actual Result

throw error because tuple does not have insert attribute.

Additional context

No response

Would you like to implement a fix?

Yes

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in opentelemetry/instrumentation/grpc/init.py at the _instrument server wrapper around line 338, and reproduce the failure with a tuple-valued interceptors argument from py-grpc-prometheus. Confirm that grpc.server accepts the resulting interceptors and that the AttributeError no longer occurs when the interceptor is supplied as a tuple.

Written by the indexing model from the issue text.

Assessment

Tech stack
grpc, python
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.