github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_conv_traceid_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package funcs 7 8 import ( 9 "testing" 10 "time" 11 12 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 13 "github.com/GuanceCloud/cliutils/point" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestConvW3CTraceID(t *testing.T) { 18 cases := []struct { 19 name, pl, in string 20 key string 21 fail bool 22 expect any 23 }{ 24 { 25 name: "value type: string", 26 in: `18962fdd9eea517f2ae0771ea69d6e16`, 27 pl: ` 28 grok(_, "%{NOTSPACE:trace_id}") 29 30 conv_traceid_w3c_to_dd(trace_id) 31 `, 32 key: "trace_id", 33 expect: "3089600317904219670", 34 fail: false, 35 }, 36 37 { 38 name: "value type: string", 39 in: `2f7cdb2b45447bcb43820ddf56d9a654`, 40 pl: ` 41 grok(_, "%{NOTSPACE:trace_id}") 42 43 conv_traceid_w3c_to_dd(trace_id) 44 `, 45 key: "trace_id", 46 expect: "4864465800399529556", 47 fail: false, 48 }, 49 50 { 51 name: "value type: string", 52 in: `43820ddf56d9a654`, 53 pl: ` 54 grok(_, "%{NOTSPACE:trace_id}") 55 56 conv_traceid_w3c_to_dd(trace_id) 57 `, 58 key: "trace_id", 59 expect: "4864465800399529556", 60 fail: false, 61 }, 62 63 { 64 name: "value type: string", 65 in: `03820ddf56d9a654`, 66 pl: ` 67 grok(_, "%{NOTSPACE:trace_id}") 68 69 conv_traceid_w3c_to_dd(trace_id) 70 `, 71 key: "trace_id", 72 expect: "252779781972141652", 73 fail: false, 74 }, 75 76 { 77 name: "value type: string", 78 in: `0f7cdb2b45447bcb43820ddf56d9a654`, 79 pl: ` 80 grok(_, "%{NOTSPACE:trace_id}") 81 82 conv_traceid_w3c_to_dd(trace_id) 83 `, 84 key: "trace_id", 85 expect: "4864465800399529556", 86 fail: false, 87 }, 88 89 { 90 name: "value type: string", 91 in: `10f7cdb2b45447bcb43820ddf56d9a654`, 92 pl: ` 93 grok(_, "%{NOTSPACE:trace_id}") 94 95 conv_traceid_w3c_to_dd(trace_id) 96 `, 97 key: "trace_id", 98 // 原样返回 99 expect: "10f7cdb2b45447bcb43820ddf56d9a654", 100 fail: false, 101 }, 102 } 103 104 for idx, tc := range cases { 105 t.Run(tc.name, func(t *testing.T) { 106 runner, err := NewTestingRunner(tc.pl) 107 if err != nil { 108 if tc.fail { 109 t.Logf("[%d]expect error: %s", idx, err) 110 } else { 111 t.Errorf("[%d] failed: %s", idx, err) 112 } 113 return 114 } 115 pt := ptinput.NewPlPoint(point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 116 117 errR := runScript(runner, pt) 118 119 if errR == nil { 120 v, _, ok := pt.Get(tc.key) 121 assert.Equal(t, nil, ok) 122 assert.Equal(t, tc.expect, v) 123 t.Logf("[%d] PASS", idx) 124 } else { 125 t.Error(errR) 126 } 127 }) 128 } 129 }