-
Notifications
You must be signed in to change notification settings - Fork 960
chore: add profiling labels for pprof analysis #19232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package httpmw | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"runtime/pprof" | ||
|
||
"github.com/coder/coder/v2/coderd/pproflabel" | ||
) | ||
|
||
// WithProfilingLabels adds a pprof label to all http request handlers. This is | ||
// primarily used to determine if load is coming from background jobs, or from | ||
// http traffic. | ||
func WithProfilingLabels(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
// Label to differentiate between http and websocket requests. Websocket requests | ||
// are assumed to be long-lived and more resource consuming. | ||
requestType := "http" | ||
if r.Header.Get("Upgrade") == "websocket" { | ||
requestType = "websocket" | ||
} | ||
|
||
pprof.Do(ctx, pproflabel.Service(pproflabel.ServiceHTTPServer, "request_type", requestType), func(ctx context.Context) { | ||
r = r.WithContext(ctx) | ||
next.ServeHTTP(rw, r) | ||
}) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package pproflabel | ||
|
||
import ( | ||
"context" | ||
"runtime/pprof" | ||
) | ||
|
||
// Go is just a convince wrapper to set off a labeled goroutine. | ||
func Go(ctx context.Context, labels pprof.LabelSet, f func(context.Context)) { | ||
go pprof.Do(ctx, labels, f) | ||
} | ||
|
||
const ( | ||
ServiceTag = "service" | ||
|
||
ServiceHTTPServer = "http-api" | ||
ServiceLifecycles = "lifecycle-executor" | ||
ServiceMetricCollector = "metrics-collector" | ||
ServicePrebuildReconciler = "prebuilds-reconciler" | ||
ServiceTerraformProvisioner = "terraform-provisioner" | ||
) | ||
|
||
func Service(name string, pairs ...string) pprof.LabelSet { | ||
return pprof.Labels(append([]string{ServiceTag, name}, pairs...)...) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -333,6 +333,7 @@ func New(ctx context.Context, opts *Options) (*Server, error) { | |
r.Use( | ||
// TODO: @emyrk Should we standardize these in some other package? | ||
httpmw.Recover(s.Logger), | ||
httpmw.WithProfilingLabels, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth discriminating on ws vs https traffic? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh that is a really good idea. They should have that upgrade header iirc. Will add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
tracing.StatusWriterMiddleware, | ||
tracing.Middleware(s.TracerProvider), | ||
httpmw.AttachRequestID, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up: would be cool to label pubsub as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which pubsub? The database pubsub listeners?
coder/coderd/database/pubsub/pubsub.go
Line 246 in cb1ec21