@@ -129,8 +129,9 @@ NextAddField:
129
129
}
130
130
131
131
// ExtractFields returns logging fields from the context.
132
- // Logging interceptor adds fields into context when used.
133
- // If there are no fields in the context, returns an empty Fields value.
132
+ // Fields can be added from the context using InjectFields. For example logging interceptor adds some (base) fields
133
+ // into context when used.
134
+ // If there are no fields in the context, it returns an empty Fields value.
134
135
// Extracted fields are useful to construct your own logger that has fields from gRPC interceptors.
135
136
func ExtractFields (ctx context.Context ) Fields {
136
137
t , ok := ctx .Value (fieldsCtxMarkerKey ).(Fields )
@@ -142,13 +143,14 @@ func ExtractFields(ctx context.Context) Fields {
142
143
return n
143
144
}
144
145
145
- // InjectFields allows adding fields to any existing Fields that will be used by the logging interceptor.
146
- // For explicitness, in case of duplicates, first field occurrence wins (immutability of fields). This also
147
- // applies to all fields created by logging middleware. It uses labels from this context as a base, so fields like "grpc.service"
148
- // can be overridden if your you add custom middleware that injects "grpc.service" before logging middleware injects those.
149
- // Don't overuse overriding to avoid surprises.
146
+ // InjectFields allows adding fields to any existing Fields that will be used by the logging interceptor or can be
147
+ // extracted further in ExtractFields.
148
+ // For explicitness, in case of duplicates, the newest field occurrence wins. This allows nested components to update
149
+ // popular fields like grpc.component (e.g. server invoking gRPC client).
150
+ //
151
+ // Don't overuse mutation of fields to avoid surprises.
150
152
func InjectFields (ctx context.Context , f Fields ) context.Context {
151
- return context .WithValue (ctx , fieldsCtxMarkerKey , ExtractFields (ctx ). WithUnique ( f ))
153
+ return context .WithValue (ctx , fieldsCtxMarkerKey , f . WithUnique ( ExtractFields (ctx )))
152
154
}
153
155
154
156
// InjectLogField is like InjectFields, just for one field.
0 commit comments