Skip to content

Commit

Permalink
configuerable histogram buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
myaser committed Jan 18, 2023
1 parent ae27984 commit 69467af
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
27 changes: 26 additions & 1 deletion internal/kubenurse/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/servicecheck/servicecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"},
)
Expand Down
2 changes: 1 addition & 1 deletion internal/servicecheck/servicecheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 69467af

Please sign in to comment.