Skip to content

Commit c88547c

Browse files
authored
Add Goroutine stack inspector to admin/monitor (#19207)
Continues on from #19202. Following the addition of pprof labels we can now more easily understand the relationship between a goroutine and the requests that spawn them. This PR takes advantage of the labels and adds a few others, then provides a mechanism for the monitoring page to query the pprof goroutine profile. The binary profile that results from this profile is immediately piped in to the google library for parsing this and then stack traces are formed for the goroutines. If the goroutine is within a context or has been created from a goroutine within a process context it will acquire the process description labels for that process. The goroutines are mapped with there associate pids and any that do not have an associated pid are placed in a group at the bottom as unbound. In this way we should be able to more easily examine goroutines that have been stuck. A manager command `gitea manager processes` is also provided that can export the processes (with or without stacktraces) to the command line. Signed-off-by: Andrew Thornton <[email protected]>
1 parent 9c349a4 commit c88547c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1479
-595
lines changed

cmd/manager.go

+30-349
Large diffs are not rendered by default.

cmd/manager_logging.go

+382
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,382 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package cmd
6+
7+
import (
8+
"fmt"
9+
"net/http"
10+
"os"
11+
12+
"code.gitea.io/gitea/modules/log"
13+
"code.gitea.io/gitea/modules/private"
14+
"github.com/urfave/cli"
15+
)
16+
17+
var (
18+
defaultLoggingFlags = []cli.Flag{
19+
cli.StringFlag{
20+
Name: "group, g",
21+
Usage: "Group to add logger to - will default to \"default\"",
22+
}, cli.StringFlag{
23+
Name: "name, n",
24+
Usage: "Name of the new logger - will default to mode",
25+
}, cli.StringFlag{
26+
Name: "level, l",
27+
Usage: "Logging level for the new logger",
28+
}, cli.StringFlag{
29+
Name: "stacktrace-level, L",
30+
Usage: "Stacktrace logging level",
31+
}, cli.StringFlag{
32+
Name: "flags, F",
33+
Usage: "Flags for the logger",
34+
}, cli.StringFlag{
35+
Name: "expression, e",
36+
Usage: "Matching expression for the logger",
37+
}, cli.StringFlag{
38+
Name: "prefix, p",
39+
Usage: "Prefix for the logger",
40+
}, cli.BoolFlag{
41+
Name: "color",
42+
Usage: "Use color in the logs",
43+
}, cli.BoolFlag{
44+
Name: "debug",
45+
},
46+
}
47+
48+
subcmdLogging = cli.Command{
49+
Name: "logging",
50+
Usage: "Adjust logging commands",
51+
Subcommands: []cli.Command{
52+
{
53+
Name: "pause",
54+
Usage: "Pause logging (Gitea will buffer logs up to a certain point and will drop them after that point)",
55+
Flags: []cli.Flag{
56+
cli.BoolFlag{
57+
Name: "debug",
58+
},
59+
},
60+
Action: runPauseLogging,
61+
}, {
62+
Name: "resume",
63+
Usage: "Resume logging",
64+
Flags: []cli.Flag{
65+
cli.BoolFlag{
66+
Name: "debug",
67+
},
68+
},
69+
Action: runResumeLogging,
70+
}, {
71+
Name: "release-and-reopen",
72+
Usage: "Cause Gitea to release and re-open files used for logging",
73+
Flags: []cli.Flag{
74+
cli.BoolFlag{
75+
Name: "debug",
76+
},
77+
},
78+
Action: runReleaseReopenLogging,
79+
}, {
80+
Name: "remove",
81+
Usage: "Remove a logger",
82+
ArgsUsage: "[name] Name of logger to remove",
83+
Flags: []cli.Flag{
84+
cli.BoolFlag{
85+
Name: "debug",
86+
}, cli.StringFlag{
87+
Name: "group, g",
88+
Usage: "Group to add logger to - will default to \"default\"",
89+
},
90+
},
91+
Action: runRemoveLogger,
92+
}, {
93+
Name: "add",
94+
Usage: "Add a logger",
95+
Subcommands: []cli.Command{
96+
{
97+
Name: "console",
98+
Usage: "Add a console logger",
99+
Flags: append(defaultLoggingFlags,
100+
cli.BoolFlag{
101+
Name: "stderr",
102+
Usage: "Output console logs to stderr - only relevant for console",
103+
}),
104+
Action: runAddConsoleLogger,
105+
}, {
106+
Name: "file",
107+
Usage: "Add a file logger",
108+
Flags: append(defaultLoggingFlags, []cli.Flag{
109+
cli.StringFlag{
110+
Name: "filename, f",
111+
Usage: "Filename for the logger - this must be set.",
112+
}, cli.BoolTFlag{
113+
Name: "rotate, r",
114+
Usage: "Rotate logs",
115+
}, cli.Int64Flag{
116+
Name: "max-size, s",
117+
Usage: "Maximum size in bytes before rotation",
118+
}, cli.BoolTFlag{
119+
Name: "daily, d",
120+
Usage: "Rotate logs daily",
121+
}, cli.IntFlag{
122+
Name: "max-days, D",
123+
Usage: "Maximum number of daily logs to keep",
124+
}, cli.BoolTFlag{
125+
Name: "compress, z",
126+
Usage: "Compress rotated logs",
127+
}, cli.IntFlag{
128+
Name: "compression-level, Z",
129+
Usage: "Compression level to use",
130+
},
131+
}...),
132+
Action: runAddFileLogger,
133+
}, {
134+
Name: "conn",
135+
Usage: "Add a net conn logger",
136+
Flags: append(defaultLoggingFlags, []cli.Flag{
137+
cli.BoolFlag{
138+
Name: "reconnect-on-message, R",
139+
Usage: "Reconnect to host for every message",
140+
}, cli.BoolFlag{
141+
Name: "reconnect, r",
142+
Usage: "Reconnect to host when connection is dropped",
143+
}, cli.StringFlag{
144+
Name: "protocol, P",
145+
Usage: "Set protocol to use: tcp, unix, or udp (defaults to tcp)",
146+
}, cli.StringFlag{
147+
Name: "address, a",
148+
Usage: "Host address and port to connect to (defaults to :7020)",
149+
},
150+
}...),
151+
Action: runAddConnLogger,
152+
}, {
153+
Name: "smtp",
154+
Usage: "Add an SMTP logger",
155+
Flags: append(defaultLoggingFlags, []cli.Flag{
156+
cli.StringFlag{
157+
Name: "username, u",
158+
Usage: "Mail server username",
159+
}, cli.StringFlag{
160+
Name: "password, P",
161+
Usage: "Mail server password",
162+
}, cli.StringFlag{
163+
Name: "host, H",
164+
Usage: "Mail server host (defaults to: 127.0.0.1:25)",
165+
}, cli.StringSliceFlag{
166+
Name: "send-to, s",
167+
Usage: "Email address(es) to send to",
168+
}, cli.StringFlag{
169+
Name: "subject, S",
170+
Usage: "Subject header of sent emails",
171+
},
172+
}...),
173+
Action: runAddSMTPLogger,
174+
},
175+
},
176+
},
177+
},
178+
}
179+
)
180+
181+
func runRemoveLogger(c *cli.Context) error {
182+
setup("manager", c.Bool("debug"))
183+
group := c.String("group")
184+
if len(group) == 0 {
185+
group = log.DEFAULT
186+
}
187+
name := c.Args().First()
188+
ctx, cancel := installSignals()
189+
defer cancel()
190+
191+
statusCode, msg := private.RemoveLogger(ctx, group, name)
192+
switch statusCode {
193+
case http.StatusInternalServerError:
194+
return fail("InternalServerError", msg)
195+
}
196+
197+
fmt.Fprintln(os.Stdout, msg)
198+
return nil
199+
}
200+
201+
func runAddSMTPLogger(c *cli.Context) error {
202+
setup("manager", c.Bool("debug"))
203+
vals := map[string]interface{}{}
204+
mode := "smtp"
205+
if c.IsSet("host") {
206+
vals["host"] = c.String("host")
207+
} else {
208+
vals["host"] = "127.0.0.1:25"
209+
}
210+
211+
if c.IsSet("username") {
212+
vals["username"] = c.String("username")
213+
}
214+
if c.IsSet("password") {
215+
vals["password"] = c.String("password")
216+
}
217+
218+
if !c.IsSet("send-to") {
219+
return fmt.Errorf("Some recipients must be provided")
220+
}
221+
vals["sendTos"] = c.StringSlice("send-to")
222+
223+
if c.IsSet("subject") {
224+
vals["subject"] = c.String("subject")
225+
} else {
226+
vals["subject"] = "Diagnostic message from Gitea"
227+
}
228+
229+
return commonAddLogger(c, mode, vals)
230+
}
231+
232+
func runAddConnLogger(c *cli.Context) error {
233+
setup("manager", c.Bool("debug"))
234+
vals := map[string]interface{}{}
235+
mode := "conn"
236+
vals["net"] = "tcp"
237+
if c.IsSet("protocol") {
238+
switch c.String("protocol") {
239+
case "udp":
240+
vals["net"] = "udp"
241+
case "unix":
242+
vals["net"] = "unix"
243+
}
244+
}
245+
if c.IsSet("address") {
246+
vals["address"] = c.String("address")
247+
} else {
248+
vals["address"] = ":7020"
249+
}
250+
if c.IsSet("reconnect") {
251+
vals["reconnect"] = c.Bool("reconnect")
252+
}
253+
if c.IsSet("reconnect-on-message") {
254+
vals["reconnectOnMsg"] = c.Bool("reconnect-on-message")
255+
}
256+
return commonAddLogger(c, mode, vals)
257+
}
258+
259+
func runAddFileLogger(c *cli.Context) error {
260+
setup("manager", c.Bool("debug"))
261+
vals := map[string]interface{}{}
262+
mode := "file"
263+
if c.IsSet("filename") {
264+
vals["filename"] = c.String("filename")
265+
} else {
266+
return fmt.Errorf("filename must be set when creating a file logger")
267+
}
268+
if c.IsSet("rotate") {
269+
vals["rotate"] = c.Bool("rotate")
270+
}
271+
if c.IsSet("max-size") {
272+
vals["maxsize"] = c.Int64("max-size")
273+
}
274+
if c.IsSet("daily") {
275+
vals["daily"] = c.Bool("daily")
276+
}
277+
if c.IsSet("max-days") {
278+
vals["maxdays"] = c.Int("max-days")
279+
}
280+
if c.IsSet("compress") {
281+
vals["compress"] = c.Bool("compress")
282+
}
283+
if c.IsSet("compression-level") {
284+
vals["compressionLevel"] = c.Int("compression-level")
285+
}
286+
return commonAddLogger(c, mode, vals)
287+
}
288+
289+
func runAddConsoleLogger(c *cli.Context) error {
290+
setup("manager", c.Bool("debug"))
291+
vals := map[string]interface{}{}
292+
mode := "console"
293+
if c.IsSet("stderr") && c.Bool("stderr") {
294+
vals["stderr"] = c.Bool("stderr")
295+
}
296+
return commonAddLogger(c, mode, vals)
297+
}
298+
299+
func commonAddLogger(c *cli.Context, mode string, vals map[string]interface{}) error {
300+
if len(c.String("level")) > 0 {
301+
vals["level"] = log.FromString(c.String("level")).String()
302+
}
303+
if len(c.String("stacktrace-level")) > 0 {
304+
vals["stacktraceLevel"] = log.FromString(c.String("stacktrace-level")).String()
305+
}
306+
if len(c.String("expression")) > 0 {
307+
vals["expression"] = c.String("expression")
308+
}
309+
if len(c.String("prefix")) > 0 {
310+
vals["prefix"] = c.String("prefix")
311+
}
312+
if len(c.String("flags")) > 0 {
313+
vals["flags"] = log.FlagsFromString(c.String("flags"))
314+
}
315+
if c.IsSet("color") {
316+
vals["colorize"] = c.Bool("color")
317+
}
318+
group := "default"
319+
if c.IsSet("group") {
320+
group = c.String("group")
321+
}
322+
name := mode
323+
if c.IsSet("name") {
324+
name = c.String("name")
325+
}
326+
ctx, cancel := installSignals()
327+
defer cancel()
328+
329+
statusCode, msg := private.AddLogger(ctx, group, name, mode, vals)
330+
switch statusCode {
331+
case http.StatusInternalServerError:
332+
return fail("InternalServerError", msg)
333+
}
334+
335+
fmt.Fprintln(os.Stdout, msg)
336+
return nil
337+
}
338+
339+
func runPauseLogging(c *cli.Context) error {
340+
ctx, cancel := installSignals()
341+
defer cancel()
342+
343+
setup("manager", c.Bool("debug"))
344+
statusCode, msg := private.PauseLogging(ctx)
345+
switch statusCode {
346+
case http.StatusInternalServerError:
347+
return fail("InternalServerError", msg)
348+
}
349+
350+
fmt.Fprintln(os.Stdout, msg)
351+
return nil
352+
}
353+
354+
func runResumeLogging(c *cli.Context) error {
355+
ctx, cancel := installSignals()
356+
defer cancel()
357+
358+
setup("manager", c.Bool("debug"))
359+
statusCode, msg := private.ResumeLogging(ctx)
360+
switch statusCode {
361+
case http.StatusInternalServerError:
362+
return fail("InternalServerError", msg)
363+
}
364+
365+
fmt.Fprintln(os.Stdout, msg)
366+
return nil
367+
}
368+
369+
func runReleaseReopenLogging(c *cli.Context) error {
370+
ctx, cancel := installSignals()
371+
defer cancel()
372+
373+
setup("manager", c.Bool("debug"))
374+
statusCode, msg := private.ReleaseReopenLogging(ctx)
375+
switch statusCode {
376+
case http.StatusInternalServerError:
377+
return fail("InternalServerError", msg)
378+
}
379+
380+
fmt.Fprintln(os.Stdout, msg)
381+
return nil
382+
}

0 commit comments

Comments
 (0)