dynatrace-oss / OneAgent-SDK-Python-AutoInstrumentation

autodynatrace, a python library that implements automatic instrumentation using the OneAgent SDK for Python
Other
62 stars 28 forks source link

Django Wrapper AttributeError due to usage of functools.partial() #87

Closed avodaqstephan closed 3 months ago

avodaqstephan commented 1 year ago

Describe the bug

I came across an AttributeError due the usage of functools.partial() because of the missing view_func.__name__ attribute which is necessary in the little utils.py. functools.partial() generated functions don't have these argument.

Expected behavior Expected is at least some kind of error handling instead of breaking the application.

Log Message

2023-08-02 09:39:20,730 - ERROR - log - Internal Server Error: /api/openapi.json
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/exception.py", line 56, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = middleware_method(
  File "/usr/local/lib/python3.10/dist-packages/autodynatrace/wrappers/django/middlewares.py", line 49, in process_view
    name = func_name(view_func)
  File "/usr/local/lib/python3.10/dist-packages/autodynatrace/wrappers/utils.py", line 2, in func_name
    return view_func.__name__
AttributeError: 'functools.partial' object has no attribute '__name__'

Idea The name of the initial function can be found in the partial arguments partial.func.__name__. So a possible solution could be:

def func_name(view_func):
    try:
        name = view_func.__name__
    except AttributeError:
        try:
            name = view_func.func.__name__
        except AttributeError:
            name = "unknown_function"
    return name

I'm not sure if this is "generic" enough. Looking forward to your feedback.

Cheerio, Stephan

avodaqstephan commented 11 months ago

cautious bump

Any input is appreciated

avodaqstephan commented 11 months ago

@dlopes7 Please let me know, what is needed to proceed with that issue.