github.com/matrixorigin/matrixone@v0.7.0/pkg/util/trace/impl/motrace/report_log_test.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package motrace 16 17 import ( 18 "context" 19 "github.com/matrixorigin/matrixone/pkg/util/trace" 20 "testing" 21 "time" 22 23 "github.com/matrixorigin/matrixone/pkg/util/stack" 24 "github.com/stretchr/testify/require" 25 "go.uber.org/zap" 26 "go.uber.org/zap/zapcore" 27 ) 28 29 func TestReportZap(t *testing.T) { 30 type args struct { 31 jsonEncoder zapcore.Encoder 32 entry zapcore.Entry 33 fields []zapcore.Field 34 } 35 spanField := trace.ContextField(trace.ContextWithSpanContext(context.Background(), trace.SpanContext{})) 36 entry := zapcore.Entry{ 37 Level: zapcore.InfoLevel, 38 Time: time.Unix(0, 0), 39 LoggerName: "test", 40 Message: "info message", 41 Caller: zapcore.NewEntryCaller(uintptr(stack.Caller(3)), "file", 123, true), 42 } 43 encoder := zapcore.NewJSONEncoder( 44 zapcore.EncoderConfig{ 45 StacktraceKey: "stacktrace", 46 SkipLineEnding: true, 47 LineEnding: zapcore.DefaultLineEnding, 48 EncodeLevel: zapcore.LowercaseLevelEncoder, 49 EncodeTime: zapcore.EpochTimeEncoder, 50 EncodeDuration: zapcore.SecondsDurationEncoder, 51 EncodeCaller: zapcore.ShortCallerEncoder, 52 }) 53 intField := zap.Int("key", 1) 54 strField := zap.String("str", "1") 55 boolField := zap.Bool("bool", true) 56 tests := []struct { 57 name string 58 args args 59 want string 60 }{ 61 { 62 name: "normal", 63 args: args{ 64 jsonEncoder: encoder, 65 entry: entry, 66 fields: []zapcore.Field{intField}, 67 }, 68 want: `{"key":1}`, 69 }, 70 { 71 name: "remove first span", 72 args: args{ 73 jsonEncoder: encoder, 74 entry: entry, 75 fields: []zapcore.Field{spanField, intField, strField}, 76 }, 77 want: `{"str":"1","key":1}`, 78 }, 79 { 80 name: "remove middle span", 81 args: args{ 82 jsonEncoder: encoder, 83 entry: entry, 84 fields: []zapcore.Field{intField, spanField, strField}, 85 }, 86 want: `{"key":1,"str":"1"}`, 87 }, 88 { 89 name: "remove double middle span", 90 args: args{ 91 jsonEncoder: encoder, 92 entry: entry, 93 fields: []zapcore.Field{intField, spanField, spanField, strField, boolField}, 94 }, 95 want: `{"key":1,"bool":true,"str":"1"}`, 96 }, 97 { 98 name: "remove end span", 99 args: args{ 100 jsonEncoder: encoder, 101 entry: entry, 102 fields: []zapcore.Field{intField, strField, spanField, spanField}, 103 }, 104 want: `{"key":1,"str":"1"}`, 105 }, 106 { 107 name: "remove multi span", 108 args: args{ 109 jsonEncoder: encoder, 110 entry: entry, 111 fields: []zapcore.Field{intField, strField, spanField, boolField, spanField, spanField}, 112 }, 113 want: `{"key":1,"str":"1","bool":true}`, 114 }, 115 } 116 for _, tt := range tests { 117 t.Run(tt.name, func(t *testing.T) { 118 got, err := ReportZap(tt.args.jsonEncoder, tt.args.entry, tt.args.fields) 119 require.Equal(t, nil, err) 120 require.Equal(t, tt.want, got.String()) 121 }) 122 } 123 }