Skip to content

Commit 393630c

Browse files
authored
Add enableLokiLogs to commonlib signals and new log signal type (#1399)
* Add enableLokiLogs to commonlib signals * Update common in logs * Update changelog * whitespace fmt
1 parent db094d2 commit 393630c

8 files changed

+403
-15
lines changed

common-lib/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 0.3.0
2+
3+
- [Signal] add new signal `log`.
4+
- [Signal] add enableLokiLogs=true|false to signals init.
5+
- [Signal] `withExprWrappersMixin(offset)` - wrap signal expression into additional function on top of existing wrappers.
6+
7+
18
# 0.2.0
29

310
- [Signal] `withOffset(offset)` - add offset modifier to the expression.

common-lib/common/signal/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Init level:
8282
|rangeFunction| Rate function to use for counter metrics.|rate,irate,delta,idelta,increase|`rate`|`increase`|
8383
|varAdHocEnabled| Attach ad hoc labels to variables generated. |`true`,`false`|`false`|`false`|
8484
|varAdHocLabels| Limit ad hoc to the specific labels |*|`["environment"]`|`[]`|
85+
|enableLokiLogs| Add additional loki datasource to variables generation |`true`,`false`|`true`|`false`|
8586

8687
Signal's level:
8788

common-lib/common/signal/base.libsonnet

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';
1818
): {
1919

2020
local prometheusQuery = g.query.prometheus,
21-
local lokiQuery = g.query.loki,
2221
local this = self,
2322
local hasValueMaps = std.length(this.getValueMappings(this.sourceMaps)) > 0,
2423
local legendCustomTemplate = sourceMaps[0].legendCustomTemplate,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
local g = import '../g.libsonnet';
2+
local utils = import '../utils.libsonnet';
3+
local base = import './base.libsonnet';
4+
local signalUtils = import './utils.libsonnet';
5+
local lokiQuery = g.query.loki;
6+
base {
7+
new(
8+
name,
9+
type,
10+
unit,
11+
nameShort,
12+
description,
13+
aggLevel,
14+
aggFunction,
15+
vars,
16+
datasource,
17+
sourceMaps,
18+
):
19+
base.new(
20+
name,
21+
type,
22+
unit,
23+
nameShort,
24+
description,
25+
aggLevel,
26+
aggFunction,
27+
vars,
28+
datasource,
29+
sourceMaps=sourceMaps,
30+
)
31+
{
32+
common(type)::
33+
// override panel-wide --mixed-- datasource
34+
lokiQuery.withDatasource('${%s}' % datasource)
35+
+ g.panel.logs.panelOptions.withDescription(description),
36+
37+
38+
//Return as grafana panel target(query+legend)
39+
asTarget(name=name):
40+
lokiQuery.new(
41+
'${%s}' % datasource,
42+
self.asPanelExpression(),
43+
)
44+
+ lokiQuery.withRefId(name),
45+
46+
asGauge()::
47+
error 'asGauge() is not supported for log metrics.',
48+
asStat()::
49+
error 'asStat() is not supported for log metrics.',
50+
asTimeSeries()::
51+
error 'asTimeSeries() is not supported for log metrics.',
52+
asTableTarget():
53+
error 'asTableTarget() is not supported for log metrics.',
54+
asTableColumn():
55+
error 'asTableColumn() is not supported for log metrics.',
56+
asTable():
57+
error 'asTable() is not supported for log metrics.',
58+
asOverride():
59+
error 'asOverride() is not supported for log metrics.',
60+
asStatusHistory():
61+
error 'asStatusHistory() is not supported for log metrics.',
62+
withOffset():
63+
error 'withOffset() is not supported for log metrics.',
64+
withTopK():
65+
error 'withTopK() is not supported for log metrics.',
66+
},
67+
68+
}

common-lib/common/signal/signal.libsonnet

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local counter = import './counter.libsonnet';
55
local gauge = import './gauge.libsonnet';
66
local histogram = import './histogram.libsonnet';
77
local info = import './info.libsonnet';
8+
local log = import './log.libsonnet';
89
local raw = import './raw.libsonnet';
910
local stub = import './stub.libsonnet';
1011
{
@@ -31,8 +32,8 @@ local stub = import './stub.libsonnet';
3132
// DEPRECATED. Use unmarshallJsonMulti instead.
3233
unmarshallJson(signalsJson):
3334
self.init(
34-
datasource=std.get(signalsJson, 'datasource', 'datasource'),
35-
datasourceLabel=std.get(signalsJson, 'datasourceLabel', 'Data source'),
35+
datasource=std.get(signalsJson, 'datasource', if std.get(signalsJson, 'enableLokiLogs', false) then 'prometheus_datasource' else 'datasource'),
36+
datasourceLabel=std.get(signalsJson, 'datasourceLabel', if std.get(signalsJson, 'enableLokiLogs', false) then 'Prometheus data source' else 'Data source'),
3637
filteringSelector=[signalsJson.filteringSelector],
3738
groupLabels=signalsJson.groupLabels,
3839
instanceLabels=signalsJson.instanceLabels,
@@ -46,6 +47,7 @@ local stub = import './stub.libsonnet';
4647
rangeFunction=std.get(signalsJson, 'rangeFunction', 'rate'), // rate, irate , delta, increase, idelta...
4748
varAdHocEnabled=std.get(signalsJson, 'varAdHocEnabled', false),
4849
varAdHocLabels=std.get(signalsJson, 'varAdHocLabels', []),
50+
enableLokiLogs=std.get(signalsJson, 'enableLokiLogs', false),
4951
)
5052
+
5153
{
@@ -85,8 +87,8 @@ local stub = import './stub.libsonnet';
8587
);
8688

8789
self.init(
88-
datasource=std.get(signalsJson, 'datasource', 'datasource'),
89-
datasourceLabel=std.get(signalsJson, 'datasourceLabel', 'Data source'),
90+
datasource=std.get(signalsJson, 'datasource', if std.get(signalsJson, 'enableLokiLogs', false) then 'prometheus_datasource' else 'datasource'),
91+
datasourceLabel=std.get(signalsJson, 'datasourceLabel', if std.get(signalsJson, 'enableLokiLogs', false) then 'Prometheus data source' else 'Data source'),
9092
filteringSelector=[signalsJson.filteringSelector],
9193
groupLabels=signalsJson.groupLabels,
9294
instanceLabels=signalsJson.instanceLabels,
@@ -100,6 +102,7 @@ local stub = import './stub.libsonnet';
100102
rangeFunction=std.get(signalsJson, 'rangeFunction', std.get(signalsJson, 'rangeFunction', 'rate')), // rate, irate , delta, increase, idelta...
101103
varAdHocEnabled=std.get(signalsJson, 'varAdHocEnabled', false),
102104
varAdHocLabels=std.get(signalsJson, 'varAdHocLabels', []),
105+
enableLokiLogs=std.get(signalsJson, 'enableLokiLogs', false),
103106
)
104107
+
105108
{
@@ -177,10 +180,12 @@ local stub = import './stub.libsonnet';
177180
rangeFunction='rate',
178181
varAdHocEnabled=false,
179182
varAdHocLabels=[],
183+
enableLokiLogs=false,
180184
): self {
181185

182186
local this = self,
183-
datasource:: datasource,
187+
datasource:: if enableLokiLogs && datasource == 'datasource' then 'prometheus_datasource' else datasource,
188+
datasourceLabel:: if enableLokiLogs && datasourceLabel == 'Data source' then 'Prometheus data source' else datasourceLabel,
184189
aggLevel:: aggLevel,
185190
aggKeepLabels:: aggKeepLabels,
186191
aggFunction:: aggFunction,
@@ -191,10 +196,11 @@ local stub = import './stub.libsonnet';
191196
groupLabels,
192197
instanceLabels,
193198
varMetric=varMetric,
194-
prometheusDatasourceName=datasource,
195-
prometheusDatasourceLabel=datasourceLabel,
199+
prometheusDatasourceName=this.datasource,
200+
prometheusDatasourceLabel=this.datasourceLabel,
196201
adHocEnabled=varAdHocEnabled,
197202
adHocLabels=varAdHocLabels,
203+
enableLokiLogs=enableLokiLogs,
198204
),
199205
// vars are used in templating(legend+expressions)
200206
templatingVariables: {
@@ -216,7 +222,7 @@ local stub = import './stub.libsonnet';
216222
grafanaVariables.datasources[type],
217223

218224
//name: metric simple name
219-
//type: counter, gauge, histogram, // TODO: info metric, status_map metric....
225+
//type: counter, gauge, histogram, raw, info, log
220226
//unit: simple unit
221227
//description: metric description
222228
//exprTemplate: expression template
@@ -246,7 +252,7 @@ local stub = import './stub.libsonnet';
246252
std.prune(
247253
{
248254
checks: [
249-
if (type != 'gauge' && type != 'histogram' && type != 'counter' && type != 'raw' && type != 'info' && type != 'stub') then error "type must be one of 'gauge','histogram','counter','raw','info' Got %s for %s" % [type, name],
255+
if (type != 'gauge' && type != 'histogram' && type != 'counter' && type != 'raw' && type != 'info' && type != 'stub' && type != 'log') then error "type must be one of 'gauge','histogram','counter','raw','info','log'. Got %s for %s" % [type, name],
250256
if (aggLevel != 'none' && aggLevel != 'instance' && aggLevel != 'group') then error "aggLevel must be one of 'group','instance' or 'none'",
251257
],
252258
}
@@ -260,7 +266,7 @@ local stub = import './stub.libsonnet';
260266
description=description,
261267
aggLevel=aggLevel,
262268
aggFunction=aggFunction,
263-
datasource=datasource,
269+
datasource=this.datasource,
264270
vars=this.templatingVariables,
265271
sourceMaps=sourceMaps,
266272
)
@@ -273,7 +279,7 @@ local stub = import './stub.libsonnet';
273279
description=description,
274280
aggLevel=aggLevel,
275281
aggFunction=aggFunction,
276-
datasource=datasource,
282+
datasource=this.datasource,
277283
vars=this.templatingVariables,
278284
sourceMaps=sourceMaps,
279285
)
@@ -286,7 +292,7 @@ local stub = import './stub.libsonnet';
286292
description=description,
287293
aggLevel=aggLevel,
288294
aggFunction=aggFunction,
289-
datasource=datasource,
295+
datasource=this.datasource,
290296
vars=this.templatingVariables,
291297
sourceMaps=sourceMaps,
292298
)
@@ -299,7 +305,20 @@ local stub = import './stub.libsonnet';
299305
description=description,
300306
aggLevel=aggLevel,
301307
aggFunction=aggFunction,
302-
datasource=datasource,
308+
datasource=this.datasource,
309+
vars=this.templatingVariables,
310+
sourceMaps=sourceMaps,
311+
)
312+
else if type == 'log' then
313+
log.new(
314+
name=name,
315+
type=type,
316+
unit='none',
317+
nameShort=nameShort,
318+
description=description,
319+
aggLevel=aggLevel,
320+
aggFunction=aggFunction,
321+
datasource='loki_datasource',
303322
vars=this.templatingVariables,
304323
sourceMaps=sourceMaps,
305324
)
@@ -311,7 +330,7 @@ local stub = import './stub.libsonnet';
311330
description=description,
312331
aggLevel=aggLevel,
313332
aggFunction=aggFunction,
314-
datasource=datasource,
333+
datasource=this.datasource,
315334
vars=this.templatingVariables,
316335
sourceMaps=sourceMaps,
317336
)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
local signal = import './signal.libsonnet';
2+
local test = import 'jsonnetunit/test.libsonnet';
3+
4+
local m1 = signal.init(
5+
filteringSelector=['job="abc"'],
6+
interval='5m',
7+
aggLevel='instance',
8+
aggFunction='max',
9+
enableLokiLogs=true,
10+
).addSignal(
11+
name='API server requests',
12+
nameShort='requests',
13+
type='counter',
14+
unit='requests',
15+
description='API server calls.',
16+
sourceMaps=[
17+
{
18+
expr: 'apiserver_request_total{%(queriesSelector)s}',
19+
rangeFunction: 'rate',
20+
aggKeepLabels: [],
21+
legendCustomTemplate: null,
22+
infoLabel: null,
23+
},
24+
],
25+
)
26+
;
27+
28+
{
29+
30+
asTarget: {
31+
local raw = m1.asTarget(),
32+
testResult: test.suite({
33+
testLegend: {
34+
actual: raw.legendFormat,
35+
expect: '{{instance}}: requests',
36+
},
37+
testExpression: {
38+
actual: raw.expr,
39+
expect: 'max by (job,instance) (\n rate(apiserver_request_total{job="abc",job=~"$job",instance=~"$instance"}[5m])\n)',
40+
},
41+
}),
42+
},
43+
asTimeSeries:
44+
{
45+
local raw = m1.asTimeSeries(),
46+
testResult: test.suite({
47+
testTStitle: {
48+
actual: raw.title,
49+
expect: 'API server requests',
50+
},
51+
testUnit: {
52+
actual: raw.fieldConfig.overrides[0].properties[0].value,
53+
expect: 'rps',
54+
},
55+
testTStype: {
56+
actual: raw.type,
57+
expect: 'timeseries',
58+
},
59+
testTSversion: {
60+
actual: raw.pluginVersion,
61+
expect: 'v11.0.0',
62+
},
63+
testTSUid: {
64+
actual: raw.datasource,
65+
expect: {
66+
uid: '${prometheus_datasource}',
67+
type: 'prometheus',
68+
},
69+
},
70+
}),
71+
},
72+
73+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
local signal = import './signal.libsonnet';
2+
local test = import 'jsonnetunit/test.libsonnet';
3+
4+
local m1 = signal.init(
5+
filteringSelector=['foo="bar"'],
6+
interval='10m',
7+
aggLevel='group',
8+
aggFunction='avg',
9+
).addSignal(
10+
name='Log query',
11+
nameShort='Log',
12+
type='log',
13+
unit='none',
14+
description='Log query.',
15+
sourceMaps=[
16+
{
17+
expr: '{%(queriesSelector)s} | json',
18+
rangeFunction: 'rate',
19+
aggKeepLabels: [],
20+
legendCustomTemplate: null,
21+
infoLabel: null,
22+
},
23+
],
24+
25+
);
26+
27+
{
28+
asTarget: {
29+
local raw = m1.asTarget(),
30+
testResult: test.suite({
31+
testExpression: {
32+
actual: raw.expr,
33+
expect: '{foo="bar",job=~"$job",instance=~"$instance"} | json',
34+
},
35+
testTSUid: {
36+
actual: raw.datasource,
37+
expect: {
38+
uid: '${loki_datasource}',
39+
type: 'loki',
40+
},
41+
},
42+
}),
43+
},
44+
}

0 commit comments

Comments
 (0)