Skip to content

Commit

Permalink
Add profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby authored Nov 24, 2019
2 parents ce97bab + a81d0f1 commit de2d29f
Show file tree
Hide file tree
Showing 104 changed files with 4,959 additions and 11,696 deletions.
7 changes: 5 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
service:
golangci-lint-version: 1.16.0
golangci-lint-version: 1.20.0
run:
deadline: 5m
issues-exit-code: 1
Expand All @@ -13,6 +13,8 @@ linters-settings:
locale: US
gocyclo:
min-complexity: 20
wsl:
allow-trailing-comment: true

issues:
exclude-rules:
Expand All @@ -26,10 +28,11 @@ issues:
- funlen
- linters:
- stylecheck
source: "package (cert_manager|domain_checker)"
source: "package (acme_client_manager|cert_manager|domain_checker)"

linters:
enable-all: true
disable:
- gochecknoglobals
- wsl

2 changes: 1 addition & 1 deletion cmd/a_main-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/gobuffalo/packr"

"github.com/rekby/lets-proxy2/internal/profiler"
"github.com/rekby/lets-proxy2/internal/tlslistener"

"github.com/rekby/lets-proxy2/internal/proxy"
Expand Down Expand Up @@ -40,6 +41,7 @@ type configType struct {
Proxy proxy.Config
CheckDomains domain_checker.Config
Listen tlslistener.Config
Profiler profiler.Config
}

//nolint:maligned
Expand Down Expand Up @@ -135,6 +137,7 @@ func mergeConfigByFilepath(ctx context.Context, c *configType, filename string)
log.DebugFatal(logger, err, "Restore workdir to", zap.String("dir", dir))
}()
}

mergeConfigBytes(ctx, c, content, filename)
}

Expand Down
1 change: 1 addition & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ StorageDir = "storage2"
`), 0600)

var config configType

mergeConfigBytes(ctx, &config, defaultConfig(ctx), "")
mergeConfigByFilepath(ctx, &config, filepath.Join(tmpDir, "config.toml"))
td.CmpDeeply(config.General.IssueTimeout, 1)
Expand Down
4 changes: 2 additions & 2 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func initLogger(config logConfig) *zap.Logger {
if err != nil {
panic("Can't open stderr to log")
}

writers = append(writers, writer)
}

Expand Down Expand Up @@ -77,7 +78,7 @@ func initLogger(config logConfig) *zap.Logger {
func parseLogLevel(logLevelS string) (zapcore.Level, error) {
logLevelS = strings.TrimSpace(logLevelS)
logLevelS = strings.ToLower(logLevelS)
switch logLevelS {
switch logLevelS { //nolint:wsl
case "debug":
return zapcore.DebugLevel, nil
case "info":
Expand All @@ -91,7 +92,6 @@ func parseLogLevel(logLevelS string) (zapcore.Level, error) {
default:
return zapcore.InfoLevel, errors.New("undefined log level")
}

}

func getLogOptions(config logConfig) (res []zap.Option) {
Expand Down
31 changes: 31 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"runtime"
"time"

"go.uber.org/zap/zapcore"

"github.com/rekby/lets-proxy2/internal/cert_manager"
"github.com/rekby/lets-proxy2/internal/profiler"

_ "github.com/kardianos/minwinsvc"
"github.com/rekby/lets-proxy2/internal/acme_client_manager"
Expand Down Expand Up @@ -57,6 +60,8 @@ func startProgram(config *configType) {

logger.Info("StartAutoRenew program version", zap.String("version", version()))

startProfiler(ctx, config.Profiler)

err := os.MkdirAll(config.General.StorageDir, defaultDirMode)
log.InfoFatal(logger, err, "Create storage dir", zap.String("dir", config.General.StorageDir))

Expand Down Expand Up @@ -98,3 +103,29 @@ func startProgram(config *configType) {
}
log.DebugErrorCtx(ctx, effectiveError, "Handle request stopped")
}

func startProfiler(ctx context.Context, config profiler.Config) {
logger := zc.L(ctx)

if !config.Enable {
logger.Info("Profiler disabled")
return
}

go func() {
httpServer := http.Server{
Addr: config.BindAddress,
Handler: profiler.New(logger.Named("profiler"), config),
}

logger.Info("Start profiler", zap.String("bind_address", httpServer.Addr))
err := httpServer.ListenAndServe()
var logLevel zapcore.Level
if err == http.ErrServerClosed {
logLevel = zapcore.InfoLevel
} else {
logLevel = zapcore.ErrorLevel
}
log.LevelParam(logger, logLevel, "Profiler stopped")
}()
}
10 changes: 10 additions & 0 deletions cmd/static/default-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,13 @@ TLSAddresses = [":443"]

# Bind addresses without TLS secure (for HTTP reverse proxy and http-01 validation without redirect to https)
TCPAddresses = []

[Profiler]
Enable = false

# IP networks for allow to use profiler.
# Default - deny from all.
# Example:
# [ "1.2.3.4/32", "192.168.0.0/24", "::1/128" ]
AllowedNetworks = []
BindAddress = "localhost:31344"
Loading

0 comments on commit de2d29f

Please sign in to comment.