Repository navigation
Expand file tree
/
Copy pathdebug.go
More file actions
300 lines (264 loc) · 7.87 KB
/
Copy pathdebug.go
File metadata and controls
300 lines (264 loc) · 7.87 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package debug
import (
"runtime"
"runtime/debug"
"strings"
"sync"
"time"
"github.com/goceleris/celeris"
"github.com/goceleris/celeris/observe"
)
type memStatsCache struct {
mu sync.Mutex
data memoryResponse
cachedAt time.Time
ttl time.Duration
}
type statusResponse struct {
// Uptime is the elapsed time since the middleware was initialized, formatted as a Go duration string.
Uptime string `json:"uptime"`
// GoVersion is the Go toolchain version (e.g. "go1.26").
GoVersion string `json:"go_version"`
}
type memoryResponse struct {
// Alloc is the current heap allocation in bytes.
Alloc uint64 `json:"alloc"`
// TotalAlloc is the cumulative bytes allocated over the process lifetime (never decreases).
TotalAlloc uint64 `json:"total_alloc"`
// Sys is the total bytes of memory obtained from the OS.
Sys uint64 `json:"sys"`
// HeapInuse is the bytes in in-use heap spans.
HeapInuse uint64 `json:"heap_inuse"`
// HeapIdle is the bytes in idle (unused) heap spans.
HeapIdle uint64 `json:"heap_idle"`
// NumGC is the number of completed GC cycles.
NumGC uint32 `json:"num_gc"`
// GCCPUFrac is the fraction of CPU time spent in GC since the program started (0.0 to 1.0).
GCCPUFrac float64 `json:"gc_cpu_fraction"`
}
type buildResponse struct {
// Module is the main module path from build info (e.g. "github.com/goceleris/celeris").
Module string `json:"module"`
// GoVersion is the Go toolchain version used to build the binary.
GoVersion string `json:"go_version"`
// VCS contains version-control metadata (e.g. "vcs.revision", "vcs.time", "vcs.modified").
VCS map[string]string `json:"vcs,omitempty"`
}
type runtimeResponse struct {
// Goroutines is the current number of goroutines.
Goroutines int `json:"goroutines"`
// NumCPU is the number of logical CPUs available to the process.
NumCPU int `json:"num_cpu"`
// GOMAXPROCS is the current value of GOMAXPROCS (max concurrently executing goroutines).
GOMAXPROCS int `json:"gomaxprocs"`
}
// allEndpoints is the canonical list of endpoint names.
var allEndpoints = [...]string{"status", "metrics", "config", "routes", "memory", "build", "runtime"}
func isEndpointEnabled(endpoints map[string]bool, name string) bool {
if endpoints == nil {
return true
}
enabled, ok := endpoints[name]
return ok && enabled
}
// New creates a debug middleware with the given config.
func New(config ...Config) celeris.HandlerFunc {
cfg := defaultConfigCopy()
if len(config) > 0 {
cfg = config[0]
}
cfg = applyDefaults(cfg)
cfg.validate()
skipMap := make(map[string]struct{}, len(cfg.SkipPaths))
for _, p := range cfg.SkipPaths {
skipMap[p] = struct{}{}
}
prefix := strings.TrimRight(cfg.Prefix, "/")
prefixSlash := prefix + "/"
startTime := time.Now()
statusPath := prefix + "/status"
metricsPath := prefix + "/metrics"
configPath := prefix + "/config"
routesPath := prefix + "/routes"
memoryPath := prefix + "/memory"
buildPath := prefix + "/build"
runtimePath := prefix + "/runtime"
endpoints := cfg.Endpoints
msCache := &memStatsCache{ttl: cfg.MemStatsTTL}
// Pre-build the index listing of available endpoints.
available := make([]string, 0, len(allEndpoints))
for _, name := range allEndpoints {
if isEndpointEnabled(endpoints, name) {
available = append(available, prefixSlash+name)
}
}
return func(c *celeris.Context) error {
path := c.Path()
if _, ok := skipMap[path]; ok {
return c.Next()
}
// Check prefix before Skip so the Skip function is only consulted
// for requests that actually target a debug endpoint.
if path != prefix && !strings.HasPrefix(path, prefixSlash) {
return c.Next()
}
if cfg.Skip != nil && cfg.Skip(c) {
return c.Next()
}
if path == prefix || path == prefixSlash {
if cfg.AuthFunc != nil && !cfg.AuthFunc(c) {
return c.NoContent(403)
}
method := c.Method()
if method != "GET" && method != "HEAD" {
return c.NoContent(405)
}
return c.JSON(200, available)
}
if cfg.AuthFunc != nil && !cfg.AuthFunc(c) {
return c.NoContent(403)
}
method := c.Method()
if method != "GET" && method != "HEAD" {
return c.NoContent(405)
}
switch path {
case statusPath:
if !isEndpointEnabled(endpoints, "status") {
return c.NoContent(404)
}
return handleStatus(c, startTime)
case metricsPath:
if !isEndpointEnabled(endpoints, "metrics") {
return c.NoContent(404)
}
return handleMetrics(c, cfg.Collector)
case configPath:
if !isEndpointEnabled(endpoints, "config") {
return c.NoContent(404)
}
return handleConfig(c)
case routesPath:
if !isEndpointEnabled(endpoints, "routes") {
return c.NoContent(404)
}
return handleRoutes(c, cfg.Server)
case memoryPath:
if !isEndpointEnabled(endpoints, "memory") {
return c.NoContent(404)
}
return handleMemory(c, msCache)
case buildPath:
if !isEndpointEnabled(endpoints, "build") {
return c.NoContent(404)
}
return handleBuild(c)
case runtimePath:
if !isEndpointEnabled(endpoints, "runtime") {
return c.NoContent(404)
}
return handleRuntime(c)
default:
return c.NoContent(404)
}
}
}
func handleStatus(c *celeris.Context, startTime time.Time) error {
return c.JSON(200, statusResponse{
Uptime: time.Since(startTime).String(),
GoVersion: runtime.Version(),
})
}
func handleMetrics(c *celeris.Context, collector *observe.Collector) error {
if collector == nil {
return c.NoContent(501)
}
return c.JSON(200, collector.Snapshot())
}
type configResponse struct {
// GoVersion is the Go toolchain version (e.g. "go1.26").
GoVersion string `json:"go_version"`
// GoOS is the target operating system (e.g. "linux", "darwin").
GoOS string `json:"go_os"`
// GoArch is the target architecture (e.g. "amd64", "arm64").
GoArch string `json:"go_arch"`
// NumCPU is the number of logical CPUs available to the process.
NumCPU int `json:"num_cpu"`
// Goroutines is the current number of goroutines at the time of the request.
Goroutines int `json:"goroutines"`
}
var cachedConfig = configResponse{
GoVersion: runtime.Version(),
GoOS: runtime.GOOS,
GoArch: runtime.GOARCH,
NumCPU: runtime.NumCPU(),
}
func handleConfig(c *celeris.Context) error {
resp := cachedConfig
resp.Goroutines = runtime.NumGoroutine()
return c.JSON(200, resp)
}
func handleRoutes(c *celeris.Context, server *celeris.Server) error {
if server == nil {
return c.JSON(200, []any{})
}
return c.JSON(200, server.Routes())
}
func handleMemory(c *celeris.Context, cache *memStatsCache) error {
// Check cache under lock (fast path).
cache.mu.Lock()
if cache.ttl > 0 && time.Since(cache.cachedAt) < cache.ttl {
resp := cache.data
cache.mu.Unlock()
return c.JSON(200, resp)
}
cache.mu.Unlock()
// Read memstats outside the lock to avoid holding the mutex across
// the STW pause that ReadMemStats triggers.
var m runtime.MemStats
runtime.ReadMemStats(&m)
resp := memoryResponse{
Alloc: m.Alloc,
TotalAlloc: m.TotalAlloc,
Sys: m.Sys,
HeapInuse: m.HeapInuse,
HeapIdle: m.HeapIdle,
NumGC: m.NumGC,
GCCPUFrac: m.GCCPUFraction,
}
// Re-acquire to store the fresh data.
cache.mu.Lock()
cache.data = resp
cache.cachedAt = time.Now()
cache.mu.Unlock()
return c.JSON(200, resp)
}
func handleRuntime(c *celeris.Context) error {
return c.JSON(200, runtimeResponse{
Goroutines: runtime.NumGoroutine(),
NumCPU: runtime.NumCPU(),
GOMAXPROCS: runtime.GOMAXPROCS(0),
})
}
// cachedBuild is computed once at init time since build info never changes.
var cachedBuild = func() buildResponse {
resp := buildResponse{
GoVersion: runtime.Version(),
}
if info, ok := debug.ReadBuildInfo(); ok {
resp.Module = info.Main.Path
vcs := make(map[string]string)
for _, s := range info.Settings {
if strings.HasPrefix(s.Key, "vcs.") {
vcs[s.Key] = s.Value
}
}
if len(vcs) > 0 {
resp.VCS = vcs
}
}
return resp
}()
func handleBuild(c *celeris.Context) error {
return c.JSON(200, cachedBuild)
}