-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
144 lines (128 loc) · 3.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"runtime/debug"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/zeebo/errs/v2"
"golang.org/x/sync/errgroup"
"storj.io/eventkit"
"storj.io/eventkit/eventkitd-bigquery/bigquery"
)
func main() {
c := cobra.Command{
Use: "eventkit-time [--tag=value] command args",
Short: "CLI tool for performance tests, like `time` but results are sent to an eventkit target",
}
_ = c.Flags().StringP("name", "n", "test", "Name of the event sending out")
_ = c.Flags().StringP("destination", "d", "localhost:9000", "UDP host and port to send out package, or bq:project/dataset to directly send data to BQ")
_ = c.Flags().StringSliceP("tag", "t", []string{}, "Custom tags to add to the events")
_ = c.Flags().StringP("instance", "i", "", "Instance name of the eventkitd monitoring (default: hostname)")
_ = c.Flags().StringP("scope", "s", "eventkit-time", "Scope to use for events")
version := c.Flags().BoolP("version", "v", false, "Scope to use for events")
viper.SetConfigName("eventkit-time")
viper.SetEnvPrefix("EVENTKIT")
viper.AutomaticEnv()
err := viper.BindPFlags(c.Flags())
if err != nil {
panic(err)
}
c.RunE = func(cmd *cobra.Command, args []string) error {
if *version {
if bi, ok := debug.ReadBuildInfo(); ok {
for _, s := range bi.Settings {
if strings.HasPrefix(s.Key, "vcs.") {
fmt.Printf("%+v\n", s.Key+"="+s.Value)
}
}
}
return nil
}
if len(args) == 0 {
return errs.Errorf("Command is missing")
}
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return err
}
}
return execute(viper.GetString("destination"), viper.GetString("name"), args, viper.GetStringSlice("tag"), viper.GetString("scope"), viper.GetString("instance"))
}
err = c.Execute()
if err != nil {
log.Fatalf("%++v", err)
}
}
func execute(dest string, name string, args []string, customTags []string, scope string, instance string) error {
ek := eventkit.DefaultRegistry.Scope(scope)
if instance == "" {
instance, _ = os.Hostname()
if instance == "" {
instance = "unknown"
}
}
destCtx, cancel := context.WithCancel(context.Background())
defer cancel()
var client eventkit.Destination
if strings.HasPrefix(dest, "bq:") {
var err error
dest = strings.TrimPrefix(dest, "bq:")
parts := strings.Split(dest, "/")
client, err = bigquery.NewBigQueryDestination(destCtx, "eventkit-time", parts[0], parts[1])
if err != nil {
return err
}
} else {
client = eventkit.NewUDPClient("eventkit-time", "0.0.1", instance, dest)
}
eventkit.DefaultRegistry.AddDestination(client)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
w := errgroup.Group{}
w.Go(func() error {
client.Run(ctx)
return nil
})
var cmd = exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
start := time.Now()
err := cmd.Run()
if err != nil {
return err
}
t := time.Since(start)
var tags []eventkit.Tag
tags = append(tags, eventkit.String("cmd", args[0]))
tags = append(tags, eventkit.Int64("duration-ms", t.Milliseconds()))
usage, hasUsage, err := GetChildrenUsage()
if err != nil {
return errs.Wrap(err)
}
if hasUsage {
tags = append(tags, eventkit.Int64("user-time-ms", usage.UserTime.Milliseconds()))
tags = append(tags, eventkit.Int64("system-time-ms", usage.SystemTime.Milliseconds()))
tags = append(tags, eventkit.Int64("max-rss", usage.MaxRSS))
}
for _, c := range customTags {
parts := strings.SplitN(c, "=", 2)
tags = append(tags, eventkit.String(parts[0], parts[1]))
}
for _, e := range os.Environ() {
parts := strings.SplitN(e, "=", 2)
if strings.HasPrefix(parts[0], "EVENTKIT_TAG_") {
tagName := strings.TrimPrefix(parts[0], "EVENTKIT_TAG_")
tagName = strings.ToLower(tagName)
tags = append(tags, eventkit.String(tagName, parts[1]))
}
}
ek.Event(name, tags...)
cancel()
return w.Wait()
}