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

AttributeError in unpatch_handler_class when child class inherits from instrumented parent handler in opentelemetry-instrumentation-tornado #3072

Open
f-kanari opened this issue Dec 6, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@f-kanari
Copy link

f-kanari commented Dec 6, 2024

Describe your environment

Python version: Python 3.12
Package version:

  • opentelemetry-instrumentation-tornado: 0.49b2
  • tornado: 6.4.2

What happened?

OpenTelemetry Tornado instrumentation throws AttributeError when unpatch_handler_class is called on a child class after its parent class has been instrumented. This occurs due to inconsistent behavior between getattr and delattr when handling class variables in inheritance.

Steps to Reproduce

  1. Install dependencies:
python>=3.12
opentelemetry-instrumentation-tornado>=0.49b2
tornado>=6.4.2
  1. Create the following test code:
import tornado.web
import tornado.httputil
import unittest.mock
import opentelemetry.instrumentation.tornado


class Handler1(tornado.web.RequestHandler):
    pass


class Handler2(Handler1):
    def initialize(self):
        opentelemetry.instrumentation.tornado.unpatch_handler_class(type(self))


def main():
    opentelemetry.instrumentation.tornado.TornadoInstrumentor().instrument()
    app = tornado.web.Application()
    req = tornado.httputil.HTTPServerRequest(
        connection=tornado.http1connection.HTTP1Connection(
            unittest.mock.MagicMock(), False
        )
    )
    Handler1(app, req)  # Parent handler instantiation
    Handler2(app, req)  # Child handler instantiation
  1. Run the code

Expected Result

The code should execute without errors, properly handling the unpatching of the child handler class.

Actual Result

An AttributeError is raised:

AttributeError: type object 'Handler2' has no attribute '_otel_patched_key'

Additional context

The issue stems from inconsistent behavior in Python's attribute handling:

  1. getattr(cls, '_otel_patched_key') traverses the inheritance chain
  2. delattr(cls, '_otel_patched_key') only operates on the immediate class

The sequence causing the error:

  1. Parent class (Handler1) gets instrumented with _otel_patched_key
  2. Child class (Handler2) calls unpatch_handler_class
  3. getattr check passes (finds attribute in parent)
  4. delattr fails (can't find attribute in child)

Would you like to implement a fix?

None

@f-kanari f-kanari added the bug Something isn't working label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant