Skip to content

Commit bea7c0e

Browse files
logging: store the propagated context in the reporter (#740)
Signed-off-by: Olivier Cano <ocano@scaleway.com>
1 parent 3ce33cf commit bea7c0e

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

interceptors/logging/interceptors.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ func reportable(logger Logger, opts *options) interceptors.CommonReportableFunc
187187
if d, ok := ctx.Deadline(); ok {
188188
singleUseFields = singleUseFields.AppendUnique(Fields{"grpc.request.deadline", d.Format(opts.timestampFormat)})
189189
}
190+
ctx = InjectFields(ctx, fields)
190191
return &reporter{
191192
CallMeta: c,
192193
ctx: ctx,
@@ -195,7 +196,7 @@ func reportable(logger Logger, opts *options) interceptors.CommonReportableFunc
195196
fields: fields.WithUnique(singleUseFields),
196197
logger: logger,
197198
kind: kind,
198-
}, InjectFields(ctx, fields)
199+
}, ctx
199200
}
200201
}
201202

interceptors/logging/interceptors_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,12 @@ func TestSuite(t *testing.T) {
193193
&baseLoggingSuite{
194194
logger: newMockLogger(),
195195
InterceptorTestSuite: &testpb.InterceptorTestSuite{
196-
TestService: &testpb.TestPingService{},
196+
TestService: &testpb.TestPingService{
197+
PingFunc: func(ctx context.Context) {
198+
// Modify the fields already in the context.
199+
logging.AddFields(ctx, logging.Fields{"business-field", "baguette"})
200+
},
201+
},
197202
},
198203
},
199204
}
@@ -255,7 +260,8 @@ func (s *loggingClientServerSuite) TestPing() {
255260
AssertFieldNotEmpty(s.T(), "grpc.start_time").
256261
AssertFieldNotEmpty(s.T(), "grpc.request.deadline").
257262
AssertField(s.T(), "grpc.code", "OK").
258-
AssertFieldNotEmpty(s.T(), "grpc.time_ms").AssertNoMoreTags(s.T())
263+
AssertFieldNotEmpty(s.T(), "grpc.time_ms").
264+
AssertField(s.T(), "business-field", "baguette").AssertNoMoreTags(s.T())
259265

260266
clientFinishCallLogLine := lines[0]
261267
assert.Equal(s.T(), logging.LevelDebug, clientFinishCallLogLine.lvl)

testing/testpb/pingservice.go

+4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ var _ TestServiceServer = &TestPingService{}
2727

2828
type TestPingService struct {
2929
UnimplementedTestServiceServer
30+
PingFunc func(ctx context.Context)
3031
}
3132

3233
func (s *TestPingService) PingEmpty(_ context.Context, _ *PingEmptyRequest) (*PingEmptyResponse, error) {
3334
return &PingEmptyResponse{}, nil
3435
}
3536

3637
func (s *TestPingService) Ping(ctx context.Context, ping *PingRequest) (*PingResponse, error) {
38+
if s.PingFunc != nil {
39+
s.PingFunc(ctx)
40+
}
3741
// Modify the ctx value to verify the logger sees the value updated from the initial value
3842
n := ExtractCtxTestNumber(ctx)
3943
if n != nil {

0 commit comments

Comments
 (0)