Skip to content

Add BaggageLogProcessor to opentelemetry-processor-baggage#4371

Open
Manvi2402 wants to merge 1 commit intoopen-telemetry:mainfrom
Manvi2402:add-baggage-log-processor
Open

Add BaggageLogProcessor to opentelemetry-processor-baggage#4371
Manvi2402 wants to merge 1 commit intoopen-telemetry:mainfrom
Manvi2402:add-baggage-log-processor

Conversation

@Manvi2402
Copy link
Copy Markdown

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


def on_emit(self, log_record: ReadWriteLogRecord) -> None:
baggage = get_all_baggage()
for key, value in baggage.items():
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.

It would be good to add collision handling here. It could be that a key in baggage is the same as a key added from stdlib logging, calls to logging.emit, or a user's custom LogRecordProcessor

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.

Thanks @Manvi2402 for adding that. Could this also be documented, either in the class or the function docstring?

baggage = get_all_baggage()
for key, value in baggage.items():
if self._baggage_key_predicate(key):
log_record.log_record.attributes[key] = value
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.

Could there also be a configurable limit on how many attributes are added? Logs can take up a lot of storage quickly

values will appear in all outgoing HTTP headers from the application.
"""

def __init__(self, baggage_key_predicate: BaggageKeyPredicateT) -> None:
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.

Could this support multiple predicates? For example if user wants an allow-list for more heterogeneous keys that don't all have the same prefix/suffix.

@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
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.


### Added
- Add `BaggageLogProcessor` to `opentelemetry-processor-baggage`
([#4062](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/4062))
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.

Nit: our convention is to link the PRs

Suggested change
([#4062](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/4062))
([#4371](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4371))

def __init__(
self,
baggage_key_predicate: BaggageKeyPredicatesT,
max_baggage_attributes: Optional[int] = None,
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.

The spec outlines 128 as a max for attributes though not specific to baggage. Maybe we could use this as a default?

)


class BaggageLogProcessorTest(unittest.TestCase):
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.

Please add another test case for supporting multiple predicates, and also update README with an example

@tammy-baylis-swi tammy-baylis-swi requested a review from a team March 31, 2026 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

Add baggage attributes to log records

2 participants