Skip to content

Commit d71aa9b

Browse files
authored
Merge pull request #418 from xmidt-org/denopink/wrp-validators/improve-logging-and-setup
patch: improve setup and logging for wrp validators
2 parents d8f1c77 + 750c5ab commit d71aa9b

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ require (
2121
github.com/xmidt-org/webpa-common/v2 v2.3.2
2222
github.com/xmidt-org/wrp-go/v3 v3.5.2
2323
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.54.0
24-
go.uber.org/multierr v1.11.0
2524
go.uber.org/zap v1.27.0
2625
)
2726

@@ -115,6 +114,7 @@ require (
115114
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
116115
go.uber.org/dig v1.18.0 // indirect
117116
go.uber.org/fx v1.22.2 // indirect
117+
go.uber.org/multierr v1.11.0 // indirect
118118
golang.org/x/crypto v0.26.0 // indirect
119119
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
120120
golang.org/x/mod v0.17.0 // indirect

primaryHandler.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/xmidt-org/clortho/clorthozap"
2525
"github.com/xmidt-org/touchstone"
2626
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
27-
"go.uber.org/multierr"
2827
"go.uber.org/zap"
2928

3029
"github.com/xmidt-org/webpa-common/secure/handler"
@@ -69,16 +68,19 @@ const (
6968
deviceID = "deviceID"
7069

7170
enforceCheck = "enforce"
71+
72+
zapWRPValidatorLabel = "wrp_validator_level"
7273
)
7374

7475
// Default values
7576
const (
7677
UnknownPartner = "unknown"
7778
)
7879

79-
var errNoDeviceName = errors.New("no device name")
80-
81-
type validators []wrpvalidator.Validator
80+
var (
81+
errNoDeviceName = errors.New("no device name")
82+
errWRPValidatorConfigError = errors.New("failed to configure wrp validators")
83+
)
8284

8385
func authChain(v *viper.Viper, logger *zap.Logger, registry xmetrics.Registry, tf *touchstone.Factory) (alice.Chain, error) {
8486
if registry == nil {
@@ -581,25 +583,31 @@ func validateWRP(v *viper.Viper, logger *zap.Logger, tf *touchstone.Factory) (fu
581583

582584
if valsConig := v.Get(wrpValidatorConfigKey); valsConig != nil {
583585
if b, err := json.Marshal(valsConig); err != nil {
584-
return nil, err
586+
return nil, errors.Join(errWRPValidatorConfigError, err)
585587
} else if err = json.Unmarshal(b, &vals); err != nil {
586-
return nil, err
588+
return nil, errors.Join(errWRPValidatorConfigError, err)
587589
}
588590

589591
labelNames := []string{wrpvalidator.ClientIDLabel, wrpvalidator.PartnerIDLabel, wrpvalidator.MessageTypeLabel}
590592
for _, v := range vals {
591593
if err := v.AddMetric(tf, labelNames...); err != nil {
592-
errs = multierr.Append(errs, err)
594+
errs = errors.Join(errs, err)
593595
}
594596
}
595597
}
596598

599+
if errs != nil {
600+
return nil, errors.Join(errWRPValidatorConfigError, errs)
601+
}
602+
597603
return func(delegate http.Handler) http.Handler {
598604
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
599605
if msg, ok := wrpcontext.GetMessage(r.Context()); ok {
600606
var (
607+
infoErrors error
601608
warningErrors error
602609
failureError error
610+
unknownError error
603611
satClientID = "N/A"
604612
partnerID = device.UnknownPartner
605613
)
@@ -628,19 +636,35 @@ func validateWRP(v *viper.Viper, logger *zap.Logger, tf *touchstone.Factory) (fu
628636
)
629637

630638
switch v.Level() {
631-
case wrpvalidator.ErrorLevel:
632-
failureError = multierr.Append(failureError, err)
639+
case wrpvalidator.InfoLevel:
640+
infoErrors = errors.Join(infoErrors, err)
633641
case wrpvalidator.WarningLevel:
634-
warningErrors = multierr.Append(warningErrors, err)
642+
warningErrors = errors.Join(warningErrors, err)
643+
case wrpvalidator.ErrorLevel:
644+
failureError = errors.Join(failureError, err)
645+
default:
646+
unknownError = errors.Join(unknownError, err)
635647
}
636648
}
637649

650+
if unknownError != nil {
651+
logger.Warn("WRP message validation errors found",
652+
zap.Error(unknownError), zap.String(zapWRPValidatorLabel, wrpvalidator.UnknownLevel.String()))
653+
}
654+
655+
if infoErrors != nil {
656+
logger.Warn("WRP message validation errors found",
657+
zap.Error(infoErrors), zap.String(zapWRPValidatorLabel, wrpvalidator.InfoLevel.String()))
658+
}
659+
638660
if warningErrors != nil {
639-
logger.Warn("WRP message validation warnings found", zap.Error(warningErrors))
661+
logger.Warn("WRP message validation errors found",
662+
zap.Error(warningErrors), zap.String(zapWRPValidatorLabel, wrpvalidator.WarningLevel.String()))
640663
}
641664

642665
if failureError != nil {
643-
logger.Error("WRP message validation failures found", zap.Error(failureError))
666+
logger.Error("WRP message validation (failure error level) found",
667+
zap.Error(failureError), zap.String(zapWRPValidatorLabel, wrpvalidator.ErrorLevel.String()))
644668
w.Header().Set("Content-Type", "application/json")
645669
w.WriteHeader(http.StatusBadRequest)
646670
fmt.Fprintf(

0 commit comments

Comments
 (0)