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

converted basculechecks metrics to use touchstone #95

Merged
merged 3 commits into from
Apr 29, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Added metric listener for auth validation outcome. [#81](https://github.com/xmidt-org/bascule/pull/81)
- Moved checks to their own package and added capability checks. [#85](https://github.com/xmidt-org/bascule/pull/85)
- Removed emperror package dependency. [#94](https://github.com/xmidt-org/bascule/pull/94)
- Converted basculechecks to use touchstone metrics. []()

## [v0.9.0]
- added helper function for building basic auth map [#59](https://github.com/xmidt-org/bascule/pull/59)
Expand Down
85 changes: 6 additions & 79 deletions basculechecks/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,8 @@
package basculechecks

import (
"fmt"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/go-kit/kit/metrics"
gokitprometheus "github.com/go-kit/kit/metrics/prometheus"
"github.com/go-kit/kit/metrics/provider"
"github.com/prometheus/client_golang/prometheus"
"github.com/xmidt-org/themis/xlog"
themisXmetrics "github.com/xmidt-org/themis/xmetrics"
"github.com/xmidt-org/webpa-common/xmetrics"

"github.com/xmidt-org/touchstone"
"go.uber.org/fx"
)

Expand Down Expand Up @@ -68,85 +58,22 @@ const (
capabilityCheckHelpMsg = "Counter for the capability checker, providing outcome information by client, partner, and endpoint"
)

// Metrics returns the Metrics relevant to this package targeting our older non uber/fx applications.
// To initialize the metrics, use NewAuthCapabilityCheckMeasures().
func Metrics() []xmetrics.Metric {
return []xmetrics.Metric{
{
Name: AuthCapabilityCheckOutcome,
Type: xmetrics.CounterType,
Help: capabilityCheckHelpMsg,
LabelNames: []string{OutcomeLabel, ReasonLabel, ClientIDLabel, PartnerIDLabel, EndpointLabel},
},
}
}

// ProvideMetrics provides the metrics relevant to this package as uber/fx options.
// This is now deprecated in favor of ProvideMetricsVec.
func ProvideMetrics() fx.Option {
return fx.Provide(
themisXmetrics.ProvideCounter(prometheus.CounterOpts{
return fx.Options(
touchstone.CounterVec(prometheus.CounterOpts{
Name: AuthCapabilityCheckOutcome,
Help: capabilityCheckHelpMsg,
ConstLabels: nil,
}, OutcomeLabel, ReasonLabel, ClientIDLabel, PartnerIDLabel, EndpointLabel),
)
}

// ProvideMetricsVec provides the metrics relevant to this package as uber/fx options.
// The provided metrics are prometheus vectors which gives access to more advanced operations such as CurryWith(labels).
func ProvideMetricsVec() fx.Option {
return fx.Provide(
themisXmetrics.ProvideCounterVec(prometheus.CounterOpts{
Name: AuthCapabilityCheckOutcome,
Help: capabilityCheckHelpMsg,
ConstLabels: nil,
}, ServerLabel, OutcomeLabel, ReasonLabel, ClientIDLabel, PartnerIDLabel, EndpointLabel),
}, ServerLabel, OutcomeLabel, ReasonLabel, ClientIDLabel,
PartnerIDLabel, EndpointLabel),
)
}

// AuthCapabilityCheckMeasures describes the defined metrics that will be used by clients
type AuthCapabilityCheckMeasures struct {
CapabilityCheckOutcome metrics.Counter
}

// NewAuthCapabilityCheckMeasures realizes desired metrics. It's intended to be used alongside Metrics() for
// our older non uber/fx applications.
func NewAuthCapabilityCheckMeasures(p provider.Provider) *AuthCapabilityCheckMeasures {
return &AuthCapabilityCheckMeasures{
CapabilityCheckOutcome: p.NewCounter(AuthCapabilityCheckOutcome),
}
}

// BaseMeasuresIn is an uber/fx parameter with base metrics ready to be curried into child metrics based on
// custom labels.
type BaseMeasuresIn struct {
fx.In
Logger log.Logger
CapabilityCheckOutcome *prometheus.CounterVec `name:"auth_capability_check"`
}

// MeasuresFactory facilitates the creation of child metrics based on server labels.
type MeasuresFactory struct {
ServerName string
}

// NewMeasures builds the metric listener from the provided raw metrics.
func (m MeasuresFactory) NewMeasures(in BaseMeasuresIn) (*AuthCapabilityCheckMeasures, error) {
capabilityCheckOutcomeCounterVec, err := in.CapabilityCheckOutcome.CurryWith(prometheus.Labels{ServerLabel: m.ServerName})
if err != nil {
return nil, err
}
in.Logger.Log(level.Key(), level.DebugValue(), xlog.MessageKey(), "building auth capability measures", ServerLabel, m.ServerName)
return &AuthCapabilityCheckMeasures{
CapabilityCheckOutcome: gokitprometheus.NewCounter(capabilityCheckOutcomeCounterVec),
}, nil
}

// Annotated provides the measures as an annotated component with the name "[SERVER]_bascule_capability_measures"
func (m MeasuresFactory) Annotated() fx.Annotated {
return fx.Annotated{
Name: fmt.Sprintf("%s_bascule_capability_measures", m.ServerName),
Target: m.NewMeasures,
}
CapabilityCheckOutcome *prometheus.CounterVec `name:"auth_capability_check"`
}
34 changes: 0 additions & 34 deletions basculechecks/metrics_test.go

This file was deleted.

54 changes: 46 additions & 8 deletions basculechecks/metricvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"fmt"
"regexp"

"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cast"
"github.com/xmidt-org/bascule"
"go.uber.org/fx"
)

// CapabilitiesChecker is an object that can determine if a request is
Expand Down Expand Up @@ -56,6 +58,7 @@ type MetricValidator struct {
Measures *AuthCapabilityCheckMeasures
Endpoints []*regexp.Regexp
ErrorOut bool
Server string
}

// Check is a function for authorization middleware. The function parses the
Expand All @@ -74,18 +77,33 @@ func (m MetricValidator) Check(ctx context.Context, _ bascule.Token) error {

auth, ok := bascule.FromContext(ctx)
if !ok {
m.Measures.CapabilityCheckOutcome.With(OutcomeLabel, failureOutcome, ReasonLabel, TokenMissing, ClientIDLabel, "", PartnerIDLabel, "", EndpointLabel, "").Add(1)
m.Measures.CapabilityCheckOutcome.With(prometheus.Labels{
ServerLabel: m.Server,
OutcomeLabel: failureOutcome,
ReasonLabel: TokenMissing,
ClientIDLabel: "",
PartnerIDLabel: "",
EndpointLabel: "",
}).Add(1)
if m.ErrorOut {
return ErrNoAuth
}
return nil
}

client, partnerID, endpoint, reason, err := m.prepMetrics(auth)
labels := []string{ClientIDLabel, client, PartnerIDLabel, partnerID, EndpointLabel, endpoint}
labels := prometheus.Labels{
ServerLabel: m.Server,
ClientIDLabel: client,
PartnerIDLabel: partnerID,
EndpointLabel: endpoint,
OutcomeLabel: AcceptedOutcome,
ReasonLabel: "",
}
if err != nil {
labels = append(labels, OutcomeLabel, failureOutcome, ReasonLabel, reason)
m.Measures.CapabilityCheckOutcome.With(labels...).Add(1)
labels[OutcomeLabel] = failureOutcome
labels[ReasonLabel] = reason
m.Measures.CapabilityCheckOutcome.With(labels).Add(1)
if m.ErrorOut {
return err
}
Expand All @@ -99,17 +117,17 @@ func (m MetricValidator) Check(ctx context.Context, _ bascule.Token) error {

reason, err = m.C.CheckAuthentication(auth, v)
if err != nil {
labels = append(labels, OutcomeLabel, failureOutcome, ReasonLabel, reason)
m.Measures.CapabilityCheckOutcome.With(labels...).Add(1)
labels[OutcomeLabel] = failureOutcome
labels[ReasonLabel] = reason
m.Measures.CapabilityCheckOutcome.With(labels).Add(1)
if m.ErrorOut {
return fmt.Errorf("endpoint auth for %v on %v failed: %v",
auth.Request.Method, auth.Request.URL.EscapedPath(), err)
}
return nil
}

labels = append(labels, OutcomeLabel, AcceptedOutcome, ReasonLabel, "")
m.Measures.CapabilityCheckOutcome.With(labels...).Add(1)
m.Measures.CapabilityCheckOutcome.With(labels).Add(1)
return nil
}

Expand Down Expand Up @@ -177,3 +195,23 @@ func determineEndpointMetric(endpoints []*regexp.Regexp, urlHit string) string {
}
return "not_recognized"
}

func ProvideMetricValidator(server string) fx.Option {
return fx.Provide(
fx.Annotated{
Name: fmt.Sprintf("%s_bascule_capability_measures", server),
// TODO: this will be fixed when Metric Validator gets its own New()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be part of another PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah: #87 👍

// function and Options.
Target: func(checker CapabilitiesChecker, measures *AuthCapabilityCheckMeasures,
endpoints []*regexp.Regexp, errorOut bool) MetricValidator {
return MetricValidator{
C: checker,
Measures: measures,
Endpoints: endpoints,
ErrorOut: errorOut,
Server: server,
}
},
},
)
}
12 changes: 9 additions & 3 deletions basculechecks/metricvalidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"regexp"
"testing"

"github.com/go-kit/kit/metrics/generic"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -126,9 +126,15 @@ func TestMetricValidatorCheck(t *testing.T) {
mockCapabilitiesChecker.On("CheckAuthentication", mock.Anything, mock.Anything).Return(tc.checkReason, tc.checkErr).Once()
}

counter := generic.NewCounter("test_capability_check")
mockMeasures := AuthCapabilityCheckMeasures{
CapabilityCheckOutcome: counter,
CapabilityCheckOutcome: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "testMetadataCounter",
Help: "testMetadataCounter",
},
[]string{ServerLabel, OutcomeLabel, ReasonLabel, ClientIDLabel,
PartnerIDLabel, EndpointLabel},
),
}

m := MetricValidator{
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/spf13/cast v1.3.1
github.com/stretchr/testify v1.7.0
github.com/xmidt-org/arrange v0.1.9
github.com/xmidt-org/themis v0.4.4
github.com/xmidt-org/touchstone v0.0.3
github.com/xmidt-org/webpa-common v1.11.5
go.uber.org/fx v1.13.1
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2/go.mod h1:ARgC
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/abdullin/seq v0.0.0-20160510034733-d5467c17e7af/go.mod h1:5Jv4cbFiHJMsVxt52+i0Ha45fjshj6wxYr1r19tB9bw=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
Expand Down Expand Up @@ -308,7 +307,6 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/influxdata/influxdb v1.5.1-0.20180921190457-8d679cf0c36e/go.mod h1:qZna6X/4elxqT3yI9iZYdZrWWdeFOOprn86kgg4+IzY=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/influxdata/influxdb1-client v0.0.0-20200515024757-02f0bf5dbca3/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/influxdata/influxdb1-client v0.0.0-20200827194710-b269163b24ab h1:HqW4xhhynfjrtEiiSGcQUd6vrK23iMam1FO8rI7mwig=
github.com/influxdata/influxdb1-client v0.0.0-20200827194710-b269163b24ab/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/jarcoal/httpmock v0.0.0-20180424175123-9c70cfe4a1da/go.mod h1:ks+b9deReOc7jgqp+e7LuFiCBH6Rm5hL32cLcEAArb4=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down Expand Up @@ -587,7 +585,6 @@ github.com/xmidt-org/bascule v0.8.0/go.mod h1:dPxlbNT3lCwYAtOq2zbzyzTEKgM+azLSbK
github.com/xmidt-org/bascule v0.8.1/go.mod h1:dPxlbNT3lCwYAtOq2zbzyzTEKgM+azLSbKKcVmgSHBY=
github.com/xmidt-org/bascule v0.9.0/go.mod h1:C64nSBtUTTK/f2/mCvvp/qJhav5raD0T+by68DCp/gU=
github.com/xmidt-org/httpaux v0.1.2/go.mod h1:qZnH2uObGPwHnOz8HcPNlbcd3gKEvdmxbIK3rgbQhto=
github.com/xmidt-org/themis v0.4.4 h1:KewitRxStW1xOehDBi0YyGZyRv3PjFdYUEDvQFf1Nmk=
github.com/xmidt-org/themis v0.4.4/go.mod h1:0qRYFvKdrQhwjxH/1nAiTgBGT4cegJR76gfEYF5P7so=
github.com/xmidt-org/touchstone v0.0.3 h1:6x+iQvCDNHQpChaxbv6bmmiWu+BkxCRKlOq7GdxkpG4=
github.com/xmidt-org/touchstone v0.0.3/go.mod h1:++4yF9lobCmQ6U5XOSFKysRtB0avwoXJ80MW+8Kl7ok=
Expand Down Expand Up @@ -866,7 +863,6 @@ gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKW
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/square/go-jose.v2 v2.3.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
Expand Down