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

rebased from rizkybiz/circonus-irondb-datasource.git b64-fixes-final #36

Closed
wants to merge 10 commits into from
12 changes: 12 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": ["@grafana/eslint-config"],
"parserOptions": {
"tsconfigRootDir": "./"
},
"rules": {
"strict": "off"
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ npm-debug.log
.DS_Store
*.swp
package-lock.json
yarn.lock
coverage
jest*
3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('@grafana/toolkit/src/config/prettier.plugin.config.json'),
};
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

## 0.9.5

* Fix base64 encoding and decoding of tag values in variables
* Made returned tag values respect activity windows

## 0.9.4

* Fix labels for untagged metrics
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"author": "Circonus",
"license": "BSD-3-clause",
"dependencies": {
"memoizee": "^0.4.14"
"memoizee": "^0.4.14",
"tslib": "^2.2.0"
},
"devDependencies": {
"@grafana/data": "^7.2.0",
Expand Down
65 changes: 32 additions & 33 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,23 @@ import memoize from 'memoizee';
import { Memoized } from 'memoizee';
import {
metaInterpolateLabel,
decodeNameAndTags,
isStatsdCounter,
taglessName,
taglessNameAndTags,
decodeTag,
encodeTag,
splitTags,
mergeTags,
TagSet,
decodeTagsInLabel,
SegmentType,
} from './irondb_query';

import {
ArrayVector,
DataFrame,
Field,
FieldType,
formatLabels,
Labels,
MutableField,
ScopedVars,
TIME_SERIES_TIME_FIELD_NAME,
TIME_SERIES_VALUE_FIELD_NAME,
DataSourceApi,
Expand All @@ -39,15 +36,11 @@ import {
AnnotationQueryRequest,
AnnotationEvent,
LoadingState,
TimeSeries,
isTableData,
} from '@grafana/data';
import * as Mustache from 'mustache';

const log = Log('IrondbDatasource');

import { map } from 'rxjs/operators';

const DURATION_UNITS_DEFAULT = 's';
const DURATION_UNITS = {
ms: 1,
Expand Down Expand Up @@ -311,7 +304,7 @@ export default class IrondbDatasource extends DataSourceApi<IrondbQueryInterface
options.method = 'GET';
options.url = options.url + '/alert';
const alertId = this.templateSrv.replace(query.annotation['alertId']);
const alertQuery = this.templateSrv.replace(query.annotation['annotationQueryText']);
const alertQuery = this.templateSrv.replace(query.annotation['annotationQueryText'], null, this.interpolateExpr);
if (alertId !== '') {
options.url = options.url + '/' + alertId;
} else {
Expand Down Expand Up @@ -396,7 +389,7 @@ export default class IrondbDatasource extends DataSourceApi<IrondbQueryInterface
let notes =
rule !== undefined && rule !== null && rule['notes'] !== null && rule['notes'] !== ''
? rule['notes']
: 'Oh no!';
: '';

notes = Mustache.render(notes, tags);

Expand Down Expand Up @@ -469,28 +462,23 @@ export default class IrondbDatasource extends DataSourceApi<IrondbQueryInterface
}

interpolateExpr(value: string | string[] = [], variable: any) {
if (typeof value === 'string') {
value = encodeTag(SegmentType.TagVal, value, false);
} else {
for (let i in value) {
value[i] = encodeTag(SegmentType.TagVal, value[i], false);
}
}
// if no multi or include all do not regexEscape
if (!variable.multi && !variable.includeAll) {
return value;
}

if (typeof value === 'string') {
return value;
} else {
return value.join(',');
}

if (value.length === 1) {
return value[0];
}

let q = '';
for (let i = 0; i < value.length; i++) {
if (i > 0) {
q = q + ',';
}
q = q + value[i];
i = i + 1;
}
return q;
Comment on lines +479 to -493
Copy link
Author

Choose a reason for hiding this comment

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

this change is just replacing a longhand form of value.join(',')

}

// This is used by the Dashboard Admin Variable Setup function
Expand Down Expand Up @@ -528,7 +516,7 @@ export default class IrondbDatasource extends DataSourceApi<IrondbQueryInterface
if (tagCat !== '') {
return this.metricFindTagValsQuery(metricQuery, tagCat, from, to).then((results) => {
return _.map(results.data, (result) => {
return { value: result };
return { value: decodeTag(result) };
});
});
} else {
Expand All @@ -546,7 +534,7 @@ export default class IrondbDatasource extends DataSourceApi<IrondbQueryInterface
return this.irondbType === 'standalone' ? '/' + this.accountId : '';
}

metricTagsQuery(query: string, allowEmptyWildcard = false, from?: number = null, to?: number = null) {
metricTagsQuery(query: string, allowEmptyWildcard = false, from: number = null, to: number = null) {
Copy link
Author

Choose a reason for hiding this comment

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

might not want or need this change

if (query === '' || query === undefined || (!allowEmptyWildcard && query === 'and(__name:*)')) {
return Promise.resolve({ data: [] });
}
Expand Down Expand Up @@ -797,7 +785,8 @@ export default class IrondbDatasource extends DataSourceApi<IrondbQueryInterface
end += interval;
const caqlQuery = this.templateSrv.replace(
irondbOptions['caql']['names'][i].leaf_name,
irondbOptions['scopedVars']
irondbOptions['scopedVars'],
this.interpolateExpr
);
options.url = options.url + '/caql_v1?format=DF4&start=' + start.toFixed(3);
options.url = options.url + '&end=' + end.toFixed(3);
Expand Down Expand Up @@ -829,7 +818,11 @@ export default class IrondbDatasource extends DataSourceApi<IrondbQueryInterface
}
options.method = 'GET';
options.url = options.url + '/alert';
const alertQuery = this.templateSrv.replace(irondbOptions['alert']['names'][i], irondbOptions['scopedVars']);
const alertQuery = this.templateSrv.replace(
irondbOptions['alert']['names'][i],
irondbOptions['scopedVars'],
this.interpolateExpr
);
if (alertQuery.startsWith('alert_id:')) {
options.url = options.url + '/' + alertQuery.split(':')[1];
} else {
Expand Down Expand Up @@ -1328,7 +1321,7 @@ export default class IrondbDatasource extends DataSourceApi<IrondbQueryInterface
}

buildFetchParamsAsync(cleanOptions, target, start, end) {
const rawQuery = this.templateSrv.replace(target['query'], cleanOptions['scopedVars']);
const rawQuery = this.templateSrv.replace(target['query'], cleanOptions['scopedVars'], this.interpolateExpr);

return this.metricTagsQuery(rawQuery, false, start, end)
.then((result) => {
Expand All @@ -1347,13 +1340,13 @@ export default class IrondbDatasource extends DataSourceApi<IrondbQueryInterface
}

buildAlertQueryAsync(cleanOptions, target, start, end) {
let rawQuery = this.templateSrv.replace(target['query'], cleanOptions['scopedVars']);
let rawQuery = this.templateSrv.replace(target['query'], cleanOptions['scopedVars'], this.interpolateExpr);
if (target['alert_id'] !== '') {
rawQuery = 'alert_id:' + this.templateSrv.replace(target['alert_id'], cleanOptions['scopedVars']);
}
cleanOptions['alert']['names'].push(rawQuery);
cleanOptions['alert']['local_filters'].push(
this.templateSrv.replace(target['local_filter'], cleanOptions['scopedVars'])
this.templateSrv.replace(target['local_filter'], cleanOptions['scopedVars'], this.interpolateExpr)
);
cleanOptions['alert']['local_filter_matches'].push(target['local_filter_match']);
cleanOptions['alert']['counts_only'] = target.querytype === 'alert_counts';
Expand Down Expand Up @@ -1637,7 +1630,13 @@ export default class IrondbDatasource extends DataSourceApi<IrondbQueryInterface
vstr = v.toString();
const tsstr = ts.toString();
if (_.isUndefined(lookaside[vstr])) {
lookaside[vstr] = { target: vstr, title: vstr, tags: labels, datapoints: [], _ts: {} };
lookaside[vstr] = {
target: vstr,
title: vstr,
tags: labels,
datapoints: [],
_ts: {},
};
dataFrames.push(lookaside[vstr]);
}
if (_.isUndefined(lookaside[vstr]._ts[tsstr])) {
Expand Down
20 changes: 16 additions & 4 deletions src/irondb_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,10 @@ export default class IrondbQuery {
metricName = metricName.slice(11, -1) || '*';
const tags = metricName.split(',');
metricName = tags.shift();
this.segments.push({ type: SegmentType.MetricName, value: decodeTag(metricName) });
this.segments.push({
type: SegmentType.MetricName,
value: decodeTag(metricName),
});

let first = true;
for (let tag of tags) {
Expand All @@ -349,17 +352,26 @@ export default class IrondbQuery {
tagIndex = 3;
}
if (tagOp) {
this.segments.push({ type: SegmentType.TagOp, value: tagCat.slice(0, tagIndex) });
this.segments.push({
type: SegmentType.TagOp,
value: tagCat.slice(0, tagIndex),
});
tagCat = tagCat.slice(tagIndex);
}
this.segments.push({ type: SegmentType.TagCat, value: decodeTag(tagCat) });
this.segments.push({
type: SegmentType.TagCat,
value: decodeTag(tagCat),
});
this.segments.push({ type: SegmentType.TagPair });
let end = 0;
while (tagVal.endsWith(')')) {
tagVal = tagVal.slice(0, -1);
end++;
}
this.segments.push({ type: SegmentType.TagVal, value: decodeTag(tagVal) });
this.segments.push({
type: SegmentType.TagVal,
value: decodeTag(tagVal),
});
for (let i = 0; i < end; i++) {
this.segments.push({ type: SegmentType.TagPlus });
this.segments.push({ type: SegmentType.TagEnd });
Expand Down
13 changes: 10 additions & 3 deletions src/query_ctrl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash';
import Log from './log';
import IrondbQuery from './irondb_query';
/* eslint-disable-next-line no-duplicate-imports */
// eslint-disable-next-line no-duplicate-imports
import { SegmentType, taglessName, decodeTag, encodeTag } from './irondb_query';
import { QueryCtrl } from 'grafana/app/plugins/sdk';
import appEvents from 'grafana/app/core/app_events';
Expand Down Expand Up @@ -50,7 +50,10 @@ export class IrondbQueryCtrl extends QueryCtrl {
{ value: 'derive', text: 'rate of change (derive)' },
{ value: 'derive_stddev', text: 'rate of change σ (derive_stddev)' },
{ value: 'counter', text: 'rate of positive change (counter)' },
{ value: 'counter_stddev', text: 'rate of positive change σ (counter_stddev)' },
{
value: 'counter_stddev',
text: 'rate of positive change σ (counter_stddev)',
},
];
rollupTypeOptions = [
{ value: 'automatic', text: 'automatic' },
Expand Down Expand Up @@ -520,7 +523,11 @@ export class IrondbQueryCtrl extends QueryCtrl {
segmentIndex + 1,
0,
this.mapSegment({ type: SegmentType.TagOp, value: segment.value }),
this.mapSegment({ type: SegmentType.TagCat, value: 'select tag', fake: true }),
this.mapSegment({
type: SegmentType.TagCat,
value: 'select tag',
fake: true,
}),
this.mapSegment({ type: SegmentType.TagPair }),
this.newSelectTagValSegment(),
this.buildSelectTagPlusSegment(),
Expand Down