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

Observability metrics for the gRPC (ext-authz) and HTTP (wristband) servers #225

Merged
merged 9 commits into from
Mar 7, 2022
Prev Previous commit
Next Next commit
pkg/metrics decoupled from pkg/common
  • Loading branch information
guicassolato committed Mar 2, 2022
commit e578f816702835835af3a84fe4ac3168a5853f85
6 changes: 0 additions & 6 deletions pkg/common/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ type TypedEvaluator interface {
GetType() string
}

type Monitorable interface {
NamedEvaluator
TypedEvaluator
Measured() bool
}

type Prioritizable interface {
GetPriority() int
}
Expand Down
75 changes: 5 additions & 70 deletions pkg/common/mocks/mock_common.go

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

12 changes: 6 additions & 6 deletions pkg/config/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ func (config *AuthorizationConfig) GetType() string {
}
}

// impl:Monitorable

func (config *AuthorizationConfig) Measured() bool {
return config.MetricsEnabled
}

// impl:Prioritizable

func (config *AuthorizationConfig) GetPriority() int {
Expand All @@ -82,3 +76,9 @@ func (config *AuthorizationConfig) GetPriority() int {
func (config *AuthorizationConfig) GetConditions() []common.JSONPatternMatchingRule {
return config.Conditions
}

// impl:metrics.Object

func (config *AuthorizationConfig) Measured() bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just an idea, measured to me sounds more like something related to size or distance, maybe exportMetrics would be easier to read?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed to MetricsEnabled().

return config.MetricsEnabled
}
12 changes: 6 additions & 6 deletions pkg/config/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,6 @@ func (config *IdentityConfig) GetType() string {
}
}

// impl:Monitorable

func (config *IdentityConfig) Measured() bool {
return config.MetricsEnabled
}

// impl:Prioritizable

func (config *IdentityConfig) GetPriority() int {
Expand Down Expand Up @@ -189,3 +183,9 @@ func (config *IdentityConfig) FindSecretByName(lookup types.NamespacedName) *v1.
return nil
}
}

// impl:metrics.Object

func (config *IdentityConfig) Measured() bool {
return config.MetricsEnabled
}
12 changes: 6 additions & 6 deletions pkg/config/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ func (config *MetadataConfig) GetType() string {
}
}

// impl:Monitorable

func (config *MetadataConfig) Measured() bool {
return config.MetricsEnabled
}

// impl:Prioritizable

func (config *MetadataConfig) GetPriority() int {
Expand All @@ -88,3 +82,9 @@ func (config *MetadataConfig) GetPriority() int {
func (config *MetadataConfig) GetConditions() []common.JSONPatternMatchingRule {
return config.Conditions
}

// impl:metrics.Object

func (config *MetadataConfig) Measured() bool {
return config.MetricsEnabled
}
12 changes: 6 additions & 6 deletions pkg/config/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ func (config *ResponseConfig) GetType() string {
}
}

// impl:Monitorable

func (config *ResponseConfig) Measured() bool {
return config.MetricsEnabled
}

// impl:Prioritizable

func (config *ResponseConfig) GetPriority() int {
Expand All @@ -117,6 +111,12 @@ func (config *ResponseConfig) GetWristbandIssuer() common.WristbandIssuer {
return config.Wristband
}

// impl:metrics.Object

func (config *ResponseConfig) Measured() bool {
return config.MetricsEnabled
}

func WrapResponses(responses map[*ResponseConfig]interface{}) (responseHeaders map[string]string, responseMetadata map[string]interface{}) {
responseHeaders = make(map[string]string)
responseMetadata = make(map[string]interface{})
Expand Down
37 changes: 19 additions & 18 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package metrics
import (
"fmt"

"github.com/kuadrant/authorino/pkg/common"

"github.com/prometheus/client_golang/prometheus"
)

type Object interface {
GetType() string
GetName() string

Measured() bool
}

func Register(metrics ...prometheus.Collector) {
prometheus.MustRegister(metrics...)
}
Expand Down Expand Up @@ -49,8 +54,8 @@ func ReportMetricWithStatus(metric *prometheus.CounterVec, status string, labels
ReportMetric(metric, extendLabelValuesWithStatus(status, labels...)...)
}

func ReportMetricWithEvaluator(metric *prometheus.CounterVec, evaluator common.AuthConfigEvaluator, labels ...string) {
if labels, err := extendLabelValuesWithEvaluator(evaluator, labels...); err == nil {
func ReportMetricWithObject(metric *prometheus.CounterVec, obj Object, labels ...string) {
if labels, err := extendLabelValuesWithObject(obj, labels...); err == nil {
ReportMetric(metric, labels...)
}
}
Expand All @@ -71,8 +76,8 @@ func ReportTimedMetricWithStatus(metric *prometheus.HistogramVec, f func(), stat
ReportTimedMetric(metric, f, extendLabelValuesWithStatus(status, labels...)...)
}

func ReportTimedMetricWithEvaluator(metric *prometheus.HistogramVec, f func(), evaluator common.AuthConfigEvaluator, labels ...string) {
if labels, err := extendLabelValuesWithEvaluator(evaluator, labels...); err == nil {
func ReportTimedMetricWithObject(metric *prometheus.HistogramVec, f func(), obj Object, labels ...string) {
if labels, err := extendLabelValuesWithObject(obj, labels...); err == nil {
ReportTimedMetric(metric, f, labels...)
} else {
f()
Expand All @@ -92,17 +97,13 @@ func extendLabelValuesWithStatus(status string, baseLabels ...string) []string {
return labels
}

func extendLabelValuesWithEvaluator(evaluator common.AuthConfigEvaluator, baseLabels ...string) ([]string, error) {
if ev, ok := evaluator.(common.Monitorable); ok {
if !ev.Measured() {
return nil, fmt.Errorf("metrics are disabled for the evaluator")
}

labels := make([]string, len(baseLabels))
copy(labels, baseLabels)
labels = append(labels, ev.GetType(), ev.GetName())
return labels, nil
} else {
return baseLabels, fmt.Errorf("cannot cast evaluator to monitorable")
func extendLabelValuesWithObject(obj Object, baseLabels ...string) ([]string, error) {
if obj == nil || !obj.Measured() {
return nil, fmt.Errorf("metrics are disabled")
}

labels := make([]string, len(baseLabels))
copy(labels, baseLabels)
labels = append(labels, obj.GetType(), obj.GetName())
return labels, nil
}
Loading