Skip to content

feat: Add BaggageLogProcessor to opentelemetry-processor-baggage#4371

Merged
lzchen merged 13 commits into
open-telemetry:mainfrom
Manvi2402:add-baggage-log-processor
May 7, 2026
Merged

feat: Add BaggageLogProcessor to opentelemetry-processor-baggage#4371
lzchen merged 13 commits into
open-telemetry:mainfrom
Manvi2402:add-baggage-log-processor

Conversation

@Manvi2402

Copy link
Copy Markdown
Contributor

Description

Adds BaggageLogProcessor which reads entries stored in Baggage from the current context and adds the baggage entries' keys and values to the log record as attributes on emit.

This is analogous to the existing BaggageSpanProcessor but for logs.

Fixes #4062

Type of change

Please delete options that are not relevant.

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

How Has This Been Tested?

Added unit tests in test_baggage_log_processor.py:

  • test_check_the_baggage - verifies BaggageLogProcessor is instance of LogRecordProcessor
  • test_baggage_added_to_log_record - verifies baggage is added to log attributes
  • test_baggage_with_prefix - verifies predicate filtering with string prefix
  • test_baggage_with_regex - verifies predicate filtering with regex
  • test_no_baggage_not_added - verifies no baggage keys added when baggage is empty

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • [ x ] No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

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

@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

Thanks @Manvi2402 for submitting. I think this is a nice analog feature and iiuc shouldn't interfere with the Logging API stabilization efforts. I've left some comments.

Please could you also:

  • tox -e precommit
  • tox -e lint-processor-baggage

@tammy-baylis-swi tammy-baylis-swi moved this to Reviewed PRs that need fixes in Python PR digest Mar 30, 2026
@tammy-baylis-swi
tammy-baylis-swi requested a review from a team March 30, 2026 17:47
@Manvi2402
Manvi2402 force-pushed the add-baggage-log-processor branch from 176f8d3 to 3449a3d Compare March 31, 2026 03:17
@Manvi2402

Copy link
Copy Markdown
Contributor Author

Thanks for the review @tammy-baylis-swi . I've addressed all the comments in the latest commit 3449a3d

Added collision handling to avoid overwriting existing log record attributes.
Added max_baggage_attributes parameter to limit number of baggage attributes added.
Added support for multiple predicates via a sequence of predicates.

Comment thread CHANGELOG.md Outdated
@tammy-baylis-swi
tammy-baylis-swi requested a review from a team March 31, 2026 17:01
…s=128, multiple predicates test and README example
@Manvi2402

Copy link
Copy Markdown
Contributor Author

Thanks for the follow-up @tammy-baylis-swi I've addressed all the comments in the latest commit:

Added docstring to on_emit documenting collision handling behavior
Changed max_baggage_attributes default to 128 as per the spec
Added test_multiple_predicates test case
Updated README with multiple predicates example
Fixed CHANGELOG to link PR #4371 instead of issue

@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

Hi @Manvi2402 please add a bit more test coverage for configurable max_baggage_attributes then run these again:

  • tox -e precommit
  • tox -e lint-processor-baggage

@Manvi2402

Copy link
Copy Markdown
Contributor Author

Hi @tammy-baylis-swi , I've added the test for max_baggage_attributes. I tried running tox -e precommit and tox -e lint-processor-baggage locally but they fail on Windows because sh command is not available.

@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

Hi @tammy-baylis-swi , I've added the test for max_baggage_attributes. I tried running tox -e precommit and tox -e lint-processor-baggage locally but they fail on Windows because sh command is not available.

Thanks @Manvi2402 !

I'm not a Windows user myself but I think those tox commands should work if you use git bash or WSL. Could you give one of those a try?

@Manvi2402

Copy link
Copy Markdown
Contributor Author

Hi @tammy-baylis-swi , I ran tox -e precommit and it passed after ruff auto-fixed some formatting. I also fixed the pylint issues in log_processor.py (naming convention and staticmethod). The only remaining pylint warning is ALLOW_ALL_BAGGAGE_KEYS in the existing processor.py which was there before my changes.

@MikeGoldsmith MikeGoldsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for me. Thank you @Manvi2402.

@MikeGoldsmith MikeGoldsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Manvi2402 🚀

@MikeGoldsmith MikeGoldsmith changed the title Add BaggageLogProcessor to opentelemetry-processor-baggage feat: Add BaggageLogProcessor to opentelemetry-processor-baggage Apr 13, 2026
@MikeGoldsmith MikeGoldsmith moved this from Reviewed PRs that need fixes to Approved PRs in Python PR digest Apr 13, 2026
@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

Hi @tammy-baylis-swi , I ran tox -e precommit and it passed after ruff auto-fixed some formatting. I also fixed the pylint issues in log_processor.py (naming convention and staticmethod). The only remaining pylint warning is ALLOW_ALL_BAGGAGE_KEYS in the existing processor.py which was there before my changes.

Thanks @Manvi2402 , glad you got it working locally. Sorry to sound like a broken record but there's just a few more unsorted-imports and missing-newline-at-end-of-file to fix, ptal!

@Manvi2402

Copy link
Copy Markdown
Contributor Author

Hi @tammy-baylis-swi thank you for your patience and guidance. I've fixed the unsorted imports and missing newlines.

@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

Thanks @Manvi2402 ! And one more from the linting:

************* Module opentelemetry.processor.baggage.log_processor
opentelemetry-processor-baggage/src/opentelemetry/processor/baggage/log_processor.py:80:4: W0221: Number of parameters was 2 in 'LogRecordProcessor.force_flush' and is now 1 in overriding 'BaggageLogProcessor.force_flush' method (arguments-differ)

-----------------------------------
Your code has been rated at 9.96/10

lint-processor-baggage: exit 4 (4.71 seconds) /home/runner/work/opentelemetry-python-contrib/opentelemetry-python-contrib> sh -c 'cd processor && pylint --rcfile ../.pylintrc opentelemetry-processor-baggage' pid=2539
  lint-processor-baggage: FAIL code 4 (9.83=setup[5.11]+cmd[4.71] seconds)
  evaluation failed :( (10.42 seconds)

@tammy-baylis-swi tammy-baylis-swi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Thank you!

@github-actions

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Apr 30, 2026
lzchen added 3 commits May 7, 2026 14:24
Replaced Apache License comments with SPDX identifier.
Replaced Apache License header with SPDX identifier.
@lzchen
lzchen enabled auto-merge (squash) May 7, 2026 19:49
@lzchen
lzchen merged commit 605ffec into open-telemetry:main May 7, 2026
753 checks passed
@github-project-automation github-project-automation Bot moved this from Approved PRs to Done in Python PR digest May 7, 2026
@bschoenmaeckers

Copy link
Copy Markdown
Contributor

Thanks!

liustve added a commit to aws-observability/aws-otel-python-instrumentation that referenced this pull request Jun 17, 2026
Automated update of OpenTelemetry dependencies.

**Build Status:** ❌
[failure](https://github.com/aws-observability/aws-otel-python-instrumentation/actions/runs/27184408784)

**Updated versions:**
- [OpenTelemetry
Python](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.42.1):
1.42.1
- [OpenTelemetry
Contrib](https://github.com/open-telemetry/opentelemetry-python-contrib/releases/tag/v0.63b1):
0.63b1
-
[opentelemetry-sdk-extension-aws](https://pypi.org/project/opentelemetry-sdk-extension-aws/2.1.0/):
2.1.0
-
[opentelemetry-propagator-aws-xray](https://pypi.org/project/opentelemetry-propagator-aws-xray/1.0.2/):
1.0.2

**Upstream releases with breaking changes:**
Note: the mechanism to detect upstream breaking changes is not perfect.
Be sure to check all new releases and understand if any additional
changes need to be addressed.


**opentelemetry-python-contrib:**
- [Version
1.41.0/0.62b0](https://github.com/open-telemetry/opentelemetry-python-contrib/releases/tag/v0.62b0)

*Description of changes:*

- Drops the `opentelemetry-instrumentation-boto` runtime dependency
(legacy boto2 instrumentation), removed entirely upstream in:
open-telemetry/opentelemetry-python-contrib#4303

- Drops Python 3.9 support to match upstream's new minimum (Python
≥3.10): open-telemetry/opentelemetry-python#5076
(core) and
open-telemetry/opentelemetry-python-contrib#4412
(contrib)

- Switches `from importlib_metadata import version` to stdlib `from
importlib.metadata import version` after upstream dropped the
third-party `importlib-metadata` package from `opentelemetry-api`:
open-telemetry/opentelemetry-python#3234

- Refactors the botocore patches (`_botocore_patches.py`) to use the new
`ExtensionRegistry` API. Upstream split `_KNOWN_EXTENSIONS` into
`_BOTOCORE_EXTENSIONS` + `_AIOBOTOCORE_EXTENSIONS`, removed
`_find_extension`, and moved `_safe_invoke` to
`opentelemetry.instrumentation.botocore.utils` as part of adding
aiobotocore instrumentation:
open-telemetry/opentelemetry-python-contrib#4049

- Removes the Starlette ASGI `suppress_http_instrumentation` patch
(which the existing ADOT TODO already flagged) since the ASGI middleware
now natively honors `suppress_http_instrumentation` upstream:
open-telemetry/opentelemetry-python-contrib#4375

- Adapts the configurator tests to upstream's renamed
`BaggageSpanProcessor._baggage_key_predicate` → `_predicates` (now a
list of callables, after the constructor was reworked to accept either a
single predicate or a sequence):
open-telemetry/opentelemetry-python-contrib#4371

- Adapts `test_otlp_attribute_vs_body_types` to upstream's
`_VALID_ANY_VALUE_TYPES` switching `typing.Mapping`/`typing.Sequence` to
`collections.abc.Mapping`/`collections.abc.Sequence`:
open-telemetry/opentelemetry-python#5133

- Bumps the mysqlclient contract-test pin from `2.2.4` to `2.2.8` and
updates the matching `db.name` assertion. The 0.62b0 dbapi
semantic-convention migration
(open-telemetry/opentelemetry-python-contrib#4109)
added an empty-string filter inside `_set_db_name`, which exposed a
latent gap: mysqlclient < 2.2.7 didn't expose a Python-level `conn.db`
attribute (added upstream in
PyMySQL/mysqlclient#752, shipped in mysqlclient
2.2.7), so OTel's dbapi instrumentation defaulted `database` to `""` and
the empty value used to land on the span unconditionally. After 0.62b0
the empty value is dropped, so the assertion `db.name == ""` now fails
with "key not found". Bumping mysqlclient lets `conn.db` flow through as
the actual database name (`testdb`) so the assertion becomes meaningful.


By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.

---------

Co-authored-by: github-actions <[email protected]>
Co-authored-by: Steve Liu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Add baggage attributes to log records

5 participants