Skip to content
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

wip: support native histograms along classic histograms in panels #1136

Closed
wants to merge 6 commits into from
Closed
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
216 changes: 205 additions & 11 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 @@
},
},

addShowHistoricDataVariable():: self {
templating+: {
list+: [{
current: {
selected: true,
text: 'show',
value: '1',
},
description: 'When setting this option to 1, panels will query and show deprecated low precision histogram metrics.',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
description: 'When setting this option to 1, panels will query and show deprecated low precision histogram metrics.',
description: 'When setting this option to show, panels will query and show deprecated low precision histogram metrics.',

hide: 0,
includeAll: false,
label: 'Show historic data',
multi: false,
name: 'show_classic_histograms',
query: 'hide : 0,show : 1',
options: [
{
selected: false,
text: 'hide',
value: '0'
},
{
selected: true,
text: 'show',
value: '1'
}
],
skipUrlSync: false,
type: 'custom',
useTags: false,
}],
},
},

dashboardLinkUrl(title, url):: self {
links+: [
{
Expand Down Expand Up @@ -420,18 +456,20 @@
},
],

httpStatusColors:: {
'1xx': '#EAB839',
'2xx': '#7EB26D',
'3xx': '#6ED0E0',
'4xx': '#EF843C',
'5xx': '#E24D42',
OK: '#7EB26D',
success: '#7EB26D',
'error': '#E24D42',
cancel: '#A9A9A9',
},

qpsPanel(selector, statusLabelName='status_code'):: {
aliasColors: {
'1xx': '#EAB839',
'2xx': '#7EB26D',
'3xx': '#6ED0E0',
'4xx': '#EF843C',
'5xx': '#E24D42',
OK: '#7EB26D',
success: '#7EB26D',
'error': '#E24D42',
cancel: '#A9A9A9',
},
aliasColors: $.httpStatusColors,
targets: [
{
expr:
Expand All @@ -448,6 +486,90 @@
],
} + $.stack,

// Assumes that the metricName is for a histogram (as opposed to qpsPanel above)
// Assumes that there is a dashboard variable named show_classic_histograms, values are 0 or 1
qpsPanelNativeHistogram(title, metricName, selector, statusLabelName='status_code'):: $.timeseriesPanel(title) {
fieldConfig+: {
defaults+: {
custom+: {
lineWidth: 0,
fillOpacity: 100, // Get solid fill.
stacking: {
mode: 'normal', // This will be overridden for classic series to hide those behind.
group: 'A'
},
},
unit: 'reqps',
min: 0,
},
overrides+: [{
matcher: {
id: 'byRegexp',
options: '(historic_)?' + status,
},
properties: [
{
id: 'color',
value: {
mode: 'fixed',
fixedColor: $.httpStatusColors[status],
},
},
],
} for status in std.objectFieldsAll($.httpStatusColors)] + [
// Make the classic histogram query results be in the backround stacked.
{
matcher: {
id: 'byFrameRefID',
options: 'A_classic',
},
properties: [
{
id: 'custom.stacking',
value: {
mode: 'normal',
group: 'B',
},
},
],
},
],
},
targets: [
{
expr:
|||
sum by (status) (
label_replace(label_replace(%(metricQuery)s,
"status", "${1}xx", "%(label)s", "([0-9]).."),
"status", "${1}", "%(label)s", "([a-zA-Z]+)"))
||| % {
metricQuery: utils.nativeClassicHistogramCountRate(metricName, selector).native,
label: statusLabelName,
},
format: 'time_series',
legendFormat: '{{status}}',
refId: 'A',
},
{
expr:
|||
sum by (status) (
label_replace(label_replace(%(metricQuery)s,
"status", "${1}xx", "%(label)s", "([0-9]).."),
"status", "${1}", "%(label)s", "([a-zA-Z]+)"))
< ($show_classic_histograms * +Inf)
||| % {
metricQuery: utils.nativeClassicHistogramCountRate(metricName, selector).classic,
label: statusLabelName,
},
format: 'time_series',
legendFormat: 'historic_{{status}}',
refId: 'A_classic',
},
],
} + $.stack,

latencyPanel(metricName, selector, multiplier='1e3'):: {
nullPointMode: 'null as zero',
targets: [
Expand All @@ -473,6 +595,78 @@
yaxes: $.yaxes('ms'),
},

// Assumes that there is a dashboard variable named show_classic_histograms, values are 0 or 1
latencyPanelNativeHistogram(title, metricName, selector, multiplier='1e3'):: $.timeseriesPanel(title) {
nullPointMode: 'null as zero',
targets: [
{
expr: '(%(metricQuery)s) * %(multiplier)s' % {
metricQuery: utils.nativeClassicHistogramQuantile('0.99', metricName, selector).native,
multiplier: multiplier,
},
format: 'time_series',
legendFormat: '99th percentile',
refId: 'A',
},
{
expr: '(%(metricQuery)s) * %(multiplier)s < ($show_classic_histograms * +Inf)' % {
metricQuery: utils.nativeClassicHistogramQuantile('0.99', metricName, selector).classic,
multiplier: multiplier,
},
format: 'time_series',
legendFormat: 'Historic 99th percentile',
refId: 'A_classic',
},
{
expr: '(%(metricQuery)s) * %(multiplier)s' % {
metricQuery: utils.nativeClassicHistogramQuantile('0.50', metricName, selector).native,
multiplier: multiplier,
},
format: 'time_series',
legendFormat: '50th percentile',
refId: 'B',
},
{
expr: '(%(metricQuery)s) * %(multiplier)s < ($show_classic_histograms * +Inf)' % {
metricQuery: utils.nativeClassicHistogramQuantile('0.50', metricName, selector).classic,
multiplier: multiplier,
},
format: 'time_series',
legendFormat: 'Historic 50th percentile',
refId: 'B_classic',
},
{
expr:
|||
%(multiplier)s * sum(%(sumMetricQuery)s) /
sum(%(countMetricQuery)s)
||| % {
sumMetricQuery: utils.nativeClassicHistogramSumRate(metricName, selector).native,
countMetricQuery: utils.nativeClassicHistogramCountRate(metricName, selector).native,
multiplier: multiplier,
},
format: 'time_series',
legendFormat: 'Average',
refId: 'C',
},
{
expr:
|||
%(multiplier)s * sum(%(sumMetricQuery)s) /
sum(%(countMetricQuery)s) < ($show_classic_histograms * +Inf)
||| % {
sumMetricQuery: utils.nativeClassicHistogramSumRate(metricName, selector).classic,
countMetricQuery: utils.nativeClassicHistogramCountRate(metricName, selector).classic,
multiplier: multiplier,
},
format: 'time_series',
legendFormat: 'Historic average',
refId: 'C_classic',
},
],
yaxes: $.yaxes('ms'),
},

selector:: {
eq(label, value):: { label: label, op: '=', value: value },
neq(label, value):: { label: label, op: '!=', value: value },
Expand Down
55 changes: 55 additions & 0 deletions mixin-utils/utils.libsonnet
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
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')::
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 ' ';
{
classic: 'histogram_quantile(%(percentile)s, sum%(classicSumBy)s(rate(%(metric)s_bucket{%(selector)s}[%(rateInterval)s])))' % {
classicSumBy: classicSumBy,
metric: metric,
percentile: percentile,
rateInterval: rate_interval,
selector: selector,
},
native: 'histogram_quantile(%(percentile)s, sum%(nativeSumBy)s(rate(%(metric)s{%(selector)s}[%(rateInterval)s])))' % {
metric: metric,
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,
},
},

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