Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0652b18

Browse files
authored
feat: mount pprof and metrics to /api/v2/debug for admins (#20353)
Adds the following debug routes for people with the `debug_info:read`permission:- `/api/v2/debug/pprof` for `net/http/pprof` - `/` - `/cmdline` - `/profile` - `/symbol` - `/trace` - `/*`- `/api/v2/debug/metrics` for Prometheus metrics
1 parent5a18cf4 commit0652b18

File tree

5 files changed

+348
-3
lines changed

5 files changed

+348
-3
lines changed

‎coderd/apidoc/docs.go‎

Lines changed: 132 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/apidoc/swagger.json‎

Lines changed: 120 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/coderd.go‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"io"
1313
"net/http"
14+
httppprof"net/http/pprof"
1415
"net/url"
1516
"path/filepath"
1617
"regexp"
@@ -32,6 +33,7 @@ import (
3233
"github.com/google/uuid"
3334
"github.com/klauspost/compress/zstd"
3435
"github.com/prometheus/client_golang/prometheus"
36+
"github.com/prometheus/client_golang/prometheus/promhttp"
3537
httpSwagger"github.com/swaggo/http-swagger/v2"
3638
"go.opentelemetry.io/otel/trace"
3739
"golang.org/x/xerrors"
@@ -1512,7 +1514,8 @@ func New(options *Options) *API {
15121514
r.Route("/debug",func(r chi.Router) {
15131515
r.Use(
15141516
apiKeyMiddleware,
1515-
// Ensure only owners can access debug endpoints.
1517+
// Ensure only users with the debug_info:read (e.g. only owners)
1518+
// can view debug endpoints.
15161519
func(next http.Handler) http.Handler {
15171520
returnhttp.HandlerFunc(func(rw http.ResponseWriter,r*http.Request) {
15181521
if!api.Authorize(r,policy.ActionRead,rbac.ResourceDebugInfo) {
@@ -1545,6 +1548,41 @@ func New(options *Options) *API {
15451548
})
15461549
}
15471550
r.Method("GET","/expvar",expvar.Handler())// contains DERP metrics as well as cmdline and memstats
1551+
1552+
r.Route("/pprof",func(r chi.Router) {
1553+
r.Use(func(next http.Handler) http.Handler {
1554+
// Some of the pprof handlers strip the `/debug/pprof`
1555+
// prefix, so we need to strip our additional prefix as
1556+
// well.
1557+
returnhttp.StripPrefix("/api/v2",next)
1558+
})
1559+
1560+
// Serve the index HTML page.
1561+
r.Get("/",func(w http.ResponseWriter,r*http.Request) {
1562+
// Redirect to include a trailing slash, otherwise links on
1563+
// the generated HTML page will be broken.
1564+
if!strings.HasSuffix(r.URL.Path,"/") {
1565+
http.Redirect(w,r,"/api/v2/debug/pprof/",http.StatusTemporaryRedirect)
1566+
return
1567+
}
1568+
httppprof.Index(w,r)
1569+
})
1570+
1571+
// Handle any out of the box pprof handlers that don't get
1572+
// dealt with by the default index handler. See httppprof.init.
1573+
r.Get("/cmdline",httppprof.Cmdline)
1574+
r.Get("/profile",httppprof.Profile)
1575+
r.Get("/symbol",httppprof.Symbol)
1576+
r.Get("/trace",httppprof.Trace)
1577+
1578+
// Index will handle any standard and custom runtime/pprof
1579+
// profiles.
1580+
r.Get("/*",httppprof.Index)
1581+
})
1582+
1583+
r.Get("/metrics",promhttp.InstrumentMetricHandler(
1584+
options.PrometheusRegistry,promhttp.HandlerFor(options.PrometheusRegistry, promhttp.HandlerOpts{}),
1585+
).ServeHTTP)
15481586
})
15491587
// Manage OAuth2 applications that can use Coder as an OAuth2 provider.
15501588
r.Route("/oauth2-provider",func(r chi.Router) {

‎coderd/coderdtest/swaggerparser.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ func VerifySwaggerDefinitions(t *testing.T, router chi.Router, swaggerComments [
160160
t.Run(method+" "+route,func(t*testing.T) {
161161
t.Parallel()
162162

163-
// This route is for compatibility purposes and is not documented.
164-
ifroute=="/workspaceagents/me/metadata" {
163+
// Wildcard routes break the swaggo parser, so we do not document
164+
// them.
165+
ifstrings.HasSuffix(route,"/*") {
165166
return
166167
}
167168

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp