-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecording.ts
141 lines (129 loc) · 3.39 KB
/
recording.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import type { KubernetesPrometheusRules , PrometheusRule, RecordingRule, Group, Labels } from './common.ts'
import { stringify as yamlStringify, EXTENDED_SCHEMA } from 'https://deno.land/[email protected]/encoding/yaml.ts'
export { stringify as yamlStringify, EXTENDED_SCHEMA } from 'https://deno.land/[email protected]/encoding/yaml.ts'
type Percentile =
| "90"
| "95"
| "99"
export const percentiles: Percentile[] = [
"90",
"95",
"99",
]
type TimeFrame = "1m" | "5m" | "30m" | "1h" | "6h" | "1d"
type Range = {
interval: TimeFrame;
windows: TimeFrame[];
}
export const ranges: Range[] = [
{
interval: "1m",
windows: ["1m", "5m"],
},
{
interval: "5m",
windows: ["30m", "1h"],
},
{
interval: "1h",
windows: ["6h", "1d"],
},
]
type ExpressionTemplate = (percentile: Percentile, app: string, window: TimeFrame) => string
type BaseQuery = {
name: string
expr: ExpressionTemplate
labels?: Labels
}
type Query = {
name: string
expr: ExpressionTemplate
recordingRule: (groupName: GroupName, app: string, percentile: Percentile, window: TimeFrame) => RecordingRule
}
export function latencyQuery(query: BaseQuery): Query {
return {
...query,
recordingRule: (groupName, app, percentile, window) => ({
record: ["job", groupName, query.name, percentile, window].join(":"),
expr: query.expr(percentile, app, window),
})
}
}
export function throughputQuery(query: BaseQuery): Query {
return {
...query,
recordingRule: (groupName, app, percentile, window) => ({
record: ["job", groupName, query.name, window].join(":"),
expr: query.expr(percentile, app, window),
labels: query.labels
})
}
}
type GroupName = "latency" | "throughput"
export function recordingPrometheusRule(
prometheusName: string,
groupName: GroupName,
queries: Query[],
ranges: Range[],
percentiles: Percentile[],
apps: string[],
): KubernetesPrometheusRules {
return {
kind: "Prometheus/Rule",
name: `${prometheusName}-prometheus-rules-${groupName}`,
spec: {
prometheus: `prometheus-${prometheusName}`,
...prometheusRuleFor(groupName, queries, ranges, percentiles, apps),
}
}
}
export function prometheusRuleFor(
groupName: GroupName,
queries: Query[],
ranges: Range[],
percentiles: Percentile[],
apps: string[],
): PrometheusRule {
return {
groups: [...group(groupName, queries, ranges, percentiles, apps)]
}
}
function* group(
groupName: GroupName,
queries: Query[],
ranges: Range[],
percentiles: Percentile[],
apps: string[],
): Generator<Group> {
for (const range of ranges) {
yield {
name: `${groupName}_${range.interval}`,
interval: range.interval,
rules: [...rule(groupName, queries, percentiles, apps, range.windows)],
}
}
return
}
function* rule(
groupName: GroupName,
queries: Query[],
percentiles: Percentile[],
apps: string[],
windows: TimeFrame[],
): Generator<RecordingRule> {
for (const query of queries) {
for (const percentile of percentiles) {
for (const app of apps) {
for (const window of windows) {
yield query.recordingRule(groupName, app, percentile, window)
}
}
}
}
return
}
export async function writeYaml(fileName: string, rule: Record<string, unknown>) {
const yaml = yamlStringify(rule, {schema: EXTENDED_SCHEMA})
await Deno.writeTextFile(fileName, yaml)
console.log(`${fileName} written`)
}