forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 6
/
context.go
41 lines (32 loc) · 760 Bytes
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package grpclogging
import (
"context"
"go.uber.org/zap/zapcore"
)
type fieldKeyType struct{}
var fieldKey = &fieldKeyType{}
func ZapFields(ctx context.Context) []zapcore.Field {
fields, ok := ctx.Value(fieldKey).([]zapcore.Field)
if ok {
return fields
}
return nil
}
func Fields(ctx context.Context) []interface{} {
fields, ok := ctx.Value(fieldKey).([]zapcore.Field)
if !ok {
return nil
}
genericFields := make([]interface{}, len(fields))
for i := range fields {
genericFields[i] = fields[i]
}
return genericFields
}
func WithFields(ctx context.Context, fields []zapcore.Field) context.Context {
return context.WithValue(ctx, fieldKey, fields)
}