Skip to content

Basic native histogram utilities #1164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions grafana-builder/grafana.libsonnet
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local utils = import 'mixin-utils/utils.libsonnet';

{
dashboard(title, uid='', datasource='default', datasource_regex=''):: {
// Stuff that isn't materialised.
Expand Down Expand Up @@ -70,6 +72,40 @@
},
},

addShowNativeLatencyVariable():: self {
templating+: {
list+: [{
current: {
selected: true,
text: 'classic',
value: '1',
},
description: 'Choose between showing latencies based on low precision classic or high precision native histogram metrics.',
hide: 0,
includeAll: false,
label: 'Latency metrics',
multi: false,
name: 'latency_metrics',
query: 'native : -1,classic : 1',
options: [
{
selected: false,
text: 'native',
value: '-1',
},
{
selected: true,
text: 'classic',
value: '1',
},
],
skipUrlSync: false,
type: 'custom',
useTags: false,
}],
},
},

dashboardLinkUrl(title, url):: self {
links+: [
{
Expand Down
87 changes: 87 additions & 0 deletions mixin-utils/utils.libsonnet
Original file line number Diff line number Diff line change
@@ -1,6 +1,93 @@
local g = import 'grafana-builder/grafana.libsonnet';

{
// The classicNativeHistogramQuantile function is used to calculate histogram quantiles from native histograms or classic histograms.
// Metric name should be provided without _bucket suffix.
nativeClassicHistogramQuantile(percentile, metric, selector, sum_by=[], rate_interval='$__rate_interval', multiplier='')::
local classicSumBy = if std.length(sum_by) > 0 then ' by (%(lbls)s) ' % { lbls: std.join(',', ['le'] + sum_by) } else ' by (le) ';
local nativeSumBy = if std.length(sum_by) > 0 then ' by (%(lbls)s) ' % { lbls: std.join(',', sum_by) } else ' ';
local multiplierStr = if multiplier == '' then '' else ' * %s' % multiplier;
{
classic: 'histogram_quantile(%(percentile)s, sum%(classicSumBy)s(rate(%(metric)s_bucket{%(selector)s}[%(rateInterval)s])))%(multiplierStr)s' % {
classicSumBy: classicSumBy,
metric: metric,
multiplierStr: multiplierStr,
percentile: percentile,
rateInterval: rate_interval,
selector: selector,
},
native: 'histogram_quantile(%(percentile)s, sum%(nativeSumBy)s(rate(%(metric)s{%(selector)s}[%(rateInterval)s])))%(multiplierStr)s' % {
metric: metric,
multiplierStr: multiplierStr,
nativeSumBy: nativeSumBy,
percentile: percentile,
rateInterval: rate_interval,
selector: selector,
},
},

// The classicNativeHistogramSumRate function is used to calculate the histogram sum of rate from native histograms or classic histograms.
// Metric name should be provided without _sum suffix.
nativeClassicHistogramSumRate(metric, selector, rate_interval='$__rate_interval')::
{
classic: 'rate(%(metric)s_sum{%(selector)s}[%(rateInterval)s])' % {
metric: metric,
rateInterval: rate_interval,
selector: selector,
},
native: 'histogram_sum(rate(%(metric)s{%(selector)s}[%(rateInterval)s]))' % {
metric: metric,
rateInterval: rate_interval,
selector: selector,
},
},


// The classicNativeHistogramCountRate function is used to calculate the histogram count of rate from native histograms or classic histograms.
// Metric name should be provided without _count suffix.
nativeClassicHistogramCountRate(metric, selector, rate_interval='$__rate_interval')::
{
classic: 'rate(%(metric)s_count{%(selector)s}[%(rateInterval)s])' % {
metric: metric,
rateInterval: rate_interval,
selector: selector,
},
native: 'histogram_count(rate(%(metric)s{%(selector)s}[%(rateInterval)s]))' % {
metric: metric,
rateInterval: rate_interval,
selector: selector,
},
},

// TODO(krajorama) Switch to histogram_avg function for native histograms later.
nativeClassicHistogramAverageRate(metric, selector, rate_interval='$__rate_interval', multiplier='')::
local multiplierStr = if multiplier == '' then '' else '%s * ' % multiplier;
{
classic: |||
%(multiplier)ssum(%(sumMetricQuery)s) /
sum(%(countMetricQuery)s)
||| % {
sumMetricQuery: $.nativeClassicHistogramSumRate(metric, selector, rate_interval).classic,
countMetricQuery: $.nativeClassicHistogramCountRate(metric, selector, rate_interval).classic,
multiplier: multiplierStr,
},
native: |||
%(multiplier)ssum(%(sumMetricQuery)s) /
sum(%(countMetricQuery)s)
||| % {
sumMetricQuery: $.nativeClassicHistogramSumRate(metric, selector, rate_interval).native,
countMetricQuery: $.nativeClassicHistogramCountRate(metric, selector, rate_interval).native,
multiplier: multiplierStr,
},
},

// showClassicHistogramQuery wraps a query defined as map {classic: q, native: q}, and compares the classic query
// to dashboard variable which should take -1 or +1 as values in order to hide or show the classic query.
showClassicHistogramQuery(query, dashboard_variable='latency_metrics'):: '%s < ($%s * +Inf)' % [query.classic, dashboard_variable],
// showNativeHistogramQuery wraps a query defined as map {classic: q, native: q}, and compares the native query
// to dashboard variable which should take -1 or +1 as values in order to show or hide the native query.
showNativeHistogramQuery(query, dashboard_variable='latency_metrics'):: '%s < ($%s * -Inf)' % [query.native, dashboard_variable],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, with this utility function, Grafana would always run all the (possibly very expensive) queries, only to get an empty result in one of the cases. (Remember, the PromQL engine doesn't do any smart moves to short-cut evaluations that can never ever return anything.)

Isn't there a Grafana nob we can turn here? Like switching off and on the query on the Grafana level?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately there's no Grafana nob at the moment. So the alternative is to optimize away the query or try to get Grafana to implement something. Both seems like a kind of optimization to be honest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sure, this is just about optimization. I was just hoping there is a simple way of accomplishing it.


histogramRules(metric, labels, interval='1m', record_native=false)::
local vars = {
metric: metric,
Expand Down