Enable configuring delegated zap-logging for embed server.

This commit is contained in:
Piotr Tabor 2021-03-16 22:21:13 +01:00
parent a84bd093b0
commit 725a8c5e02
5 changed files with 29 additions and 31 deletions

View File

@ -21,6 +21,7 @@ import (
"os" "os"
"sync" "sync"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"go.etcd.io/etcd/pkg/v3/logutil" "go.etcd.io/etcd/pkg/v3/logutil"
"go.uber.org/zap" "go.uber.org/zap"
@ -232,7 +233,11 @@ func NewZapCoreLoggerBuilder(lg *zap.Logger, cr zapcore.Core, syncer zapcore.Wri
cfg.loggerWriteSyncer = syncer cfg.loggerWriteSyncer = syncer
grpcLogOnce.Do(func() { grpcLogOnce.Do(func() {
grpclog.SetLoggerV2(logutil.NewGRPCLoggerV2FromZapCore(cr, syncer)) if cr != nil && syncer != nil {
grpclog.SetLoggerV2(logutil.NewGRPCLoggerV2FromZapCore(cr, syncer))
} else {
grpc_zap.ReplaceGrpcLoggerV2(cfg.logger.Named("grpc"))
}
}) })
return nil return nil
} }

View File

@ -29,6 +29,7 @@ import (
"go.etcd.io/etcd/pkg/v3/fileutil" "go.etcd.io/etcd/pkg/v3/fileutil"
"go.etcd.io/etcd/pkg/v3/testutil" "go.etcd.io/etcd/pkg/v3/testutil"
"go.etcd.io/etcd/server/v3/embed" "go.etcd.io/etcd/server/v3/embed"
"go.etcd.io/etcd/tests/v3/integration"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -64,15 +65,11 @@ func createSnapshotFile(t *testing.T, kvs []kv) string {
urls := newEmbedURLs(clusterN * 2) urls := newEmbedURLs(clusterN * 2)
cURLs, pURLs := urls[:clusterN], urls[clusterN:] cURLs, pURLs := urls[:clusterN], urls[clusterN:]
cfg := embed.NewConfig() cfg := integration.NewEmbedConfig(t, "default")
cfg.Logger = "zap"
cfg.LogOutputs = []string{"/dev/null"}
cfg.Name = "default"
cfg.ClusterState = "new" cfg.ClusterState = "new"
cfg.LCUrls, cfg.ACUrls = cURLs, cURLs cfg.LCUrls, cfg.ACUrls = cURLs, cURLs
cfg.LPUrls, cfg.APUrls = pURLs, pURLs cfg.LPUrls, cfg.APUrls = pURLs, pURLs
cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, pURLs[0].String()) cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, pURLs[0].String())
cfg.Dir = filepath.Join(t.TempDir(), fmt.Sprint(time.Now().Nanosecond()))
srv, err := embed.StartEtcd(cfg) srv, err := embed.StartEtcd(cfg)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -18,7 +18,6 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"path/filepath"
"testing" "testing"
"time" "time"
@ -26,6 +25,7 @@ import (
"go.etcd.io/etcd/pkg/v3/testutil" "go.etcd.io/etcd/pkg/v3/testutil"
"go.etcd.io/etcd/server/v3/embed" "go.etcd.io/etcd/server/v3/embed"
"go.etcd.io/etcd/server/v3/etcdserver" "go.etcd.io/etcd/server/v3/etcdserver"
"go.etcd.io/etcd/tests/v3/integration"
) )
// TestSnapshotV3RestoreMultiMemberAdd ensures that multiple members // TestSnapshotV3RestoreMultiMemberAdd ensures that multiple members
@ -37,6 +37,7 @@ func TestSnapshotV3RestoreMultiMemberAdd(t *testing.T) {
clusterN := 3 clusterN := 3
cURLs, pURLs, srvs := restoreCluster(t, clusterN, dbPath) cURLs, pURLs, srvs := restoreCluster(t, clusterN, dbPath)
defer func() { defer func() {
for i := 0; i < clusterN; i++ { for i := 0; i < clusterN; i++ {
os.RemoveAll(srvs[i].Config().Dir) os.RemoveAll(srvs[i].Config().Dir)
@ -62,10 +63,7 @@ func TestSnapshotV3RestoreMultiMemberAdd(t *testing.T) {
// wait for membership reconfiguration apply // wait for membership reconfiguration apply
time.Sleep(testutil.ApplyTimeout) time.Sleep(testutil.ApplyTimeout)
cfg := embed.NewConfig() cfg := integration.NewEmbedConfig(t, "3")
cfg.Logger = "zap"
cfg.LogOutputs = []string{"/dev/null"}
cfg.Name = "3"
cfg.InitialClusterToken = testClusterTkn cfg.InitialClusterToken = testClusterTkn
cfg.ClusterState = "existing" cfg.ClusterState = "existing"
cfg.LCUrls, cfg.ACUrls = newCURLs, newCURLs cfg.LCUrls, cfg.ACUrls = newCURLs, newCURLs
@ -76,7 +74,6 @@ func TestSnapshotV3RestoreMultiMemberAdd(t *testing.T) {
} }
cfg.InitialCluster = cfg.InitialCluster[1:] cfg.InitialCluster = cfg.InitialCluster[1:]
cfg.InitialCluster += fmt.Sprintf(",%s=%s", cfg.Name, newPURLs[0].String()) cfg.InitialCluster += fmt.Sprintf(",%s=%s", cfg.Name, newPURLs[0].String())
cfg.Dir = filepath.Join(t.TempDir(), fmt.Sprint(time.Now().Nanosecond()))
srv, err := embed.StartEtcd(cfg) srv, err := embed.StartEtcd(cfg)
if err != nil { if err != nil {

View File

@ -29,6 +29,7 @@ import (
"go.etcd.io/etcd/etcdctl/v3/snapshot" "go.etcd.io/etcd/etcdctl/v3/snapshot"
"go.etcd.io/etcd/pkg/v3/testutil" "go.etcd.io/etcd/pkg/v3/testutil"
"go.etcd.io/etcd/server/v3/embed" "go.etcd.io/etcd/server/v3/embed"
"go.etcd.io/etcd/tests/v3/integration"
"go.uber.org/zap/zaptest" "go.uber.org/zap/zaptest"
) )
@ -43,16 +44,12 @@ func TestSnapshotV3RestoreSingle(t *testing.T) {
urls := newEmbedURLs(clusterN * 2) urls := newEmbedURLs(clusterN * 2)
cURLs, pURLs := urls[:clusterN], urls[clusterN:] cURLs, pURLs := urls[:clusterN], urls[clusterN:]
cfg := embed.NewConfig() cfg := integration.NewEmbedConfig(t, "s1")
cfg.Logger = "zap"
cfg.LogOutputs = []string{"/dev/null"}
cfg.Name = "s1"
cfg.InitialClusterToken = testClusterTkn cfg.InitialClusterToken = testClusterTkn
cfg.ClusterState = "existing" cfg.ClusterState = "existing"
cfg.LCUrls, cfg.ACUrls = cURLs, cURLs cfg.LCUrls, cfg.ACUrls = cURLs, cURLs
cfg.LPUrls, cfg.APUrls = pURLs, pURLs cfg.LPUrls, cfg.APUrls = pURLs, pURLs
cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, pURLs[0].String()) cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, pURLs[0].String())
cfg.Dir = filepath.Join(t.TempDir(), fmt.Sprint(time.Now().Nanosecond()))
sp := snapshot.NewV3(zaptest.NewLogger(t)) sp := snapshot.NewV3(zaptest.NewLogger(t))
pss := make([]string, 0, len(pURLs)) pss := make([]string, 0, len(pURLs))
@ -108,13 +105,11 @@ func TestSnapshotV3RestoreSingle(t *testing.T) {
func TestSnapshotV3RestoreMulti(t *testing.T) { func TestSnapshotV3RestoreMulti(t *testing.T) {
kvs := []kv{{"foo1", "bar1"}, {"foo2", "bar2"}, {"foo3", "bar3"}} kvs := []kv{{"foo1", "bar1"}, {"foo2", "bar2"}, {"foo3", "bar3"}}
dbPath := createSnapshotFile(t, kvs) dbPath := createSnapshotFile(t, kvs)
defer os.RemoveAll(dbPath)
clusterN := 3 clusterN := 3
cURLs, _, srvs := restoreCluster(t, clusterN, dbPath) cURLs, _, srvs := restoreCluster(t, clusterN, dbPath)
defer func() { defer func() {
for i := 0; i < clusterN; i++ { for i := 0; i < clusterN; i++ {
os.RemoveAll(srvs[i].Config().Dir)
srvs[i].Close() srvs[i].Close()
} }
}() }()
@ -143,7 +138,8 @@ func TestSnapshotV3RestoreMulti(t *testing.T) {
// TestCorruptedBackupFileCheck tests if we can correctly identify a corrupted backup file. // TestCorruptedBackupFileCheck tests if we can correctly identify a corrupted backup file.
func TestCorruptedBackupFileCheck(t *testing.T) { func TestCorruptedBackupFileCheck(t *testing.T) {
dbPath := "testdata/corrupted_backup.db" dbPath := integration.MustAbsPath("testdata/corrupted_backup.db")
integration.BeforeTest(t)
if _, err := os.Stat(dbPath); err != nil { if _, err := os.Stat(dbPath); err != nil {
t.Fatalf("test file [%s] does not exist: %v", dbPath, err) t.Fatalf("test file [%s] does not exist: %v", dbPath, err)
} }
@ -178,21 +174,16 @@ func createSnapshotFile(t *testing.T, kvs []kv) string {
urls := newEmbedURLs(clusterN * 2) urls := newEmbedURLs(clusterN * 2)
cURLs, pURLs := urls[:clusterN], urls[clusterN:] cURLs, pURLs := urls[:clusterN], urls[clusterN:]
cfg := embed.NewConfig() cfg := integration.NewEmbedConfig(t, "default")
cfg.Logger = "zap"
cfg.LogOutputs = []string{"/dev/null"}
cfg.Name = "default"
cfg.ClusterState = "new" cfg.ClusterState = "new"
cfg.LCUrls, cfg.ACUrls = cURLs, cURLs cfg.LCUrls, cfg.ACUrls = cURLs, cURLs
cfg.LPUrls, cfg.APUrls = pURLs, pURLs cfg.LPUrls, cfg.APUrls = pURLs, pURLs
cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, pURLs[0].String()) cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, pURLs[0].String())
cfg.Dir = filepath.Join(t.TempDir(), fmt.Sprint(time.Now().Nanosecond()))
srv, err := embed.StartEtcd(cfg) srv, err := embed.StartEtcd(cfg)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer func() { defer func() {
os.RemoveAll(cfg.Dir)
srv.Close() srv.Close()
}() }()
select { select {
@ -244,18 +235,15 @@ func restoreCluster(t *testing.T, clusterN int, dbPath string) (
cfgs := make([]*embed.Config, clusterN) cfgs := make([]*embed.Config, clusterN)
for i := 0; i < clusterN; i++ { for i := 0; i < clusterN; i++ {
cfg := embed.NewConfig() cfg := integration.NewEmbedConfig(t, fmt.Sprintf("%d", i))
cfg.Logger = "zap"
cfg.LogOutputs = []string{"/dev/null"}
cfg.Name = fmt.Sprintf("%d", i)
cfg.InitialClusterToken = testClusterTkn cfg.InitialClusterToken = testClusterTkn
cfg.ClusterState = "existing" cfg.ClusterState = "existing"
cfg.LCUrls, cfg.ACUrls = []url.URL{cURLs[i]}, []url.URL{cURLs[i]} cfg.LCUrls, cfg.ACUrls = []url.URL{cURLs[i]}, []url.URL{cURLs[i]}
cfg.LPUrls, cfg.APUrls = []url.URL{pURLs[i]}, []url.URL{pURLs[i]} cfg.LPUrls, cfg.APUrls = []url.URL{pURLs[i]}, []url.URL{pURLs[i]}
cfg.InitialCluster = ics cfg.InitialCluster = ics
cfg.Dir = filepath.Join(t.TempDir(), fmt.Sprint(time.Now().Nanosecond()+i))
sp := snapshot.NewV3(zaptest.NewLogger(t)) sp := snapshot.NewV3(zaptest.NewLogger(t))
if err := sp.Restore(snapshot.RestoreConfig{ if err := sp.Restore(snapshot.RestoreConfig{
SnapshotPath: dbPath, SnapshotPath: dbPath,
Name: cfg.Name, Name: cfg.Name,
@ -266,6 +254,7 @@ func restoreCluster(t *testing.T, clusterN int, dbPath string) (
}); err != nil { }); err != nil {
t.Fatal(err) t.Fatal(err)
} }
cfgs[i] = cfg cfgs[i] = cfg
} }

View File

@ -17,10 +17,12 @@ package integration
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"testing"
grpc_logsettable "github.com/grpc-ecosystem/go-grpc-middleware/logging/settable" grpc_logsettable "github.com/grpc-ecosystem/go-grpc-middleware/logging/settable"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap" grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"go.etcd.io/etcd/pkg/v3/testutil" "go.etcd.io/etcd/pkg/v3/testutil"
"go.etcd.io/etcd/server/v3/embed"
"go.uber.org/zap/zaptest" "go.uber.org/zap/zaptest"
) )
@ -54,3 +56,11 @@ func MustAbsPath(path string) string {
} }
return abs return abs
} }
func NewEmbedConfig(t testing.TB, name string) *embed.Config {
cfg := embed.NewConfig()
cfg.Name = name
cfg.ZapLoggerBuilder = embed.NewZapCoreLoggerBuilder(zaptest.NewLogger(t).Named(cfg.Name), nil, nil)
cfg.Dir = t.TempDir()
return cfg
}