mirror of
https://github.com/go-gitea/gitea.git
synced 2024-10-31 08:37:35 -04:00
c88547ce71
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 <art27@cantab.net>
215 lines
6.1 KiB
Go
215 lines
6.1 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package private
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
)
|
|
|
|
// Shutdown calls the internal shutdown function
|
|
func Shutdown(ctx context.Context) (int, string) {
|
|
reqURL := setting.LocalURL + "api/internal/manager/shutdown"
|
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
|
resp, err := req.Response()
|
|
if err != nil {
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
}
|
|
|
|
return http.StatusOK, "Shutting down"
|
|
}
|
|
|
|
// Restart calls the internal restart function
|
|
func Restart(ctx context.Context) (int, string) {
|
|
reqURL := setting.LocalURL + "api/internal/manager/restart"
|
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
|
resp, err := req.Response()
|
|
if err != nil {
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
}
|
|
|
|
return http.StatusOK, "Restarting"
|
|
}
|
|
|
|
// FlushOptions represents the options for the flush call
|
|
type FlushOptions struct {
|
|
Timeout time.Duration
|
|
NonBlocking bool
|
|
}
|
|
|
|
// FlushQueues calls the internal flush-queues function
|
|
func FlushQueues(ctx context.Context, timeout time.Duration, nonBlocking bool) (int, string) {
|
|
reqURL := setting.LocalURL + "api/internal/manager/flush-queues"
|
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
|
if timeout > 0 {
|
|
req.SetTimeout(timeout+10*time.Second, timeout+10*time.Second)
|
|
}
|
|
req = req.Header("Content-Type", "application/json")
|
|
jsonBytes, _ := json.Marshal(FlushOptions{
|
|
Timeout: timeout,
|
|
NonBlocking: nonBlocking,
|
|
})
|
|
req.Body(jsonBytes)
|
|
resp, err := req.Response()
|
|
if err != nil {
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
}
|
|
|
|
return http.StatusOK, "Flushed"
|
|
}
|
|
|
|
// PauseLogging pauses logging
|
|
func PauseLogging(ctx context.Context) (int, string) {
|
|
reqURL := setting.LocalURL + "api/internal/manager/pause-logging"
|
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
|
resp, err := req.Response()
|
|
if err != nil {
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
}
|
|
|
|
return http.StatusOK, "Logging Paused"
|
|
}
|
|
|
|
// ResumeLogging resumes logging
|
|
func ResumeLogging(ctx context.Context) (int, string) {
|
|
reqURL := setting.LocalURL + "api/internal/manager/resume-logging"
|
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
|
resp, err := req.Response()
|
|
if err != nil {
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
}
|
|
|
|
return http.StatusOK, "Logging Restarted"
|
|
}
|
|
|
|
// ReleaseReopenLogging releases and reopens logging files
|
|
func ReleaseReopenLogging(ctx context.Context) (int, string) {
|
|
reqURL := setting.LocalURL + "api/internal/manager/release-and-reopen-logging"
|
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
|
resp, err := req.Response()
|
|
if err != nil {
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
}
|
|
|
|
return http.StatusOK, "Logging Restarted"
|
|
}
|
|
|
|
// LoggerOptions represents the options for the add logger call
|
|
type LoggerOptions struct {
|
|
Group string
|
|
Name string
|
|
Mode string
|
|
Config map[string]interface{}
|
|
}
|
|
|
|
// AddLogger adds a logger
|
|
func AddLogger(ctx context.Context, group, name, mode string, config map[string]interface{}) (int, string) {
|
|
reqURL := setting.LocalURL + "api/internal/manager/add-logger"
|
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
|
req = req.Header("Content-Type", "application/json")
|
|
jsonBytes, _ := json.Marshal(LoggerOptions{
|
|
Group: group,
|
|
Name: name,
|
|
Mode: mode,
|
|
Config: config,
|
|
})
|
|
req.Body(jsonBytes)
|
|
resp, err := req.Response()
|
|
if err != nil {
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
}
|
|
|
|
return http.StatusOK, "Added"
|
|
}
|
|
|
|
// RemoveLogger removes a logger
|
|
func RemoveLogger(ctx context.Context, group, name string) (int, string) {
|
|
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/remove-logger/%s/%s", url.PathEscape(group), url.PathEscape(name))
|
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
|
resp, err := req.Response()
|
|
if err != nil {
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
}
|
|
|
|
return http.StatusOK, "Removed"
|
|
}
|
|
|
|
// Processes return the current processes from this gitea instance
|
|
func Processes(ctx context.Context, out io.Writer, flat, noSystem, stacktraces, json bool, cancel string) (int, string) {
|
|
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/processes?flat=%t&no-system=%t&stacktraces=%t&json=%t&cancel-pid=%s", flat, noSystem, stacktraces, json, url.QueryEscape(cancel))
|
|
|
|
req := newInternalRequest(ctx, reqURL, "GET")
|
|
resp, err := req.Response()
|
|
if err != nil {
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
}
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err.Error()
|
|
}
|
|
return http.StatusOK, ""
|
|
}
|