Skip to content

Commit 1925cb7

Browse files
authored
Support renamed metrics (and fix build_info queries) (#61)
* Support renamed metrics * Fix build_info query to support case where build_info is not present * Fix latency query * Remove dead code * Remove more dead code * Remove trailing slash
1 parent eccde4c commit 1925cb7

File tree

1 file changed

+12
-29
lines changed

1 file changed

+12
-29
lines changed

src/queries.ts

+12-29
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
const COUNTER_NAME = "function_calls_count";
2-
const HISTOGRAM_BUCKET_NAME = "function_calls_duration_bucket";
3-
4-
const ADD_BUILD_INFO_LABELS =
5-
"* on (instance, job) group_left(version, commit) last_over_time(build_info[1s])";
1+
const COUNTER_NAME = "function_calls(_count)?(_total)?";
62

73
export function getRequestRate(
84
functionName: string,
@@ -19,36 +15,21 @@ export function getCalledByRequestRate(functionName: string) {
1915
${getSumQuery(COUNTER_NAME, { caller: functionName })}`;
2016
}
2117

22-
export function getErrorRatio(functionName: string) {
23-
return `# Percentage of calls to the \`${functionName}\` function that return errors, averaged over 5 minute windows
24-
25-
${getSumQuery(COUNTER_NAME, { function: functionName, result: "error" })} /
26-
${getSumQuery(COUNTER_NAME, { function: functionName })}`;
27-
}
28-
2918
export function getCalledByErrorRatio(functionName: string) {
3019
return `# Percentage of calls to functions called by \`${functionName}\` that return errors, averaged over 5 minute windows
3120
3221
${getSumQuery(COUNTER_NAME, { caller: functionName, result: "error" })} /
3322
${getSumQuery(COUNTER_NAME, { caller: functionName })}`;
3423
}
3524

36-
export function getLatency(functionName: string) {
37-
const latency = `sum by (le, function, module, commit, version) (rate(${HISTOGRAM_BUCKET_NAME}{function="${functionName}"}[5m]) ${ADD_BUILD_INFO_LABELS})`;
38-
39-
return `# 95th and 99th percentile latencies for the \`${functionName}\` function
40-
41-
label_replace(histogram_quantile(0.99, ${latency}), "percentile_latency", "99", "", "") or
42-
label_replace(histogram_quantile(0.95, ${latency}), "percentile_latency", "95", "", "")`;
43-
}
44-
4525
export function getSumQuery(
4626
metricName: string,
4727
labels: Record<string, string> = {},
4828
) {
4929
return `sum by (function, module) (
5030
rate(
51-
${metricName}{
31+
{
32+
__name__=~"${metricName}",
5233
${Object.entries(labels)
5334
.map(
5435
([key, value]) => ` ${key}="${value}"
@@ -65,7 +46,7 @@ export function generateRequestRateQuery(
6546
return `sum by (function, module, version, commit) (
6647
rate(
6748
{
68-
__name__=~"function_calls_count(?:_total)?",
49+
__name__=~"function_calls(_count)?(_total)?",
6950
function=~"${functionName}",
7051
module=~"${moduleName || ".*"}"
7152
}[5m]
@@ -86,7 +67,7 @@ export function generateErrorRatioQuery(
8667
sum by(function, module, version, commit) (
8768
rate(
8869
{
89-
__name__=~"function_calls_count(?:_total)?",
70+
__name__=~"function_calls(_count)?(_total)?",
9071
result="error",
9172
function=~"${functionName}",
9273
module=~"${moduleName || ".*"}"
@@ -102,7 +83,7 @@ export function generateErrorRatioQuery(
10283
sum by(function, module, version, commit) (
10384
rate(
10485
{
105-
__name__=~"function_calls_count(?:_total)?",
86+
__name__=~"function_calls(_count)?(_total)?",
10687
function=~"${functionName}",
10788
module=~"${moduleName || ".*"}"
10889
}[5m]
@@ -125,14 +106,15 @@ export function generateLatencyQuery(
125106
0.99,
126107
sum by (le, function, module, commit, version) (
127108
rate(
128-
function_calls_duration_bucket{
109+
{
110+
__name__=~"function_calls_duration(_seconds)?_bucket",
129111
function=~"${functionName}",
130112
module=~"${moduleName || ".*"}"
131113
}[5m]
132114
)
133115
# Attach the "version" and "commit" labels from the "build_info" metric
134116
* on (instance, job) group_left(version, commit) (
135-
last_over_time(build_info[1s])
117+
last_over_time(build_info[1s]) or on (instance, job) up
136118
)
137119
)
138120
),
@@ -146,14 +128,15 @@ export function generateLatencyQuery(
146128
0.95,
147129
sum by (le, function, module, commit, version) (
148130
rate(
149-
function_calls_duration_bucket{
131+
{
132+
__name__=~"function_calls_duration(_seconds)?_bucket",
150133
function=~"${functionName}",
151134
module=~"${moduleName || ".*"}"
152135
}[5m]
153136
)
154137
# Attach the "version" and "commit" labels from the "build_info" metric
155138
* on (instance, job) group_left(version, commit) (
156-
last_over_time(build_info[1s])
139+
last_over_time(build_info[1s]) or on (instance, job) up
157140
)
158141
)
159142
),

0 commit comments

Comments
 (0)