Skip to content

Commit b3b3688

Browse files
committed
feat(api): add /debug/ endpoints
1 parent 9429784 commit b3b3688

File tree

8 files changed

+89
-46
lines changed

8 files changed

+89
-46
lines changed

api-docs.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,22 @@ parameters:
11241124
x-example: 13
11251125

11261126
paths:
1127+
/debug/gc:
1128+
post:
1129+
summary: Garbage collector
1130+
description: Run the garbage collector
1131+
responses:
1132+
204:
1133+
description: Successful "OK" reply
1134+
1135+
/debug/pprof/dump:
1136+
post:
1137+
summary: Dump pprof
1138+
description: Dump pprof
1139+
responses:
1140+
204:
1141+
description: Successful "OK" reply
1142+
11271143
/ping:
11281144
get:
11291145
summary: PING test

api/debug/gc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package debug
2+
3+
import (
4+
"net/http"
5+
"runtime"
6+
)
7+
8+
func GC(w http.ResponseWriter, r *http.Request) {
9+
runtime.GC()
10+
w.WriteHeader(http.StatusNoContent)
11+
}

api/debug/pprof.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package debug
2+
3+
import (
4+
"github.com/semaphoreui/semaphore/util"
5+
log "github.com/sirupsen/logrus"
6+
"net/http"
7+
"os"
8+
"path"
9+
"runtime/pprof"
10+
"strconv"
11+
"time"
12+
)
13+
14+
func Dump(w http.ResponseWriter, r *http.Request) {
15+
16+
f, err := os.Create(path.Join(util.Config.Profiling.DumpsDir, "mem-"+strconv.Itoa(int(time.Now().Unix()))+".prof"))
17+
18+
defer f.Close()
19+
20+
if err != nil {
21+
log.WithError(err).WithFields(log.Fields{
22+
"context": "pprof",
23+
}).Error("error creating mem.prof")
24+
http.Error(w, err.Error(), http.StatusInternalServerError)
25+
return
26+
}
27+
28+
err = pprof.WriteHeapProfile(f)
29+
30+
if err != nil {
31+
log.WithError(err).WithFields(log.Fields{
32+
"context": "pprof",
33+
}).Error("Failed to write memory profile")
34+
http.Error(w, err.Error(), http.StatusInternalServerError)
35+
return
36+
}
37+
38+
w.WriteHeader(http.StatusNoContent)
39+
}

api/router.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"embed"
66
"fmt"
7+
"github.com/semaphoreui/semaphore/api/debug"
78
"github.com/semaphoreui/semaphore/pkg/tz"
89
"net/http"
910
"os"
@@ -147,6 +148,10 @@ func Route() *mux.Router {
147148

148149
adminAPI.Path("/cache").HandlerFunc(clearCache).Methods("DELETE", "HEAD")
149150

151+
debugAPI := adminAPI.PathPrefix("/debug").Subrouter()
152+
debugAPI.Path("/gc").HandlerFunc(debug.GC).Methods("POST")
153+
debugAPI.Path("/pprof/dump").HandlerFunc(debug.Dump).Methods("POST")
154+
150155
globalRunnersAPI := adminAPI.PathPrefix("/runners").Subrouter()
151156
globalRunnersAPI.Use(globalRunnerMiddleware)
152157
globalRunnersAPI.Path("/{runner_id}").HandlerFunc(getGlobalRunner).Methods("GET", "HEAD")

cli/cmd/root.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/semaphoreui/semaphore/services/profiling"
65
"net/http"
76
"net/url"
87
"os"
@@ -84,7 +83,6 @@ func runService() {
8483
go sockets.StartWS()
8584
go schedulePool.Run()
8685
go taskPool.Run()
87-
go profiling.StartProfiling()
8886

8987
route := api.Route()
9088

openapi.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ tags:
2323
- name: integration
2424
description: Integration API
2525
paths:
26+
/debug/gc:
27+
post:
28+
summary: Garbage collector
29+
description: Run the garbage collector
30+
responses:
31+
'204':
32+
description: Successful "OK" reply
33+
34+
/debug/pprof/dump:
35+
post:
36+
summary: Dump pprof
37+
description: Dump pprof
38+
responses:
39+
'204':
40+
description: Successful "OK" reply
41+
2642
"/ping":
2743
get:
2844
summary: PING test

services/profiling/pprof.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

util/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ type ScheduleConfig struct {
208208
}
209209

210210
type ProfilingConfig struct {
211-
Enabled bool `json:"enabled,omitempty" env:"SEMAPHORE_PROFILING_ENABLED"`
212-
Port int `json:"port,omitempty" env:"SEMAPHORE_PROFILING_PORT" default:"6060"`
211+
Enabled bool `json:"enabled,omitempty" env:"SEMAPHORE_PROFILING_ENABLED"`
212+
DumpsDir string `json:"dumps_dir,omitempty" env:"SEMAPHORE_PROFILING_DUMPS_DIR" default:"/tmp/semaphore/profiler"`
213213
}
214214

215215
// ConfigType mapping between Config and the json file that sets it

0 commit comments

Comments
 (0)