mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Enable configuring delegated zap-logging for embed server.
This commit is contained in:
parent
a84bd093b0
commit
725a8c5e02
@ -21,6 +21,7 @@ import (
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
|
||||
"go.etcd.io/etcd/pkg/v3/logutil"
|
||||
|
||||
"go.uber.org/zap"
|
||||
@ -232,7 +233,11 @@ func NewZapCoreLoggerBuilder(lg *zap.Logger, cr zapcore.Core, syncer zapcore.Wri
|
||||
cfg.loggerWriteSyncer = syncer
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ import (
|
||||
"go.etcd.io/etcd/pkg/v3/fileutil"
|
||||
"go.etcd.io/etcd/pkg/v3/testutil"
|
||||
"go.etcd.io/etcd/server/v3/embed"
|
||||
"go.etcd.io/etcd/tests/v3/integration"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@ -64,15 +65,11 @@ func createSnapshotFile(t *testing.T, kvs []kv) string {
|
||||
urls := newEmbedURLs(clusterN * 2)
|
||||
cURLs, pURLs := urls[:clusterN], urls[clusterN:]
|
||||
|
||||
cfg := embed.NewConfig()
|
||||
cfg.Logger = "zap"
|
||||
cfg.LogOutputs = []string{"/dev/null"}
|
||||
cfg.Name = "default"
|
||||
cfg := integration.NewEmbedConfig(t, "default")
|
||||
cfg.ClusterState = "new"
|
||||
cfg.LCUrls, cfg.ACUrls = cURLs, cURLs
|
||||
cfg.LPUrls, cfg.APUrls = pURLs, pURLs
|
||||
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)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -18,7 +18,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -26,6 +25,7 @@ import (
|
||||
"go.etcd.io/etcd/pkg/v3/testutil"
|
||||
"go.etcd.io/etcd/server/v3/embed"
|
||||
"go.etcd.io/etcd/server/v3/etcdserver"
|
||||
"go.etcd.io/etcd/tests/v3/integration"
|
||||
)
|
||||
|
||||
// TestSnapshotV3RestoreMultiMemberAdd ensures that multiple members
|
||||
@ -37,6 +37,7 @@ func TestSnapshotV3RestoreMultiMemberAdd(t *testing.T) {
|
||||
|
||||
clusterN := 3
|
||||
cURLs, pURLs, srvs := restoreCluster(t, clusterN, dbPath)
|
||||
|
||||
defer func() {
|
||||
for i := 0; i < clusterN; i++ {
|
||||
os.RemoveAll(srvs[i].Config().Dir)
|
||||
@ -62,10 +63,7 @@ func TestSnapshotV3RestoreMultiMemberAdd(t *testing.T) {
|
||||
// wait for membership reconfiguration apply
|
||||
time.Sleep(testutil.ApplyTimeout)
|
||||
|
||||
cfg := embed.NewConfig()
|
||||
cfg.Logger = "zap"
|
||||
cfg.LogOutputs = []string{"/dev/null"}
|
||||
cfg.Name = "3"
|
||||
cfg := integration.NewEmbedConfig(t, "3")
|
||||
cfg.InitialClusterToken = testClusterTkn
|
||||
cfg.ClusterState = "existing"
|
||||
cfg.LCUrls, cfg.ACUrls = newCURLs, newCURLs
|
||||
@ -76,7 +74,6 @@ func TestSnapshotV3RestoreMultiMemberAdd(t *testing.T) {
|
||||
}
|
||||
cfg.InitialCluster = cfg.InitialCluster[1:]
|
||||
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)
|
||||
if err != nil {
|
||||
|
@ -29,6 +29,7 @@ import (
|
||||
"go.etcd.io/etcd/etcdctl/v3/snapshot"
|
||||
"go.etcd.io/etcd/pkg/v3/testutil"
|
||||
"go.etcd.io/etcd/server/v3/embed"
|
||||
"go.etcd.io/etcd/tests/v3/integration"
|
||||
"go.uber.org/zap/zaptest"
|
||||
)
|
||||
|
||||
@ -43,16 +44,12 @@ func TestSnapshotV3RestoreSingle(t *testing.T) {
|
||||
urls := newEmbedURLs(clusterN * 2)
|
||||
cURLs, pURLs := urls[:clusterN], urls[clusterN:]
|
||||
|
||||
cfg := embed.NewConfig()
|
||||
cfg.Logger = "zap"
|
||||
cfg.LogOutputs = []string{"/dev/null"}
|
||||
cfg.Name = "s1"
|
||||
cfg := integration.NewEmbedConfig(t, "s1")
|
||||
cfg.InitialClusterToken = testClusterTkn
|
||||
cfg.ClusterState = "existing"
|
||||
cfg.LCUrls, cfg.ACUrls = cURLs, cURLs
|
||||
cfg.LPUrls, cfg.APUrls = pURLs, pURLs
|
||||
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))
|
||||
pss := make([]string, 0, len(pURLs))
|
||||
@ -108,13 +105,11 @@ func TestSnapshotV3RestoreSingle(t *testing.T) {
|
||||
func TestSnapshotV3RestoreMulti(t *testing.T) {
|
||||
kvs := []kv{{"foo1", "bar1"}, {"foo2", "bar2"}, {"foo3", "bar3"}}
|
||||
dbPath := createSnapshotFile(t, kvs)
|
||||
defer os.RemoveAll(dbPath)
|
||||
|
||||
clusterN := 3
|
||||
cURLs, _, srvs := restoreCluster(t, clusterN, dbPath)
|
||||
defer func() {
|
||||
for i := 0; i < clusterN; i++ {
|
||||
os.RemoveAll(srvs[i].Config().Dir)
|
||||
srvs[i].Close()
|
||||
}
|
||||
}()
|
||||
@ -143,7 +138,8 @@ func TestSnapshotV3RestoreMulti(t *testing.T) {
|
||||
|
||||
// TestCorruptedBackupFileCheck tests if we can correctly identify a corrupted backup file.
|
||||
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 {
|
||||
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)
|
||||
cURLs, pURLs := urls[:clusterN], urls[clusterN:]
|
||||
|
||||
cfg := embed.NewConfig()
|
||||
cfg.Logger = "zap"
|
||||
cfg.LogOutputs = []string{"/dev/null"}
|
||||
cfg.Name = "default"
|
||||
cfg := integration.NewEmbedConfig(t, "default")
|
||||
cfg.ClusterState = "new"
|
||||
cfg.LCUrls, cfg.ACUrls = cURLs, cURLs
|
||||
cfg.LPUrls, cfg.APUrls = pURLs, pURLs
|
||||
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)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
os.RemoveAll(cfg.Dir)
|
||||
srv.Close()
|
||||
}()
|
||||
select {
|
||||
@ -244,18 +235,15 @@ func restoreCluster(t *testing.T, clusterN int, dbPath string) (
|
||||
|
||||
cfgs := make([]*embed.Config, clusterN)
|
||||
for i := 0; i < clusterN; i++ {
|
||||
cfg := embed.NewConfig()
|
||||
cfg.Logger = "zap"
|
||||
cfg.LogOutputs = []string{"/dev/null"}
|
||||
cfg.Name = fmt.Sprintf("%d", i)
|
||||
cfg := integration.NewEmbedConfig(t, fmt.Sprintf("%d", i))
|
||||
cfg.InitialClusterToken = testClusterTkn
|
||||
cfg.ClusterState = "existing"
|
||||
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.InitialCluster = ics
|
||||
cfg.Dir = filepath.Join(t.TempDir(), fmt.Sprint(time.Now().Nanosecond()+i))
|
||||
|
||||
sp := snapshot.NewV3(zaptest.NewLogger(t))
|
||||
|
||||
if err := sp.Restore(snapshot.RestoreConfig{
|
||||
SnapshotPath: dbPath,
|
||||
Name: cfg.Name,
|
||||
@ -266,6 +254,7 @@ func restoreCluster(t *testing.T, clusterN int, dbPath string) (
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfgs[i] = cfg
|
||||
}
|
||||
|
||||
|
@ -17,10 +17,12 @@ package integration
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
grpc_logsettable "github.com/grpc-ecosystem/go-grpc-middleware/logging/settable"
|
||||
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
|
||||
"go.etcd.io/etcd/pkg/v3/testutil"
|
||||
"go.etcd.io/etcd/server/v3/embed"
|
||||
"go.uber.org/zap/zaptest"
|
||||
)
|
||||
|
||||
@ -54,3 +56,11 @@ func MustAbsPath(path string) string {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user