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
1717from opentelemetry .baggage import get_all as get_all_baggage
1818from opentelemetry .sdk ._logs import LogRecordProcessor , ReadWriteLogRecord
1919
2020from opentelemetry .processor .baggage .processor import BaggageKeyPredicateT
2121
22- BaggageKeyPredicatesT = Union [BaggageKeyPredicateT , Sequence [BaggageKeyPredicateT ]]
22+ _BaggageKeyPredicatesT = Union [BaggageKeyPredicateT , Sequence [BaggageKeyPredicateT ]]
2323
2424
2525class 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+
0 commit comments