@@ -11,6 +11,7 @@ import (
11
11
"io"
12
12
"runtime"
13
13
"sort"
14
+ "strconv"
14
15
"strings"
15
16
"sync"
16
17
"testing"
@@ -172,9 +173,14 @@ type loggingClientServerSuite struct {
172
173
* baseLoggingSuite
173
174
}
174
175
175
- func customFields (_ context.Context ) logging.Fields {
176
+ func customFields (ctx context.Context ) logging.Fields {
177
+ var val string
178
+ n := testpb .ExtractCtxTestNumber (ctx )
179
+ if n != nil {
180
+ val = strconv .Itoa (* n )
181
+ }
176
182
// Add custom fields. The second one overrides the first one.
177
- return logging.Fields {"custom-field" , "foo" , "custom-field" , "yolo" }
183
+ return logging.Fields {"custom-field" , "foo" , "custom-field" , "yolo" , "custom-ctx-field" , val }
178
184
}
179
185
180
186
func TestSuite (t * testing.T ) {
@@ -232,13 +238,17 @@ func (s *loggingClientServerSuite) TestPing() {
232
238
assert .Equal (s .T (), logging .LevelDebug , serverStartCallLogLine .lvl )
233
239
assert .Equal (s .T (), "started call" , serverStartCallLogLine .msg )
234
240
_ = assertStandardFields (s .T (), logging .KindServerFieldValue , serverStartCallLogLine .fields , "Ping" , interceptors .Unary )
241
+ // This field is zero initially, but will be updated by the service, which we should see after the call is finished
242
+ serverStartCallLogLine .fields .AssertField (s .T (), "custom-ctx-field" , "0" )
235
243
236
244
serverFinishCallLogLine := lines [2 ]
237
245
assert .Equal (s .T (), logging .LevelDebug , serverFinishCallLogLine .lvl )
238
246
assert .Equal (s .T (), "finished call" , serverFinishCallLogLine .msg )
239
247
serverFinishCallFields := assertStandardFields (s .T (), logging .KindServerFieldValue , serverFinishCallLogLine .fields , "Ping" , interceptors .Unary )
240
248
serverFinishCallFields .AssertFieldNotEmpty (s .T (), "peer.address" ).
241
249
AssertField (s .T (), "custom-field" , "yolo" ).
250
+ // should be updated from 0 to 42
251
+ AssertField (s .T (), "custom-ctx-field" , "42" ).
242
252
AssertFieldNotEmpty (s .T (), "grpc.start_time" ).
243
253
AssertFieldNotEmpty (s .T (), "grpc.request.deadline" ).
244
254
AssertField (s .T (), "grpc.code" , "OK" ).
@@ -249,6 +259,8 @@ func (s *loggingClientServerSuite) TestPing() {
249
259
assert .Equal (s .T (), "finished call" , clientFinishCallLogLine .msg )
250
260
clientFinishCallFields := assertStandardFields (s .T (), logging .KindClientFieldValue , clientFinishCallLogLine .fields , "Ping" , interceptors .Unary )
251
261
clientFinishCallFields .AssertField (s .T (), "custom-field" , "yolo" ).
262
+ // should be updated from 0 to 42
263
+ AssertField (s .T (), "custom-ctx-field" , "42" ).
252
264
AssertField (s .T (), "grpc.request.value" , "something" ).
253
265
AssertFieldNotEmpty (s .T (), "grpc.start_time" ).
254
266
AssertFieldNotEmpty (s .T (), "grpc.request.deadline" ).
@@ -285,6 +297,8 @@ func (s *loggingClientServerSuite) TestPingList() {
285
297
assert .Equal (s .T (), "finished call" , serverFinishCallLogLine .msg )
286
298
serverFinishCallFields := assertStandardFields (s .T (), logging .KindServerFieldValue , serverFinishCallLogLine .fields , "PingList" , interceptors .ServerStream )
287
299
serverFinishCallFields .AssertField (s .T (), "custom-field" , "yolo" ).
300
+ // should be updated from 0 to 42
301
+ AssertField (s .T (), "custom-ctx-field" , "42" ).
288
302
AssertFieldNotEmpty (s .T (), "peer.address" ).
289
303
AssertFieldNotEmpty (s .T (), "grpc.start_time" ).
290
304
AssertFieldNotEmpty (s .T (), "grpc.request.deadline" ).
@@ -297,6 +311,8 @@ func (s *loggingClientServerSuite) TestPingList() {
297
311
clientFinishCallFields := assertStandardFields (s .T (), logging .KindClientFieldValue , clientFinishCallLogLine .fields , "PingList" , interceptors .ServerStream )
298
312
clientFinishCallFields .AssertFieldNotEmpty (s .T (), "grpc.start_time" ).
299
313
AssertField (s .T (), "custom-field" , "yolo" ).
314
+ // should be updated from 0 to 42
315
+ AssertField (s .T (), "custom-ctx-field" , "42" ).
300
316
AssertFieldNotEmpty (s .T (), "grpc.request.deadline" ).
301
317
AssertField (s .T (), "grpc.code" , "OK" ).
302
318
AssertFieldNotEmpty (s .T (), "grpc.time_ms" ).AssertNoMoreTags (s .T ())
@@ -344,23 +360,27 @@ func (s *loggingClientServerSuite) TestPingError_WithCustomLevels() {
344
360
assert .Equal (t , "finished call" , serverFinishCallLogLine .msg )
345
361
serverFinishCallFields := assertStandardFields (t , logging .KindServerFieldValue , serverFinishCallLogLine .fields , "PingError" , interceptors .Unary )
346
362
serverFinishCallFields .AssertField (s .T (), "custom-field" , "yolo" ).
363
+ // should be updated from 0 to 42
364
+ AssertField (s .T (), "custom-ctx-field" , "42" ).
347
365
AssertFieldNotEmpty (t , "peer.address" ).
348
366
AssertFieldNotEmpty (t , "grpc.start_time" ).
349
367
AssertFieldNotEmpty (t , "grpc.request.deadline" ).
350
368
AssertField (t , "grpc.code" , tcase .code .String ()).
351
369
AssertField (t , "grpc.error" , fmt .Sprintf ("rpc error: code = %s desc = Userspace error" , tcase .code .String ())).
352
- AssertFieldNotEmpty (t , "grpc.time_ms" ).AssertNoMoreTags (t )
370
+ AssertFieldNotEmpty (s . T () , "grpc.time_ms" ).AssertNoMoreTags (s . T () )
353
371
354
372
clientFinishCallLogLine := lines [0 ]
355
373
assert .Equal (t , tcase .level , clientFinishCallLogLine .lvl )
356
374
assert .Equal (t , "finished call" , clientFinishCallLogLine .msg )
357
375
clientFinishCallFields := assertStandardFields (t , logging .KindClientFieldValue , clientFinishCallLogLine .fields , "PingError" , interceptors .Unary )
358
376
clientFinishCallFields .AssertField (s .T (), "custom-field" , "yolo" ).
377
+ // should be updated from 0 to 42
378
+ AssertField (s .T (), "custom-ctx-field" , "42" ).
359
379
AssertFieldNotEmpty (t , "grpc.start_time" ).
360
380
AssertFieldNotEmpty (t , "grpc.request.deadline" ).
361
381
AssertField (t , "grpc.code" , tcase .code .String ()).
362
382
AssertField (t , "grpc.error" , fmt .Sprintf ("rpc error: code = %s desc = Userspace error" , tcase .code .String ())).
363
- AssertFieldNotEmpty (t , "grpc.time_ms" ).AssertNoMoreTags (t )
383
+ AssertFieldNotEmpty (s . T () , "grpc.time_ms" ).AssertNoMoreTags (s . T () )
364
384
})
365
385
}
366
386
}
0 commit comments