Skip to content

Commit 391c2dd

Browse files
committed
Address review comments: add docstring, default max_baggage_attributes=128, multiple predicates test and README example
1 parent 3449a3d commit 391c2dd

4 files changed

Lines changed: 52 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Added
1515
- Add `BaggageLogProcessor` to `opentelemetry-processor-baggage`
16-
([#4062](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/4062))
16+
([#4371](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4371))
1717

1818
- `opentelemetry-instrumentation-confluent-kafka`: Loosen confluent-kafka upper bound to <3.0.0
1919
([#4289](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4289))

processor/opentelemetry-processor-baggage/README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ For example, to only copy baggage entries that match the regex `^key.+`:
101101
regex_predicate = lambda baggage_key: re.match(r"^key.+", baggage_key) is not None
102102
logger_provider.add_log_record_processor(BaggageLogProcessor(regex_predicate))
103103

104+
For example, to copy baggage entries matching multiple predicates:
105+
106+
::
107+
108+
multiple_predicates = [
109+
lambda baggage_key: baggage_key.startswith("my-key"),
110+
lambda baggage_key: baggage_key.startswith("other-key"),
111+
]
112+
logger_provider.add_log_record_processor(BaggageLogProcessor(multiple_predicates))
113+
104114
References
105115
----------
106116
* `OpenTelemetry Project <https://opentelemetry.io/>`_

processor/opentelemetry-processor-baggage/src/opentelemetry/processor/baggage/log_processor.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Callable, Optional, Sequence, Union
15+
from typing import Sequence, Union
1616

1717
from opentelemetry.baggage import get_all as get_all_baggage
1818
from opentelemetry.sdk._logs import LogRecordProcessor, ReadWriteLogRecord
1919

2020
from opentelemetry.processor.baggage.processor import BaggageKeyPredicateT
2121

22-
BaggageKeyPredicatesT = Union[BaggageKeyPredicateT, Sequence[BaggageKeyPredicateT]]
22+
_BaggageKeyPredicatesT = Union[BaggageKeyPredicateT, Sequence[BaggageKeyPredicateT]]
2323

2424

2525
class BaggageLogProcessor(LogRecordProcessor):
@@ -40,8 +40,8 @@ class BaggageLogProcessor(LogRecordProcessor):
4040

4141
def __init__(
4242
self,
43-
baggage_key_predicate: BaggageKeyPredicatesT,
44-
max_baggage_attributes: Optional[int] = None,
43+
baggage_key_predicate: _BaggageKeyPredicatesT,
44+
max_baggage_attributes: int = 128,
4545
) -> None:
4646
if callable(baggage_key_predicate):
4747
self._predicates = [baggage_key_predicate]
@@ -53,10 +53,19 @@ def _matches(self, key: str) -> bool:
5353
return any(predicate(key) for predicate in self._predicates)
5454

5555
def on_emit(self, log_record: ReadWriteLogRecord) -> None:
56+
"""Add baggage entries as log record attributes on emit.
57+
58+
Baggage keys are filtered using the provided predicate(s).
59+
If a baggage key already exists in the log record attributes,
60+
it will not be overwritten to avoid collisions with attributes
61+
added by stdlib logging, calls to logging.emit, or custom
62+
LogRecordProcessors. At most max_baggage_attributes baggage
63+
entries will be added.
64+
"""
5665
baggage = get_all_baggage()
5766
count = 0
5867
for key, value in baggage.items():
59-
if self._max_baggage_attributes is not None and count >= self._max_baggage_attributes:
68+
if count >= self._max_baggage_attributes:
6069
break
6170
if self._matches(key):
6271
if key not in log_record.log_record.attributes:
@@ -67,4 +76,5 @@ def shutdown(self) -> None:
6776
pass
6877

6978
def force_flush(self, timeout_millis: int = 30000) -> bool:
70-
return True
79+
return True
80+

processor/opentelemetry-processor-baggage/tests/test_baggage_log_processor.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,31 @@ def has_prefix(baggage_key: str) -> bool:
111111
@staticmethod
112112
def matches_regex(baggage_key: str) -> bool:
113113
return re.match(r"que.*", baggage_key) is not None
114+
def test_multiple_predicates(self):
115+
token1 = attach(set_baggage("queen", "bee"))
116+
token2 = attach(set_baggage("king", "cobra"))
117+
logger_provider = LoggerProvider()
118+
logger_provider.add_log_record_processor(
119+
BaggageLogProcessor([
120+
lambda key: key.startswith("que"),
121+
lambda key: key.startswith("kin"),
122+
])
123+
)
124+
exporter = InMemoryLogRecordExporter()
125+
logger_provider.add_log_record_processor(
126+
BatchLogRecordProcessor(exporter)
127+
)
128+
logger = logger_provider.get_logger("test-logger")
129+
logger.emit(None)
130+
logger_provider.force_flush()
131+
logs = exporter.get_finished_logs()
132+
attributes = logs[-1].log_record.attributes
133+
self.assertEqual(attributes.get("queen"), "bee")
134+
self.assertEqual(attributes.get("king"), "cobra")
135+
detach(token2)
136+
detach(token1)
114137

115138

116139
if __name__ == "__main__":
117-
unittest.main()
140+
unittest.main()
141+

0 commit comments

Comments
 (0)