1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00
kyverno/pkg/breaker/breaker.go
Ammar Yasser c56c60c136
Reports controller circuit breaker (#11329)
* chore: Fix spelling issue in breaker logging

Signed-off-by: aerosouund <aerosound161@gmail.com>

* feat: Introduce circuit breaking in background report scanning

Add the breaker as a field of the background controller and use it in the storeReport method which handles report creation

Signed-off-by: aerosouund <aerosound161@gmail.com>

* feat: Add required flags and instantiation for the circuit breaker in the background reports controller

Signed-off-by: aerosouund <aerosound161@gmail.com>

* fix: Add flag for max background reports in the reports controller

Signed-off-by: ammar <ammar.yasser@vodafone.com>

* chore: Update flag description to use ephemeralreports instead of background reports

Signed-off-by: aerosouund <aerosound161@gmail.com>

* chore: Use a less verbose description for the flag

Co-authored-by: shuting <shuting@nirmata.com>
Signed-off-by: Ammar Yasser <aerosound161@gmail.com>

---------

Signed-off-by: aerosouund <aerosound161@gmail.com>
Signed-off-by: ammar <ammar.yasser@vodafone.com>
Signed-off-by: Ammar Yasser <aerosound161@gmail.com>
Co-authored-by: ammar <ammar.yasser@vodafone.com>
Co-authored-by: Jim Bugwadia <jim@nirmata.com>
Co-authored-by: shuting <shuting@nirmata.com>
2024-10-11 07:34:41 +00:00

66 lines
1.6 KiB
Go

package breaker
import (
"context"
"github.com/kyverno/kyverno/pkg/logging"
"github.com/kyverno/kyverno/pkg/metrics"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
sdkmetric "go.opentelemetry.io/otel/metric"
)
type Breaker interface {
Do(context.Context, func(context.Context) error) error
}
type breaker struct {
name string
drops sdkmetric.Int64Counter
total sdkmetric.Int64Counter
open func(context.Context) bool
}
func NewBreaker(name string, open func(context.Context) bool) *breaker {
logger := logging.WithName("circuit-breaker")
meter := otel.GetMeterProvider().Meter(metrics.MeterName)
drops, err := meter.Int64Counter(
"kyverno_breaker_drops",
sdkmetric.WithDescription("track the number of times the breaker failed open and dropped"),
)
if err != nil {
logger.Error(err, "Failed to create instrument, kyverno_breaker_drops")
}
total, err := meter.Int64Counter(
"kyverno_breaker_total",
sdkmetric.WithDescription("track number of times the breaker was invoked"),
)
if err != nil {
logger.Error(err, "Failed to create instrument, kyverno_breaker_total")
}
return &breaker{
name: name,
drops: drops,
total: total,
open: open,
}
}
func (b *breaker) Do(ctx context.Context, inner func(context.Context) error) error {
attributes := sdkmetric.WithAttributes(
attribute.String("circuit_name", b.name),
)
if b.total != nil {
b.total.Add(ctx, 1, attributes)
}
if b.open != nil && b.open(ctx) {
if b.drops != nil {
b.drops.Add(ctx, 1, attributes)
}
return nil
}
if inner == nil {
return nil
}
return inner(ctx)
}