|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/signal" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + "syscall" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/prometheus/client_golang/api" |
| 17 | + v1 "github.com/prometheus/client_golang/api/prometheus/v1" |
| 18 | + "github.com/urfave/cli/v2" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + prometheusAddressFlag = "prometheus" |
| 23 | + periodFlag = "period" |
| 24 | + outputFlag = "output" |
| 25 | +) |
| 26 | + |
| 27 | +type measurement struct { |
| 28 | + AvgEbpfRate float64 `json:"avgEbpfRate"` |
| 29 | + AvgLostEventsRate float64 `json:"avgLostEventsRate"` |
| 30 | + LostEvents int `json:"lostEvents"` |
| 31 | +} |
| 32 | + |
| 33 | +func (m measurement) Print() { |
| 34 | + log.Printf("\n") |
| 35 | + fmt.Printf("Events/Sec: %f\n", m.AvgEbpfRate) |
| 36 | + fmt.Printf("EventsLost/Sec: %f\n", m.AvgLostEventsRate) |
| 37 | + fmt.Printf("Events Lost: %d\n", m.LostEvents) |
| 38 | + fmt.Println("===============================================") |
| 39 | +} |
| 40 | +func (m measurement) PrintJson() { |
| 41 | + res, _ := json.Marshal(m) |
| 42 | + fmt.Println(string(res)) |
| 43 | +} |
| 44 | + |
| 45 | +type OutputMode string |
| 46 | + |
| 47 | +const ( |
| 48 | + jsonOutput OutputMode = "json" |
| 49 | + prettyOutput OutputMode = "pretty" |
| 50 | +) |
| 51 | + |
| 52 | +func main() { |
| 53 | + app := cli.App{ |
| 54 | + Name: "tracee-bench", |
| 55 | + Usage: "A prometheus based performance probe for tracee", |
| 56 | + Flags: []cli.Flag{ |
| 57 | + &cli.StringFlag{ |
| 58 | + Name: prometheusAddressFlag, |
| 59 | + Usage: "address of a prometheus instance tracking tracee", |
| 60 | + Value: "http://localhost:9090", |
| 61 | + }, |
| 62 | + &cli.IntFlag{ |
| 63 | + Name: periodFlag, |
| 64 | + Usage: "period of scraping in seconds", |
| 65 | + Value: 5, |
| 66 | + }, |
| 67 | + &cli.StringFlag{ |
| 68 | + Name: outputFlag, |
| 69 | + Usage: "set output format (options: pretty, json)", |
| 70 | + Value: "pretty", |
| 71 | + }, |
| 72 | + }, |
| 73 | + Action: func(ctx *cli.Context) error { |
| 74 | + address := ctx.String(prometheusAddressFlag) |
| 75 | + |
| 76 | + if address == "" { |
| 77 | + return fmt.Errorf("prometheus address required for tracee-er") |
| 78 | + } |
| 79 | + |
| 80 | + client, err := api.NewClient(api.Config{ |
| 81 | + Address: address, |
| 82 | + }) |
| 83 | + |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + done := sigHandler() |
| 89 | + prom := v1.NewAPI(client) |
| 90 | + ticker := time.NewTicker(time.Duration(ctx.Int(periodFlag)) * time.Second) |
| 91 | + |
| 92 | + // promql queries |
| 93 | + const ( |
| 94 | + eventspersec = "events/sec" |
| 95 | + lostpersec = "lost/sec" |
| 96 | + rulespersec = "rules/sec" |
| 97 | + lostoverall = "lost_events" |
| 98 | + ) |
| 99 | + queries := map[string]struct { |
| 100 | + queryName string |
| 101 | + query string |
| 102 | + }{ |
| 103 | + eventspersec: {queryName: "average ebpf_events/sec", query: "rate(tracee_ebpf_events_total[1m])"}, |
| 104 | + lostpersec: {queryName: "average ebpf_lostevents/sec", query: "rate(tracee_ebpf_lostevents_total[1m])"}, |
| 105 | + lostoverall: {queryName: "lost events", query: "tracee_ebpf_lostevents_total"}, |
| 106 | + } |
| 107 | + |
| 108 | + outputMode := OutputMode(ctx.String(outputFlag)) |
| 109 | + if outputMode == prettyOutput { |
| 110 | + fmt.Println("===================TRACEE-ER===================") |
| 111 | + } |
| 112 | + go func() { |
| 113 | + for { |
| 114 | + select { |
| 115 | + case <-done: |
| 116 | + { |
| 117 | + return |
| 118 | + } |
| 119 | + case now := <-ticker.C: |
| 120 | + { |
| 121 | + |
| 122 | + measurement := measurement{} |
| 123 | + wg := sync.WaitGroup{} |
| 124 | + wg.Add((len(queries))) |
| 125 | + for field, query := range queries { |
| 126 | + go func(queryField string, queryName string, query string) { |
| 127 | + defer wg.Done() |
| 128 | + res, _, err := prom.Query(context.Background(), query, now) |
| 129 | + if err != nil { |
| 130 | + log.Printf("failed to fetch %s: %v\n", queryName, err) |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + queryResString := res.String() |
| 135 | + if queryResString == "" { |
| 136 | + log.Printf("failed to fetch %s: empty\n", queryName) |
| 137 | + return |
| 138 | + } |
| 139 | + val, _ := parseQueryResString(queryResString) |
| 140 | + switch queryField { |
| 141 | + case eventspersec: |
| 142 | + measurement.AvgEbpfRate = val |
| 143 | + case lostpersec: |
| 144 | + measurement.AvgLostEventsRate = val |
| 145 | + case lostoverall: |
| 146 | + measurement.LostEvents = int(val) |
| 147 | + } |
| 148 | + }(field, query.queryName, query.query) |
| 149 | + } |
| 150 | + wg.Wait() |
| 151 | + switch outputMode { |
| 152 | + case prettyOutput: |
| 153 | + measurement.Print() |
| 154 | + case jsonOutput: |
| 155 | + measurement.PrintJson() |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + }() |
| 161 | + <-done |
| 162 | + return nil |
| 163 | + }, |
| 164 | + } |
| 165 | + err := app.Run(os.Args) |
| 166 | + if err != nil { |
| 167 | + log.Fatal(err) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func parseQueryResString(queryRes string) (float64, error) { |
| 172 | + startIndex := strings.LastIndex(queryRes, "=> ") + 3 |
| 173 | + lastIndex := strings.LastIndex(queryRes, "@[") - 1 |
| 174 | + return strconv.ParseFloat(queryRes[startIndex:lastIndex], 64) |
| 175 | +} |
| 176 | + |
| 177 | +func sigHandler() chan bool { |
| 178 | + sigs := make(chan os.Signal, 1) |
| 179 | + done := make(chan bool, 1) |
| 180 | + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) |
| 181 | + go func() { |
| 182 | + <-sigs |
| 183 | + done <- true |
| 184 | + }() |
| 185 | + return done |
| 186 | +} |
0 commit comments