[#8] internal: Add full timestamp and endpoint to logs

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-02-01 21:46:11 +03:00 committed by Alex Vanin
parent e52a315752
commit 4a2b01f5a4
3 changed files with 57 additions and 0 deletions

47
internal/logging/log.go Normal file
View file

@ -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
}

View file

@ -8,6 +8,7 @@ import (
"github.com/TrueCloudLab/frostfs-sdk-go/client" "github.com/TrueCloudLab/frostfs-sdk-go/client"
frostfsecdsa "github.com/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa" frostfsecdsa "github.com/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
"github.com/TrueCloudLab/frostfs-sdk-go/session" "github.com/TrueCloudLab/frostfs-sdk-go/session"
"github.com/TrueCloudLab/xk6-frostfs/internal/logging"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"go.k6.io/k6/js/modules" "go.k6.io/k6/js/modules"
@ -41,6 +42,8 @@ func init() {
// NewModuleInstance implements the modules.Module interface and returns // NewModuleInstance implements the modules.Module interface and returns
// a new instance for each VU. // a new instance for each VU.
func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
logging.InitTimestamp(vu)
mi := &Native{vu: vu} mi := &Native{vu: vu}
return mi return mi
} }
@ -52,6 +55,8 @@ func (n *Native) Exports() modules.Exports {
} }
func (n *Native) Connect(endpoint, hexPrivateKey string, dialTimeout, streamTimeout int) (*Client, error) { func (n *Native) Connect(endpoint, hexPrivateKey string, dialTimeout, streamTimeout int) (*Client, error) {
logging.LogWithField(n.vu, "endpoint", endpoint)
var ( var (
cli client.Client cli client.Client
pk *keys.PrivateKey pk *keys.PrivateKey

View file

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/TrueCloudLab/xk6-frostfs/internal/logging"
"github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3"
@ -41,6 +42,8 @@ func init() {
// NewModuleInstance implements the modules.Module interface and returns // NewModuleInstance implements the modules.Module interface and returns
// a new instance for each VU. // a new instance for each VU.
func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
logging.InitTimestamp(vu)
mi := &S3{vu: vu} mi := &S3{vu: vu}
return mi return mi
} }
@ -52,6 +55,8 @@ func (s *S3) Exports() modules.Exports {
} }
func (s *S3) Connect(endpoint string, params map[string]string) (*Client, error) { 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) { resolver := aws.EndpointResolverWithOptionsFunc(func(_, _ string, _ ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{ return aws.Endpoint{
URL: endpoint, URL: endpoint,