mirror of
https://github.com/zrepl/zrepl.git
synced 2024-11-24 09:24:09 +01:00
10a14a8c50
package trace: - introduce the concept of tasks and spans, tracked as linked list within ctx - see package-level docs for an overview of the concepts - **main feature 1**: unique stack of task and span IDs - makes it easy to follow a series of log entries in concurrent code - **main feature 2**: ability to produce a chrome://tracing-compatible trace file - either via an env variable or a `zrepl pprof` subcommand - this is not a CPU profile, we already have go pprof for that - but it is very useful to visually inspect where the replication / snapshotter / pruner spends its time ( fixes #307 ) usage in package daemon/logging: - goal: every log entry should have a trace field with the ID stack from package trace - make `logging.GetLogger(ctx, Subsys)` the authoritative `logger.Logger` factory function - the context carries a linked list of injected fields which `logging.GetLogger` adds to the logger it returns - `logging.GetLogger` also uses package `trace` to get the task-and-span-stack and injects it into the returned logger's fields
122 lines
2.7 KiB
Go
122 lines
2.7 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/kr/pretty"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/pflag"
|
|
"github.com/zrepl/yaml-config"
|
|
|
|
"github.com/zrepl/zrepl/cli"
|
|
"github.com/zrepl/zrepl/config"
|
|
"github.com/zrepl/zrepl/daemon/job"
|
|
"github.com/zrepl/zrepl/daemon/logging"
|
|
"github.com/zrepl/zrepl/logger"
|
|
)
|
|
|
|
var configcheckArgs struct {
|
|
format string
|
|
what string
|
|
}
|
|
|
|
var ConfigcheckCmd = &cli.Subcommand{
|
|
Use: "configcheck",
|
|
Short: "check if config can be parsed without errors",
|
|
SetupFlags: func(f *pflag.FlagSet) {
|
|
f.StringVar(&configcheckArgs.format, "format", "", "dump parsed config object [pretty|yaml|json]")
|
|
f.StringVar(&configcheckArgs.what, "what", "all", "what to print [all|config|jobs|logging]")
|
|
},
|
|
Run: func(ctx context.Context, subcommand *cli.Subcommand, args []string) error {
|
|
formatMap := map[string]func(interface{}){
|
|
"": func(i interface{}) {},
|
|
"pretty": func(i interface{}) {
|
|
if _, err := pretty.Println(i); err != nil {
|
|
panic(err)
|
|
}
|
|
},
|
|
"json": func(i interface{}) {
|
|
if err := json.NewEncoder(os.Stdout).Encode(subcommand.Config()); err != nil {
|
|
panic(err)
|
|
}
|
|
},
|
|
"yaml": func(i interface{}) {
|
|
if err := yaml.NewEncoder(os.Stdout).Encode(subcommand.Config()); err != nil {
|
|
panic(err)
|
|
}
|
|
},
|
|
}
|
|
|
|
formatter, ok := formatMap[configcheckArgs.format]
|
|
if !ok {
|
|
return fmt.Errorf("unsupported --format %q", configcheckArgs.format)
|
|
}
|
|
|
|
var hadErr bool
|
|
// further: try to build jobs
|
|
confJobs, err := job.JobsFromConfig(subcommand.Config())
|
|
if err != nil {
|
|
err := errors.Wrap(err, "cannot build jobs from config")
|
|
if configcheckArgs.what == "jobs" {
|
|
return err
|
|
} else {
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
confJobs = nil
|
|
hadErr = true
|
|
}
|
|
}
|
|
|
|
// further: try to build logging outlets
|
|
outlets, err := logging.OutletsFromConfig(*subcommand.Config().Global.Logging)
|
|
if err != nil {
|
|
err := errors.Wrap(err, "cannot build logging from config")
|
|
if configcheckArgs.what == "logging" {
|
|
return err
|
|
} else {
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
outlets = nil
|
|
hadErr = true
|
|
}
|
|
}
|
|
|
|
whatMap := map[string]func(){
|
|
"all": func() {
|
|
o := struct {
|
|
config *config.Config
|
|
jobs []job.Job
|
|
logging *logger.Outlets
|
|
}{
|
|
subcommand.Config(),
|
|
confJobs,
|
|
outlets,
|
|
}
|
|
formatter(o)
|
|
},
|
|
"config": func() {
|
|
formatter(subcommand.Config())
|
|
},
|
|
"jobs": func() {
|
|
formatter(confJobs)
|
|
},
|
|
"logging": func() {
|
|
formatter(outlets)
|
|
},
|
|
}
|
|
|
|
wf, ok := whatMap[configcheckArgs.what]
|
|
if !ok {
|
|
return fmt.Errorf("unsupported --format %q", configcheckArgs.what)
|
|
}
|
|
wf()
|
|
|
|
if hadErr {
|
|
return fmt.Errorf("config parsing failed")
|
|
} else {
|
|
return nil
|
|
}
|
|
},
|
|
}
|