2022-11-09 11:52:20 +01:00
package handlers
import (
2022-11-17 16:17:52 +01:00
"context"
2022-12-09 18:12:37 +01:00
"net/http"
2022-11-28 19:11:11 +01:00
"strings"
2022-11-09 11:52:20 +01:00
"time"
"github.com/go-logr/logr"
2022-11-28 19:11:11 +01:00
"github.com/kyverno/kyverno/pkg/config"
2022-12-09 10:49:45 +01:00
"github.com/kyverno/kyverno/pkg/metrics"
2023-06-19 11:09:08 +02:00
"go.opentelemetry.io/otel"
2022-11-28 19:11:11 +01:00
"go.opentelemetry.io/otel/attribute"
2023-05-11 12:16:48 +02:00
"go.opentelemetry.io/otel/metric"
2024-01-19 10:36:28 +01:00
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
2022-11-09 11:52:20 +01:00
)
2022-11-30 10:30:24 +01:00
func ( inner AdmissionHandler ) WithMetrics ( logger logr . Logger , metricsConfig config . MetricsConfiguration , attrs ... attribute . KeyValue ) AdmissionHandler {
2022-12-09 10:49:45 +01:00
return inner . withMetrics ( logger , metricsConfig , attrs ... ) . WithTrace ( "METRICS" )
2022-11-09 11:52:20 +01:00
}
2022-11-30 10:30:24 +01:00
func ( inner AdmissionHandler ) withMetrics ( logger logr . Logger , metricsConfig config . MetricsConfiguration , attrs ... attribute . KeyValue ) AdmissionHandler {
2023-06-19 11:09:08 +02:00
meter := otel . GetMeterProvider ( ) . Meter ( metrics . MeterName )
2023-02-23 18:54:33 +01:00
requestsMetric , err := meter . Int64Counter (
2022-12-11 22:51:15 +01:00
"kyverno_admission_requests" ,
2023-05-11 12:16:48 +02:00
metric . WithDescription ( "can be used to track the number of admission requests encountered by Kyverno in the cluster" ) ,
2022-11-28 19:11:11 +01:00
)
if err != nil {
logger . Error ( err , "Failed to create instrument, kyverno_admission_requests_total" )
}
2023-02-23 18:54:33 +01:00
durationMetric , err := meter . Float64Histogram (
2022-11-28 19:11:11 +01:00
"kyverno_admission_review_duration_seconds" ,
2023-05-11 12:16:48 +02:00
metric . WithDescription ( "can be used to track the latencies (in seconds) associated with the entire individual admission review. For example, if an incoming request trigger, say, five policies, this metric will track the e2e latency associated with the execution of all those policies" ) ,
2022-11-28 19:11:11 +01:00
)
if err != nil {
logger . Error ( err , "Failed to create instrument, kyverno_admission_review_duration_seconds" )
}
2023-04-04 07:11:18 +02:00
return func ( ctx context . Context , logger logr . Logger , request AdmissionRequest , startTime time . Time ) AdmissionResponse {
2022-11-28 09:59:05 +01:00
response := inner ( ctx , logger , request , startTime )
2022-11-28 19:11:11 +01:00
namespace := request . Namespace
if metricsConfig . CheckNamespace ( namespace ) {
operation := strings . ToLower ( string ( request . Operation ) )
2022-11-30 10:30:24 +01:00
attributes := [ ] attribute . KeyValue {
attribute . String ( "resource_kind" , request . Kind . Kind ) ,
attribute . String ( "resource_namespace" , namespace ) ,
attribute . String ( "resource_request_operation" , operation ) ,
2023-04-03 20:08:57 +02:00
attribute . Bool ( "request_allowed" , response . Allowed ) ,
2022-11-30 10:30:24 +01:00
}
attributes = append ( attributes , attrs ... )
2022-12-09 18:12:37 +01:00
if durationMetric != nil {
2022-11-28 19:11:11 +01:00
defer func ( ) {
latency := int64 ( time . Since ( startTime ) )
2022-12-09 18:12:37 +01:00
durationInSeconds := float64 ( latency ) / float64 ( 1000 * 1000 * 1000 )
2023-05-11 12:16:48 +02:00
durationMetric . Record ( ctx , durationInSeconds , metric . WithAttributes ( attributes ... ) )
2022-11-28 19:11:11 +01:00
} ( )
}
2022-12-09 18:12:37 +01:00
if requestsMetric != nil {
2023-05-11 12:16:48 +02:00
requestsMetric . Add ( ctx , 1 , metric . WithAttributes ( attributes ... ) )
2022-11-28 19:11:11 +01:00
}
}
2022-11-28 09:59:05 +01:00
return response
2022-11-09 11:52:20 +01:00
}
}
2022-12-09 18:12:37 +01:00
func ( inner HttpHandler ) WithMetrics ( logger logr . Logger , attrs ... attribute . KeyValue ) HttpHandler {
return inner . withMetrics ( logger , attrs ... ) . WithTrace ( "METRICS" )
}
func ( inner HttpHandler ) withMetrics ( logger logr . Logger , attrs ... attribute . KeyValue ) HttpHandler {
2023-06-19 11:09:08 +02:00
meter := otel . GetMeterProvider ( ) . Meter ( metrics . MeterName )
2023-02-23 18:54:33 +01:00
requestsMetric , err := meter . Int64Counter (
2023-05-15 21:26:17 +02:00
"kyverno_http_requests" ,
2023-05-11 12:16:48 +02:00
metric . WithDescription ( "can be used to track the number of http requests" ) ,
2022-12-09 18:12:37 +01:00
)
if err != nil {
2023-05-15 21:26:17 +02:00
logger . Error ( err , "Failed to create instrument, kyverno_http_requests" )
2022-12-09 18:12:37 +01:00
}
2023-02-23 18:54:33 +01:00
durationMetric , err := meter . Float64Histogram (
2022-12-09 18:12:37 +01:00
"kyverno_http_requests_duration_seconds" ,
2023-05-11 12:16:48 +02:00
metric . WithDescription ( "can be used to track the latencies (in seconds) associated with the entire individual http request." ) ,
2022-12-09 18:12:37 +01:00
)
if err != nil {
logger . Error ( err , "Failed to create instrument, kyverno_http_requests_duration_seconds" )
}
return func ( writer http . ResponseWriter , request * http . Request ) {
startTime := time . Now ( )
attributes := [ ] attribute . KeyValue {
2023-02-23 18:54:33 +01:00
// semconv.HTTPHostKey.String(request.Host),
2022-12-09 18:12:37 +01:00
semconv . HTTPMethodKey . String ( request . Method ) ,
semconv . HTTPURLKey . String ( request . RequestURI ) ,
}
attributes = append ( attributes , attrs ... )
if requestsMetric != nil {
2023-05-11 12:16:48 +02:00
requestsMetric . Add ( request . Context ( ) , 1 , metric . WithAttributes ( attributes ... ) )
2022-12-09 18:12:37 +01:00
}
if durationMetric != nil {
defer func ( ) {
latency := int64 ( time . Since ( startTime ) )
durationInSeconds := float64 ( latency ) / float64 ( 1000 * 1000 * 1000 )
2023-05-11 12:16:48 +02:00
durationMetric . Record ( request . Context ( ) , durationInSeconds , metric . WithAttributes ( attributes ... ) )
2022-12-09 18:12:37 +01:00
} ( )
}
inner ( writer , request )
}
}