update adapters & fixes

This commit is contained in:
Pavel 2023-10-18 16:55:46 +03:00
parent 4bca4c491e
commit e9e9c4c442
9 changed files with 47 additions and 19 deletions

View File

@ -43,7 +43,7 @@ func (c CounterProcessor) Process(id int, counter Counter) bool { // nolint:varn
err := c.Notifier(apiURL, apiKey, counter.Message())
if err != nil {
c.debugLog("cannot send notification for counter",
slog.Int(logger.CounterIDAttr, id), logger.ErrAttr(err), slog.String(logger.FailureMessageAttr, counter.Message()))
slog.Int(logger.CounterIDAttr, id), logger.Err(err), slog.String(logger.FailureMessageAttr, counter.Message()))
}
counter.FailureProcessed()
return true
@ -81,7 +81,7 @@ func (c CounterProcessor) Process(id int, counter Counter) bool { // nolint:varn
err := c.Notifier(apiURL, apiKey, c.getErrorText(counter.Name(), c.Error, lang))
if err != nil {
c.debugLog("cannot send notification for counter",
slog.Int(logger.CounterIDAttr, id), logger.ErrAttr(err), slog.String(logger.FailureMessageAttr, counter.Message()))
slog.Int(logger.CounterIDAttr, id), logger.Err(err), slog.String(logger.FailureMessageAttr, counter.Message()))
}
counter.CountersProcessed()
return true

View File

@ -96,7 +96,7 @@ func (t *CounterProcessorTest) Test_FailureProcessed() {
p.Process(1, c)
c.AssertExpectations(t.T())
t.Assert().Contains(log.String(), "skipping counter because its failure is already processed")
t.Assert().Contains(log.String(), "id=1")
t.Assert().Contains(log.String(), "counterId=1")
}
func (t *CounterProcessorTest) Test_CounterFailed_CannotFindConnection() {
@ -109,7 +109,7 @@ func (t *CounterProcessorTest) Test_CounterFailed_CannotFindConnection() {
p.Process(1, c)
c.AssertExpectations(t.T())
t.Assert().Contains(log.String(), "cannot find connection data for counter")
t.Assert().Contains(log.String(), "id=1")
t.Assert().Contains(log.String(), "counterId=1")
}
func (t *CounterProcessorTest) Test_CounterFailed_ErrWhileNotifying() {
@ -124,9 +124,9 @@ func (t *CounterProcessorTest) Test_CounterFailed_ErrWhileNotifying() {
p.Process(1, c)
c.AssertExpectations(t.T())
t.Assert().Contains(log.String(), "cannot send notification for counter")
t.Assert().Contains(log.String(), "id=1")
t.Assert().Contains(log.String(), "counterId=1")
t.Assert().Contains(log.String(), `error="http status code: 500"`)
t.Assert().Contains(log.String(), `message="error message"`)
t.Assert().Contains(log.String(), `failureMessage="error message"`)
t.Assert().Equal(t.apiURL, n.apiURL)
t.Assert().Equal(t.apiKey, n.apiKey)
t.Assert().Equal("error message", n.message)
@ -160,7 +160,7 @@ func (t *CounterProcessorTest) Test_TooFewRequests() {
p.Process(1, c)
c.AssertExpectations(t.T())
t.Assert().Contains(log.String(),
fmt.Sprintf(`msg="skipping counter because it has too few requests" id=%d minRequests=%d`, 1, DefaultMinRequests))
fmt.Sprintf(`msg="skipping counter because it has too few requests" counterId=%d minRequests=%d`, 1, DefaultMinRequests))
}
func (t *CounterProcessorTest) Test_ThresholdNotPassed() {
@ -206,7 +206,7 @@ func (t *CounterProcessorTest) Test_ThresholdPassed_NoConnectionFound() {
p.Process(1, c)
c.AssertExpectations(t.T())
t.Assert().Contains(log.String(), "cannot find connection data for counter")
t.Assert().Contains(log.String(), "id=1")
t.Assert().Contains(log.String(), "counterId=1")
t.Assert().Empty(n.message)
}
@ -224,7 +224,7 @@ func (t *CounterProcessorTest) Test_ThresholdPassed_NotifyingError() {
p.Process(1, c)
c.AssertExpectations(t.T())
t.Assert().Contains(log.String(), `msg="cannot send notification for counter" id=1 error="unknown error" message=""`)
t.Assert().Contains(log.String(), `msg="cannot send notification for counter" counterId=1 error="unknown error" failureMessage=""`)
t.Assert().Equal(`default error [{"Name":"MockedCounter"}]`, n.message)
}

View File

@ -154,7 +154,7 @@ func NewJobManager() *JobManager {
func DefaultJobErrorHandler() JobErrorHandler {
return func(name string, err error, log logger.Logger) {
if err != nil && name != "" {
log.Error("job failed with an error", slog.String("job", name), logger.ErrAttr(err))
log.Error("job failed with an error", slog.String("job", name), logger.Err(err))
}
}
}

View File

@ -1,6 +1,7 @@
package logger
import (
"fmt"
"log/slog"
"net/http"
)
@ -18,7 +19,7 @@ const (
HTTPStatusNameAttr = "httpStatusName"
)
func ErrAttr(err any) slog.Attr {
func Err(err any) slog.Attr {
if err == nil {
return slog.String(ErrorAttr, "<nil>")
}
@ -34,5 +35,12 @@ func HTTPStatusName(code int) slog.Attr {
}
func Body(val any) slog.Attr {
return slog.Any(BodyAttr, val)
switch item := val.(type) {
case string:
return slog.String(BodyAttr, item)
case []byte:
return slog.String(BodyAttr, string(item))
default:
return slog.String(BodyAttr, fmt.Sprintf("%#v", val))
}
}

View File

@ -0,0 +1,18 @@
package logger
import (
"fmt"
v1 "github.com/retailcrm/mg-transport-api-client-go/v1"
)
type mgTransportClientAdapter struct {
log Logger
}
func MGTransportClientAdapter(log Logger) v1.DebugLogger {
return &mgTransportClientAdapter{log: log}
}
func (m *mgTransportClientAdapter) Debugf(msg string, args ...interface{}) {
m.log.Debug(fmt.Sprintf(msg, args...))
}

View File

@ -65,13 +65,13 @@ func NewModuleFeaturesUploader(
awsConfig.WithCredentialsProvider(customProvider),
)
if err != nil {
log.Error("cannot load S3 configuration", logger.ErrAttr(err))
log.Error("cannot load S3 configuration", logger.Err(err))
return nil
}
client := manager.NewUploader(s3.NewFromConfig(cfg))
if err != nil {
log.Error("cannot load S3 configuration", logger.ErrAttr(err))
log.Error("cannot load S3 configuration", logger.Err(err))
return nil
}
@ -91,14 +91,14 @@ func (s *ModuleFeaturesUploader) Upload() {
content, err := os.ReadFile(s.featuresFilename)
if err != nil {
s.log.Error("cannot read markdown file %s %s", slog.String("fileName", s.featuresFilename), logger.ErrAttr(err))
s.log.Error("cannot read markdown file %s %s", slog.String("fileName", s.featuresFilename), logger.Err(err))
return
}
for _, lang := range languages {
translated, err := s.translate(content, lang)
if err != nil {
s.log.Error("cannot translate module features file", slog.String("lang", lang.String()), logger.ErrAttr(err))
s.log.Error("cannot translate module features file", slog.String("lang", lang.String()), logger.Err(err))
continue
}
@ -106,7 +106,7 @@ func (s *ModuleFeaturesUploader) Upload() {
resp, err := s.uploadFile(html, lang.String())
if err != nil {
s.log.Error("cannot upload file", slog.String("lang", lang.String()), logger.ErrAttr(err))
s.log.Error("cannot upload file", slog.String("lang", lang.String()), logger.Err(err))
continue
}

View File

@ -250,7 +250,7 @@ func (s *Sentry) recoveryMiddleware() gin.HandlerFunc { // nolint
if l != nil {
// TODO: Check if we can output stacktraces with prefix data like before if we really need it.
stack := stacktrace.FormattedStack(3, "trace: ")
formattedErr := logger.ErrAttr(err)
formattedErr := logger.Err(err)
httpRequest, _ := httputil.DumpRequest(c.Request, false)
headers := strings.Split(string(httpRequest), "\r\n")
for idx, header := range headers {

2
go.mod
View File

@ -24,7 +24,7 @@ require (
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
github.com/pkg/errors v0.9.1
github.com/retailcrm/api-client-go/v2 v2.1.3
github.com/retailcrm/mg-transport-api-client-go v1.1.32
github.com/retailcrm/mg-transport-api-client-go v1.3.4
github.com/retailcrm/zabbix-metrics-collector v1.0.0
github.com/stretchr/testify v1.8.3
go.uber.org/atomic v1.10.0

2
go.sum
View File

@ -379,6 +379,8 @@ github.com/retailcrm/api-client-go/v2 v2.1.3 h1:AVcp9oeSOm6+3EWXCgdQs+XE3PTjzCKK
github.com/retailcrm/api-client-go/v2 v2.1.3/go.mod h1:1yTZl9+gd3+/k0kAJe7sYvC+mL4fqMwIwtnSgSWZlkQ=
github.com/retailcrm/mg-transport-api-client-go v1.1.32 h1:IBPltSoD5q2PPZJbNC/prK5F9rEVPXVx/ZzDpi7HKhs=
github.com/retailcrm/mg-transport-api-client-go v1.1.32/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI=
github.com/retailcrm/mg-transport-api-client-go v1.3.4 h1:HIn4eorABNfudn7hr5Rd6XYC/ieDTqCkaq6wv0AFTBE=
github.com/retailcrm/mg-transport-api-client-go v1.3.4/go.mod h1:gDe/tj7t3Hr/uwIFSBVgGAmP85PoLajVl1A+skBo1Ro=
github.com/retailcrm/zabbix-metrics-collector v1.0.0 h1:ju3rhpgVoiKII6oXEJEf2eoJy5bNcYAmOPRp1oPWDmA=
github.com/retailcrm/zabbix-metrics-collector v1.0.0/go.mod h1:3Orc+gfSg1tXj89QNvOn22t0cO1i2whR/4NJUGonWJA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=