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

[v10.0.x] Tempo: Use pipe in TraceQL by default for multi-value variables #70321

Merged
merged 1 commit into from Jun 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions .betterer.results
Expand Up @@ -4890,9 +4890,7 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "11"],
[0, 0, 0, "Unexpected any. Specify a different type.", "12"],
[0, 0, 0, "Unexpected any. Specify a different type.", "13"],
[0, 0, 0, "Unexpected any. Specify a different type.", "14"],
[0, 0, 0, "Unexpected any. Specify a different type.", "15"],
[0, 0, 0, "Unexpected any. Specify a different type.", "16"]
[0, 0, 0, "Unexpected any. Specify a different type.", "14"]
],
"public/app/plugins/datasource/tempo/datasource.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"],
Expand Down
58 changes: 34 additions & 24 deletions public/app/plugins/datasource/tempo/datasource.test.ts
@@ -1,5 +1,6 @@
import { lastValueFrom, Observable, of } from 'rxjs';
import { createFetchResponse } from 'test/helpers/createFetchResponse';
import { initTemplateSrv } from 'test/helpers/initTemplateSrv';

import {
DataFrame,
Expand Down Expand Up @@ -74,26 +75,39 @@ describe('Tempo data source', () => {
refId: 'linked',
expr: '{instance="$interpolationVar"}',
},
query: '$interpolationVar',
query: '$interpolationVarWithPipe',
spanName: '$interpolationVar',
serviceName: '$interpolationVar',
search: '$interpolationVar',
minDuration: '$interpolationVar',
maxDuration: '$interpolationVar',
filters: [],
};
}
let templateSrv: TemplateSrv;
const text = 'interpolationText';
const textWithPipe = 'interpolationTextOne|interpolationTextTwo';

beforeEach(() => {
templateSrv = initTemplateSrv('key', [
{
type: 'custom',
name: 'interpolationVar',
current: { value: [text] },
},
{
type: 'custom',
name: 'interpolationVarWithPipe',
current: { value: [textWithPipe] },
},
]);
});

it('when traceId query for dashboard->explore', async () => {
const templateSrv: any = { replace: jest.fn() };
const ds = new TempoDatasource(defaultSettings, templateSrv);
const text = 'interpolationText';
templateSrv.replace.mockReturnValue(text);

const queries = ds.interpolateVariablesInQueries([getQuery()], {
interpolationVar: { text: text, value: text },
});
expect(templateSrv.replace).toBeCalledTimes(7);
expect(queries[0].linkedQuery?.expr).toBe(text);
expect(queries[0].query).toBe(text);
const queries = ds.interpolateVariablesInQueries([getQuery()], {});
expect(queries[0].linkedQuery?.expr).toBe(`{instance=\"${text}\"}`);
expect(queries[0].query).toBe(textWithPipe);
expect(queries[0].serviceName).toBe(text);
expect(queries[0].spanName).toBe(text);
expect(queries[0].search).toBe(text);
Expand All @@ -102,22 +116,18 @@ describe('Tempo data source', () => {
});

it('when traceId query for template variable', async () => {
const templateSrv: any = { replace: jest.fn() };
const scopedText = 'scopedInterpolationText';
const ds = new TempoDatasource(defaultSettings, templateSrv);
const text = 'interpolationText';
templateSrv.replace.mockReturnValue(text);

const resp = ds.applyTemplateVariables(getQuery(), {
interpolationVar: { text: text, value: text },
interpolationVar: { text: scopedText, value: scopedText },
});
expect(templateSrv.replace).toBeCalledTimes(7);
expect(resp.linkedQuery?.expr).toBe(text);
expect(resp.query).toBe(text);
expect(resp.serviceName).toBe(text);
expect(resp.spanName).toBe(text);
expect(resp.search).toBe(text);
expect(resp.minDuration).toBe(text);
expect(resp.maxDuration).toBe(text);
expect(resp.linkedQuery?.expr).toBe(`{instance=\"${scopedText}\"}`);
expect(resp.query).toBe(textWithPipe);
expect(resp.serviceName).toBe(scopedText);
expect(resp.spanName).toBe(scopedText);
expect(resp.search).toBe(scopedText);
expect(resp.minDuration).toBe(scopedText);
expect(resp.maxDuration).toBe(scopedText);
});
});

Expand Down
4 changes: 2 additions & 2 deletions public/app/plugins/datasource/tempo/datasource.ts
Expand Up @@ -26,7 +26,7 @@ import {
TemplateSrv,
getTemplateSrv,
} from '@grafana/runtime';
import { BarGaugeDisplayMode, TableCellDisplayMode } from '@grafana/schema';
import { BarGaugeDisplayMode, TableCellDisplayMode, VariableFormatID } from '@grafana/schema';
import { NodeGraphOptions } from 'app/core/components/NodeGraphSettings';
import { TraceToLogsOptions } from 'app/core/components/TraceToLogs/TraceToLogsSettings';
import { serializeParams } from 'app/core/utils/fetch';
Expand Down Expand Up @@ -353,7 +353,7 @@ export class TempoDatasource extends DataSourceWithBackend<TempoQuery, TempoJson

return {
...expandedQuery,
query: this.templateSrv.replace(query.query ?? '', scopedVars),
query: this.templateSrv.replace(query.query ?? '', scopedVars, VariableFormatID.Pipe),
serviceName: this.templateSrv.replace(query.serviceName ?? '', scopedVars),
spanName: this.templateSrv.replace(query.spanName ?? '', scopedVars),
search: this.templateSrv.replace(query.search ?? '', scopedVars),
Expand Down