Skip to content

feat(cli): support fine-grained server log filtering #8748

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ func (a *agent) createTailnet(ctx context.Context, agentID uuid.UUID, derpMap *t
network, err := tailnet.NewConn(&tailnet.Options{
Addresses: a.wireguardAddresses(agentID),
DERPMap: derpMap,
Logger: a.logger.Named("tailnet"),
Logger: a.logger.Named("net.tailnet"),
ListenPort: a.tailnetListenPort,
BlockEndpoints: disableDirectConnections,
})
Expand Down
67 changes: 60 additions & 7 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
)
if cfg.AccessURL.String() == "" {
cliui.Infof(inv.Stderr, "Opening tunnel so workspaces can connect to your deployment. For production scenarios, specify an external access URL")
tunnel, err = devtunnel.New(ctx, logger.Named("devtunnel"), cfg.WgtunnelHost.String())
tunnel, err = devtunnel.New(ctx, logger.Named("net.devtunnel"), cfg.WgtunnelHost.String())
if err != nil {
return xerrors.Errorf("create tunnel: %w", err)
}
Expand Down Expand Up @@ -1751,6 +1751,50 @@ func IsLocalhost(host string) bool {
return host == "localhost" || host == "127.0.0.1" || host == "::1"
}

var _ slog.Sink = &filterSink{}

type filterSink struct {
next []slog.Sink
re *regexp.Regexp
}

func (f *filterSink) 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 *filterSink) LogEntry(ctx context.Context, ent slog.SinkEntry) {
logName := strings.Join(ent.LoggerNames, ".")
if f.re != nil && !f.re.MatchString(logName) {
return
}
for _, sink := range f.next {
sink.LogEntry(ctx, ent)
}
}

func (f *filterSink) Sync() {
for _, sink := range f.next {
sink.Sync()
}
}

func BuildLogger(inv *clibase.Invocation, cfg *codersdk.DeploymentValues) (slog.Logger, func(), error) {
var (
sinks = []slog.Sink{}
Expand Down Expand Up @@ -1795,16 +1839,25 @@ func BuildLogger(inv *clibase.Invocation, cfg *codersdk.DeploymentValues) (slog.
sinks = append(sinks, tracing.SlogSink{})
}

level := slog.LevelInfo
if cfg.Verbose {
level = slog.LevelDebug
}

// User should log to null device if they don't want logs.
if len(sinks) == 0 {
return slog.Logger{}, nil, xerrors.New("no loggers provided")
}

return slog.Make(sinks...).Leveled(level), func() {
filter := &filterSink{next: sinks}

err = filter.compile(cfg.Logging.Filter.Value())
if err != nil {
return slog.Logger{}, nil, xerrors.Errorf("compile filters: %w", err)
}

level := slog.LevelInfo
// Debug logging is always enabled if a filter is present.
if cfg.Verbose || filter.re != nil {
level = slog.LevelDebug
}

return slog.Make(filter).Leveled(level), func() {
for _, closer := range closers {
_ = closer()
}
Expand Down
10 changes: 5 additions & 5 deletions cli/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ func TestServer(t *testing.T) {

root, _ := clitest.New(t,
"server",
"--verbose",
"--log-filter=.*",
"--in-memory",
"--http-address", ":0",
"--access-url", "http://example.com",
Expand All @@ -1322,7 +1322,7 @@ func TestServer(t *testing.T) {

root, _ := clitest.New(t,
"server",
"--verbose",
"--log-filter=.*",
"--in-memory",
"--http-address", ":0",
"--access-url", "http://example.com",
Expand All @@ -1339,7 +1339,7 @@ func TestServer(t *testing.T) {

root, _ := clitest.New(t,
"server",
"--verbose",
"--log-filter=.*",
"--in-memory",
"--http-address", ":0",
"--access-url", "http://example.com",
Expand All @@ -1359,7 +1359,7 @@ func TestServer(t *testing.T) {

inv, _ := clitest.New(t,
"server",
"--verbose",
"--log-filter=.*",
"--in-memory",
"--http-address", ":0",
"--access-url", "http://example.com",
Expand Down Expand Up @@ -1393,7 +1393,7 @@ func TestServer(t *testing.T) {
// HTTP.
inv, _ := clitest.New(t,
"server",
"--verbose",
"--log-filter=.*",
"--in-memory",
"--http-address", ":0",
"--access-url", "http://example.com",
Expand Down
10 changes: 5 additions & 5 deletions cli/testdata/coder_server_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ Use a YAML configuration file when your server launch become unwieldy.
--log-json string, $CODER_LOGGING_JSON
Output JSON logs to a given file.

-l, --log-filter string-array, $CODER_LOG_FILTER
Filter debug logs by matching against a given regex. Use .* to match
all debug logs.

--log-stackdriver string, $CODER_LOGGING_STACKDRIVER
Output Stackdriver compatible logs to a given file.

-v, --verbose bool, $CODER_VERBOSE
Output debug-level logs.

Introspection / Prometheus Options
--prometheus-address host:port, $CODER_PROMETHEUS_ADDRESS (default: 127.0.0.1:2112)
The bind address to serve prometheus metrics.
Expand All @@ -106,8 +107,7 @@ Use a YAML configuration file when your server launch become unwieldy.
--trace-logs bool, $CODER_TRACE_LOGS
Enables capturing of logs as events in traces. This is useful for
debugging, but may result in a very large amount of events being sent
to the tracing backend which may incur significant costs. If the
verbose flag was supplied, debug-level logs will be included.
to the tracing backend which may incur significant costs.

--trace bool, $CODER_TRACE_ENABLE
Whether application tracing data is collected. It exports to a backend
Expand Down
7 changes: 5 additions & 2 deletions cli/testdata/server-config.yaml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,17 @@ introspection:
enable: false
# Enables capturing of logs as events in traces. This is useful for debugging, but
# may result in a very large amount of events being sent to the tracing backend
# which may incur significant costs. If the verbose flag was supplied, debug-level
# logs will be included.
# which may incur significant costs.
# (default: <unset>, type: bool)
captureLogs: false
logging:
# Output debug-level logs.
# (default: <unset>, type: bool)
verbose: false
# Filter debug logs by matching against a given regex. Use .* to match all debug
# logs.
# (default: <unset>, type: string-array)
filter: []
# Output human-readable logs to a given file.
# (default: /dev/stderr, type: string)
humanPath: /dev/stderr
Expand Down
6 changes: 6 additions & 0 deletions coderd/apidoc/docs.go

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

6 changes: 6 additions & 0 deletions coderd/apidoc/swagger.json

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

2 changes: 1 addition & 1 deletion coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ func (api *API) _dialWorkspaceAgentTailnet(agentID uuid.UUID) (*codersdk.Workspa
conn, err := tailnet.NewConn(&tailnet.Options{
Addresses: []netip.Prefix{netip.PrefixFrom(tailnet.IP(), 128)},
DERPMap: api.DERPMap(),
Logger: api.Logger.Named("tailnet"),
Logger: api.Logger.Named("net.tailnet"),
BlockEndpoints: api.DeploymentValues.DERP.Config.BlockDirect.Value(),
})
if err != nil {
Expand Down
31 changes: 22 additions & 9 deletions codersdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,10 @@ type SwaggerConfig struct {
}

type LoggingConfig struct {
Human clibase.String `json:"human" typescript:",notnull"`
JSON clibase.String `json:"json" typescript:",notnull"`
Stackdriver clibase.String `json:"stackdriver" typescript:",notnull"`
Filter clibase.StringArray `json:"log_filter" typescript:",notnull"`
Human clibase.String `json:"human" typescript:",notnull"`
JSON clibase.String `json:"json" typescript:",notnull"`
Stackdriver clibase.String `json:"stackdriver" typescript:",notnull"`
}

type DangerousConfig struct {
Expand Down Expand Up @@ -533,6 +534,16 @@ when required by your organization's security policy.`,
Group: &deploymentGroupNetworking,
YAML: "redirectToAccessURL",
}
logFilter := clibase.Option{
Name: "Log Filter",
Description: "Filter debug logs by matching against a given regex. Use .* to match all debug logs.",
Flag: "log-filter",
FlagShorthand: "l",
Env: "CODER_LOG_FILTER",
Value: &c.Logging.Filter,
Group: &deploymentGroupIntrospectionLogging,
YAML: "filter",
}
opts := clibase.OptionSet{
{
Name: "Access URL",
Expand Down Expand Up @@ -1159,7 +1170,7 @@ when required by your organization's security policy.`,
},
{
Name: "Capture Logs in Traces",
Description: "Enables capturing of logs as events in traces. This is useful for debugging, but may result in a very large amount of events being sent to the tracing backend which may incur significant costs. If the verbose flag was supplied, debug-level logs will be included.",
Description: "Enables capturing of logs as events in traces. This is useful for debugging, but may result in a very large amount of events being sent to the tracing backend which may incur significant costs.",
Flag: "trace-logs",
Env: "CODER_TRACE_LOGS",
Value: &c.Trace.CaptureLogs,
Expand Down Expand Up @@ -1249,12 +1260,14 @@ when required by your organization's security policy.`,
Flag: "verbose",
Env: "CODER_VERBOSE",
FlagShorthand: "v",

Value: &c.Verbose,
Group: &deploymentGroupIntrospectionLogging,
YAML: "verbose",
Annotations: clibase.Annotations{}.Mark(annotationExternalProxies, "true"),
Hidden: true,
UseInstead: []clibase.Option{logFilter},
Value: &c.Verbose,
Group: &deploymentGroupIntrospectionLogging,
YAML: "verbose",
Annotations: clibase.Annotations{}.Mark(annotationExternalProxies, "true"),
},
logFilter,
{
Name: "Human Log Location",
Description: "Output human-readable logs to a given file.",
Expand Down
1 change: 1 addition & 0 deletions docs/api/general.md

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

14 changes: 9 additions & 5 deletions docs/api/schemas.md

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

22 changes: 11 additions & 11 deletions docs/cli/server.md

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

Loading