...
1 package kit_test
2
3 import (
4 "time"
5
6 "github.com/go-kit/log"
7 grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
8 "github.com/grpc-ecosystem/go-grpc-middleware/logging/kit"
9 grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
10
11 "google.golang.org/grpc"
12 )
13
14 var (
15 customFunc kit.CodeToLevel
16 )
17
18
19 func Example_initialization() {
20
21 logger := log.NewNopLogger()
22
23 opts := []kit.Option{
24 kit.WithLevels(customFunc),
25 }
26
27 _ = grpc.NewServer(
28 grpc_middleware.WithUnaryServerChain(
29 grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
30 kit.UnaryServerInterceptor(logger, opts...),
31 ),
32 grpc_middleware.WithStreamServerChain(
33 grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
34 kit.StreamServerInterceptor(logger, opts...),
35 ),
36 )
37 }
38
39 func Example_initializationWithDurationFieldOverride() {
40
41 logger := log.NewNopLogger()
42
43 opts := []kit.Option{
44 kit.WithDurationField(func(duration time.Duration) []interface{} {
45 return kit.DurationToTimeMillisField(duration)
46 }),
47 }
48 _ = grpc.NewServer(
49 grpc_middleware.WithUnaryServerChain(
50 grpc_ctxtags.UnaryServerInterceptor(),
51 kit.UnaryServerInterceptor(logger, opts...),
52 ),
53 grpc_middleware.WithStreamServerChain(
54 grpc_ctxtags.StreamServerInterceptor(),
55 kit.StreamServerInterceptor(logger, opts...),
56 ),
57 )
58 }
59
60 func ExampleWithDecider() {
61 opts := []kit.Option{
62 kit.WithDecider(func(methodFullName string, err error) bool {
63
64 if err == nil && methodFullName == "blah.foo.healthcheck" {
65 return false
66 }
67
68
69 return true
70 }),
71 }
72
73 _ = []grpc.ServerOption{
74 grpc_middleware.WithStreamServerChain(
75 grpc_ctxtags.StreamServerInterceptor(),
76 kit.StreamServerInterceptor(log.NewNopLogger(), opts...)),
77 grpc_middleware.WithUnaryServerChain(
78 grpc_ctxtags.UnaryServerInterceptor(),
79 kit.UnaryServerInterceptor(log.NewNopLogger(), opts...)),
80 }
81 }
82
View as plain text