-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added metrics for bascule * Small Fixes * removed json tags * added nil check * updated glide
- Loading branch information
1 parent
bc84b2a
commit ae0ba6e
Showing
5 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters