|
| 1 | +package prometheus |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + "regexp" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + DefaultPanelWidth = 1200 |
| 12 | + DefaultScrapeInterval = 60 |
| 13 | +) |
| 14 | + |
| 15 | +type IntervalOptions struct { |
| 16 | + Start time.Time |
| 17 | + End time.Time |
| 18 | + // PanelWidth refers to the width of the panel, measured in pixels |
| 19 | + // This is used to |
| 20 | + PanelWidth int |
| 21 | + // ScrapeInterval refers to the interval at which the metric is scraped, measured in number of seconds |
| 22 | + ScrapeInterval int |
| 23 | + // MaxDataPoints refers to the maximum number of data points to plot (if 0, use PanelWidth) |
| 24 | + MaxDataPoints int |
| 25 | + // MinInterval refers to the minimum interval to use (if 0, use ScrapeInterval) |
| 26 | + MinInterval int |
| 27 | +} |
| 28 | + |
| 29 | +// IntervalCalculator handles interval calculations and query substitution |
| 30 | +type IntervalCalculator struct { |
| 31 | + interval int // calculated $__interval in seconds |
| 32 | + rateInterval int // calculated $__rate_interval in seconds |
| 33 | +} |
| 34 | + |
| 35 | +// NewIntervalCalculator creates a new calculator with the given parameters |
| 36 | +func NewIntervalCalculator(opts IntervalOptions) *IntervalCalculator { |
| 37 | + calc := &IntervalCalculator{} |
| 38 | + calc.calculate(opts) |
| 39 | + return calc |
| 40 | +} |
| 41 | + |
| 42 | +// calculate computes $__interval and $__rate_interval |
| 43 | +func (c *IntervalCalculator) calculate(opts IntervalOptions) { |
| 44 | + // Set defaults |
| 45 | + width := opts.PanelWidth |
| 46 | + if width == 0 { |
| 47 | + width = DefaultPanelWidth |
| 48 | + } |
| 49 | + |
| 50 | + scrapeInterval := opts.ScrapeInterval |
| 51 | + if scrapeInterval == 0 { |
| 52 | + scrapeInterval = DefaultScrapeInterval |
| 53 | + } |
| 54 | + |
| 55 | + maxDataPoints := opts.MaxDataPoints |
| 56 | + if maxDataPoints == 0 { |
| 57 | + maxDataPoints = width |
| 58 | + } |
| 59 | + |
| 60 | + minInterval := opts.MinInterval |
| 61 | + if minInterval == 0 { |
| 62 | + minInterval = scrapeInterval |
| 63 | + } |
| 64 | + |
| 65 | + // Calculate range in seconds |
| 66 | + rangeSeconds := int(opts.End.Sub(opts.Start).Seconds()) |
| 67 | + |
| 68 | + // Calculate $__interval (basic interval) |
| 69 | + // interval = range / desired_points |
| 70 | + interval := rangeSeconds / maxDataPoints |
| 71 | + |
| 72 | + // Ensure minimum interval |
| 73 | + if interval < minInterval { |
| 74 | + interval = minInterval |
| 75 | + } |
| 76 | + |
| 77 | + c.interval = interval |
| 78 | + |
| 79 | + // Calculate $__rate_interval |
| 80 | + // rate_interval = max(interval + scrape_interval, 4 * scrape_interval) |
| 81 | + optionA := interval + scrapeInterval |
| 82 | + optionB := 4 * scrapeInterval |
| 83 | + |
| 84 | + c.rateInterval = int(math.Max(float64(optionA), float64(optionB))) |
| 85 | +} |
| 86 | + |
| 87 | +// GetInterval returns the calculated $__interval in seconds |
| 88 | +func (c *IntervalCalculator) GetInterval() int { |
| 89 | + return c.interval |
| 90 | +} |
| 91 | + |
| 92 | +// GetRateInterval returns the calculated $__rate_interval in seconds |
| 93 | +func (c *IntervalCalculator) GetRateInterval() int { |
| 94 | + return c.rateInterval |
| 95 | +} |
| 96 | + |
| 97 | +// GetIntervalString returns $__interval as a duration string (e.g., "60s") |
| 98 | +func (c *IntervalCalculator) GetIntervalString() string { |
| 99 | + return formatDuration(c.interval) |
| 100 | +} |
| 101 | + |
| 102 | +// GetRateIntervalString returns $__rate_interval as a duration string |
| 103 | +func (c *IntervalCalculator) GetRateIntervalString() string { |
| 104 | + return formatDuration(c.rateInterval) |
| 105 | +} |
| 106 | + |
| 107 | +// SubstituteVariables replaces ${__interval} and ${__rate_interval} in the query |
| 108 | +func (c *IntervalCalculator) SubstituteVariables(query string) string { |
| 109 | + // Replace ${__interval} and $__interval |
| 110 | + query = replaceVariable(query, "__interval", c.GetIntervalString()) |
| 111 | + |
| 112 | + // Replace ${__rate_interval} and $__rate_interval |
| 113 | + query = replaceVariable(query, "__rate_interval", c.GetRateIntervalString()) |
| 114 | + |
| 115 | + return query |
| 116 | +} |
| 117 | + |
| 118 | +// replaceVariable replaces both ${var} and $var patterns |
| 119 | +func replaceVariable(query, varName, value string) string { |
| 120 | + // Pattern 1: ${__variable} |
| 121 | + pattern1 := regexp.MustCompile(`\$\{` + varName + `\}`) |
| 122 | + query = pattern1.ReplaceAllString(query, value) |
| 123 | + |
| 124 | + // Pattern 2: $__variable (without braces) |
| 125 | + pattern2 := regexp.MustCompile(`\$` + varName + `\b`) |
| 126 | + query = pattern2.ReplaceAllString(query, value) |
| 127 | + |
| 128 | + return query |
| 129 | +} |
| 130 | + |
| 131 | +// formatDuration converts seconds to Prometheus duration string |
| 132 | +func formatDuration(seconds int) string { |
| 133 | + if seconds < 60 { |
| 134 | + return fmt.Sprintf("%ds", seconds) |
| 135 | + } else if seconds < 3600 { |
| 136 | + minutes := seconds / 60 |
| 137 | + remainder := seconds % 60 |
| 138 | + if remainder == 0 { |
| 139 | + return fmt.Sprintf("%dm", minutes) |
| 140 | + } |
| 141 | + return fmt.Sprintf("%dm%ds", minutes, remainder) |
| 142 | + } else if seconds < 86400 { |
| 143 | + hours := seconds / 3600 |
| 144 | + remainder := seconds % 3600 |
| 145 | + if remainder == 0 { |
| 146 | + return fmt.Sprintf("%dh", hours) |
| 147 | + } |
| 148 | + minutes := remainder / 60 |
| 149 | + return fmt.Sprintf("%dh%dm", hours, minutes) |
| 150 | + } else { |
| 151 | + days := seconds / 86400 |
| 152 | + remainder := seconds % 86400 |
| 153 | + if remainder == 0 { |
| 154 | + return fmt.Sprintf("%dd", days) |
| 155 | + } |
| 156 | + hours := remainder / 3600 |
| 157 | + return fmt.Sprintf("%dd%dh", days, hours) |
| 158 | + } |
| 159 | +} |
0 commit comments