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

Invalid type WSGIRequest for attribute 'request' value opentelemetry #2808

Open
saichander17 opened this issue Aug 21, 2024 · 2 comments
Open
Labels
bug Something isn't working

Comments

@saichander17
Copy link

Describe your environment

OS: (Alpine Docker)
Python version: (Python 3.8)
SDK version: (e.g., 1.22.0)
API version: (e.g., 1.22.0)

What happened?

Invalid type WSGIRequest for attribute 'request' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types

I'm getting this warning when I integrated opentelemetry in my Django application. I don't know if it's a bug or I'm doing something wrong. Has anyone encountered this previously?

Steps to Reproduce

Added these to my requirements

opentelemetry-distro==0.43b0
opentelemetry-exporter-otlp==1.22.0

Added the following to my Dockerfile

RUN opentelemetry-bootstrap --action=install
ENV DJANGO_SETTINGS_MODULE myapp.settings
CMD OTEL_RESOURCE_ATTRIBUTES=service.name=testing_app OTEL_EXPORTER_OTLP_ENDPOINT="http://<IP>:4317" OTEL_EXPORTER_OTLP_PROTOCOL=grpc opentelemetry-instrument gunicorn myapp.wsgi:application -c gunicorn.conf.py

Expected Result

I shouldn't get that warning? I'm not sure here!

Actual Result

I'm getting the warning
Invalid type WSGIRequest for attribute 'request' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types

Additional context

I raised the issue first in opentelemtry-python but was suggested that I raise it here.

Would you like to implement a fix?

None

@jeremydvoss
Copy link
Contributor

I have not been able to reproduce this. Could you provide a greater list of dependency versions for the docker container. For instance:
opentelemetry-instrumentation-django
gunicorn

Consider providing a full repro (Dockerfile + dependencies)

@Okan0
Copy link

Okan0 commented Sep 19, 2024

The problem is reproducable if the opentelemetry Logger is used as the root logger in Django:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": (
                f"%(levelname)s [trace_id=%(otelTraceID)s span_id=%(otelSpanID)s] "
                "%(asctime)s %(pathname)s %(process)d %(thread)d %(message)s"
            )
        },
        "simple": {
            "format": (f"%(levelname)s [trace_id=%(otelTraceID)s span_id=%(otelSpanID)s]" "%(asctime)s %(message)s")
        },
    },
    "handlers": {
        # [...]
        "opentelemetry": {
            "class": "opentelemetry.sdk._logs.LoggingHandler"
        },
    },
    "loggers": {
        #  [...]
    },
    "root": {
        "handlers": [
            #[...]
            "opentelemetry",
        ],
        "level": "INFO",
    },
}

I fixed it by defining a subclass of the original LoggingHandler and use that class for the otlp handler instead:

from logging import LogRecord

from opentelemetry.sdk._logs import LoggingHandler as OpenTelemetryLoggingHandler


class LoggingHandler(OpenTelemetryLoggingHandler):
    @staticmethod
    def _get_attributes(record: LogRecord):
        attributes = OpenTelemetryLoggingHandler._get_attributes(record)
        if "request" in attributes:
            attributes["request"] = f'{attributes["request"].method} {attributes["request"].path}'
        return attributes

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

3 participants