github.com/GuanceCloud/cliutils@v1.1.21/pipeline/manager/script.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 manager for managing pipeline scripts 7 package manager 8 9 import ( 10 "fmt" 11 "time" 12 13 "github.com/GuanceCloud/cliutils/point" 14 plengine "github.com/GuanceCloud/platypus/pkg/engine" 15 plruntime "github.com/GuanceCloud/platypus/pkg/engine/runtime" 16 17 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 18 "github.com/GuanceCloud/cliutils/pipeline/ptinput/funcs" 19 "github.com/GuanceCloud/cliutils/pipeline/ptinput/plcache" 20 "github.com/GuanceCloud/cliutils/pipeline/ptinput/plmap" 21 "github.com/GuanceCloud/cliutils/pipeline/ptinput/ptwindow" 22 "github.com/GuanceCloud/cliutils/pipeline/stats" 23 ) 24 25 type Option struct { 26 MaxFieldValLen int // deprecated 27 DisableAddStatusField bool 28 IgnoreStatus []string 29 ScriptMap map[string]string 30 } 31 32 type PlScript struct { 33 name string // script name 34 script string // script content 35 ns string // script 所属 namespace 36 37 proc *plruntime.Script 38 plBuks *plmap.AggBuckets 39 ptWindow *ptwindow.WindowPool 40 cache *plcache.Cache 41 42 tags map[string]string 43 updateTS int64 44 45 category point.Category 46 } 47 48 func NewScripts(scripts, scriptTags map[string]string, ns string, cat point.Category, 49 buks ...*plmap.AggBuckets, 50 ) (map[string]*PlScript, map[string]error) { 51 var plbuks *plmap.AggBuckets 52 if len(buks) > 0 { 53 plbuks = buks[0] 54 } 55 56 switch cat { //nolint:exhaustive 57 case point.MetricDeprecated: 58 cat = point.Metric 59 case point.UnknownCategory, point.DynamicDWCategory: 60 retErr := map[string]error{} 61 for k := range scripts { 62 retErr[k] = fmt.Errorf("unsupported category: %s", cat) 63 } 64 return nil, retErr 65 } 66 67 ret, retErr := plengine.ParseScript(scripts, funcs.FuncsMap, funcs.FuncsCheckMap) 68 69 retScipt := map[string]*PlScript{} 70 71 for name, ng := range ret { 72 cache, _ := plcache.NewCache(time.Second, 100) 73 ptWin := ptwindow.NewManager() 74 75 sTags := map[string]string{ 76 "category": cat.String(), 77 "name": name, 78 "namespace": ns, 79 80 "lang": "platypus", 81 } 82 83 for k, v := range scriptTags { 84 if _, ok := sTags[k]; !ok { 85 sTags[k] = v 86 } 87 } 88 89 retScipt[name] = &PlScript{ 90 script: scripts[name], 91 name: name, 92 ns: ns, 93 category: cat, 94 proc: ng, 95 updateTS: time.Now().UnixNano(), 96 plBuks: plbuks, 97 tags: sTags, 98 cache: cache, 99 ptWindow: ptWin, 100 } 101 } 102 103 return retScipt, retErr 104 } 105 106 func (script *PlScript) Engine() *plruntime.Script { 107 return script.proc 108 } 109 110 func (script *PlScript) SetAggBuks(buks *plmap.AggBuckets) { 111 script.plBuks = buks 112 } 113 114 func (script *PlScript) Run(plpt ptinput.PlInputPt, signal plruntime.Signal, opt *Option, 115 ) error { 116 startTime := time.Now() 117 if script.proc == nil { 118 return fmt.Errorf("no script") 119 } 120 121 if plpt == nil { 122 return fmt.Errorf("no data") 123 } 124 125 plpt.SetAggBuckets(script.plBuks) 126 plpt.SetCache(script.cache) 127 plpt.SetPtWinPool(script.ptWindow) 128 129 err := script.proc.Run(plpt, signal) 130 if err != nil { 131 stats.WriteMetric(script.tags, 1, 0, 1, time.Since(startTime)) 132 return err 133 } 134 135 if script.category == point.Logging { 136 var disable bool 137 var ignore []string 138 139 if opt != nil { 140 disable = opt.DisableAddStatusField 141 ignore = opt.IgnoreStatus 142 } 143 144 ProcLoggingStatus(plpt, disable, ignore) 145 } 146 147 if plpt.Dropped() { 148 stats.WriteMetric(script.tags, 1, 1, 0, time.Since(startTime)) 149 } else { 150 stats.WriteMetric(script.tags, 1, 0, 0, time.Since(startTime)) 151 } 152 153 plpt.KeyTime2Time() 154 155 for _, v := range plpt.GetSubPoint() { 156 v.KeyTime2Time() 157 } 158 159 return nil 160 } 161 162 func (script *PlScript) Name() string { 163 return script.name 164 } 165 166 func (script *PlScript) Category() point.Category { 167 return script.category 168 } 169 170 func (script *PlScript) NS() string { 171 return script.ns 172 }