github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_create_point.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 "fmt" 10 "reflect" 11 "time" 12 13 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 14 "github.com/GuanceCloud/cliutils/point" 15 "github.com/GuanceCloud/platypus/pkg/ast" 16 "github.com/GuanceCloud/platypus/pkg/engine/runtime" 17 "github.com/GuanceCloud/platypus/pkg/errchain" 18 "github.com/spf13/cast" 19 ) 20 21 func CreatePointChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 22 if err := normalizeFuncArgsDeprecated(funcExpr, []string{ 23 "name", "tags", "fields", 24 "ts", "category", "after_use", 25 }, 3); err != nil { 26 return runtime.NewRunError(ctx, err.Error(), funcExpr.NamePos) 27 } 28 29 if arg := funcExpr.Param[5]; arg != nil { 30 switch arg.NodeType { //nolint:exhaustive 31 case ast.TypeStringLiteral: 32 usecall := &ast.CallExpr{ 33 Name: "use", 34 NamePos: arg.StartPos(), 35 Param: []*ast.Node{funcExpr.Param[5]}, 36 } 37 funcExpr.PrivateData = usecall 38 return UseChecking(ctx, usecall) 39 default: 40 return runtime.NewRunError(ctx, fmt.Sprintf("param after_use expects StringLiteral, got %s", 41 arg.NodeType), arg.StartPos()) 42 } 43 } 44 45 return nil 46 } 47 48 func CreatePoint(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 49 var ptName string 50 var ptTags map[string]string 51 ptFields := map[string]any{} 52 ptTime := time.Now() 53 ptCat := point.Metric 54 55 name, _, err := runtime.RunStmt(ctx, funcExpr.Param[0]) 56 if err != nil { 57 return err 58 } 59 60 if name == nil { 61 return runtime.NewRunError(ctx, 62 "type of parameter expected to be string, got nil", 63 funcExpr.Param[0].StartPos()) 64 } 65 66 if v, ok := name.(string); ok { 67 ptName = v 68 } else { 69 return runtime.NewRunError(ctx, fmt.Sprintf( 70 "type of parameter expected to be string, got %s", 71 reflect.TypeOf(v)), funcExpr.Param[0].StartPos()) 72 } 73 74 if tags, _, err := runtime.RunStmt(ctx, funcExpr.Param[1]); err != nil { 75 return nil 76 } else if tags != nil { 77 if tKV, ok := tags.(map[string]any); ok { 78 if len(tKV) > 0 { 79 ptTags = map[string]string{} 80 for tagK, tagV := range tKV { 81 if val, ok := tagV.(string); ok { 82 ptTags[tagK] = val 83 } 84 } 85 } 86 } else { 87 return runtime.NewRunError(ctx, fmt.Sprintf( 88 "type of parameter expected to be map, got %s", 89 reflect.TypeOf(tags)), funcExpr.Param[1].StartPos()) 90 } 91 } 92 93 if fields, _, err := runtime.RunStmt(ctx, funcExpr.Param[2]); err != nil { 94 return nil 95 } else if fields != nil { 96 if fKV, ok := fields.(map[string]any); ok { 97 if len(fKV) > 0 { 98 ptFields = map[string]any{} 99 for fK, fV := range fKV { 100 switch fV := fV.(type) { 101 case int32, int8, int16, int, 102 uint, uint16, uint32, uint64, uint8: 103 ptFields[fK] = cast.ToInt64(fV) 104 case float32: 105 ptFields[fK] = cast.ToFloat64(fV) 106 case []byte: 107 ptFields[fK] = string(fV) 108 case string, bool, float64, int64: 109 ptFields[fK] = fV 110 } 111 } 112 } 113 } else { 114 return runtime.NewRunError(ctx, fmt.Sprintf( 115 "type of parameter expected to be map, got %s", 116 reflect.TypeOf(fields)), funcExpr.Param[2].StartPos()) 117 } 118 } 119 120 if arg := funcExpr.Param[3]; arg != nil { 121 if pTS, _, err := runtime.RunStmt(ctx, arg); err != nil { 122 return nil 123 } else if pTS != nil { 124 if ts, ok := pTS.(int64); ok { 125 if ts > 0 { 126 ptTime = time.Unix(0, ts) 127 } 128 } else { 129 return runtime.NewRunError(ctx, fmt.Sprintf( 130 "type of parameter expected to be int64, got %s", 131 reflect.TypeOf(pTS)), arg.StartPos()) 132 } 133 } 134 } 135 136 if arg := funcExpr.Param[4]; arg != nil { 137 if catName, _, err := runtime.RunStmt(ctx, arg); err != nil { 138 return nil 139 } else if catName != nil { 140 if catN, ok := catName.(string); ok { 141 ptCat = ptCategory(catN) 142 if ptCat == point.UnknownCategory { 143 return nil 144 } 145 } else { 146 return runtime.NewRunError(ctx, fmt.Sprintf( 147 "type of parameter expected to be str, got %s", 148 reflect.TypeOf(catName)), arg.StartPos()) 149 } 150 } 151 } 152 153 plpt := ptinput.NewPlPoint(ptCat, ptName, ptTags, ptFields, ptTime) 154 if arg := funcExpr.Param[5]; arg != nil { 155 if refCall, ok := funcExpr.PrivateData.(*ast.CallExpr); ok { 156 if srcipt, ok := refCall.PrivateData.(*runtime.Script); ok { 157 if err := srcipt.Run(plpt, ctx.Signal()); err != nil { 158 return err.ChainAppend(ctx.Name(), funcExpr.NamePos) 159 } 160 } 161 } 162 } 163 164 if ptIn, err := getPoint(ctx.InData()); err == nil { 165 ptIn.AppendSubPoint(plpt) 166 } 167 168 return nil 169 } 170 171 func ptCategory(cat string) point.Category { 172 switch cat { 173 case point.SLogging, point.CL: 174 return point.Logging 175 case point.SMetric, point.CM: 176 return point.Metric 177 case point.STracing, point.CT: 178 return point.Tracing 179 case point.SRUM, point.CR: 180 return point.RUM 181 case point.SNetwork, point.CN: 182 return point.Network 183 case point.SObject, point.CO: 184 return point.Object 185 case point.SCustomObject, point.CCO: 186 return point.CustomObject 187 case point.SSecurity, point.CS: 188 return point.Security 189 case point.SDialTesting, point.CDT: 190 return point.DialTesting 191 } 192 return point.UnknownCategory 193 }