...
1
2
3
4
5
6
7 package logger
8
9 import "context"
10
11
12
13 type contextKey string
14
15 const (
16 contextKeyOperation contextKey = "operation"
17 contextKeyOperationID contextKey = "operationID"
18 )
19
20
21 func WithOperationName(ctx context.Context, operation string) context.Context {
22 return context.WithValue(ctx, contextKeyOperation, operation)
23 }
24
25
26 func WithOperationID(ctx context.Context, operationID int32) context.Context {
27 return context.WithValue(ctx, contextKeyOperationID, operationID)
28 }
29
30
31 func OperationName(ctx context.Context) (string, bool) {
32 operationName := ctx.Value(contextKeyOperation)
33 if operationName == nil {
34 return "", false
35 }
36
37 return operationName.(string), true
38 }
39
40
41 func OperationID(ctx context.Context) (int32, bool) {
42 operationID := ctx.Value(contextKeyOperationID)
43 if operationID == nil {
44 return 0, false
45 }
46
47 return operationID.(int32), true
48 }
49
View as plain text