Fix profiler path (#339)

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2021-04-25 21:07:23 +02:00
committed by GitHub
parent d7d01b6e40
commit de7eeadcd2
5 changed files with 66 additions and 22 deletions

View File

@@ -39,7 +39,8 @@ RUN apk --update --no-cache add \
COPY --from=build /usr/local/bin/diun /usr/local/bin/diun COPY --from=build /usr/local/bin/diun /usr/local/bin/diun
RUN diun --version 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" ] ENTRYPOINT [ "diun" ]

View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"path"
"runtime" "runtime"
"strings" "strings"
"syscall" "syscall"
@@ -77,8 +78,12 @@ func main() {
log.Debug().Msg(cfg.String()) log.Debug().Msg(cfg.String())
// Profiler // Profiler
if len(cli.Profiler) > 0 { if len(cli.Profiler) > 0 && len(cli.ProfilerPath) > 0 {
profilePath := profile.ProfilePath(cfg.Db.Path) 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 { switch cli.Profiler {
case "cpu": case "cpu":
defer profile.Start(profile.CPUProfile, profilePath).Stop() defer profile.Start(profile.CPUProfile, profilePath).Stop()

View File

@@ -121,3 +121,37 @@ regopts:
``` ```
Or you can tweak the [`schedule` setting](config/watch.md#schedule) with something like `0 */6 * * *` (every 6 hours). 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

View File

@@ -15,16 +15,18 @@ Usage: diun
Docker image update notifier. More info: https://github.com/crazy-max/diun Docker image update notifier. More info: https://github.com/crazy-max/diun
Flags: Flags:
-h, --help Show context-sensitive help. -h, --help Show context-sensitive help.
--version --version
--config=STRING Diun configuration file ($CONFIG). --config=STRING Diun configuration file ($CONFIG).
--profiler=STRING Profiler to use ($PROFILER). --profiler-path=STRING Base path where profiling files are written
--log-level="info" Set log level ($LOG_LEVEL). ($PROFILER_PATH).
--log-json Enable JSON logging output ($LOG_JSON). --profiler=STRING Profiler to use ($PROFILER).
--log-caller Add file:line of the caller to log output --log-level="info" Set log level ($LOG_LEVEL).
($LOG_CALLER). --log-json Enable JSON logging output ($LOG_JSON).
--log-nocolor Disables the colorized output ($LOG_NOCOLOR). --log-caller Add file:line of the caller to log output
--test-notif Test notification settings. ($LOG_CALLER).
--log-nocolor Disables the colorized output ($LOG_NOCOLOR).
--test-notif Test notification settings.
``` ```
## Environment variables ## Environment variables
@@ -34,7 +36,8 @@ Following environment variables can be used in place:
| Name | Default | Description | | Name | Default | Description |
|--------------------|---------------|---------------| |--------------------|---------------|---------------|
| `CONFIG` | | Diun configuration file | | `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_LEVEL` | `info` | Log level output |
| `LOG_JSON` | `false` | Enable JSON logging output | | `LOG_JSON` | `false` | Enable JSON logging output |
| `LOG_CALLER` | `false` | Enable to add `file:line` of the caller | | `LOG_CALLER` | `false` | Enable to add `file:line` of the caller |

View File

@@ -4,12 +4,13 @@ import "github.com/alecthomas/kong"
// Cli holds command line args, flags and cmds // Cli holds command line args, flags and cmds
type Cli struct { type Cli struct {
Version kong.VersionFlag Version kong.VersionFlag
Cfgfile string `kong:"name='config',env='CONFIG',help='Diun configuration file.'"` 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.'"` ProfilerPath string `kong:"name='profiler-path',env='PROFILER_PATH',help='Base path where profiling files are written.'"`
LogLevel string `kong:"name='log-level',env='LOG_LEVEL',default='info',help='Set log level.'"` Profiler string `kong:"name='profiler',env='PROFILER',enum='cpu,mem,alloc,heap,routines,mutex,threads,block',help='Profiler to use.'"`
LogJSON bool `kong:"name='log-json',env='LOG_JSON',default='false',help='Enable JSON logging output.'"` LogLevel string `kong:"name='log-level',env='LOG_LEVEL',default='info',help='Set log level.'"`
LogCaller bool `kong:"name='log-caller',env='LOG_CALLER',default='false',help='Add file:line of the caller to log output.'"` LogJSON bool `kong:"name='log-json',env='LOG_JSON',default='false',help='Enable JSON logging output.'"`
LogNoColor bool `kong:"name='log-nocolor',env='LOG_NOCOLOR',default='false',help='Disables the colorized output.'"` LogCaller bool `kong:"name='log-caller',env='LOG_CALLER',default='false',help='Add file:line of the caller to log output.'"`
TestNotif bool `kong:"name='test-notif',default='false',help='Test notification settings.'"` 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.'"`
} }