Skip to content

Commit

Permalink
[debug] allow to expose activate via config (gomods#1284)
Browse files Browse the repository at this point in the history
This CL addresses issue gomods#1177 introducing two new configuration params
allowing to activate pprof.
- `ATHENS_ENABLE_PPROF`
- `ATHENS_PPROF_PORT`

pprof won't be exposed by default.
  • Loading branch information
benjaminch authored and marpio committed Jun 24, 2019
1 parent dea04b1 commit e832c83
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"os/signal"
"time"

_ "net/http/pprof"

"github.com/gomods/athens/cmd/proxy/actions"
"github.com/gomods/athens/pkg/build"
"github.com/gomods/athens/pkg/config"
Expand Down Expand Up @@ -61,6 +63,15 @@ func main() {
close(idleConnsClosed)
}()

if conf.EnablePprof {
go func() {
// pprof to be exposed on a different port than the application for security matters, not to expose profiling data and avoid DoS attacks (profiling slows down the service)
// https://www.farsightsecurity.com/txt-record/2016/10/28/cmikk-go-remote-profiling/
log.Printf("Starting `pprof` at port %v", conf.PprofPort)
log.Fatal(http.ListenAndServe(conf.PprofPort, nil))
}()
}

log.Printf("Starting application at port %v", conf.Port)
if cert != "" && key != "" {
err = srv.ListenAndServeTLS(conf.TLSCertFile, conf.TLSKeyFile)
Expand Down
8 changes: 8 additions & 0 deletions config.dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ LogLevel = "debug"
# Env override: ATHENS_CLOUD_RUNTIME
CloudRuntime = "none"

# EnablePprof specifies if the pprof endpoints should be exposed.
# Note that this option is not meant to be activated forever on a server
# and should be desactivated once not needed.
EnablePprof = false

# PprofPort specifies the port on which pprof will be exposed if activated.
PprofPort = ":3001"

# The filename for the include exclude filter.
# Env override: ATHENS_FILTER_FILE
#
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Config struct {
ProtocolWorkers int `validate:"required" envconfig:"ATHENS_PROTOCOL_WORKERS"`
LogLevel string `validate:"required" envconfig:"ATHENS_LOG_LEVEL"`
CloudRuntime string `validate:"required" envconfig:"ATHENS_CLOUD_RUNTIME"`
EnablePprof bool `envconfig:"ATHENS_ENABLE_PPROF"`
PprofPort string `envconfig:"ATHENS_PPROF_PORT"`
FilterFile string `envconfig:"ATHENS_FILTER_FILE"`
TraceExporterURL string `envconfig:"ATHENS_TRACE_EXPORTER_URL"`
TraceExporter string `envconfig:"ATHENS_TRACE_EXPORTER"`
Expand Down Expand Up @@ -78,6 +80,8 @@ func defaultConfig() *Config {
ProtocolWorkers: 30,
LogLevel: "debug",
CloudRuntime: "none",
EnablePprof: false,
PprofPort: ":3001",
StatsExporter: "prometheus",
TimeoutConf: TimeoutConf{Timeout: 300},
StorageType: "memory",
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func TestEnvOverrides(t *testing.T) {
StorageType: "minio",
GlobalEndpoint: "mytikas.gomods.io",
Port: ":7000",
EnablePprof: false,
PprofPort: ":3001",
BasicAuthUser: "testuser",
BasicAuthPass: "testpass",
ForceSSL: true,
Expand Down Expand Up @@ -256,6 +258,8 @@ func TestParseExampleConfig(t *testing.T) {
StorageType: "memory",
GlobalEndpoint: "http://localhost:3001",
Port: ":3000",
EnablePprof: false,
PprofPort: ":3001",
BasicAuthUser: "",
BasicAuthPass: "",
Storage: expStorage,
Expand Down Expand Up @@ -297,6 +301,8 @@ func getEnvMap(config *Config) map[string]string {
envVars["ATHENS_STORAGE_TYPE"] = config.StorageType
envVars["ATHENS_GLOBAL_ENDPOINT"] = config.GlobalEndpoint
envVars["ATHENS_PORT"] = config.Port
envVars["ATHENS_ENABLE_PPROF"] = strconv.FormatBool(config.EnablePprof)
envVars["ATHENS_PPROF_PORT"] = config.PprofPort
envVars["BASIC_AUTH_USER"] = config.BasicAuthUser
envVars["BASIC_AUTH_PASS"] = config.BasicAuthPass
envVars["PROXY_FORCE_SSL"] = strconv.FormatBool(config.ForceSSL)
Expand Down

0 comments on commit e832c83

Please sign in to comment.