Skip to content

Logging API accepts optional Context at LogRecord init else get_current; deprecates trace_id, span_id, trace_flags #4597

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

Merged

Conversation

tammy-baylis-swi
Copy link
Contributor

@tammy-baylis-swi tammy-baylis-swi commented May 22, 2025

Description

  1. Updates Logging API and SDK to accept an additional, optional otel Context at LogRecord init. If not provided, current context is used.
  2. Adds order-of-precedence so that, if provided Context includes a valid current span, its span information will be used instead of any trace_id, span_id, trace_flags provided at init. Deprecates old LogRecord init and logs a warning (eb28a8d)
  3. LogRecord's to_json does a serialization of what it can in Context (e.g. string, int) then casts to string for other values (e.g. Span). Removed in 73e7b39 as discussed with Python SIG.
  4. LoggingHandler's _translate inits LogRecord with current context. Logging SDK (LogRecord init) defaults to current context in 676d9ff, as discussed with Python SIG.

Let me know if too many changes for one PR and I can break it up or remove anything out of scope.

Fixes #4328

Replaces #4584

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Added unit test coverage
  • Installed SDK changes into my vendor's distro and logs export ok

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@tammy-baylis-swi tammy-baylis-swi changed the title Logs API/SDK accepts additional, optional Context Logging API accepts optional Context with priority over trace_id etc, and LoggingHandler passes current Context May 24, 2025
@aabmass
Copy link
Member

aabmass commented Jun 5, 2025

Let's get one more approval and merge

@tammy-baylis-swi tammy-baylis-swi requested a review from emdneto June 11, 2025 16:38
@tammy-baylis-swi
Copy link
Contributor Author

tammy-baylis-swi commented Jun 11, 2025

Not sure why CI is failing; tox -e py313-test-opentelemetry-sdk is ok on my local

EDIT: The custom warning introduced in eb28a8d made tests fail, which I adjusted in 52a5e93

@tammy-baylis-swi tammy-baylis-swi changed the title Logging API accepts optional Context with priority over trace_id etc, and LoggingHandler passes current Context Logging API accepts optional Context at LogRecord init else get_current; deprecates trace_id, span_id, trace_flags Jun 11, 2025
@emdneto
Copy link
Member

emdneto commented Jun 12, 2025

Not sure why CI is failing; tox -e py313-test-opentelemetry-sdk is ok on my local

EDIT: The custom warning introduced in eb28a8d made tests fail, which I adjusted in 52a5e93

Facing the same issue in another PR

@tammy-baylis-swi
Copy link
Contributor Author

Facing the same issue in another PR

Ah and this new issue: #4630

Wdyt, shoud I revert 52a5e93 on this one?

@tammy-baylis-swi tammy-baylis-swi requested a review from emdneto June 12, 2025 15:23
@tammy-baylis-swi tammy-baylis-swi requested a review from xrmx June 13, 2025 16:59
@emdneto emdneto mentioned this pull request Jun 17, 2025
4 tasks
@xrmx xrmx merged commit 9a68c94 into open-telemetry:main Jun 17, 2025
353 checks passed
@lzchen lzchen mentioned this pull request Jun 25, 2025
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Logging API should accept Context instead of specific trace_id, span_id, trace_flags
4 participants