diff --git a/Dockerfile b/Dockerfile index 21d108c7..7eead2ca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,7 +39,8 @@ RUN apk --update --no-cache add \ COPY --from=build /usr/local/bin/diun /usr/local/bin/diun RUN diun --version -ENV DIUN_DB_PATH="/data/diun.db" +ENV PROFILER_PATH="/profiler" \ + DIUN_DB_PATH="/data/diun.db" -VOLUME [ "/data" ] +VOLUME [ "/data", "/profiler" ] ENTRYPOINT [ "diun" ] diff --git a/cmd/main.go b/cmd/main.go index 1bc277b4..bef5c76c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "os/signal" + "path" "runtime" "strings" "syscall" @@ -77,8 +78,12 @@ func main() { log.Debug().Msg(cfg.String()) // Profiler - if len(cli.Profiler) > 0 { - profilePath := profile.ProfilePath(cfg.Db.Path) + if len(cli.Profiler) > 0 && len(cli.ProfilerPath) > 0 { + profilerPath := path.Clean(cli.ProfilerPath) + if err = os.MkdirAll(profilerPath, os.ModePerm); err != nil { + log.Fatal().Err(err).Msg("Cannot create profiler folder") + } + profilePath := profile.ProfilePath(profilerPath) switch cli.Profiler { case "cpu": defer profile.Start(profile.CPUProfile, profilePath).Stop() diff --git a/docs/faq.md b/docs/faq.md index d0b56003..349e7290 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -121,3 +121,37 @@ regopts: ``` Or you can tweak the [`schedule` setting](config/watch.md#schedule) with something like `0 */6 * * *` (every 6 hours). + +## Profiling + +Diun provides a simple way to manage runtime/pprof profiling through [`--profiler-path` and `--profiler` flags](usage/cli.md#options): + +```yaml +version: "3.5" + +services: + diun: + image: crazymax/diun:latest + volumes: + - "./data:/data" + - "./profiler:/profiler" + - "/var/run/docker.sock:/var/run/docker.sock" + environment: + - "TZ=Europe/Paris" + - "LOG_LEVEL=info" + - "PROFILER_PATH=/profiler" + - "PROFILER=mem" + - "DIUN_PROVIDERS_DOCKER=true" + restart: always +``` + +Following profilers are available: + +* `cpu` enables cpu profiling +* `mem` enables memory profiling +* `alloc` enables memory profiling and changes which type of memory to profile allocations +* `heap` enables memory profiling and changes which type of memory profiling to profile the heap +* `routines` enables goroutine profiling +* `mutex` enables mutex profiling +* `threads` enables thread creation profiling +* `block` enables block (contention) profiling diff --git a/docs/usage/cli.md b/docs/usage/cli.md index 06f4a886..71878788 100644 --- a/docs/usage/cli.md +++ b/docs/usage/cli.md @@ -15,16 +15,18 @@ Usage: diun Docker image update notifier. More info: https://github.com/crazy-max/diun Flags: - -h, --help Show context-sensitive help. + -h, --help Show context-sensitive help. --version - --config=STRING Diun configuration file ($CONFIG). - --profiler=STRING Profiler to use ($PROFILER). - --log-level="info" Set log level ($LOG_LEVEL). - --log-json Enable JSON logging output ($LOG_JSON). - --log-caller Add file:line of the caller to log output - ($LOG_CALLER). - --log-nocolor Disables the colorized output ($LOG_NOCOLOR). - --test-notif Test notification settings. + --config=STRING Diun configuration file ($CONFIG). + --profiler-path=STRING Base path where profiling files are written + ($PROFILER_PATH). + --profiler=STRING Profiler to use ($PROFILER). + --log-level="info" Set log level ($LOG_LEVEL). + --log-json Enable JSON logging output ($LOG_JSON). + --log-caller Add file:line of the caller to log output + ($LOG_CALLER). + --log-nocolor Disables the colorized output ($LOG_NOCOLOR). + --test-notif Test notification settings. ``` ## Environment variables @@ -34,7 +36,8 @@ Following environment variables can be used in place: | Name | Default | Description | |--------------------|---------------|---------------| | `CONFIG` | | Diun configuration file | -| `PROFILER` | | Profiler to use | +| `PROFILER_PATH` | | Base path where profiling files are written | +| `PROFILER` | | [Profiler](../faq.md#profiling) to use | | `LOG_LEVEL` | `info` | Log level output | | `LOG_JSON` | `false` | Enable JSON logging output | | `LOG_CALLER` | `false` | Enable to add `file:line` of the caller | diff --git a/internal/model/cli.go b/internal/model/cli.go index 0a87aa08..ab0ffda1 100644 --- a/internal/model/cli.go +++ b/internal/model/cli.go @@ -4,12 +4,13 @@ import "github.com/alecthomas/kong" // Cli holds command line args, flags and cmds type Cli struct { - Version kong.VersionFlag - Cfgfile string `kong:"name='config',env='CONFIG',help='Diun configuration file.'"` - Profiler string `kong:"name='profiler',env='PROFILER',enum='cpu,mem,alloc,heap,routines,mutex,threads,block',help='Profiler to use.'"` - LogLevel string `kong:"name='log-level',env='LOG_LEVEL',default='info',help='Set log level.'"` - LogJSON bool `kong:"name='log-json',env='LOG_JSON',default='false',help='Enable JSON logging output.'"` - LogCaller bool `kong:"name='log-caller',env='LOG_CALLER',default='false',help='Add file:line of the caller to log output.'"` - LogNoColor bool `kong:"name='log-nocolor',env='LOG_NOCOLOR',default='false',help='Disables the colorized output.'"` - TestNotif bool `kong:"name='test-notif',default='false',help='Test notification settings.'"` + Version kong.VersionFlag + Cfgfile string `kong:"name='config',env='CONFIG',help='Diun configuration file.'"` + ProfilerPath string `kong:"name='profiler-path',env='PROFILER_PATH',help='Base path where profiling files are written.'"` + Profiler string `kong:"name='profiler',env='PROFILER',enum='cpu,mem,alloc,heap,routines,mutex,threads,block',help='Profiler to use.'"` + LogLevel string `kong:"name='log-level',env='LOG_LEVEL',default='info',help='Set log level.'"` + LogJSON bool `kong:"name='log-json',env='LOG_JSON',default='false',help='Enable JSON logging output.'"` + LogCaller bool `kong:"name='log-caller',env='LOG_CALLER',default='false',help='Add file:line of the caller to log output.'"` + LogNoColor bool `kong:"name='log-nocolor',env='LOG_NOCOLOR',default='false',help='Disables the colorized output.'"` + TestNotif bool `kong:"name='test-notif',default='false',help='Test notification settings.'"` }