Skip to content

Commit

Permalink
logging: store the propagated context in the reporter (#740)
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Cano <[email protected]>
  • Loading branch information
kindermoumoute authored Dec 28, 2024
1 parent 3ce33cf commit bea7c0e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 2 additions & 1 deletion interceptors/logging/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func reportable(logger Logger, opts *options) interceptors.CommonReportableFunc
if d, ok := ctx.Deadline(); ok {
singleUseFields = singleUseFields.AppendUnique(Fields{"grpc.request.deadline", d.Format(opts.timestampFormat)})
}
ctx = InjectFields(ctx, fields)
return &reporter{
CallMeta: c,
ctx: ctx,
Expand All @@ -195,7 +196,7 @@ func reportable(logger Logger, opts *options) interceptors.CommonReportableFunc
fields: fields.WithUnique(singleUseFields),
logger: logger,
kind: kind,
}, InjectFields(ctx, fields)
}, ctx
}
}

Expand Down
10 changes: 8 additions & 2 deletions interceptors/logging/interceptors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ func TestSuite(t *testing.T) {
&baseLoggingSuite{
logger: newMockLogger(),
InterceptorTestSuite: &testpb.InterceptorTestSuite{
TestService: &testpb.TestPingService{},
TestService: &testpb.TestPingService{
PingFunc: func(ctx context.Context) {
// Modify the fields already in the context.
logging.AddFields(ctx, logging.Fields{"business-field", "baguette"})
},
},
},
},
}
Expand Down Expand Up @@ -255,7 +260,8 @@ func (s *loggingClientServerSuite) TestPing() {
AssertFieldNotEmpty(s.T(), "grpc.start_time").
AssertFieldNotEmpty(s.T(), "grpc.request.deadline").
AssertField(s.T(), "grpc.code", "OK").
AssertFieldNotEmpty(s.T(), "grpc.time_ms").AssertNoMoreTags(s.T())
AssertFieldNotEmpty(s.T(), "grpc.time_ms").
AssertField(s.T(), "business-field", "baguette").AssertNoMoreTags(s.T())

clientFinishCallLogLine := lines[0]
assert.Equal(s.T(), logging.LevelDebug, clientFinishCallLogLine.lvl)
Expand Down
4 changes: 4 additions & 0 deletions testing/testpb/pingservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ var _ TestServiceServer = &TestPingService{}

type TestPingService struct {
UnimplementedTestServiceServer
PingFunc func(ctx context.Context)
}

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

func (s *TestPingService) Ping(ctx context.Context, ping *PingRequest) (*PingResponse, error) {
if s.PingFunc != nil {
s.PingFunc(ctx)
}
// Modify the ctx value to verify the logger sees the value updated from the initial value
n := ExtractCtxTestNumber(ctx)
if n != nil {
Expand Down

0 comments on commit bea7c0e

Please sign in to comment.