github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_conv_traceid_hex_to_dec.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 "encoding/binary" 10 "encoding/hex" 11 "fmt" 12 "strconv" 13 14 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 15 "github.com/GuanceCloud/platypus/pkg/ast" 16 "github.com/GuanceCloud/platypus/pkg/engine/runtime" 17 "github.com/GuanceCloud/platypus/pkg/errchain" 18 ) 19 20 func ConvTraceIDW3C2DDChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 21 if len(funcExpr.Param) != 1 { 22 return runtime.NewRunError(ctx, fmt.Sprintf( 23 "func %s expected 1 args", funcExpr.Name), funcExpr.NamePos) 24 } 25 26 if _, err := getKeyName(funcExpr.Param[0]); err != nil { 27 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 28 } 29 30 return nil 31 } 32 33 func ConvTraceIDW3C2DD(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 34 if len(funcExpr.Param) != 1 { 35 return runtime.NewRunError(ctx, fmt.Sprintf( 36 "func %s expected 1 args", funcExpr.Name), funcExpr.NamePos) 37 } 38 39 key, err := getKeyName(funcExpr.Param[0]) 40 if err != nil { 41 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 42 } 43 44 k, err := ctx.GetKey(key) 45 if err != nil { 46 l.Debug(err) 47 return nil 48 } 49 50 w3cTraceID, ok := k.Value.(string) 51 if !ok { 52 return nil 53 } 54 55 if ddTraceID, err := convTraceW3CToDD(w3cTraceID); err != nil { 56 l.Debug(err) 57 } else { 58 addKey2PtWithVal(ctx.InData(), key, ddTraceID, ast.String, ptinput.KindPtDefault) 59 } 60 return nil 61 } 62 63 func convTraceW3CToDD(hexStr string) (string, error) { 64 switch len(hexStr) { 65 case 32: 66 hexStr = hexStr[16:] 67 case 16: 68 default: 69 return "", fmt.Errorf("not trace/span id") 70 } 71 72 b, err := hex.DecodeString(hexStr) 73 if err != nil { 74 return "", err 75 } 76 77 i := binary.BigEndian.Uint64(b) 78 return strconv.FormatUint(i, 10), nil 79 }