Repository navigation
Go SDK: stop log attributes from displacing standard log fields - #73820
Conversation
|
Nice work! |
jason810496
left a comment
There was a problem hiding this comment.
SocketLogHandler.Handle builds one map per log line and writes the four standard
fields into it. event and timestamp went in before the user attributes, so a
user attribute of the same name overwrote them. level and logger went in
after the attributes and were unaffected.
Would it make more sense to keep the current behavior that if user explicitly set the "event" field themself, we should just keep it instead of overriding with the timestamp.
For For That said, you have a point that a value is dropped either way. Preserving both would mean renaming the colliding attribute instead of overwriting it, maybe we can follow the same way (logrus prefixes such keys with |
A user attribute whose key collides with a standard field replaced that field instead of losing to it. "event" is the one that hurts: the supervisor takes a task's log line out of that key, so a Dag logging an attribute called "event" lost its message entirely, with nothing left in the line to show the message had ever existed. "timestamp" was displaced the same way, leaving the supervisor a value it cannot decode as a datetime. "level" and "logger" were already immune, for no reason other than being written after the attributes rather than before them.
8bd571c to
e66f518
Compare
jason810496
left a comment
There was a problem hiding this comment.
LGTM after adding the validation against the reserved fields, thanks for the fix!
Letting the standard field win the slot still discarded a value the caller asked to log. Reserving the slot and re-keying the attribute to "fields.<key>" keeps both: the supervisor finds the message in "event" and a parsable "timestamp", and the attribute survives under a name that collides with nothing. logrus resolves the same clash the same way. Rejecting the attribute outright, as structlog's TypeError does, is not available here. slog discards a handler's error, so returning one would drop the whole line, and a panic would surface in production rather than fail fast at the call site.
|
Just tried out the auto-merge feature, hope it works well :) |
Backport successfully created: v3-4-testNote: As of Merging PRs targeted for Airflow 3.X In matter of doubt please ask in #release-management Slack channel.
|
…fields (#73820) (#75564) * Go SDK: stop log attributes from displacing standard log fields A user attribute whose key collides with a standard field replaced that field instead of losing to it. "event" is the one that hurts: the supervisor takes a task's log line out of that key, so a Dag logging an attribute called "event" lost its message entirely, with nothing left in the line to show the message had ever existed. "timestamp" was displaced the same way, leaving the supervisor a value it cannot decode as a datetime. "level" and "logger" were already immune, for no reason other than being written after the attributes rather than before them. * Move a clashing log attribute aside instead of overwriting it Letting the standard field win the slot still discarded a value the caller asked to log. Reserving the slot and re-keying the attribute to "fields.<key>" keeps both: the supervisor finds the message in "event" and a parsable "timestamp", and the attribute survives under a name that collides with nothing. logrus resolves the same clash the same way. Rejecting the attribute outright, as structlog's TypeError does, is not available here. slog discards a handler's error, so returning one would drop the whole line, and a panic would surface in production rather than fail fast at the call site. (cherry picked from commit 9761167) Co-authored-by: Henry Chen <[email protected]>
Why
SocketLogHandler.Handlebuilds one map per log line and writes the fourstandard fields into it.
eventandtimestampwent in before the userattributes, so a user attribute of the same name overwrote them.
levelandloggerwent in after the attributes and were unaffected.eventcarries the message. The supervisor reads a task's log line out of thatkey (
msg = event.pop("event", None)), so a Dag logging an attribute calledeventlost its message entirely, with nothing left in the emitted line toshow that a message had ever existed:
A displaced
timestampno longer breaks the supervisor — #74783 moves anunparseable value to
invalid_timestamp— but the line still loses the time itwas emitted at, and is stamped with the time the supervisor received it instead.
Letting the standard field simply win the slot would still discard a value the
caller asked to log. So on a clash the attribute is moved rather than dropped,
the way logrus resolves the same collision.
Rejecting the attribute outright, as structlog's
TypeErrordoes, is notavailable here:
slogdiscards a handler'serror(
_ = l.Handler().Handle(ctx, r)), so returning one would drop the whole line,and a
panicwould surface in production rather than fail fast at the callsite.
What
go-sdk/pkg/execution/logger.go—moveClashingAttrsre-keys an attributeoccupying a standard field's slot to
fields.<key>before the standardfields are written, so both values survive. The reserved keys are
event,timestamp,levelandlogger. Only the exact top-level key is reserved,so a group's
grp.eventis left alone.go-sdk/pkg/execution/logger_test.go—TestSocketLogHandlerMovesAttrsClashingWithStandardFieldscovers all fourreserved keys, asserting the standard field's value, the attribute's value
under
fields.<key>, and that the reserved slot no longer holds theattribute.
TestSocketLogHandlerStandardFieldsOverrideAttrsadditionallypins where the displaced
levelandloggervalues go.Was generative AI tooling used to co-author this PR?