k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/common/nodelocaldns_latency_prometheus.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package common 18 19 import ( 20 "fmt" 21 "time" 22 23 "k8s.io/klog/v2" 24 "k8s.io/perf-tests/clusterloader2/pkg/errors" 25 "k8s.io/perf-tests/clusterloader2/pkg/measurement" 26 measurementutil "k8s.io/perf-tests/clusterloader2/pkg/measurement/util" 27 "k8s.io/perf-tests/clusterloader2/pkg/util" 28 ) 29 30 const ( 31 nodelocaldnsLatencyPrometheusMeasurementName = "NodeLocalDNSLatencyPrometheus" 32 percLatencyQueryTemplate = `histogram_quantile(%v, sum(rate(coredns_dns_request_duration_seconds_bucket[%v])) by (le))` 33 defaultThreshold = 5 * time.Second 34 ) 35 36 var ( 37 desiredPercentiles = []float64{0.5, 0.9, 0.99} 38 ) 39 40 func init() { 41 create := func() measurement.Measurement { 42 return CreatePrometheusMeasurement(&nodelocaldnsLatencyGatherer{}) 43 } 44 if err := measurement.Register(nodelocaldnsLatencyPrometheusMeasurementName, create); err != nil { 45 klog.Fatalf("Cannot register %s: %v", nodelocaldnsLatencyPrometheusMeasurementName, err) 46 } 47 } 48 49 type nodelocaldnsLatencyGatherer struct{} 50 51 func (n *nodelocaldnsLatencyGatherer) Gather(executor QueryExecutor, startTime, endTime time.Time, config *measurement.Config) ([]measurement.Summary, error) { 52 result, err := n.getPercentileLatencies(executor, startTime, endTime) 53 if err != nil { 54 return nil, err 55 } 56 content, err := util.PrettyPrintJSON(result.ToPerfData(n.String())) 57 if err != nil { 58 return nil, err 59 } 60 summaries := []measurement.Summary{measurement.CreateSummary(n.String(), "json", content)} 61 return summaries, n.validateResult(config, result) 62 } 63 64 func (n *nodelocaldnsLatencyGatherer) String() string { 65 return nodelocaldnsLatencyPrometheusMeasurementName 66 } 67 68 func (n *nodelocaldnsLatencyGatherer) Configure(config *measurement.Config) error { 69 return nil 70 } 71 func (n *nodelocaldnsLatencyGatherer) IsEnabled(config *measurement.Config) bool { 72 return true 73 } 74 75 func (n *nodelocaldnsLatencyGatherer) validateResult(config *measurement.Config, result *measurementutil.LatencyMetric) error { 76 latencyUpperBound, err := util.GetDurationOrDefault(config.Params, "threshold", defaultThreshold) 77 if err != nil { 78 return err 79 } 80 if result.Perc99 > latencyUpperBound { 81 return errors.NewMetricViolationError( 82 "NodelocalDNS dns_request_duration_seconds", 83 fmt.Sprintf("99th Percentile Latency %v is higher than the upper bound of %s", result.Perc99, latencyUpperBound)) 84 } 85 return nil 86 } 87 88 func (n *nodelocaldnsLatencyGatherer) getPercentileLatencies(executor QueryExecutor, startTime, endTime time.Time) (*measurementutil.LatencyMetric, error) { 89 measurementDuration := endTime.Sub(startTime) 90 promDuration := measurementutil.ToPrometheusTime(measurementDuration) 91 errList := errors.NewErrorList() 92 result := &measurementutil.LatencyMetric{} 93 for _, percVal := range desiredPercentiles { 94 query := fmt.Sprintf(percLatencyQueryTemplate, percVal, promDuration) 95 samples, err := executor.Query(query, endTime) 96 if err != nil { 97 errList.Append(fmt.Errorf("failed to execute query %q, err - %v", query, err)) 98 continue 99 } 100 if len(samples) != 1 { 101 errList.Append(fmt.Errorf("got unexpected number of samples: %d for query %q", len(samples), query)) 102 continue 103 } 104 result.SetQuantile(percVal, time.Duration(float64(samples[0].Value)*float64(time.Second))) 105 } 106 if !errList.IsEmpty() { 107 return nil, fmt.Errorf("failed to compute latencies, errors - %s", errList.Error()) 108 } 109 return result, nil 110 }