diff --git a/interceptors/logging/interceptors.go b/interceptors/logging/interceptors.go index ce932a10f..c073d34a7 100644 --- a/interceptors/logging/interceptors.go +++ b/interceptors/logging/interceptors.go @@ -146,8 +146,9 @@ func reportable(logger Logger, opts *options) interceptors.CommonReportableFunc fields = fields.WithUnique(customCommonFields(kind, c, opts.grpcLogFields())) } else { // Field dups from context override the common fields. - fields = newCommonFields(kind, c).WithUnique(ExtractFields(ctx)) + fields = newCommonFields(kind, c) } + fields = fields.WithUnique(ExtractFields(ctx)) if !c.IsClient { if peer, ok := peer.FromContext(ctx); ok { diff --git a/interceptors/logging/logging.go b/interceptors/logging/logging.go index 53eb1ce20..5f0ce05f1 100644 --- a/interceptors/logging/logging.go +++ b/interceptors/logging/logging.go @@ -41,14 +41,17 @@ func newCommonFields(kind string, c interceptors.CallMeta) Fields { func customCommonFields(kind string, c interceptors.CallMeta, customFields []string) Fields { commonFields := newCommonFields(kind, c) + existing := map[any]any{} + i := commonFields.Iterator() + for i.Next() { + k, v := i.At() + existing[k] = v + } + newFields := Fields{} for _, key := range customFields { - commonFieldIterator := commonFields.Iterator() - for commonFieldIterator.Next() { - ck, cv := commonFieldIterator.At() - if ck == key { - newFields = append(newFields, ck, cv) - } + if _, ok := existing[key]; ok { + newFields = append(newFields, key, existing[key]) } } return newFields