Skip to content
Draft
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ devsy provider add devsy-org/devsy-provider-orbstack -o ORBSTACK_MEMORY=8G -o OR
devsy provider set orbstack --option ORBSTACK_ISOLATED=false
```

## Logging

Provider diagnostics are written to stderr as JSON by default. Protocol output
and command streams keep their existing destinations. Set `DEVSY_LOG_LEVEL` to
`error`, `warn`, `info`, `debug`, or `trace`. The standalone default is `info`;
Devsy supplies its effective log level when it starts the provider.

Set `DEVSY_LOG_FORMAT` to `json`, `text`, `plain`, or `logfmt` to choose the
diagnostic format. `auto` selects JSON for the provider process. Invalid values
stop the provider before it runs a command.

## Development

```sh
Expand Down
33 changes: 27 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"

"github.com/devsy-org/devsy/logging"
"github.com/devsy-org/devsy/pkg/log"
"github.com/spf13/cobra"
)
Expand All @@ -30,23 +31,43 @@ func BuildRoot() *cobra.Command {
return rootCmd
}

func initLogger() {
cfg := log.Config{Verbosity: 1}
if os.Getenv("DEVSY_DEBUG") == "true" {
cfg.Debug = true
func initLogger() error {
cfg, invalid := logging.ResolveProvider(
os.Getenv("DEVSY_LOG_LEVEL"), os.Getenv("DEVSY_LOG_FORMAT"),
)
if invalid.Level {
return errors.New("invalid DEVSY_LOG_LEVEL")
}
log.Init(cfg)
if invalid.Format {
return errors.New("invalid DEVSY_LOG_FORMAT")
}
log.Init(
log.Config{
Level: logging.LevelName(cfg.Level),
DefaultLevel: log.LevelInfoName,
Format: string(cfg.Format),
},
)
return nil
}

// Execute propagates the inner process's exit code so devsy sees it verbatim.
func Execute() {
initLogger()
if err := initLogger(); err != nil {
log.Init(log.Config{DefaultLevel: log.LevelInfoName, Format: "json"})
log.Errorf("%v", err)
_ = log.Sync()
os.Exit(1)
}
if err := BuildRoot().Execute(); err != nil {
_ = log.Sync()
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
os.Exit(exitErr.ExitCode())
}
log.Errorf("%v", err)
_ = log.Sync()
os.Exit(1)
}
_ = log.Sync()
}
214 changes: 214 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package cmd

import (
"bytes"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/devsy-org/devsy/pkg/log"
)

const (
testInfoLevel = "info"
testJSONFormat = "json"
testDebugLevel = "debug"
testTraceLevel = "trace"
testWarnLevel = "warn"
)

func TestProviderLogging(t *testing.T) {
if os.Getenv("ORBSTACK_LOG_TEST_HELPER") == "1" {
serveOrbLoggingFixture()
return
}

for _, tc := range []struct {
name string
level string
format string
want []string
}{
{"default", "", "", []string{testInfoLevel, testWarnLevel}},
{"error", "error", testJSONFormat, nil},
{testWarnLevel, testWarnLevel, testJSONFormat, []string{testWarnLevel}},
{testInfoLevel, testInfoLevel, testJSONFormat, []string{testInfoLevel, testWarnLevel}},
{testDebugLevel, testDebugLevel, testJSONFormat, []string{testDebugLevel, testInfoLevel, testWarnLevel}},
{testTraceLevel, testTraceLevel, "auto", []string{testTraceLevel, testDebugLevel, testInfoLevel, testWarnLevel}},
{"uppercase", "TRACE", "AUTO", []string{testTraceLevel, testDebugLevel, testInfoLevel, testWarnLevel}},
} {
t.Run(tc.name, func(t *testing.T) {
canary := t.Name() + "-private-value"
cmd := orbLoggingFixture(t)
cmd.Env = append(
os.Environ(),
"ORBSTACK_LOG_TEST_HELPER=1",
"ORBSTACK_LOG_TEST_EXECUTE=0",
"DEVSY_LOG_LEVEL="+tc.level,
"DEVSY_LOG_FORMAT="+tc.format,
"ORBSTACK_TEST_TOKEN="+canary,
)
var stdout, stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr
if err := cmd.Run(); err != nil {
t.Fatalf("helper: %v: %s", err, stderr.String())
}
if stdout.String() != "protocol output" {
t.Fatalf("stdout = %q", stdout.String())
}
assertRedactedOrbInfo(t, stderr.String(), canary)
levels := orbLoggedLevels(t, &stderr)
if strings.Join(levels, ",") != strings.Join(tc.want, ",") {
t.Fatalf("levels = %v, want %v", levels, tc.want)
}
})
}
}

func TestProviderTextFormats(t *testing.T) {
for _, format := range []string{"text", "plain", "logfmt", "PLAIN", "LOGFMT"} {
t.Run(format, func(t *testing.T) {
cmd := orbLoggingFixture(t)
cmd.Env = append(
os.Environ(),
"ORBSTACK_LOG_TEST_HELPER=1",
"ORBSTACK_LOG_TEST_EXECUTE=0",
"DEVSY_LOG_LEVEL=warn",
"DEVSY_LOG_FORMAT="+format,
)
var stdout, stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr
if err := cmd.Run(); err != nil {
t.Fatalf("helper: %v: %s", err, stderr.String())
}
if stdout.String() != "protocol output" ||
!strings.Contains(stderr.String(), "warn event") ||
strings.Contains(stderr.String(), "info event") {
t.Fatalf("stdout = %q, stderr = %q", stdout.String(), stderr.String())
}
})
}
}

func TestInvalidLoggingConfigStopsBeforeBackend(t *testing.T) {
for _, name := range []string{"DEVSY_LOG_LEVEL", "DEVSY_LOG_FORMAT"} {
t.Run(name, func(t *testing.T) {
dir := t.TempDir()
marker := filepath.Join(dir, "called")
orbctl := prepareOrbBackend(t, dir)
cmd := orbLoggingFixture(t)
cmd.Env = append(
os.Environ(),
"ORBSTACK_LOG_TEST_HELPER=1",
"ORBSTACK_LOG_TEST_EXECUTE=1",
"DEVSY_LOG_LEVEL=info",
"DEVSY_LOG_FORMAT=json",
"ORBSTACK_PATH="+orbctl,
"ORBSTACK_TEST_MARKER="+marker,
name+"=invalid-secret-canary",
)
var stdout, stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr
if err := cmd.Run(); err == nil {
t.Fatal("invalid configuration succeeded")
}
if stdout.Len() != 0 || strings.Contains(stderr.String(), "invalid-secret-canary") ||
!strings.Contains(stderr.String(), "invalid "+name) {
t.Fatalf("stdout = %q, stderr = %q", stdout.String(), stderr.String())
}
if _, err := os.Stat(marker); !os.IsNotExist(err) {
t.Fatalf("backend invoked or marker inaccessible: %v", err)
}
})
}
}

func serveOrbLoggingFixture() {
if os.Getenv("ORBSTACK_LOG_TEST_EXECUTE") == "1" {
os.Args = []string{"provider", "create"}
Execute()
os.Exit(0)
}
if err := initLogger(); err != nil {
os.Exit(2)
}
log.Tracef("trace event")

Check failure on line 138 in cmd/root_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: log.Tracef (typecheck)

Check failure on line 138 in cmd/root_test.go

View workflow job for this annotation

GitHub Actions / Pre-commit Checks

undefined: log.Tracef (typecheck)
log.Debugf("debug event")
log.Infof("info event %s", os.Getenv("ORBSTACK_TEST_TOKEN"))
log.Warnf("warn event")
_ = log.Sync()
if _, err := os.Stdout.WriteString("protocol output"); err != nil {
os.Exit(2)
}
os.Exit(0)
}

func orbLoggedLevels(t *testing.T, stderr *bytes.Buffer) []string {
t.Helper()
var levels []string
dec := json.NewDecoder(stderr)
for dec.More() {
var record struct {
Time string `json:"ts"`
Level string `json:"level"`
Message string `json:"msg"`
}
if err := dec.Decode(&record); err != nil {
t.Fatal(err)
}
if record.Time == "" || !strings.Contains(record.Message, "event") {
t.Fatalf("invalid diagnostic: %+v", record)
}
levels = append(levels, record.Level)
}
return levels
}

func orbLoggingFixture(t *testing.T) *exec.Cmd {
t.Helper()
executable, err := os.Executable()
if err != nil {
t.Fatal(err)
}
return &exec.Cmd{
Path: executable,
Args: []string{executable, "-test.run=^TestProviderLogging$"},
}
}

func prepareOrbBackend(t *testing.T, dir string) string {
t.Helper()
orbctl := filepath.Join(dir, "orbctl")
script := "#!/bin/sh\ntouch \"$ORBSTACK_TEST_MARKER\"\n"
root, err := os.OpenRoot(dir)
if err != nil {
t.Fatal(err)
}
defer func() { _ = root.Close() }()
file, err := root.OpenFile("orbctl", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
if err != nil {
t.Fatal(err)
}
defer func() { _ = file.Close() }()
if _, err := file.WriteString(script); err != nil {
t.Fatal(err)
}
if err := file.Chmod(0o700); err != nil {
t.Fatal(err)
}
return orbctl
}

func assertRedactedOrbInfo(t *testing.T, output, canary string) {
t.Helper()
if strings.Contains(output, "info event") &&
!strings.Contains(output, "info event ***") {
t.Fatalf("secret was not redacted in useful message: %s", output)
}
if strings.Contains(output, canary) {
t.Fatal("diagnostics exposed secret")
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ require (
github.com/devsy-org/api v1.1.0 // indirect
github.com/devsy-org/apiserver v1.5.4 // indirect
github.com/devsy-org/devsy-runtime-sdk v1.6.0 // indirect
github.com/devsy-org/devsy/logging v0.0.0-20261121002232-42213a6fbc3f
github.com/devsy-org/ssh v1.2.9 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/cli v29.8.0+incompatible // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ github.com/devsy-org/devsy v1.24.0 h1:2kGnX6BcPOISP7tT2wx6mJYwz4udqkS6GKvRTSF149
github.com/devsy-org/devsy v1.24.0/go.mod h1:goBCUWaR/Zu412YUbgP1KCHkr1pSQMerWBgTtjSgwH4=
github.com/devsy-org/devsy-runtime-sdk v1.6.0 h1:iEV4Y0Sc3LJNkTLadJSKcWcAf/oCXvC/AkHilrg/KyM=
github.com/devsy-org/devsy-runtime-sdk v1.6.0/go.mod h1:MiBP/fiY83DAS0TueEiJZSHYZ1vq85UR4KJTMfHIM+E=
github.com/devsy-org/devsy/logging v0.0.0-20261121002232-42213a6fbc3f h1:ZEosyV/3ln3ofOJLSbzwAbS4+m/1vstAabv/I2L+lyk=
github.com/devsy-org/devsy/logging v0.0.0-20261121002232-42213a6fbc3f/go.mod h1:kpwoMhSdrhnJfTJ014ht04BLw0DS7FyF33RqIIe8CKs=
github.com/devsy-org/ssh v1.2.9 h1:KHqX1xAplGFanm0FMSAojtiC9nV/UFxUeP/5jU5quak=
github.com/devsy-org/ssh v1.2.9/go.mod h1:Uff10+cSSDZk3bG07u5D9+eQ8GMGqsgE70WCUJWcHv4=
github.com/dgryski/go-metro v0.0.0-20180109044635-280f6062b5bc h1:8WFBn63wegobsYAX0YjD+8suexZDga5CctH4CCTx2+8=
Expand Down
Loading