Skip to content

Commit

Permalink
add webservice label to metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeynabRezaei committed Apr 7, 2024
1 parent ddae9a9 commit 0e4c3d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 17 additions & 8 deletions pkg/auth/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
HasUpstreamAuth = "upstream_auth_enabled"
ObjectKindLabel = "kind"
WithDownstreamDeadlineLabel = "with_downstream_deadline"
WebserviceLabel = "webservice"

MetricsKindSecret = "secret"
MetricsKindWebservice = "webservice"
Expand All @@ -33,7 +34,7 @@ var (
Name: "check_request_count",
Help: "CheckRequest count",
},
[]string{CerberusReasonLabel, CheckRequestVersionLabel, HasUpstreamAuth},
[]string{CerberusReasonLabel, CheckRequestVersionLabel, HasUpstreamAuth, WebserviceLabel},
)

reqLatency = prometheus.NewHistogramVec(
Expand All @@ -42,7 +43,7 @@ var (
Help: "CheckRequest durations (response times)",
Buckets: DurationBuckets,
},
[]string{CerberusReasonLabel, CheckRequestVersionLabel, HasUpstreamAuth},
[]string{CerberusReasonLabel, CheckRequestVersionLabel, HasUpstreamAuth, WebserviceLabel},
)

cacheUpdateCount = prometheus.NewCounter(
Expand Down Expand Up @@ -128,7 +129,7 @@ var (
Name: "upstream_auth_failed_requests_total",
Help: "Total number of failed UpstreamAuth requests",
},
[]string{"with_downstream_deadline"},
[]string{WithDownstreamDeadlineLabel},
)
)

Expand All @@ -149,39 +150,39 @@ func init() {
upstreamAuthFailedRequests,
)
}

///server
func AddReasonLabel(labels prometheus.Labels, reason CerberusReason) prometheus.Labels {
if labels == nil {
labels = prometheus.Labels{}
}
labels[CerberusReasonLabel] = string(reason)
return labels
}

///auth-cache
func AddKindLabel(labels prometheus.Labels, kind string) prometheus.Labels {
if labels == nil {
labels = prometheus.Labels{}
}
labels[ObjectKindLabel] = kind
return labels
}

///auth
func AddStatusLabel(labels prometheus.Labels, status int) prometheus.Labels {
if labels == nil {
labels = prometheus.Labels{}
}
labels[StatusCode] = strconv.Itoa(status)
return labels
}

///server
func AddUpstreamAuthLabel(labels prometheus.Labels, hasUpstreamAuth string) prometheus.Labels {
if labels == nil {
labels = prometheus.Labels{}
}
labels[HasUpstreamAuth] = hasUpstreamAuth
return labels
}

//auth
func AddWithDownstreamDeadlineLabel(labels prometheus.Labels, hasDeadline bool) prometheus.Labels {
if labels == nil {
labels = prometheus.Labels{}
Expand All @@ -193,3 +194,11 @@ func AddWithDownstreamDeadlineLabel(labels prometheus.Labels, hasDeadline bool)
}
return labels
}
///server
func AddWebserviceLabel(labels prometheus.Labels, wsvc string) prometheus.Labels {
if labels == nil {
labels = prometheus.Labels{}
}
labels[WebserviceLabel] = wsvc
return labels
}
2 changes: 2 additions & 0 deletions pkg/auth/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (a *authV2) Check(ctx context.Context, check *CheckRequestV2) (*CheckRespon
reason := CerberusReason(response.Response.Header.Get(CerberusHeaderReasonHeader))
labels := AddReasonLabel(nil, reason)
labels = AddUpstreamAuthLabel(labels, request.Context[HasUpstreamAuth])
labels = AddWebserviceLabel(labels, string(CerberusHeaderWebservice))
labels[CheckRequestVersionLabel] = MetricsCheckRequestVersion2
reqCount.With(labels).Inc()
reqLatency.With(labels).Observe(time.Since(reqStartTime).Seconds())
Expand All @@ -73,6 +74,7 @@ func (a *authV3) Check(ctx context.Context, check *CheckRequestV3) (*CheckRespon
reason := CerberusReason(response.Response.Header.Get(CerberusHeaderReasonHeader))
labels := AddReasonLabel(nil, reason)
labels = AddUpstreamAuthLabel(labels, request.Context[HasUpstreamAuth])
labels = AddWebserviceLabel(labels, string(CerberusHeaderWebservice))
labels[CheckRequestVersionLabel] = MetricsCheckRequestVersion3
reqCount.With(labels).Inc()
reqLatency.With(labels).Observe(time.Since(reqStartTime).Seconds())
Expand Down

0 comments on commit 0e4c3d0

Please sign in to comment.