Skip to content

feat: add pprof and prometheus metrics to coder server #1266

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

Merged
merged 3 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rename flags
  • Loading branch information
coadler committed May 2, 2022
commit 1c4eb1d1becb334821184ee3992dd6ce4e4bf441
4 changes: 2 additions & 2 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,9 @@ func server() *cobra.Command {

cliflag.StringVarP(root.Flags(), &accessURL, "access-url", "", "CODER_ACCESS_URL", "", "Specifies the external URL to access Coder.")
cliflag.StringVarP(root.Flags(), &address, "address", "a", "CODER_ADDRESS", "127.0.0.1:3000", "The address to serve the API and dashboard.")
cliflag.BoolVarP(root.Flags(), &promEnabled, "enable-prometheus", "", "CODER_ENABLE_PROMETHEUS", false, "Enable serving prometheus metrics on the addressdefined by --prometheus-address.")
cliflag.BoolVarP(root.Flags(), &promEnabled, "prometheus-enable", "", "CODER_PROMETHEUS_ENABLE", false, "Enable serving prometheus metrics on the addressdefined by --prometheus-address.")
cliflag.StringVarP(root.Flags(), &promAddress, "prometheus-address", "", "CODER_PROMETHEUS_ADDRESS", "127.0.0.1:2112", "The address to serve prometheus metrics.")
cliflag.BoolVarP(root.Flags(), &promEnabled, "enable-pprof", "", "CODER_ENABLE_PPROF", false, "Enable serving pprof metrics on the address defined by --pprof-address.")
cliflag.BoolVarP(root.Flags(), &promEnabled, "pprof-enable", "", "CODER_PPROF_ENABLE", false, "Enable serving pprof metrics on the address defined by --pprof-address.")
cliflag.StringVarP(root.Flags(), &pprofAddress, "pprof-address", "", "CODER_PPROF_ADDRESS", "127.0.0.1:6060", "The address to serve pprof.")
// systemd uses the CACHE_DIRECTORY environment variable!
cliflag.StringVarP(root.Flags(), &cacheDir, "cache-dir", "", "CACHE_DIRECTORY", filepath.Join(os.TempDir(), "coder-cache"), "Specifies a directory to cache binaries for provision operations.")
Expand Down
31 changes: 16 additions & 15 deletions coderd/httpmw/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
chimw "github.com/go-chi/chi/v5/middleware"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand All @@ -19,18 +19,18 @@ var (
Name: "requests_processed_total",
Help: "The total number of processed API requests",
}, []string{"code", "method", "path"})
requestsConcurrent = promauto.NewGaugeVec(prometheus.GaugeOpts{
requestsConcurrent = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "coderd",
Subsystem: "api",
Name: "concurrent_requests",
Help: "The number of concurrent API requests",
}, []string{"method", "path"})
websocketsConcurrent = promauto.NewGaugeVec(prometheus.GaugeOpts{
})
websocketsConcurrent = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "coderd",
Subsystem: "api",
Name: "concurrent_websockets",
Help: "The total number of concurrent API websockets",
}, []string{"path"})
})
websocketsDist = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "coderd",
Subsystem: "api",
Expand Down Expand Up @@ -64,11 +64,10 @@ func Prometheus(next http.Handler) http.Handler {
start = time.Now()
method = r.Method
rctx = chi.RouteContext(r.Context())
path = rctx.RoutePattern()
)
sw, ok := w.(middleware.WrapResponseWriter)
sw, ok := w.(chimw.WrapResponseWriter)
if !ok {
panic("dev error: http.ResponseWriter is not middleware.WrapResponseWriter")
panic("dev error: http.ResponseWriter is not chimw.WrapResponseWriter")
}

var (
Expand All @@ -77,24 +76,26 @@ func Prometheus(next http.Handler) http.Handler {
)
// We want to count websockets separately.
if isWebsocketUpgrade(r) {
websocketsConcurrent.WithLabelValues(path).Inc()
defer websocketsConcurrent.WithLabelValues(path).Dec()
websocketsConcurrent.Inc()
defer websocketsConcurrent.Dec()

dist = websocketsDist
distOpts = []string{path}
} else {
requestsConcurrent.WithLabelValues(method, path).Inc()
defer requestsConcurrent.WithLabelValues(method, path).Dec()
requestsConcurrent.Inc()
defer requestsConcurrent.Dec()

dist = requestsDist
distOpts = []string{method, path}
distOpts = []string{method}
}

next.ServeHTTP(w, r)

path := rctx.RoutePattern()
distOpts = append(distOpts, path)
statusStr := strconv.Itoa(sw.Status())

requestsProcessed.WithLabelValues(statusStr, method, path).Inc()
dist.WithLabelValues(distOpts...).Observe(float64(time.Since(start).Milliseconds()))
dist.WithLabelValues(distOpts...).Observe(float64(time.Since(start)) / 1e6)
})
}

Expand Down
7 changes: 4 additions & 3 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface CreateWorkspaceBuildRequest {
// This is likely an enum in an external package ("github.com/coder/coder/coderd/database.WorkspaceTransition")
readonly transition: string
readonly dry_run: boolean
readonly state: string
}

// From codersdk/organizations.go:52:6
Expand Down Expand Up @@ -265,12 +266,12 @@ export interface UpdateUserProfileRequest {
readonly username: string
}

// From codersdk/workspaces.go:94:6
// From codersdk/workspaces.go:95:6
export interface UpdateWorkspaceAutostartRequest {
readonly schedule: string
}

// From codersdk/workspaces.go:114:6
// From codersdk/workspaces.go:115:6
export interface UpdateWorkspaceAutostopRequest {
readonly schedule: string
}
Expand Down Expand Up @@ -366,7 +367,7 @@ export interface WorkspaceAgentResourceMetadata {
readonly cpu_mhz: number
}

// From codersdk/workspacebuilds.go:17:6
// From codersdk/workspacebuilds.go:18:6
export interface WorkspaceBuild {
readonly id: string
readonly created_at: string
Expand Down