k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/prometheus/manifests/dashboards/defaults.py (about) 1 #!/usr/bin/env python3 2 3 # Copyright 2019 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 import re 18 import attr 19 from grafanalib import core as g 20 21 DECREASING_ORDER_TOOLTIP = g.Tooltip(sort=g.SORT_DESC) 22 PANEL_HEIGHT = g.Pixels(300) 23 QUANTILES = [0.99, 0.9, 0.5] 24 25 SOURCE_TEMPLATE = g.Template(name="source", type="datasource", query="prometheus") 26 27 28 @attr.s 29 class Dashboard(g.Dashboard): 30 time = attr.ib(default=g.Time("now-30d", "now")) 31 # Make it possible to use $source as a source. 32 templating = attr.ib(default=g.Templating(list=[SOURCE_TEMPLATE])) 33 34 35 # Graph is a g.Graph with reasonable defaults applied. 36 @attr.s 37 class Graph(g.Graph): 38 dataSource = attr.ib(default="$source") 39 span = attr.ib(default=g.TOTAL_SPAN) 40 tooltip = attr.ib(default=DECREASING_ORDER_TOOLTIP) 41 nullPointMode = attr.ib(default=None) 42 43 44 @attr.s 45 class Row(g.Row): 46 height = attr.ib(default=PANEL_HEIGHT) 47 48 49 @attr.s 50 class Target(g.Target): 51 datasource = attr.ib(default="$source") 52 53 54 @attr.s 55 class TargetWithInterval(Target): 56 interval = attr.ib(default="5s") 57 intervalFactor = attr.ib(default=1) 58 59 60 def simple_graph(title, exprs, legend="", interval="5s", **kwargs): 61 if not isinstance(exprs, (list, tuple)): 62 exprs = [exprs] 63 if legend != "" and len(exprs) != 1: 64 raise ValueError("legend can be specified only for a 1-element exprs") 65 return Graph( 66 title=title, 67 # One graph per row. 68 targets=[ 69 Target( 70 expr=expr, legendFormat=legend, interval=interval, intervalFactor=1 71 ) 72 for expr in exprs 73 ], 74 **kwargs 75 ) 76 77 78 def show_quantiles(queryTemplate, quantiles=None, legend=""): 79 quantiles = quantiles or QUANTILES 80 targets = [] 81 for quantile in quantiles: 82 q = "{:.2f}".format(quantile) 83 l = legend or q 84 targets.append(Target(expr=queryTemplate.format(quantile=q), legendFormat=l)) 85 return targets 86 87 88 def min_max_avg(base, by, legend=""): 89 return [ 90 Target( 91 expr="{func}({query}) by ({by})".format( 92 func=f, query=base, by=", ".join(by) 93 ), 94 legendFormat="{func} {legend}".format(func=f, legend=legend), 95 ) 96 for f in ("min", "avg", "max") 97 ] 98 99 100 def any_of(*choices): 101 return "|".join(choices) 102 103 104 def one_line(text): 105 """Turns multiline PromQL string into a one line. 106 107 Useful to keep sane diffs for generated (*.json) dashboards. 108 """ 109 tokens = text.split('"') 110 for i, item in enumerate(tokens): 111 if not i % 2: 112 item = re.sub(r"\s+", "", item) 113 item = re.sub(",", ", ", item) 114 item = re.sub(r"\)by\(", ") by (", item) 115 tokens[i] = item 116 return '"'.join(tokens)