From 69467af6f3cb1e7a061cbb185cefff8611553864 Mon Sep 17 00:00:00 2001 From: Mahmoud Gaballah Date: Wed, 18 Jan 2023 10:22:54 +0100 Subject: [PATCH] configuerable histogram buckets --- internal/kubenurse/server.go | 27 +++++++++++++++++++++- internal/servicecheck/servicecheck.go | 4 ++-- internal/servicecheck/servicecheck_test.go | 2 +- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/internal/kubenurse/server.go b/internal/kubenurse/server.go index 5bb586fb..d4a075aa 100644 --- a/internal/kubenurse/server.go +++ b/internal/kubenurse/server.go @@ -6,6 +6,8 @@ import ( "fmt" "net/http" "os" + "strconv" + "strings" "sync" "time" @@ -101,8 +103,31 @@ func New(ctx context.Context, k8s kubernetes.Interface) (*Server, error) { return nil, fmt.Errorf("create k8s discovery client: %w", err) } + var histogramBuckets []float64 + + if bucketsString := os.Getenv("KUBENURSE_HISTOGRAM_BUCKETS"); bucketsString != "" { + var buckets []float64 + + for _, bucketStr := range strings.Split(bucketsString, ",") { + bucket, err := strconv.ParseFloat(bucketStr, 64) + + if err != nil { + buckets = nil + break + } + + buckets = append(buckets, bucket) + } + + histogramBuckets = buckets + } + + if histogramBuckets == nil { + histogramBuckets = prometheus.DefBuckets + } + // setup checker - chk, err := servicecheck.New(ctx, discovery, promRegistry, server.allowUnschedulable, 3*time.Second) + chk, err := servicecheck.New(ctx, discovery, promRegistry, server.allowUnschedulable, 3*time.Second, histogramBuckets) if err != nil { return nil, err } diff --git a/internal/servicecheck/servicecheck.go b/internal/servicecheck/servicecheck.go index 77d569be..a95489f1 100644 --- a/internal/servicecheck/servicecheck.go +++ b/internal/servicecheck/servicecheck.go @@ -23,7 +23,7 @@ const ( // New configures the checker with a httpClient and a cache timeout for check // results. Other parameters of the Checker struct need to be configured separately. func New(ctx context.Context, discovery *kubediscovery.Client, promRegistry *prometheus.Registry, - allowUnschedulable bool, cacheTTL time.Duration) (*Checker, error) { + allowUnschedulable bool, cacheTTL time.Duration, durationHistogramBuckets []float64) (*Checker, error) { errorCounter := prometheus.NewCounterVec( prometheus.CounterOpts{ Namespace: metricsNamespace, @@ -38,7 +38,7 @@ func New(ctx context.Context, discovery *kubediscovery.Client, promRegistry *pro Namespace: metricsNamespace, Name: "request_duration", Help: "Kubenurse request duration partitioned by target path", - Buckets: prometheus.DefBuckets, + Buckets: durationHistogramBuckets, }, []string{"type"}, ) diff --git a/internal/servicecheck/servicecheck_test.go b/internal/servicecheck/servicecheck_test.go index b6604e68..07cf4db8 100644 --- a/internal/servicecheck/servicecheck_test.go +++ b/internal/servicecheck/servicecheck_test.go @@ -40,7 +40,7 @@ func TestCombined(t *testing.T) { discovery, err := kubediscovery.New(context.Background(), fakeClient, false) r.NoError(err) - checker, err := New(context.Background(), discovery, prometheus.NewRegistry(), false, 3*time.Second) + checker, err := New(context.Background(), discovery, prometheus.NewRegistry(), false, 3*time.Second, prometheus.DefBuckets) r.NoError(err) r.NotNil(checker)