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

Commonlib table support #1290

Merged
merged 7 commits into from
Jul 24, 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
2 changes: 1 addition & 1 deletion common-lib/common/g.libsonnet
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import 'github.com/grafana/grafonnet/gen/grafonnet-v10.0.0/main.libsonnet'
import 'github.com/grafana/grafonnet/gen/grafonnet-v11.0.0/main.libsonnet'
3 changes: 2 additions & 1 deletion common-lib/common/signal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ This shifts focus of dashboard building from panels to actual signals we want to
Workflow to generate dashboards would be as follows:

- Define key indicators(signals) your want to observe.
- Use built-in functions to render each signal as a panel (i.e. `signal.asTimeSeries()`, `signal.asStat()`, `signal.asGauge()`, `signal.asStatusHistory()`)
- Use built-in functions to render each signal as a panel (i.e. `signal.asTimeSeries()`, `signal.asStat()`, `signal.asGauge()`, `signal.asStatusHistory()`, `signal.asTable(format='table|time_series'))`, `signal.asTableColumn()`)
- If you want to combine multiple signals inside a single panel, render signal not as [Grafana panel](https://grafana.github.io/grafonnet/API/panel/index.html), but as a [Target](https://grafana.github.io/grafonnet/API/panel/timeSeries/index.html#fn-queryoptionswithtargets), which is attachable to existing panel. (`signal.asTarget()`)
- If you need to put multiple signals inside single panel use `signal.asPanelMixin()` function. It would add `Target` And overrides relevant to it (units, value mappings...)
- If you need to put multiple columns inside single table use `signal.asTableColumn(format='table|time_series')` function. It would add `Target` And overrides relevant to it (units, value mappings...).
- Variables required for signals are also generated and can be attached to any dashboard
- Bonus: use stylize() functions from commonlib/panels to apply common styles to signals.

Expand Down
105 changes: 88 additions & 17 deletions common-lib/common/signal/base.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local signalUtils = import './utils.libsonnet';

{
new(
name,
signalName,
type,
unit,
description,
Expand Down Expand Up @@ -61,31 +61,60 @@ local signalUtils = import './utils.libsonnet';

unit:: signalUtils.generateUnits(type, unit, rangeFunction),
//Return as grafana panel target(query+legend)
asTarget()::
asTarget(name=signalName)::
prometheusQuery.new(
'${%s}' % datasource,
self.asPanelExpression(),
)
+ prometheusQuery.withRefId(name)
+ prometheusQuery.withLegendFormat(signalUtils.wrapLegend(name, aggLevel, legendCustomTemplate) % this.vars),
+ prometheusQuery.withLegendFormat(signalUtils.wrapLegend(name, aggLevel, legendCustomTemplate) % this.vars)
+ prometheusQuery.withFormat('time_series')
+ prometheusQuery.withInstant(false),

//Useful to compose table with instant values
asTableTarget()::
self.asTarget()
+ prometheusQuery.withFormat('table')
+ prometheusQuery.withInstant(true),

//Return as grafana panel mixin target(query+legend) + overrides(like units)
asPanelMixin()::
asPanelMixin(override='byQuery')::
g.panel.timeSeries.queryOptions.withTargetsMixin(self.asTarget())
+ g.panel.timeSeries.standardOptions.withOverridesMixin(
+ self.asOverride(override=override),

//Return table target (instant=true, format=table) + overrides
asTableColumn(override='byName', format='table')::
g.panel.table.queryOptions.withTargetsMixin(
if format == 'table' then self.asTableTarget()
else if format == 'time_series' then self.asTarget()
else error 'Unknown format, must be "table" or "time_series"'
)
+ self.asOverride(override=override),

asOverride(name=signalName, override='byQuery')::
g.panel.timeSeries.standardOptions.withOverridesMixin(
[
g.panel.timeSeries.fieldOverride.byQuery.new(name)
+ g.panel.timeSeries.fieldOverride.byQuery.withPropertiesFromOptions(
g.panel.timeSeries.standardOptions.withUnit(self.unit)
+ g.panel.timeSeries.standardOptions.withMappings(valueMapping)
),
if override == 'byQuery' then
g.panel.timeSeries.fieldOverride.byQuery.new(name)
+ g.panel.timeSeries.fieldOverride.byQuery.withPropertiesFromOptions(
g.panel.timeSeries.standardOptions.withUnit(self.unit)
+ g.panel.timeSeries.standardOptions.withMappings(valueMapping)
)
else if override == 'byName' then
g.panel.timeSeries.fieldOverride.byName.new(name)
+ g.panel.timeSeries.fieldOverride.byName.withPropertiesFromOptions(
g.panel.timeSeries.standardOptions.withUnit(self.unit)
+ g.panel.timeSeries.standardOptions.withMappings(valueMapping)
)
else error 'Unknown override type, only "byName", "byQuery" are supported.',
],
),

//Return query
asPanelExpression()::
signalUtils.wrapExpr(type, exprBase, exprWrappers=exprWrappers, aggLevel=aggLevel, rangeFunction=rangeFunction).applyFunctions()
% this.vars,

//Return query, usable in alerts/recording rules.
asRuleExpression()::
//override aggLevel to 'none', to avoid loosing labels in alerts due to by() clause:
Expand All @@ -102,28 +131,70 @@ local signalUtils = import './utils.libsonnet';
// override panel-wide --mixed-- datasource
prometheusQuery.withDatasource('${%s}' % datasource)
+ g.panel.timeSeries.panelOptions.withDescription(description)
+ g.panel.timeSeries.standardOptions.withUnit(self.unit)
+ g.panel.timeSeries.standardOptions.withMappings(valueMapping)
+ g.panel.timeSeries.queryOptions.withTargets(
self.asTarget()
),
)
+ self.asOverride(),

//Return as timeSeriesPanel
asTimeSeries()::
asTimeSeries(name=signalName)::
g.panel.timeSeries.new(name)
+ self.common,

//Return as statPanel
asStat()::
asStat(name=signalName)::
g.panel.stat.new(name)
+ self.common,
// Return as table
// Table format: all targets must have format=table, instant=true, and matching labels set.
// Timeseries format: all targets must have format=timeseries, instant=false, and matching labels set.
// Useful to show Table trends.
asTable(name=signalName, format='table')::
prometheusQuery.withDatasource('${%s}' % datasource)
+ g.panel.table.new(name)
+
if format == 'table' then
self.asTableColumn()
+ g.panel.table.queryOptions.withTransformations(
[
g.panel.table.queryOptions.transformation.withId('merge'),
g.panel.table.queryOptions.transformation.withId('renameByRegex')
+ g.panel.table.queryOptions.transformation.withOptions({
regex: 'Value #(.*)',
renamePattern: '$1',
}),
g.panel.table.queryOptions.transformation.withId('filterFieldsByName')
+ g.panel.table.queryOptions.transformation.withOptions(
{
include: {
pattern: '^(?!Time).*$',
},
}
),
]
)
else if format == 'time_series' then
self.asPanelMixin(override='byName')
+ g.panel.table.queryOptions.withTransformations(
[
g.panel.table.queryOptions.transformation.withId('timeSeriesTable'),
g.panel.table.queryOptions.transformation.withId('merge'),
g.panel.table.queryOptions.transformation.withId('renameByRegex')
+ g.panel.table.queryOptions.transformation.withOptions({
regex: 'Trend #(.*)',
renamePattern: '$1',
}),
]
)
else error 'Table format must be "time_series" or "table"',


//Return as gauge panel
asGauge()::
asGauge(name=signalName)::
g.panel.gauge.new(name)
+ self.common,
//Return as statusHistory
asStatusHistory()::
asStatusHistory(name=signalName)::
g.panel.statusHistory.new(name)
+ self.common
// limit number of DPs
Expand Down
11 changes: 8 additions & 3 deletions common-lib/common/signal/stub.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local signalUtils = import './utils.libsonnet';
): {

asTarget():: {},
asTableTarget:: {},

//Return as grafana panel mixin target(query+legend) + overrides(like units)
asPanelMixin():: {},
Expand All @@ -17,19 +18,23 @@ local signalUtils = import './utils.libsonnet';
//Return query, usable in alerts/recording rules. No aggregation is applied.
asRuleExpression():: {},
//Return as timeSeriesPanel
asTimeSeries()::
asTimeSeries(name)::
g.panel.text.new('')
+ g.panel.text.panelOptions.withTransparent(true)
+ g.panel.text.panelOptions.withDescription(name + ': Signal not found.')
+ g.panel.text.options.withContent(''),

//Return as statPanel
asStat()::
asStat(name)::
self.asTimeSeries(),

asTable(name, format)::
self.asTimeSeries(),

//Return as timeSeriesPanel
asGauge()::
asGauge(name)::
self.asTimeSeries(),
asTableColumn(override, format):: {},
},

}
4 changes: 2 additions & 2 deletions common-lib/common/signal/test_counter.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ local m1 = signal.init(
expect: 'API server requests',
},
testUnit: {
actual: m1.asTimeSeries().fieldConfig.defaults.unit,
actual: m1.asTimeSeries().fieldConfig.overrides[0].properties[1].value,
Copy link
Member

Choose a reason for hiding this comment

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

minor nit here as overrides[0].properties[1].value is not quite as readable as defaults.unit.

Would be good to have a comment for future maintainers explaining just what field is being looked at and why it's done in this manner

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a global place to to put units into any panel (fieldConfig.defaults.unit). But it will be used by multiple signals you can attach to a panel.
So in commonlib I decided to move units into overrides in order to make sure that units setting only applies to the specific signal. So that is why so long path in unit tests.

expect: 'rps',
},
testTStype: {
Expand All @@ -47,7 +47,7 @@ local m1 = signal.init(
},
testTSversion: {
actual: m1.asTimeSeries().pluginVersion,
expect: 'v10.0.0',
expect: 'v11.0.0',
},
testTSUid: {
actual: m1.asTimeSeries().datasource,
Expand Down
4 changes: 2 additions & 2 deletions common-lib/common/signal/test_gauge.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ local gauge1 = signal.init(
expect: 'Up metric',
},
testUnit: {
actual: gauge1.asTimeSeries().fieldConfig.defaults.unit,
actual: gauge1.asTimeSeries().fieldConfig.overrides[0].properties[1].value,
expect: 'short',
},
testTStype: {
Expand All @@ -45,7 +45,7 @@ local gauge1 = signal.init(
},
testTSversion: {
actual: gauge1.asTimeSeries().pluginVersion,
expect: 'v10.0.0',
expect: 'v11.0.0',
},
testTSUid: {
actual: gauge1.asTimeSeries().datasource,
Expand Down
4 changes: 2 additions & 2 deletions common-lib/common/signal/test_histogram.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ local m1 = signal.init(
expect: 'API server duration',
},
testUnit: {
actual: m1.asTimeSeries().fieldConfig.defaults.unit,
actual: m1.asTimeSeries().fieldConfig.overrides[0].properties[1].value,
expect: 'seconds',
},
testTStype: {
Expand All @@ -46,7 +46,7 @@ local m1 = signal.init(
},
testTSversion: {
actual: m1.asTimeSeries().pluginVersion,
expect: 'v10.0.0',
expect: 'v11.0.0',
},
testTSUid: {
actual: m1.asTimeSeries().datasource,
Expand Down
2 changes: 1 addition & 1 deletion common-lib/common/signal/test_info.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ local m1 = signal.init(
},
testTSversion: {
actual: m1.asStat().pluginVersion,
expect: 'v10.0.0',
expect: 'v11.0.0',
},
testTSUid: {
actual: m1.asStat().datasource,
Expand Down
2 changes: 1 addition & 1 deletion common-lib/common/signal/test_marshall_json.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ local signals = signal.unmarshallJson(jsonSignals);
},
testVersion: {
actual: panel.pluginVersion,
expect: 'v10.0.0',
expect: 'v11.0.0',
},
testUid: {
actual: panel.datasource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ local signals = signal.unmarshallJsonMulti(jsonSignals, 'otel');
},
testVersion: {
actual: panel.pluginVersion,
expect: 'v10.0.0',
expect: 'v11.0.0',
},
testUid: {
actual: panel.datasource,
Expand Down
83 changes: 83 additions & 0 deletions common-lib/common/signal/test_table.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
local signal = import './signal.libsonnet';
local test = import 'jsonnetunit/test.libsonnet';

local m1 = signal.init(
filteringSelector=['job="abc"'],
interval='5m',
aggLevel='instance',
aggFunction='max',
).addSignal(
name='API server requests',
type='counter',
unit='requests',
description='API server calls.',
expr='apiserver_request_total{%(queriesSelector)s}',
);

{

asTableColumn: {
raw:: m1.asTableColumn(),
testResult: test.suite({
testFormat: {
actual: m1.asTableColumn().targets[0].format,
expect: 'table',
},
testExpression: {
actual: m1.asTableColumn().targets[0].expr,
expect: 'max by (job,instance) (\n rate(apiserver_request_total{job="abc",job=~"$job",instance=~"$instance"}[5m])\n)',
},
testInstant: {
actual: m1.asTableColumn().targets[0].instant,
expect: true,
},
}),
},
asTableColumnWithTimeSeries: {
raw:: m1.asTableColumn(format='time_series'),
testResult: test.suite({
testFormat: {
actual: m1.asTableColumn(format='time_series').targets[0].format,
expect: 'time_series',
},
testExpression: {
actual: m1.asTableColumn(format='time_series').targets[0].expr,
expect: 'max by (job,instance) (\n rate(apiserver_request_total{job="abc",job=~"$job",instance=~"$instance"}[5m])\n)',
},
testInstant: {
actual: m1.asTableColumn(format='time_series').targets[0].instant,
expect: false,
},
}),
},
asTable:
{
raw:: m1.asTable(),
testResult: test.suite({
testTStitle: {
actual: m1.asTable().title,
expect: 'API server requests',
},
testUnit: {
actual: m1.asTable().fieldConfig.overrides[0].properties[1].value,
expect: 'rps',
},
testTStype: {
actual: m1.asTable().type,
expect: 'table',
},
testTSversion: {
actual: m1.asTimeSeries().pluginVersion,
expect: 'v11.0.0',
},
testTSUid: {
actual: m1.asTimeSeries().datasource,
expect: {
uid: '${datasource}',
type: 'prometheus',
},
},
}),
},

}
2 changes: 1 addition & 1 deletion common-lib/jsonnetfile.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"source": {
"git": {
"remote": "https://github.com/grafana/grafonnet.git",
"subdir": "gen/grafonnet-v10.0.0"
"subdir": "gen/grafonnet-v11.0.0"
}
},
"version": "main"
Expand Down
Loading