-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
191 lines (173 loc) · 4.43 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"bufio"
"context"
"debug/buildinfo"
"errors"
"flag"
"fmt"
"io/fs"
"log"
"log/slog"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"runtime/debug"
"runtime/trace"
"strconv"
"strings"
"github.com/cilium/ebpf/rlimit"
"github.com/keisku/gmon/ebpf"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
level = flag.String(
"level",
slog.LevelInfo.String(),
fmt.Sprintf("log level could be one of %q",
[]slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError},
))
levelMap = map[string]slog.Level{
"DEBUG": slog.LevelDebug,
"debug": slog.LevelDebug,
"INFO": slog.LevelInfo,
"info": slog.LevelInfo,
"WARN": slog.LevelWarn,
"warn": slog.LevelWarn,
"ERROR": slog.LevelError,
"error": slog.LevelError,
}
pid = flag.Int("pid", 0, "Useful when tracing programs that have many running instances")
binPath = flag.String("path", "", "Path to executable file to be monitored (required)")
traceOutPath = flag.String("trace", "", "Path to Go runtime/trace output")
pprofPort = flag.Int("pprof", 0, "Port to be used for pprof server. If 0, pprof server is not started")
metricsPort = flag.Int("metrics", 5500, "Port to be used for metrics server, /metrics endpoint")
printVersion = flag.Bool("version", false, "Print version information")
// Set by -ldflags at build time
Version = "unknown"
)
type promLogger struct{}
func (promLogger) Println(v ...interface{}) {
slog.Error(fmt.Sprint(v...))
}
func main() {
flag.Parse()
if *printVersion {
var gover, arch, goos, commitHash = "unknown", "unknown", "unknown", "unknown"
if info, ok := debug.ReadBuildInfo(); ok {
gover = info.GoVersion
for _, s := range info.Settings {
switch s.Key {
case "GOARCH":
arch = s.Value
case "GOOS":
goos = s.Value
case "vcs.revision":
commitHash = s.Value
}
}
}
fmt.Printf("gmon %s (%s/%s) %s, commit=%s\n", Version, goos, arch, gover, commitHash)
return
}
opts := &slog.HandlerOptions{Level: levelMap[*level]}
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, opts)))
errlog := log.New(os.Stderr, "", log.LstdFlags)
if runtime.GOARCH != "amd64" || runtime.GOOS != "linux" {
errlog.Fatalln("gmon only works on amd64 Linux")
}
binfo, err := buildinfo.ReadFile(*binPath)
if err != nil {
errlog.Fatalln(err)
}
if !isGoVersion123OrHigher(binfo.GoVersion) {
errlog.Fatalf("gmon requires Go 1.23 or higher, but %s is used for %s", binfo.GoVersion, binfo.Main.Path)
}
if *traceOutPath != "" {
traceOutFile, err := os.Create(*traceOutPath)
if err != nil {
errlog.Fatalln(err)
}
if err := trace.Start(traceOutFile); err != nil {
errlog.Fatalln(err)
}
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
if err := rlimit.RemoveMemlock(); err != nil {
errlog.Fatalln(err)
}
http.Handle("/metrics", promhttp.HandlerFor(
prometheus.DefaultGatherer,
promhttp.HandlerOpts{
ErrorLog: promLogger{},
EnableOpenMetrics: true,
},
))
go http.ListenAndServe(fmt.Sprintf(":%d", *metricsPort), nil)
ebpfConfig, err := ebpf.NewConfig(
*binPath,
*pid,
)
if err != nil {
errlog.Fatalln(err)
}
eBPFClose, err := ebpf.Run(ctx, ebpfConfig)
if err != nil {
errlog.Fatalln(err)
}
if levelMap[*level] == slog.LevelDebug {
go logTracePipe(ctx.Done())
}
if 1023 < *pprofPort {
go func() {
_ = http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", pprofPort), nil)
}()
}
<-ctx.Done()
slog.Debug("gmon exits")
eBPFClose()
}
func logTracePipe(done <-chan struct{}) {
tracePipe, err := os.Open("/sys/kernel/debug/tracing/trace_pipe")
if err != nil {
slog.Error("open trace_pipe", slog.Any("error", err))
return
}
defer tracePipe.Close()
go func() {
scanner := bufio.NewScanner(tracePipe)
for scanner.Scan() {
msg := strings.TrimSpace(scanner.Text())
if strings.Contains(msg, "gmon") {
slog.Debug(msg)
}
}
if err := scanner.Err(); err != nil {
if !errors.Is(err, fs.ErrClosed) {
slog.Error("read trace_pipe", slog.Any("error", err))
}
}
}()
<-done
}
func isGoVersion123OrHigher(v string) bool {
versionSplit := strings.Split(v, ".")
if len(versionSplit) != 3 {
return false
}
if versionSplit[0] != "go1" {
return false
}
minor, err := strconv.Atoi(versionSplit[1])
if err != nil {
return false
}
if minor < 23 {
return false
}
return true
}