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

opentelemetry.sdk._logs.LoggingHandler doesn't format #3664

Closed
a-recknagel opened this issue Feb 2, 2024 · 0 comments · Fixed by #4166 · May be fixed by #3673
Closed

opentelemetry.sdk._logs.LoggingHandler doesn't format #3664

a-recknagel opened this issue Feb 2, 2024 · 0 comments · Fixed by #4166 · May be fixed by #3673
Labels
bug Something isn't working logging

Comments

@a-recknagel
Copy link

Describe your environment

$ pip freeze
Deprecated==1.2.14
importlib-metadata==6.11.0
opentelemetry-api==1.22.0
opentelemetry-sdk==1.22.0
opentelemetry-semantic-conventions==0.43b0
typing_extensions==4.9.0
wrapt==1.16.0
zipp==3.17.0

$ python --version
Python 3.10.10

Steps to reproduce

import logging.config

from opentelemetry.sdk._logs import LoggerProvider
from opentelemetry.sdk._logs.export import ConsoleLogExporter, SimpleLogRecordProcessor
from opentelemetry.sdk.resources import Resource

logger_provider = LoggerProvider(resource=Resource.create({}))
logger_provider.add_log_record_processor(SimpleLogRecordProcessor(ConsoleLogExporter()))

logging.config.dictConfig({
    "version": 1,
    "formatters": {
        "simple": {"format": "%(name)s [%(module)s.%(lineno)s]: %(message)s"}
    },
    "handlers": {
        "console": {
            "level": "INFO",
            "formatter": "simple",
            "class": "opentelemetry.sdk._logs.LoggingHandler",
            "logger_provider": logger_provider,
        }
    },
    "loggers": {
        "__main__": {"handlers": ["console"], "level": "INFO"},
    }
})

logging.getLogger("__main__").info("foo")

What is the expected behavior?

{
    "body": "__main__ [otel_logging_example.28]: foo",
    "severity_number": "<SeverityNumber.INFO: 9>",
    "severity_text": "INFO",
    "attributes": null,
    "dropped_attributes": 0,
    "timestamp": "2024-02-02T16:11:47.407470Z",
    "trace_id": "0x00000000000000000000000000000000",
    "span_id": "0x0000000000000000",
    "trace_flags": 0,
    "resource": "BoundedAttributes({'telemetry.sdk.language': 'python', 'telemetry.sdk.name': 'opentelemetry', 'telemetry.sdk.version': '1.22.0', 'service.name': 'unknown_service'}, maxlen=None)"
}

What is the actual behavior?

{
    "body": "foo",
    "severity_number": "<SeverityNumber.INFO: 9>",
    "severity_text": "INFO",
    "attributes": null,
    "dropped_attributes": 0,
    "timestamp": "2024-02-02T16:11:47.407470Z",
    "trace_id": "0x00000000000000000000000000000000",
    "span_id": "0x0000000000000000",
    "trace_flags": 0,
    "resource": "BoundedAttributes({'telemetry.sdk.language': 'python', 'telemetry.sdk.name': 'opentelemetry', 'telemetry.sdk.version': '1.22.0', 'service.name': 'unknown_service'}, maxlen=None)"
}

Additional context
Given that I want to make use of the fact that these logs are structured, it would actually be more convenient to put the fields that I'm formatting into body right now into attributes instead:

[...]
logging.config.dictConfig({
    "version": 1,
    "handlers": {
        "console": {
            "level": "INFO",
            "class": "opentelemetry.sdk._logs.LoggingHandler",
            "logger_provider": logger_provider,
            "carry_over": ["name", "module", "lineno"],  # doesn't exist yet
        }
    },
[...]

Would result in

{
    "body": "foo",
    "severity_number": "<SeverityNumber.INFO: 9>",
    "severity_text": "INFO",
    "attributes": {
        "name": "__main__",
        "module": "otel_logging_example",
        "lineno": 28
    },
    "dropped_attributes": 0,
    "timestamp": "2024-02-02T16:11:47.407470Z",
    "trace_id": "0x00000000000000000000000000000000",
    "span_id": "0x0000000000000000",
    "trace_flags": 0,
    "resource": "BoundedAttributes({'telemetry.sdk.language': 'python', 'telemetry.sdk.name': 'opentelemetry', 'telemetry.sdk.version': '1.22.0', 'service.name': 'unknown_service'}, maxlen=None)"
}

And raising an error if someone tries to add a formatter to LoggingHandler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working logging
Projects
Status: Done
2 participants