add optional pprof endpoints

This commit is contained in:
Brad Rydzewski 2019-12-29 17:59:42 -08:00
parent 45a3181f50
commit c42273084f
6 changed files with 27 additions and 4 deletions

View file

@ -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).

View file

@ -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"`

View file

@ -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 {

View file

@ -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

View file

@ -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.

View file

@ -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)
}
}