Skip to content

Commit

Permalink
Merge pull request #152 from xmidt-org/denopink/feat/validator-metric…
Browse files Browse the repository at this point in the history
…s-config

feat: add validator metrics
  • Loading branch information
denopink authored Jan 19, 2024
2 parents 413f442 + e0879de commit 4f92637
Show file tree
Hide file tree
Showing 9 changed files with 1,046 additions and 258 deletions.
16 changes: 15 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
module github.com/xmidt-org/wrp-go/v3

go 1.19
go 1.21

require (
github.com/davecgh/go-spew v1.1.1
github.com/go-kit/kit v0.13.0
github.com/go-kit/log v0.2.1
github.com/google/uuid v1.5.0
github.com/prometheus/client_golang v1.18.0
github.com/stretchr/testify v1.8.4
github.com/ugorji/go/codec v1.2.12
github.com/xmidt-org/httpaux v0.4.0
github.com/xmidt-org/sallust v0.2.2
github.com/xmidt-org/touchstone v0.1.5
github.com/xmidt-org/webpa-common v1.11.9
go.uber.org/multierr v1.11.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.uber.org/dig v1.17.1 // indirect
go.uber.org/fx v1.20.1 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/sys v0.16.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
46 changes: 43 additions & 3 deletions go.sum

Large diffs are not rendered by default.

106 changes: 89 additions & 17 deletions simpleMessageTypes_validator.go → wrpvalidator/messageValidator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package wrp
package wrpvalidator

import (
"errors"
"fmt"
"strconv"

"github.com/prometheus/client_golang/prometheus"
"github.com/xmidt-org/touchstone"
"github.com/xmidt-org/wrp-go/v3"
"go.uber.org/multierr"
)

Expand Down Expand Up @@ -35,31 +38,109 @@ var spanFormat = map[int]string{
// SimpleEventValidators ensures messages are valid based on
// each validator in the list. SimpleEventValidators validates the following:
// UTF8 (all string fields), MessageType is valid, Source, Destination, MessageType is of SimpleEventMessageType.
func SimpleEventValidators() Validators {
return Validators{SpecValidators()}.AddFunc(SimpleEventTypeValidator)
func SimpleEventValidators(f *touchstone.Factory, labelNames ...string) (Validators, error) {
var errs error
sv, err := SpecValidators(f, labelNames...)
if err != nil {
errs = multierr.Append(errs, err)
}

stv, err := NewSimpleEventTypeValidator(f, labelNames...)
if err != nil {
errs = multierr.Append(errs, err)
}

return sv.AddFunc(stv), errs
}

// SimpleResponseRequestValidators ensures messages are valid based on
// each validator in the list. SimpleResponseRequestValidators validates the following:
// UTF8 (all string fields), MessageType is valid, Source, Destination, Spans, MessageType is of
// SimpleRequestResponseMessageType.
func SimpleResponseRequestValidators() Validators {
return Validators{SpecValidators()}.AddFunc(SimpleResponseRequestTypeValidator, SpansValidator)
func SimpleResponseRequestValidators(f *touchstone.Factory, labelNames ...string) (Validators, error) {
var errs error
sv, err := SpecValidators(f, labelNames...)
if err != nil {
errs = multierr.Append(errs, err)
}

stv, err := NewSimpleResponseRequestTypeValidator(f, labelNames...)
if err != nil {
errs = multierr.Append(errs, err)
}

spv, err := NewSpansValidator(f, labelNames...)
if err != nil {
errs = multierr.Append(errs, err)
}

return sv.AddFunc(stv, spv), errs
}

// NewSimpleResponseRequestTypeValidator is the metric variant of SimpleResponseRequestTypeValidator
func NewSimpleResponseRequestTypeValidator(f *touchstone.Factory, labelNames ...string) (ValidatorFunc, error) {
m, err := newSimpleRequestResponseMessageTypeValidatorErrorTotal(f, labelNames...)

return func(msg wrp.Message, ls prometheus.Labels) error {
err := SimpleResponseRequestTypeValidator(msg)
if err != nil {
m.With(ls).Add(1.0)
}

return err
}, err
}

// NewSimpleEventTypeValidator is the metric variant of SimpleEventTypeValidator
func NewSimpleEventTypeValidator(f *touchstone.Factory, labelNames ...string) (ValidatorFunc, error) {
m, err := newSimpleEventTypeValidatorErrorTotal(f, labelNames...)

return func(msg wrp.Message, ls prometheus.Labels) error {
err := SimpleEventTypeValidator(msg)
if err != nil {
m.With(ls).Add(1.0)
}

return err
}, err
}

// NewSpansValidator is the metric variant of SpansValidator
func NewSpansValidator(f *touchstone.Factory, labelNames ...string) (ValidatorFunc, error) {
m, err := newSpansValidatorErrorTotal(f, labelNames...)

return func(msg wrp.Message, ls prometheus.Labels) error {
err := SpansValidator(msg)
if err != nil {
m.With(ls).Add(1.0)
}

return err
}, err
}

// SimpleResponseRequestTypeValidator takes messages and validates their Type is of SimpleRequestResponseMessageType.
func SimpleResponseRequestTypeValidator(m Message) error {
if m.Type != SimpleRequestResponseMessageType {
func SimpleResponseRequestTypeValidator(m wrp.Message) error {
if m.Type != wrp.SimpleRequestResponseMessageType {
return ErrorNotSimpleResponseRequestType
}

return nil
}

// SimpleEventTypeValidator takes messages and validates their Type is of SimpleEventMessageType.
func SimpleEventTypeValidator(m wrp.Message) error {
if m.Type != wrp.SimpleEventMessageType {
return ErrorNotSimpleEventType
}

return nil
}

// TODO Do we want to include SpanParentValidator? SpanParent currently doesn't exist in the Message Struct

// SpansValidator takes messages and validates their Spans.
func SpansValidator(m Message) error {
func SpansValidator(m wrp.Message) error {
var err error
// Spans consist of individual Span(s), arrays of timing values.
for _, s := range m.Spans {
Expand All @@ -86,12 +167,3 @@ func SpansValidator(m Message) error {

return err
}

// SimpleEventTypeValidator takes messages and validates their Type is of SimpleEventMessageType.
func SimpleEventTypeValidator(m Message) error {
if m.Type != SimpleEventMessageType {
return ErrorNotSimpleEventType
}

return nil
}
Loading

0 comments on commit 4f92637

Please sign in to comment.