Skip to content

Commit

Permalink
Added metrics for bascule (#405)
Browse files Browse the repository at this point in the history
* Added metrics for bascule

* Small Fixes

* removed json tags

* added nil check

* updated glide
  • Loading branch information
kristinapathak authored and schmidtw committed May 9, 2019
1 parent bc84b2a commit ae0ba6e
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 3 deletions.
82 changes: 82 additions & 0 deletions basculechecks/metricListener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package basculechecks

import (
"time"

"github.com/Comcast/comcast-bascule/bascule"
"github.com/Comcast/comcast-bascule/bascule/basculehttp"
"github.com/SermoDigital/jose/jwt"
)

type MetricListener struct {
expLeeway time.Duration
nbfLeeway time.Duration
measures *JWTValidationMeasures
}

func (m *MetricListener) OnAuthenticated(auth bascule.Authentication) {
now := time.Now()

if m.measures == nil {
return // measure tools are not defined, skip
}

if auth.Token == nil {
return
}

c, ok := auth.Token.Attributes().Get("claims")
if !ok {
return // if there aren't any claims, skip
}
claims, ok := c.(jwt.Claims)
if !ok {
return // if claims aren't what we expect, skip
}

//how far did we land from the NBF (in seconds): ie. -1 means 1 sec before, 1 means 1 sec after
if nbf, nbfPresent := claims.NotBefore(); nbfPresent {
nbf = nbf.Add(-m.nbfLeeway)
offsetToNBF := now.Sub(nbf).Seconds()
m.measures.NBFHistogram.Observe(offsetToNBF)
}

//how far did we land from the EXP (in seconds): ie. -1 means 1 sec before, 1 means 1 sec after
if exp, expPresent := claims.Expiration(); expPresent {
exp = exp.Add(m.expLeeway)
offsetToEXP := now.Sub(exp).Seconds()
m.measures.ExpHistogram.Observe(offsetToEXP)
}
}

func (m *MetricListener) OnErrorResponse(e basculehttp.ErrorResponseReason, _ error) {
if m.measures == nil {
return
}
m.measures.ValidationReason.With(ReasonLabel, e.String()).Add(1)
}

type Option func(m *MetricListener)

func WithExpLeeway(e time.Duration) Option {
return func(m *MetricListener) {
m.expLeeway = e
}
}

func WithNbfLeeway(n time.Duration) Option {
return func(m *MetricListener) {
m.nbfLeeway = n
}
}

func NewMetricListener(m *JWTValidationMeasures, options ...Option) *MetricListener {
listener := MetricListener{
measures: m,
}

for _, o := range options {
o(&listener)
}
return &listener
}
59 changes: 59 additions & 0 deletions basculechecks/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package basculechecks

import (
"github.com/Comcast/webpa-common/xmetrics"
"github.com/go-kit/kit/metrics"
gokitprometheus "github.com/go-kit/kit/metrics/prometheus"
)

//Names for our metrics
const (
JWTValidationReasonCounter = "jwt_validation_reason"
NBFHistogram = "jwt_from_nbf_seconds"
EXPHistogram = "jwt_from_exp_seconds"
)

// labels
const (
ReasonLabel = "reason"
)

//Metrics returns the Metrics relevant to this package
func Metrics() []xmetrics.Metric {
return []xmetrics.Metric{
xmetrics.Metric{
Name: JWTValidationReasonCounter,
Type: xmetrics.CounterType,
Help: "Counter for validation resolutions per reason",
LabelNames: []string{ReasonLabel},
},
xmetrics.Metric{
Name: NBFHistogram,
Type: xmetrics.HistogramType,
Help: "Difference (in seconds) between time of JWT validation and nbf (including leeway)",
Buckets: []float64{-61, -11, -2, -1, 0, 9, 60}, // defines the upper inclusive (<=) bounds
},
xmetrics.Metric{
Name: EXPHistogram,
Type: xmetrics.HistogramType,
Help: "Difference (in seconds) between time of JWT validation and exp (including leeway)",
Buckets: []float64{-61, -11, -2, -1, 0, 9, 60},
},
}
}

//JWTValidationMeasures describes the defined metrics that will be used by clients
type JWTValidationMeasures struct {
NBFHistogram *gokitprometheus.Histogram
ExpHistogram *gokitprometheus.Histogram
ValidationReason metrics.Counter
}

//NewJWTValidationMeasures realizes desired metrics
func NewJWTValidationMeasures(r xmetrics.Registry) *JWTValidationMeasures {
return &JWTValidationMeasures{
NBFHistogram: gokitprometheus.NewHistogram(r.NewHistogramVec(NBFHistogram)),
ExpHistogram: gokitprometheus.NewHistogram(r.NewHistogramVec(EXPHistogram)),
ValidationReason: r.NewCounter(JWTValidationReasonCounter),
}
}
17 changes: 17 additions & 0 deletions basculechecks/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package basculechecks

import (
"testing"

"github.com/Comcast/webpa-common/xmetrics"
"github.com/stretchr/testify/assert"
)

func newTestJWTValidationMeasure() *JWTValidationMeasures {
return NewJWTValidationMeasures(xmetrics.MustNewRegistry(nil, Metrics))
}

func TestSimpleRun(t *testing.T) {
assert := assert.New(t)
assert.NotNil(newTestJWTValidationMeasure())
}
8 changes: 5 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ import:
version: v0.9.2
- package: github.com/miekg/dns
version: v1.0.12
- package: github.com/Comcast/comcast-bascule
version: v0.2.5

0 comments on commit ae0ba6e

Please sign in to comment.