From 0b971dffd35fecb4828649e5261683fb1d38ef7a Mon Sep 17 00:00:00 2001 From: Fran Bull Date: Mon, 24 Feb 2025 11:41:53 -0800 Subject: [PATCH] configure logging --- tsconsensus/tsconsensus.go | 11 +++++++++-- tsconsensus/tsconsensus_test.go | 22 ++++++++++++++-------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/tsconsensus/tsconsensus.go b/tsconsensus/tsconsensus.go index bce3b3473..7cb7ce25d 100644 --- a/tsconsensus/tsconsensus.go +++ b/tsconsensus/tsconsensus.go @@ -36,6 +36,7 @@ import ( "net/netip" "time" + "github.com/hashicorp/go-hclog" "github.com/hashicorp/raft" "tailscale.com/ipn/ipnstate" "tailscale.com/tsnet" @@ -223,14 +224,20 @@ func startRaft(ts *tsnet.Server, fsm *raft.FSM, self selfRaftNode, auth *authori return nil, err } - transport := raft.NewNetworkTransport(StreamLayer{ + logger := hclog.New(&hclog.LoggerOptions{ + Name: "raft-net", + Output: cfg.Raft.LogOutput, + Level: hclog.LevelFromString(cfg.Raft.LogLevel), + }) + + transport := raft.NewNetworkTransportWithLogger(StreamLayer{ s: ts, Listener: ln, auth: auth, }, cfg.MaxConnPool, cfg.ConnTimeout, - nil) // TODO pass in proper logging + logger) return raft.NewRaft(config, *fsm, logStore, stableStore, snapshots, transport) } diff --git a/tsconsensus/tsconsensus_test.go b/tsconsensus/tsconsensus_test.go index 899f99f7c..6f3ff3dca 100644 --- a/tsconsensus/tsconsensus_test.go +++ b/tsconsensus/tsconsensus_test.go @@ -182,6 +182,12 @@ func tagNodes(t *testing.T, control *testcontrol.Server, nodeKeys []key.NodePubl } } +func warnLogConfig() Config { + c := DefaultConfig() + c.Raft.LogLevel = "WARN" + return c +} + func TestStart(t *testing.T) { nettest.SkipIfNoNetwork(t) control, controlURL := startControl(t) @@ -195,7 +201,7 @@ func TestStart(t *testing.T) { waitForNodesToBeTaggedInStatus(t, ctx, one, []key.NodePublic{k}, clusterTag) sm := &fsm{} - r, err := Start(ctx, one, (*fsm)(sm), clusterTag, DefaultConfig()) + r, err := Start(ctx, one, (*fsm)(sm), clusterTag, warnLogConfig()) if err != nil { t.Fatal(err) } @@ -303,7 +309,7 @@ func TestApply(t *testing.T) { defer cancel() clusterTag := "tag:whatever" ps, _, _ := startNodesAndWaitForPeerStatus(t, ctx, clusterTag, 2) - cfg := DefaultConfig() + cfg := warnLogConfig() createConsensusCluster(t, ctx, clusterTag, ps, cfg) fut := ps[0].c.raft.Apply([]byte("woo"), 2*time.Second) @@ -353,7 +359,7 @@ func TestConfig(t *testing.T) { defer cancel() clusterTag := "tag:whatever" ps, _, _ := startNodesAndWaitForPeerStatus(t, ctx, clusterTag, 3) - cfg := DefaultConfig() + cfg := warnLogConfig() // test all is well with non default ports cfg.CommandPort = 12347 cfg.RaftPort = 11882 @@ -389,7 +395,7 @@ func TestFollowerFailover(t *testing.T) { defer cancel() clusterTag := "tag:whatever" ps, _, _ := startNodesAndWaitForPeerStatus(t, ctx, clusterTag, 3) - cfg := DefaultConfig() + cfg := warnLogConfig() createConsensusCluster(t, ctx, clusterTag, ps, cfg) smThree := ps[2].sm @@ -431,7 +437,7 @@ func TestFollowerFailover(t *testing.T) { // follower comes back smThreeAgain := &fsm{} - rThreeAgain, err := Start(ctx, ps[2].ts, (*fsm)(smThreeAgain), clusterTag, DefaultConfig()) + rThreeAgain, err := Start(ctx, ps[2].ts, (*fsm)(smThreeAgain), clusterTag, warnLogConfig()) if err != nil { t.Fatal(err) } @@ -451,7 +457,7 @@ func TestRejoin(t *testing.T) { defer cancel() clusterTag := "tag:whatever" ps, control, controlURL := startNodesAndWaitForPeerStatus(t, ctx, clusterTag, 3) - cfg := DefaultConfig() + cfg := warnLogConfig() createConsensusCluster(t, ctx, clusterTag, ps, cfg) for _, p := range ps { defer p.c.Stop(ctx) @@ -487,7 +493,7 @@ func TestOnlyTaggedPeersCanDialRaftPort(t *testing.T) { defer cancel() clusterTag := "tag:whatever" ps, control, controlURL := startNodesAndWaitForPeerStatus(t, ctx, clusterTag, 3) - cfg := DefaultConfig() + cfg := warnLogConfig() createConsensusCluster(t, ctx, clusterTag, ps, cfg) for _, p := range ps { defer p.c.Stop(ctx) @@ -617,7 +623,7 @@ func TestOnlyTaggedPeersCanJoin(t *testing.T) { defer cancel() clusterTag := "tag:whatever" ps, _, controlURL := startNodesAndWaitForPeerStatus(t, ctx, clusterTag, 3) - cfg := DefaultConfig() + cfg := warnLogConfig() createConsensusCluster(t, ctx, clusterTag, ps, cfg) for _, p := range ps { defer p.c.Stop(ctx)