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

temp: Validator with metrics demo [DO NOT MERGE] #151

Closed
wants to merge 14 commits into from
30 changes: 30 additions & 0 deletions wrpvalidator/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package wrpvalidator

import (
"github.com/prometheus/client_golang/prometheus"

Check failure on line 7 in wrpvalidator/metrics.go

View workflow job for this annotation

GitHub Actions / ci / Go Unit Tests

missing go.sum entry for module providing package github.com/prometheus/client_golang/prometheus (imported by github.com/xmidt-org/wrp-go/v3/wrpvalidator); to add:

Check failure on line 7 in wrpvalidator/metrics.go

View workflow job for this annotation

GitHub Actions / ci / Build Go Program

missing go.sum entry for module providing package github.com/prometheus/client_golang/prometheus (imported by github.com/xmidt-org/wrp-go/v3/wrpvalidator); to add:
"github.com/xmidt-org/touchstone"

Check failure on line 8 in wrpvalidator/metrics.go

View workflow job for this annotation

GitHub Actions / ci / Go Unit Tests

missing go.sum entry for module providing package github.com/xmidt-org/touchstone (imported by github.com/xmidt-org/wrp-go/v3/wrpvalidator); to add:

Check failure on line 8 in wrpvalidator/metrics.go

View workflow job for this annotation

GitHub Actions / ci / Build Go Program

missing go.sum entry for module providing package github.com/xmidt-org/touchstone (imported by github.com/xmidt-org/wrp-go/v3/wrpvalidator); to add:
)

const (
// MetricPrefix is prepended to all metrics exposed by this package.
metricPrefix = "wrp_"

// utf8ValidatorName is the name of the counter for all UTF8 validation.
utf8ValidatorName = metricPrefix + "utf8_validator"

// utf8ValidatorHelp is the help text for the UTF8 Validator metric.
utf8ValidatorHelp = "the total number of UTF8 Validator metric"
)

func NewUTF8ValidatorErrorTotal(f *touchstone.Factory, labelNames ...string) (m *prometheus.CounterVec, err error) {
return f.NewCounterVec(
prometheus.CounterOpts{
Name: utf8ValidatorName,
Help: utf8ValidatorHelp,
},
labelNames...
)
}
25 changes: 23 additions & 2 deletions wrpvalidator/spec_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"unicode"

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

const (
Expand Down Expand Up @@ -43,8 +46,26 @@ var locatorPattern = regexp.MustCompile(
// SpecValidators ensures messages are valid based on
// each spec validator in the list. Only validates the opinionated portions of the spec.
// SpecValidators validates the following fields: UTF8 (all string fields), MessageType, Source, Destination
func SpecValidators() Validators {
return Validators{}.AddFunc(UTF8Validator, MessageTypeValidator, SourceValidator, DestinationValidator)
func SpecValidators(f *touchstone.Factory, labelNames ...string) (Validators, error) {
var errs error
utf8, err := NewUTF8Validator(f, labelNames...)
if err != nil {
errs = multierr.Append(errs, err)
}

return Validators{}.AddFunc(utf8), errs
}

func NewUTF8Validator(f *touchstone.Factory, labelNames ...string) (ValidatorFunc, error) {
m, err := NewUTF8ValidatorErrorTotal(f, labelNames...)
return func(msg wrp.Message, ls prometheus.Labels) error {
err := UTF8Validator(msg)
if err != nil {
m.With(ls).Add(1.0)
}

return err
}, err
}

// UTF8Validator takes messages and validates that it contains UTF-8 strings.
Expand Down
21 changes: 21 additions & 0 deletions wrpvalidator/spec_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import (
"fmt"
"testing"

prometheus "command-line-arguments/Users/odc/go/pkg/mod/github.com/prometheus/[email protected]/prometheus/counter.go"

"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/xmidt-org/sallust"
"github.com/xmidt-org/touchstone"
"github.com/xmidt-org/wrp-go/v3"
)

func TestSpecHelperValidators(t *testing.T) {
Expand Down Expand Up @@ -186,6 +192,21 @@ func ExampleTypeValidator_Validate_specValidators() {
// Output: false true true false
}

func ExampleTypeValidator_Validate_specValidators() {
cfg := touchstone.Config{
DefaultNamespace: "n",
DefaultSubsystem: "s",
}
_, pr, err := touchstone.New(cfg)
f := touchstone.NewFactory(cfg, sallust.Default(), pr)
utf8ValidatorWithMetric, err := NewUTF8Validator(f, "sat_client_id", "foo")
if err != nil {
return
}

_ = utf8ValidatorWithMetric.Validate(wrp.Message{}, prometheus.Labels{"sat_client_id": "123", "foo": "bar"})
}

func testUTF8Validator(t *testing.T) {
var (
expectedStatus int64 = 3471
Expand Down
19 changes: 10 additions & 9 deletions wrpvalidator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"strings"

"github.com/prometheus/client_golang/prometheus"
"github.com/xmidt-org/wrp-go/v3"
"go.uber.org/multierr"
)
Expand Down Expand Up @@ -77,7 +78,7 @@ func NewValidatorError(err error, m string, f []string) ValidatorError {

// Validator is a WRP validator that allows access to the Validate function.
type Validator interface {
Validate(m wrp.Message) error
Validate(wrp.Message, prometheus.Labels) error
}

// Validators is a WRP validator that ensures messages are valid based on
Expand All @@ -86,11 +87,11 @@ type Validators []Validator

// Validate runs messages through each validator in the validators list.
// It returns as soon as the message is considered invalid, otherwise returns nil if valid.
func (vs Validators) Validate(m wrp.Message) error {
func (vs Validators) Validate(m wrp.Message, ls prometheus.Labels) error {
var err error
for _, v := range vs {
if v != nil {
err = multierr.Append(err, v.Validate(m))
err = multierr.Append(err, v.Validate(m, ls))
}
}

Expand Down Expand Up @@ -121,10 +122,10 @@ func (vs Validators) AddFunc(vf ...ValidatorFunc) Validators {

// ValidatorFunc is a WRP validator that takes messages and validates them
// against functions.
type ValidatorFunc func(wrp.Message) error
type ValidatorFunc func(wrp.Message, prometheus.Labels) error

// Validate executes its own ValidatorFunc receiver and returns the result.
func (vf ValidatorFunc) Validate(m wrp.Message) error { return vf(m) }
func (vf ValidatorFunc) Validate(m wrp.Message, ls prometheus.Labels) error { return vf(m, ls) }

// TypeValidator is a WRP validator that validates based on message type
// or using the defaultValidator if message type is unfound.
Expand All @@ -135,13 +136,13 @@ type TypeValidator struct {

// Validate validates messages based on message type or using the defaultValidator
// if message type is unfound.
func (tv TypeValidator) Validate(msg Message) error {
vs := tv.m[msg.MessageType()]
func (tv TypeValidator) Validate(m wrp.Message, ls prometheus.Labels) error {
vs := tv.m[m.MessageType()]
if vs == nil {
return tv.defaultValidator.Validate(msg)
return tv.defaultValidator.Validate(m, ls)
}

return vs.Validate(msg)
return vs.Validate(m, ls)
}

// NewTypeValidator is a TypeValidator factory.
Expand Down
Loading