Skip to content

Add baggage attributes to log records #4062

@bschoenmaeckers

Description

@bschoenmaeckers

What problem do you want to solve?

Currently only a couple of trace attributes are added to log records.

record.otelSpanID = "0"
record.otelTraceID = "0"
record.otelTraceSampled = False
nonlocal service_name
if service_name is None:
resource = getattr(provider, "resource", None)
if resource:
service_name = (
resource.attributes.get("service.name") or ""
)
else:
service_name = ""
record.otelServiceName = service_name

I would love to see baggage attributes added as well.

Describe the solution you'd like

I have 2 solutions in mind:

Add all baggage attributes by default

Like the attributes above, add them all by default. I'm not sure if this is desirable but it is convenient.

Add a LogRecordProcessor

This option only adds the attributes to the opentelemetry LogRecord. So it is not usable in other log handlers.

class BaggageLogProcessor(LogRecordProcessor):
    def __init__(self, baggage_key_predicate: BaggageKeyPredicateT) -> None:
        self._baggage_key_predicate = baggage_key_predicate

    def on_emit(self, log_record: ReadWriteLogRecord):
        baggage = get_all_baggage()
        for key, value in baggage.items():
            if self._baggage_key_predicate(key):
                log_record.log_record.attributes[key] = value

    def shutdown(self):
        pass

Describe alternatives you've considered

No response

Additional Context

No response

Would you like to implement a fix?

None

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions