Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add wrp validator metadata and metrics #157

Merged
merged 6 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ structures and supporting utilities.
## Table of Contents

- [Code of Conduct](#code-of-conduct)
- [Validators](#validators)
- [Examples](#examples)
- [Contributing](#contributing)

Expand All @@ -22,6 +23,25 @@ structures and supporting utilities.
This project and everyone participating in it are governed by the [XMiDT Code Of Conduct](https://xmidt.io/code_of_conduct/).
By participating, you agree to this Code.

## Validators

To setup application wrp validators, visit the example `ExampleMetaValidator` [GoDoc](https://pkg.go.dev/github.com/xmidt-org/wrp-go/v3/wrpvalidator#example-MetaValidator).

Application config example:
```yaml
# wrpValidators defines the wrp validators used to validate incoming wrp messages.
# (Optional)
# Available validator types: always_invalid, always_valid, utf8, msg_type, source, destination, simple_res_req, simple_event, spans
# Available validator levels: info, warning, error
# Validators can be disabled with `disable: true`, it is false by default
wrpValidators:
- type: utf8
level: warning
disable: true
- type: source
level: error
```

## Examples

To use the wrp-go library, it first should be added as an import in the file you plan to use it.
Expand Down
58 changes: 29 additions & 29 deletions wrpvalidator/messageValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,54 +35,54 @@ var spanFormat = map[int]string{
4: "status",
}

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

stv, err := NewSimpleEventTypeValidator(f, labelNames...)
stv, err := NewSimpleEventTypeWithMetric(tf, 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:
// SimpleResponseRequest ensures messages are valid based on
// each validator in the list. SimpleResponseRequest validates the following:
// UTF8 (all string fields), MessageType is valid, Source, Destination, Spans, MessageType is of
// SimpleRequestResponseMessageType.
func SimpleResponseRequestValidators(f *touchstone.Factory, labelNames ...string) (Validators, error) {
func SimpleResponseRequest(tf *touchstone.Factory, labelNames ...string) (Validators, error) {
var errs error
sv, err := SpecValidators(f, labelNames...)
sv, err := SpecWithMetrics(tf, labelNames...)
if err != nil {
errs = multierr.Append(errs, err)
}

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

spv, err := NewSpansValidator(f, labelNames...)
spv, err := NewSpansWithMetric(tf, 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...)
// NewSimpleResponseRequestTypeWithMetric returns a SimpleResponseRequestType validator with a metric middleware.
func NewSimpleResponseRequestTypeWithMetric(tf *touchstone.Factory, labelNames ...string) (ValidatorFunc, error) {
m, err := newSimpleRequestResponseMessageTypeErrorTotal(tf, labelNames...)

return func(msg wrp.Message, ls prometheus.Labels) error {
err := SimpleResponseRequestTypeValidator(msg)
err := SimpleResponseRequestType(msg)
if err != nil {
m.With(ls).Add(1.0)
}
Expand All @@ -91,12 +91,12 @@ func NewSimpleResponseRequestTypeValidator(f *touchstone.Factory, labelNames ...
}, err
}

// NewSimpleEventTypeValidator is the metric variant of SimpleEventTypeValidator
func NewSimpleEventTypeValidator(f *touchstone.Factory, labelNames ...string) (ValidatorFunc, error) {
m, err := newSimpleEventTypeValidatorErrorTotal(f, labelNames...)
// NewSimpleEventTypeWithMetric returns a SimpleEventType validator with a metric middleware.
func NewSimpleEventTypeWithMetric(tf *touchstone.Factory, labelNames ...string) (ValidatorFunc, error) {
m, err := newSimpleEventTypeErrorTotal(tf, labelNames...)

return func(msg wrp.Message, ls prometheus.Labels) error {
err := SimpleEventTypeValidator(msg)
err := SimpleEventType(msg)
if err != nil {
m.With(ls).Add(1.0)
}
Expand All @@ -105,12 +105,12 @@ func NewSimpleEventTypeValidator(f *touchstone.Factory, labelNames ...string) (V
}, err
}

// NewSpansValidator is the metric variant of SpansValidator
func NewSpansValidator(f *touchstone.Factory, labelNames ...string) (ValidatorFunc, error) {
m, err := newSpansValidatorErrorTotal(f, labelNames...)
// NewSpansWithMetric returns a Spans validator with a metric middleware.
func NewSpansWithMetric(tf *touchstone.Factory, labelNames ...string) (ValidatorFunc, error) {
m, err := newSpansErrorTotal(tf, labelNames...)

return func(msg wrp.Message, ls prometheus.Labels) error {
err := SpansValidator(msg)
err := Spans(msg)
if err != nil {
m.With(ls).Add(1.0)
}
Expand All @@ -119,17 +119,17 @@ func NewSpansValidator(f *touchstone.Factory, labelNames ...string) (ValidatorFu
}, err
}

// SimpleResponseRequestTypeValidator takes messages and validates their Type is of SimpleRequestResponseMessageType.
func SimpleResponseRequestTypeValidator(m wrp.Message) error {
// SimpleResponseRequestType takes messages and validates their Type is of SimpleRequestResponseMessageType.
func SimpleResponseRequestType(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 {
// SimpleEventType takes messages and validates their Type is of SimpleEventMessageType.
func SimpleEventType(m wrp.Message) error {
if m.Type != wrp.SimpleEventMessageType {
return ErrorNotSimpleEventType
}
Expand All @@ -139,8 +139,8 @@ func SimpleEventTypeValidator(m wrp.Message) error {

// 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 wrp.Message) error {
// Spans takes messages and validates their Spans.
func Spans(m wrp.Message) error {
var err error
// Spans consist of individual Span(s), arrays of timing values.
for _, s := range m.Spans {
Expand Down
Loading
Loading