Skip to content
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
1 change: 1 addition & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func init() {
runCmd.Flags().BoolVarP(&state.DBG_debug, "dbg-perf", "", false, "Enables performance debugging server on port 6060")
runCmd.Flags().BoolVarP(&state.DBG_trace, "dbg-trace", "", false, "Enables trace to trace.out")
runCmd.Flags().BoolVarP(&state.DBG_trace_tc, "dbg-trace-tc", "", false, "Enables logging of packet routing")
runCmd.Flags().BoolVarP(&state.DBG_log_json, "json", "j", false, "Enables structued json logging")
runCmd.Flags().StringP("config", "c", DefaultConfigPath, "Path to the config file")
runCmd.Flags().StringP("node", "n", DefaultNodeConfigPath, "Path to the node config file")
runCmd.Flags().StringP("log", "l", "", "Path to the log file (overrides config)")
Expand Down
32 changes: 20 additions & 12 deletions core/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,26 @@ func Start(ccfg state.CentralCfg, ncfg state.LocalCfg, logLevel slog.Level, conf
dispatch := make(chan func(env *state.State) error, 128)

handlers := make([]slog.Handler, 0)
handlers = append(handlers,
tint.NewHandler(os.Stderr, &tint.Options{
Level: logLevel,
AddSource: false,
CustomPrefix: string(ncfg.Id),
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
if attr.Key == "time" {
return slog.Attr{}
}
return attr
},
}))
if state.DBG_log_json {
handlers = append(handlers,
slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: logLevel,
}),
)
} else {
handlers = append(handlers,
tint.NewHandler(os.Stderr, &tint.Options{
Level: logLevel,
AddSource: false,
CustomPrefix: string(ncfg.Id),
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
if attr.Key == "time" {
return slog.Attr{}
}
return attr
},
}))
}

if ncfg.LogPath != "" {
err := os.MkdirAll(path.Dir(ncfg.LogPath), 0700)
Expand Down
7 changes: 4 additions & 3 deletions e2e/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type NodeSpec struct {
IP string
CentralConfigPath string
NodeConfigPath string
ExtraArgs []string
}

func (h *Harness) StartNodes(specs ...NodeSpec) {
Expand All @@ -116,12 +117,12 @@ func (h *Harness) StartNodes(specs ...NodeSpec) {
for _, spec := range specs {
go func(s NodeSpec) {
defer wg.Done()
h.StartNode(s.Name, s.IP, s.CentralConfigPath, s.NodeConfigPath)
h.StartNode(s.Name, s.IP, s.CentralConfigPath, s.NodeConfigPath, s.ExtraArgs...)
}(spec)
}
wg.Wait()
}
func (h *Harness) StartNode(name string, ip string, centralConfigPath, nodeConfigPath string) testcontainers.Container {
func (h *Harness) StartNode(name string, ip string, centralConfigPath, nodeConfigPath string, extraArgs ...string) testcontainers.Container {
h.t.Logf("Starting node %s at %s", name, ip)
req := testcontainers.ContainerRequest{
Image: ImageName,
Expand All @@ -141,7 +142,7 @@ func (h *Harness) StartNode(name string, ip string, centralConfigPath, nodeConfi
FileMode: 0644,
},
},
Cmd: nil, // Entrypoint already handles "run -v"
Cmd: extraArgs,
Env: map[string]string{
"NYLON_LOG_LEVEL": "debug",
},
Expand Down
65 changes: 65 additions & 0 deletions e2e/json_logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//go:build e2e

package e2e

import (
"testing"
"time"

"github.com/encodeous/nylon/state"
)

func TestJSONLogging(t *testing.T) {
h := NewHarness(t)

node1Key := state.GenerateKey()
node2Key := state.GenerateKey()

node1IP := GetIP(h.Subnet, 10)
node2IP := GetIP(h.Subnet, 11)

node1NylonIP := "10.0.0.1"
node2NylonIP := "10.0.0.2"

configDir := h.SetupTestDir()

central := state.CentralCfg{
Routers: []state.RouterCfg{
SimpleRouter("node1", node1Key.Pubkey(), node1NylonIP, node1IP),
SimpleRouter("node2", node2Key.Pubkey(), node2NylonIP, node2IP),
},
Graph: []string{
"node1, node2",
},
Timestamp: time.Now().UnixNano(),
}

centralPath := h.WriteConfig(configDir, "central.yaml", central)

node1Cfg := SimpleLocal("node1", node1Key)
node1Path := h.WriteConfig(configDir, "node1.yaml", node1Cfg)

node2Cfg := SimpleLocal("node2", node2Key)
node2Path := h.WriteConfig(configDir, "node2.yaml", node2Cfg)

h.StartNodes(
NodeSpec{
Name: "node1",
IP: node1IP,
CentralConfigPath: centralPath,
NodeConfigPath: node1Path,
ExtraArgs: []string{"--json"},
},
NodeSpec{
Name: "node2",
IP: node2IP,
CentralConfigPath: centralPath,
NodeConfigPath: node2Path,
ExtraArgs: []string{"--json"},
},
)

t.Log("Waiting for JSON log pattern...")
h.WaitForMatch("node1", `\{"time":".*","level":".*","msg":".*"`)
h.WaitForMatch("node2", `\{"time":".*","level":".*","msg":".*"`)
}
1 change: 1 addition & 0 deletions state/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package state
var DBG_log_probe = false
var DBG_log_wireguard = false
var DBG_log_repo_updates = false
var DBG_log_json = false
var DBG_debug = false
var DBG_trace = false
var DBG_trace_tc = false
Loading