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

add support for variables #7

Merged
merged 2 commits into from
Mar 7, 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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Changelog

## tip

* FEATURE: add support for variables in the query. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/5).
51 changes: 51 additions & 0 deletions src/__mocks__/datasource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { DataSourceInstanceSettings, PluginType } from '@grafana/data';
import { TemplateSrv } from '@grafana/runtime';

import { VictoriaLogsDatasource } from '../datasource';
import { Options } from '../types';

const defaultTemplateSrvMock = {
replace: (input: string) => input,
};

export function createDatasource(
templateSrvMock: Partial<TemplateSrv> = defaultTemplateSrvMock,
settings: Partial<DataSourceInstanceSettings<Options>> = {}
): VictoriaLogsDatasource {
const customSettings: DataSourceInstanceSettings<Options> = {
url: 'myloggingurl',
id: 0,
uid: '',
type: '',
name: '',
meta: {
id: 'id',
name: 'name',
type: PluginType.datasource,
module: '',
baseUrl: '',
info: {
author: {
name: 'Test',
},
description: '',
links: [],
logos: {
large: '',
small: '',
},
screenshots: [],
updated: '',
version: '',
},
},
readOnly: false,
jsonData: {
maxLines: '20',
},
access: 'direct',
...settings,
};

return new VictoriaLogsDatasource(customSettings, templateSrvMock as TemplateSrv);
}
92 changes: 92 additions & 0 deletions src/datasource.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { TemplateSrv } from "@grafana/runtime";

import { createDatasource } from "./__mocks__/datasource";
import { VictoriaLogsDatasource } from "./datasource";

const replaceMock = jest.fn().mockImplementation((a: string) => a);

const templateSrvStub = {
replace: replaceMock,
} as unknown as TemplateSrv;

beforeEach(() => {
jest.clearAllMocks();
});

describe('VictoriaLogsDatasource', () => {
let ds: VictoriaLogsDatasource;

beforeEach(() => {
ds = createDatasource(templateSrvStub);
});

describe('When interpolating variables', () => {
let customVariable: any;
beforeEach(() => {
customVariable = {
id: '',
global: false,
multi: false,
includeAll: false,
allValue: null,
query: '',
options: [],
current: {},
name: '',
type: 'custom',
label: null,
skipUrlSync: false,
index: -1,
initLock: null,
};
});

it('should only escape single quotes for string value', () => {
expect(ds.interpolateQueryExpr("abc'$^*{}[]+?.()|", customVariable)).toEqual("abc\\\\'$^*{}[]+?.()|");
});

it('should return a number for number value', () => {
expect(ds.interpolateQueryExpr(1000 as any, customVariable)).toEqual(1000);
});
});

describe('applyTemplateVariables', () => {
it('should call replace function for expr', () => {
const expr = '_stream:{app!~"$name"}'
const variables = { name: 'bar' };
replaceMock.mockImplementation(() => `_stream:{app!~"${variables.name}"}`);
const interpolatedQuery = ds.applyTemplateVariables({ expr, refId: 'A' }, {});
expect(interpolatedQuery.expr).toBe(`_stream:{app!~"bar"}`);
});

it('should replace variables in a simple string query', () => {
const expr = 'error';
replaceMock.mockImplementation((input) => input);
const interpolatedQuery = ds.applyTemplateVariables({ expr, refId: 'A' }, {});
expect(interpolatedQuery.expr).toBe('error');
});

it('should replace variables with logical operators', () => {
const expr = '$severity AND _time:$time';
const variables = { severity: 'error', time: '5m' };
replaceMock.mockImplementation(() => `${variables.severity} AND _time:${variables.time}`);
const interpolatedQuery = ds.applyTemplateVariables({ expr, refId: 'A' }, {});
expect(interpolatedQuery.expr).toBe('error AND _time:5m');
});

it('should replace variables with exact match function', () => {
const expr = 'log.level:exact("$level")';
const variables = { level: 'error' };
replaceMock.mockImplementation(() => `log.level:exact("${variables.level}")`);
const interpolatedQuery = ds.applyTemplateVariables({ expr, refId: 'A' }, {});
expect(interpolatedQuery.expr).toBe('log.level:exact("error")');
});

it('should not replace a variable if it is not declared', () => {
const expr = '_stream:{app!~"$undeclaredVariable"}';
replaceMock.mockImplementation((input) => input);
const interpolatedQuery = ds.applyTemplateVariables({ expr, refId: 'A' }, {});
expect(interpolatedQuery.expr).toBe(expr);
});
});
});
82 changes: 79 additions & 3 deletions src/datasource.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { map as lodashMap } from 'lodash';
import { Observable } from "rxjs";
import { map } from 'rxjs/operators';

import { DataQueryRequest, DataQueryResponse, DataSourceInstanceSettings } from '@grafana/data';
import { DataSourceWithBackend } from '@grafana/runtime';
import {
AdHocVariableFilter,
DataQueryRequest,
DataQueryResponse,
DataSourceInstanceSettings,
ScopedVars
} from '@grafana/data';
import { DataSourceWithBackend, getTemplateSrv, TemplateSrv } from '@grafana/runtime';

import { transformBackendResult } from "./backendResultTransformer";
import QueryEditor from "./components/QueryEditor/QueryEditor";
import { escapeLabelValueInSelector } from "./languageUtils";
import { escapeLabelValueInSelector, isRegexSelector } from "./languageUtils";
import { addLabelToQuery, queryHasFilter, removeLabelFromQuery } from "./modifyQuery";
import { replaceVariables, returnVariables } from "./parsingUtils";
import { Query, Options, ToggleFilterAction, QueryFilterOptions, FilterActionType } from './types';

export class VictoriaLogsDatasource
Expand All @@ -16,6 +24,7 @@ export class VictoriaLogsDatasource

constructor(
instanceSettings: DataSourceInstanceSettings<Options>,
private readonly templateSrv: TemplateSrv = getTemplateSrv()
) {
super(instanceSettings);

Expand Down Expand Up @@ -82,4 +91,71 @@ export class VictoriaLogsDatasource
let expression = query.expr ?? '';
return queryHasFilter(expression, filter.key, filter.value);
}

applyTemplateVariables(target: Query, scopedVars: ScopedVars, adhocFilters?: AdHocVariableFilter[]): Query {
const { __auto, __interval, __interval_ms, __range, __range_s, __range_ms, ...rest } = scopedVars || {};
const exprWithAdHoc = this.addAdHocFilters(target.expr, adhocFilters);

const variables = {
...rest,
__interval: {
value: '$__interval',
},
__interval_ms: {
value: '$__interval_ms',
},
};
return {
...target,
legendFormat: this.templateSrv.replace(target.legendFormat, rest),
expr: this.templateSrv.replace(exprWithAdHoc, variables, this.interpolateQueryExpr),
};
}

addAdHocFilters(queryExpr: string, adhocFilters?: AdHocVariableFilter[]) {
if (!adhocFilters) {
return queryExpr;
}

let expr = replaceVariables(queryExpr);

expr = adhocFilters.reduce((acc: string, filter: { key: string; operator: string; value: string }) => {
const { key, operator } = filter;
let { value } = filter;
if (isRegexSelector(operator)) {
value = regularEscape(value);
} else {
value = escapeLabelValueInSelector(value, operator);
}
return addLabelToQuery(acc, key, operator, value);
}, expr);

return returnVariables(expr);
}

interpolateQueryExpr(value: any, variable: any) {
if (!variable.multi && !variable.includeAll) {
return regularEscape(value);
}

if (typeof value === 'string') {
return specialRegexEscape(value);
}

return lodashMap(value, specialRegexEscape).join('|');
}
}

export function regularEscape(value: any) {
if (typeof value === 'string') {
return value.replace(/'/g, "\\\\'");
}
return value;
}

export function specialRegexEscape(value: any) {
if (typeof value === 'string') {
return regularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()|]/g, '\\\\$&'));
}
return value;
}
33 changes: 33 additions & 0 deletions src/parsingUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const varTypeFunc = [
(v: string) => `\$${v}`,
(v: string, f?: string) => `[[${v}${f ? `:${f}` : ''}]]`,
(v: string, f?: string) => `\$\{${v}${f ? `:${f}` : ''}\}`,
];

export const variableRegex = /\$(\w+)|\[\[([\s\S]+?)(?::(\w+))?]]|\${(\w+)(?:\.([^:^}]+))?(?::([^}]+))?}/g;

export function returnVariables(expr: string) {
const replacer = (match: string, type: any, v: any, f: any) => varTypeFunc[parseInt(type, 10)](v, f)
return expr.replace(/__V_(\d)__(.+?)__V__(?:__F__(\w+)__F__)?/g, replacer);
}


export function replaceVariables(expr: string) {
return expr.replace(variableRegex, (match, var1, var2, fmt2, var3, fieldPath, fmt3) => {
const fmt = fmt2 || fmt3;
let variable = var1;
let varType = '0';

if (var2) {
variable = var2;
varType = '1';
}

if (var3) {
variable = var3;
varType = '2';
}

return `__V_${varType}__` + variable + '__V__' + (fmt ? '__F__' + fmt + '__F__' : '');
});
}
Loading