Skip to content

Commit fb2b1fd

Browse files
dweber019pmalek
authored andcommitted
feat: add admission count metric ingress_controller_admission_count
Originally submitted by https://github.com/dweber019 in Kong/kubernetes-ingress-controller#6087.
1 parent e7394fb commit fb2b1fd

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
- DataPlane: Enable incremental config sync by default when using Konnect as control plane.
4444
This improves performance of config syncs for large configurations.
4545
[#2759](https://github.com/Kong/kong-operator/pull/2759)
46+
- Added new metric for Prometheus called `ingress_controller_admission_count`.
47+
It's a counter and has two labels `allowed` to indicate if the resource was allowed
48+
and `resource` to indicate the resource under admission.
49+
[#2803](https://github.com/Kong/kong-operator/pull/2803)
4650

4751
## [v2.1.0-alpha.0]
4852

ingress-controller/internal/admission/handler.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
ctrlref "github.com/kong/kong-operator/ingress-controller/internal/controllers/reference"
2121
"github.com/kong/kong-operator/ingress-controller/internal/gatewayapi"
2222
"github.com/kong/kong-operator/ingress-controller/internal/labels"
23+
"github.com/kong/kong-operator/ingress-controller/internal/metrics"
2324
"github.com/kong/kong-operator/ingress-controller/internal/util"
2425
)
2526

@@ -37,6 +38,9 @@ type RequestHandler struct {
3738
// routing incoming requests to the appropriate validator.
3839
validators map[string]KongValidator
3940

41+
// PromMetrics provides the Prometheus registry to record metrics.
42+
PromMetrics *metrics.GlobalCtrlRuntimeMetricsRecorder
43+
4044
Logger logr.Logger
4145
}
4246

@@ -176,6 +180,15 @@ func (h RequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
176180
)
177181
}
178182

183+
resource := review.Request.Resource
184+
h.PromMetrics.RecordAdmissionResult(
185+
response.Allowed,
186+
fmt.Sprintf(
187+
"%s.%s/%s",
188+
resource.Resource, resource.Group, resource.Version,
189+
),
190+
)
191+
179192
review.Response = response
180193

181194
if err := json.NewEncoder(w).Encode(&review); err != nil {

ingress-controller/internal/metrics/prometheus.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"net"
7+
"strconv"
78
"time"
89

910
"github.com/prometheus/client_golang/prometheus"
@@ -76,6 +77,16 @@ const (
7677
InstanceIDKey string = "instance_id"
7778
)
7879

80+
const (
81+
// AllowedKey defines the key of the metric label indicating admission was allowed.
82+
AllowedKey string = "allowed"
83+
)
84+
85+
const (
86+
// AdmissionResourceKey defines the name of the metric label indicating which dataplane this time series is relevant for.
87+
AdmissionResourceKey string = "resource"
88+
)
89+
7990
// Regular config push metrics names.
8091
const (
8192
MetricNameConfigPushCount = "ingress_controller_configuration_push_count"
@@ -86,6 +97,7 @@ const (
8697
MetricNameTranslationBrokenResources = "ingress_controller_translation_broken_resource_count"
8798
MetricNameTranslationDuration = "ingress_controller_translation_duration_milliseconds"
8899
MetricNameConfigPushDuration = "ingress_controller_configuration_push_duration_milliseconds"
100+
MetricNameAdmissionCount = "ingress_controller_admission_count"
89101
)
90102

91103
// Fallback config push metrics names.
@@ -202,6 +214,20 @@ var (
202214
[]string{SuccessKey, ProtocolKey, DataplaneKey, InstanceIDKey},
203215
)
204216

217+
admissionCount = prometheus.NewCounterVec(
218+
prometheus.CounterOpts{
219+
Name: MetricNameAdmissionCount,
220+
Help: fmt.Sprintf(
221+
"Count of admissions processed by Kong. "+
222+
"`%s` describes whether an admission was allowed. "+
223+
"`%s` describes the resource under admission. ",
224+
AllowedKey,
225+
AdmissionResourceKey,
226+
),
227+
},
228+
[]string{AllowedKey, AdmissionResourceKey},
229+
)
230+
205231
configPushSize = prometheus.NewGaugeVec(
206232
prometheus.GaugeOpts{
207233
Name: MetricNameConfigPushSize,
@@ -407,6 +433,7 @@ func init() {
407433
configPushDuration,
408434
configPushSize,
409435
configPushSuccessTime,
436+
admissionCount,
410437
fallbackTranslationCount,
411438
fallbackTranslationBrokenResources,
412439
fallbackTranslationDuration,
@@ -493,6 +520,14 @@ func (c *GlobalCtrlRuntimeMetricsRecorder) RecordTranslationBrokenResources(coun
493520
}).Set(float64(count))
494521
}
495522

523+
// RecordAdmissionResult records an admission request result.
524+
func (c *GlobalCtrlRuntimeMetricsRecorder) RecordAdmissionResult(allowed bool, resource string) {
525+
admissionCount.With(prometheus.Labels{
526+
AllowedKey: strconv.FormatBool(allowed),
527+
AdmissionResourceKey: resource,
528+
}).Inc()
529+
}
530+
496531
// RecordFallbackTranslationFailure records a failed fallback configuration translation.
497532
func (c *GlobalCtrlRuntimeMetricsRecorder) RecordFallbackTranslationFailure(duration time.Duration) {
498533
fallbackTranslationCount.With(prometheus.Labels{

0 commit comments

Comments
 (0)