- Notifications
You must be signed in to change notification settings - Fork1k
fix(enterprise/cli): add CODER_PROVISIONER_DAEMON_LOG_* options#11279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
1377d01
refactor(coder/cli): extract BuildLogger to clilog
johnstcn1963c14
move to functional options
johnstcn15e5e1c
refactor(cli): use clilog instead of BuildLogger
johnstcne83c1c0
fix(enterprise/cli): plumb through CODER_PROVISIONERD_LOG_* options
johnstcn8249612
address linter complaints
johnstcn5120a27
make fmt
johnstcn69e13ed
make gen
johnstcn98b2758
address PR comments
johnstcnf2642d0
fixup! address PR comments
johnstcnFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
211 changes: 211 additions & 0 deletionscli/clilog/clilog.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
package clilog | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"regexp" | ||
"strings" | ||
"golang.org/x/xerrors" | ||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/sloghuman" | ||
"cdr.dev/slog/sloggers/slogjson" | ||
"cdr.dev/slog/sloggers/slogstackdriver" | ||
"github.com/coder/coder/v2/cli/clibase" | ||
"github.com/coder/coder/v2/coderd/tracing" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
type ( | ||
Option func(*Builder) | ||
Builder struct { | ||
Filter []string | ||
Human string | ||
JSON string | ||
Stackdriver string | ||
Trace bool | ||
Verbose bool | ||
} | ||
) | ||
func New(opts ...Option) *Builder { | ||
b := &Builder{} | ||
for _, opt := range opts { | ||
opt(b) | ||
} | ||
return b | ||
} | ||
func WithFilter(filters ...string) Option { | ||
return func(b *Builder) { | ||
b.Filter = filters | ||
} | ||
} | ||
func WithHuman(loc string) Option { | ||
return func(b *Builder) { | ||
b.Human = loc | ||
} | ||
} | ||
func WithJSON(loc string) Option { | ||
return func(b *Builder) { | ||
b.JSON = loc | ||
} | ||
} | ||
func WithStackdriver(loc string) Option { | ||
return func(b *Builder) { | ||
b.Stackdriver = loc | ||
} | ||
} | ||
func WithTrace() Option { | ||
return func(b *Builder) { | ||
b.Trace = true | ||
} | ||
} | ||
func WithVerbose() Option { | ||
return func(b *Builder) { | ||
b.Verbose = true | ||
} | ||
} | ||
func FromDeploymentValues(vals *codersdk.DeploymentValues) Option { | ||
return func(b *Builder) { | ||
b.Filter = vals.Logging.Filter.Value() | ||
b.Human = vals.Logging.Human.Value() | ||
b.JSON = vals.Logging.JSON.Value() | ||
b.Stackdriver = vals.Logging.Stackdriver.Value() | ||
b.Trace = vals.Trace.Enable.Value() | ||
b.Verbose = vals.Verbose.Value() | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
} | ||
func (b *Builder) Build(inv *clibase.Invocation) (log slog.Logger, closeLog func(), err error) { | ||
var ( | ||
sinks = []slog.Sink{} | ||
closers = []func() error{} | ||
) | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
defer func() { | ||
if err != nil { | ||
for _, closer := range closers { | ||
_ = closer() | ||
} | ||
} | ||
}() | ||
noopClose := func() {} | ||
addSinkIfProvided := func(sinkFn func(io.Writer) slog.Sink, loc string) error { | ||
switch loc { | ||
case "": | ||
case "/dev/stdout": | ||
sinks = append(sinks, sinkFn(inv.Stdout)) | ||
case "/dev/stderr": | ||
sinks = append(sinks, sinkFn(inv.Stderr)) | ||
default: | ||
fi, err := os.OpenFile(loc, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644) | ||
if err != nil { | ||
return xerrors.Errorf("open log file %q: %w", loc, err) | ||
} | ||
closers = append(closers, fi.Close) | ||
sinks = append(sinks, sinkFn(fi)) | ||
} | ||
return nil | ||
} | ||
err = addSinkIfProvided(sloghuman.Sink, b.Human) | ||
if err != nil { | ||
return slog.Logger{}, noopClose, xerrors.Errorf("add human sink: %w", err) | ||
} | ||
err = addSinkIfProvided(slogjson.Sink, b.JSON) | ||
if err != nil { | ||
return slog.Logger{}, noopClose, xerrors.Errorf("add json sink: %w", err) | ||
} | ||
err = addSinkIfProvided(slogstackdriver.Sink, b.Stackdriver) | ||
if err != nil { | ||
return slog.Logger{}, noopClose, xerrors.Errorf("add stackdriver sink: %w", err) | ||
} | ||
if b.Trace { | ||
sinks = append(sinks, tracing.SlogSink{}) | ||
} | ||
// User should log to null device if they don't want logs. | ||
if len(sinks) == 0 { | ||
return slog.Logger{}, noopClose, xerrors.New("no loggers provided, use /dev/null to disable logging") | ||
} | ||
filter := &debugFilterSink{next: sinks} | ||
err = filter.compile(b.Filter) | ||
if err != nil { | ||
return slog.Logger{}, noopClose, xerrors.Errorf("compile filters: %w", err) | ||
} | ||
level := slog.LevelInfo | ||
// Debug logging is always enabled if a filter is present. | ||
if b.Verbose || filter.re != nil { | ||
level = slog.LevelDebug | ||
} | ||
return inv.Logger.AppendSinks(filter).Leveled(level), func() { | ||
for _, closer := range closers { | ||
_ = closer() | ||
} | ||
}, nil | ||
} | ||
var _ slog.Sink = &debugFilterSink{} | ||
type debugFilterSink struct { | ||
next []slog.Sink | ||
re *regexp.Regexp | ||
} | ||
func (f *debugFilterSink) compile(res []string) error { | ||
if len(res) == 0 { | ||
return nil | ||
} | ||
var reb strings.Builder | ||
for i, re := range res { | ||
_, _ = fmt.Fprintf(&reb, "(%s)", re) | ||
if i != len(res)-1 { | ||
_, _ = reb.WriteRune('|') | ||
} | ||
} | ||
re, err := regexp.Compile(reb.String()) | ||
if err != nil { | ||
return xerrors.Errorf("compile regex: %w", err) | ||
} | ||
f.re = re | ||
return nil | ||
} | ||
func (f *debugFilterSink) LogEntry(ctx context.Context, ent slog.SinkEntry) { | ||
if ent.Level == slog.LevelDebug { | ||
logName := strings.Join(ent.LoggerNames, ".") | ||
if f.re != nil && !f.re.MatchString(logName) && !f.re.MatchString(ent.Message) { | ||
return | ||
} | ||
} | ||
for _, sink := range f.next { | ||
sink.LogEntry(ctx, ent) | ||
} | ||
} | ||
func (f *debugFilterSink) Sync() { | ||
for _, sink := range f.next { | ||
sink.Sync() | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.