From c42273084f62309ef3cdce35a985e7f404335558 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sun, 29 Dec 2019 17:59:42 -0800 Subject: [PATCH] add optional pprof endpoints --- CHANGELOG.md | 4 ++++ cmd/drone-server/config/config.go | 1 + cmd/drone-server/inject_server.go | 19 ++++++++++++++++++- cmd/drone-server/wire_gen.go | 3 ++- version/version.go | 2 +- version/version_test.go | 2 +- 6 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf24a2f3..27d6b5b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.6.4] - 2019-12-30 +### Added +- optionally enable pprof endpoints for profiling, by [@bradrydzewski](https://github.com/bradrydzewski). + ## [1.6.3] - 2019-12-10 ### Fixed - disable caching generated yaml files by commit sha, by [@bradrydzewski](https://github.com/bradrydzewski). diff --git a/cmd/drone-server/config/config.go b/cmd/drone-server/config/config.go index 21953564..1349edc6 100644 --- a/cmd/drone-server/config/config.go +++ b/cmd/drone-server/config/config.go @@ -242,6 +242,7 @@ type ( Host string `envconfig:"DRONE_SERVER_HOST" default:"localhost:8080"` Port string `envconfig:"DRONE_SERVER_PORT" default:":8080"` Proto string `envconfig:"DRONE_SERVER_PROTO" default:"http"` + Pprof bool `envconfig:"DRONE_PPROF_ENABLED"` Acme bool `envconfig:"DRONE_TLS_AUTOCERT"` Email string `envconfig:"DRONE_TLS_EMAIL"` Cert string `envconfig:"DRONE_TLS_CERT"` diff --git a/cmd/drone-server/inject_server.go b/cmd/drone-server/inject_server.go index 9df619b0..07302bd5 100644 --- a/cmd/drone-server/inject_server.go +++ b/cmd/drone-server/inject_server.go @@ -30,12 +30,14 @@ import ( "github.com/google/wire" "github.com/go-chi/chi" + "github.com/go-chi/chi/middleware" "github.com/unrolled/secure" ) type ( healthzHandler http.Handler metricsHandler http.Handler + pprofHandler http.Handler rpcHandlerV1 http.Handler rpcHandlerV2 http.Handler ) @@ -47,6 +49,7 @@ var serverSet = wire.NewSet( web.New, provideHealthz, provideMetric, + providePprof, provideRouter, provideRPC, provideRPC2, @@ -56,7 +59,7 @@ var serverSet = wire.NewSet( // provideRouter is a Wire provider function that returns a // router that is serves the provided handlers. -func provideRouter(api api.Server, web web.Server, rpcv1 rpcHandlerV1, rpcv2 rpcHandlerV2, healthz healthzHandler, metrics *metric.Server) *chi.Mux { +func provideRouter(api api.Server, web web.Server, rpcv1 rpcHandlerV1, rpcv2 rpcHandlerV2, healthz healthzHandler, metrics *metric.Server, pprof pprofHandler) *chi.Mux { r := chi.NewRouter() r.Mount("/healthz", healthz) r.Mount("/metrics", metrics) @@ -64,6 +67,7 @@ func provideRouter(api api.Server, web web.Server, rpcv1 rpcHandlerV1, rpcv2 rpc r.Mount("/rpc/v2", rpcv2) r.Mount("/rpc", rpcv1) r.Mount("/", web.Handler()) + r.Mount("/debug", pprof) return r } @@ -80,6 +84,19 @@ func provideMetric(session core.Session, config config.Config) *metric.Server { return metric.NewServer(session, config.Prometheus.EnableAnonymousAccess) } +// providePprof is a Wire provider function that returns the +// pprof server endpoints. +func providePprof(config config.Config) pprofHandler { + if config.Server.Pprof == false { + return pprofHandler( + http.NotFoundHandler(), + ) + } + return pprofHandler( + middleware.Profiler(), + ) +} + // provideRPC is a Wire provider function that returns an rpc // handler that exposes the build manager to a remote agent. func provideRPC(m manager.BuildManager, config config.Config) rpcHandlerV1 { diff --git a/cmd/drone-server/wire_gen.go b/cmd/drone-server/wire_gen.go index a4f8691a..250a3edb 100644 --- a/cmd/drone-server/wire_gen.go +++ b/cmd/drone-server/wire_gen.go @@ -101,7 +101,8 @@ func InitializeApplication(config2 config.Config) (application, error) { mainRpcHandlerV2 := provideRPC2(buildManager, config2) mainHealthzHandler := provideHealthz() metricServer := provideMetric(session, config2) - mux := provideRouter(server, webServer, mainRpcHandlerV1, mainRpcHandlerV2, mainHealthzHandler, metricServer) + mainPprofHandler := providePprof(config2) + mux := provideRouter(server, webServer, mainRpcHandlerV1, mainRpcHandlerV2, mainHealthzHandler, metricServer, mainPprofHandler) serverServer := provideServer(mux, config2) mainApplication := newApplication(cronScheduler, datadog, runner, serverServer, userStore) return mainApplication, nil diff --git a/version/version.go b/version/version.go index c08869f6..0094f1b6 100644 --- a/version/version.go +++ b/version/version.go @@ -27,7 +27,7 @@ var ( // VersionMinor is for functionality in a backwards-compatible manner. VersionMinor int64 = 6 // VersionPatch is for backwards-compatible bug fixes. - VersionPatch int64 = 3 + VersionPatch int64 = 4 // VersionPre indicates prerelease. VersionPre = "" // VersionDev indicates development branch. Releases will be empty string. diff --git a/version/version_test.go b/version/version_test.go index 628513ec..11c5fe96 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -9,7 +9,7 @@ package version import "testing" func TestVersion(t *testing.T) { - if got, want := Version.String(), "1.6.3"; got != want { + if got, want := Version.String(), "1.6.4"; got != want { t.Errorf("Want version %s, got %s", want, got) } }