From 4a2b01f5a4d02b0f3ca87d124b78cbf5245e15d9 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 1 Feb 2023 21:46:11 +0300 Subject: [PATCH] [#8] internal: Add full timestamp and endpoint to logs Signed-off-by: Evgenii Stratonikov --- internal/logging/log.go | 47 +++++++++++++++++++++++++++++++++++++++ internal/native/native.go | 5 +++++ internal/s3/s3.go | 5 +++++ 3 files changed, 57 insertions(+) create mode 100644 internal/logging/log.go diff --git a/internal/logging/log.go b/internal/logging/log.go new file mode 100644 index 0000000..3aa0c4e --- /dev/null +++ b/internal/logging/log.go @@ -0,0 +1,47 @@ +package logging + +import ( + "github.com/sirupsen/logrus" + "go.k6.io/k6/js/modules" +) + +// InitTimestamp makes `console.log` print full timestamp instead of raw seconds. +func InitTimestamp(vu modules.VU) { + lg, ok := vu.InitEnv().Logger.(*logrus.Logger) + if !ok { + return + } + + format := lg.Formatter + switch f := format.(type) { + case *logrus.TextFormatter: + f.ForceColors = true + f.FullTimestamp = true + f.TimestampFormat = "15:04:05" + case *logrus.JSONFormatter: + f.TimestampFormat = "15:04:05" + } +} + +// LogWithField adds default field to a modules.VU logger. +func LogWithField(vu modules.VU, name string, value interface{}) { + lg, ok := vu.InitEnv().Logger.(*logrus.Logger) + if !ok { + return + } + + lg.AddHook(defaultFieldHook{name: name, value: value}) +} + +type defaultFieldHook struct { + name string + value interface{} +} + +func (defaultFieldHook) Levels() []logrus.Level { + return []logrus.Level{logrus.InfoLevel} +} +func (h defaultFieldHook) Fire(e *logrus.Entry) error { + e.Data[h.name] = h.value + return nil +} diff --git a/internal/native/native.go b/internal/native/native.go index f09b729..8edf9bf 100644 --- a/internal/native/native.go +++ b/internal/native/native.go @@ -8,6 +8,7 @@ import ( "github.com/TrueCloudLab/frostfs-sdk-go/client" frostfsecdsa "github.com/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa" "github.com/TrueCloudLab/frostfs-sdk-go/session" + "github.com/TrueCloudLab/xk6-frostfs/internal/logging" "github.com/google/uuid" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "go.k6.io/k6/js/modules" @@ -41,6 +42,8 @@ func init() { // NewModuleInstance implements the modules.Module interface and returns // a new instance for each VU. func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { + logging.InitTimestamp(vu) + mi := &Native{vu: vu} return mi } @@ -52,6 +55,8 @@ func (n *Native) Exports() modules.Exports { } func (n *Native) Connect(endpoint, hexPrivateKey string, dialTimeout, streamTimeout int) (*Client, error) { + logging.LogWithField(n.vu, "endpoint", endpoint) + var ( cli client.Client pk *keys.PrivateKey diff --git a/internal/s3/s3.go b/internal/s3/s3.go index 965bd50..f2e5077 100644 --- a/internal/s3/s3.go +++ b/internal/s3/s3.go @@ -7,6 +7,7 @@ import ( "strconv" "time" + "github.com/TrueCloudLab/xk6-frostfs/internal/logging" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/s3" @@ -41,6 +42,8 @@ func init() { // NewModuleInstance implements the modules.Module interface and returns // a new instance for each VU. func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { + logging.InitTimestamp(vu) + mi := &S3{vu: vu} return mi } @@ -52,6 +55,8 @@ func (s *S3) Exports() modules.Exports { } func (s *S3) Connect(endpoint string, params map[string]string) (*Client, error) { + logging.LogWithField(s.vu, "endpoint", endpoint) + resolver := aws.EndpointResolverWithOptionsFunc(func(_, _ string, _ ...interface{}) (aws.Endpoint, error) { return aws.Endpoint{ URL: endpoint,