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

`LoggingInstrumentor` `log_hook` only runs when there is an active span

Open Beginner friendly
#5,069 0 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: Any
Python version: Any
Package version: 0.65b0

What happened?

I’m not sure if this is a bug in the code or in the docs, but in LoggingInstrumentor, the log_hook callback/argument only runs when there is an active span.

The docs currently imply that’s not the case by giving an example that checks for the existence of the span first: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/6fef5bc5ad3f5434f09790ae9ed40b9b6fc23fa4/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/__init__.py#L111-L131

…but the actual code only runs the hook if there is a valid span with a valid context: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/6fef5bc5ad3f5434f09790ae9ed40b9b6fc23fa4/instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/__init__.py#L186-L201

In my case, I was trying to use log_hook to add information about the logger name as an attribute on the log record (I am sending to a collector I don’t control that only looks at the individual log records and not the scope.name that arrives alongside those log records), but in many of my logs there isn’t an active span and so the hook never runs.

It would be nice if the hook ran for every log record, but I can understand if that’s not what’s intended. In that case, the description and example of log_hook in the docs could be a little clearer about this.

(I’d also love to hook LoggingHandler._translate() to better cover my particular use-case, but that’s probably a whole different discussion.)

Steps to Reproduce

Set up the logging instrumentor:

from logging import LogRecord
from opentelemetry.instrumentation.logging import LoggingInstrumentor
from opentelemetry.trace import Span

def decorate_log(span: Span, record: LogRecord) -> None:
    record.my_extra_attribute = 'Yeah!'

LoggingInstrumentor().instrument(
    log_code_attributes=True,
    inject_trace_context=True,
    log_hook=decorate_log,
)

…then call my_logger.info("hello") anywhere outside an active span.

Expected Result

The log_hook gets called with the new log record and None for the span. (Or the docs are more clear that this won’t happen.)

Actual Result

The log_hook does not get called.

Additional context

No response

Would you like to implement a fix?

None

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

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 instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/init.py, comparing the log_hook example around lines 111–131 with the implementation around lines 186–201. Confirm the behavior by reproducing a log outside an active span, then determine whether the intended fix is to invoke the hook for every record or clarify the documentation; done means the behavior and documentation agree and are covered by a regression check.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability-sre
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.