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

Fix Trace List with variable #132

Merged
merged 3 commits into from
Aug 1, 2022
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
27 changes: 26 additions & 1 deletion src/components/QueryEditor/QueryEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,32 @@ import { render, screen, fireEvent, act, waitFor } from '@testing-library/react'
import { QueryEditor } from './QueryEditor';
import { Group, Region, XrayJsonData, XrayQuery, XrayQueryType } from '../../types';
import { XrayDataSource } from '../../DataSource';
import { DataSourceInstanceSettings } from '@grafana/data';
import { DataSourceInstanceSettings, ScopedVars, VariableModel } from '@grafana/data';
import * as grafanaRuntime from '@grafana/runtime';

jest.spyOn(grafanaRuntime, 'getTemplateSrv').mockImplementation(() => {
return {
getVariables(): VariableModel[] {
return [];
},
replace(target?: string, scopedVars?: ScopedVars, format?: string | Function): string {
if (!target) {
return '';
}
const vars: Record<string, { value: any }> = {
...scopedVars,
someVar: {
value: '200',
},
};
for (const key of Object.keys(vars)) {
target = target!.replace(`\$${key}`, vars[key].value);
target = target!.replace(`\${${key}}`, vars[key].value);
}
return target!;
},
};
});

const defaultProps = {
onRunQuery: undefined as any,
Expand Down
36 changes: 36 additions & 0 deletions src/components/QueryEditor/QueryEditorForm.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
import { queryTypeOptionToQueryType } from './QueryEditorForm';
import { queryTypeOptions } from './constants';
import { XrayQueryType } from '../../types';
import { ScopedVars, VariableModel } from '@grafana/data';
import * as grafanaRuntime from '@grafana/runtime';

jest.spyOn(grafanaRuntime, 'getTemplateSrv').mockImplementation(() => {
return {
getVariables(): VariableModel[] {
return [];
},
replace(target?: string, scopedVars?: ScopedVars, format?: string | Function): string {
if (!target) {
return '';
}
const vars: Record<string, { value: any }> = {
...scopedVars,
someVar: {
value: '200',
},
};
for (const key of Object.keys(vars)) {
target = target!.replace(`\$${key}`, vars[key].value);
target = target!.replace(`\${${key}}`, vars[key].value);
}
return target!;
},
};
});

describe('QueryEditor', () => {
it('sets the query type to getTrace if query is a traceID', () => {
const queryType = queryTypeOptionToQueryType([queryTypeOptions[0].value], '1-5f048fc1-4f1c9b022d6233dacd96fb84');
expect(queryType).toBe(XrayQueryType.getTrace);
});
it('sets the query type to getTraceSummaries if query is not a traceID', () => {
const queryType = queryTypeOptionToQueryType([queryTypeOptions[0].value], 'foo');
expect(queryType).toBe(XrayQueryType.getTraceSummaries);
});
it('sets the query type to getTrace if query is variable for a traceID', () => {
const queryType = queryTypeOptionToQueryType([queryTypeOptions[0].value], '$variable', {
variable: { text: '1-5f048fc1-4f1c9b022d6233dacd96fb84', value: '1-5f048fc1-4f1c9b022d6233dacd96fb84' },
Copy link
Member

Choose a reason for hiding this comment

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

wouldn't the variable be something more like "traceId" rather than an actual traceId?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The form uses a regex to check for something in this format to determine what query type to use, so the value does have to be shaped like an actual traceId. (the string being passed in as the query is $variable)

});
expect(queryType).toBe(XrayQueryType.getTrace);
});
});
11 changes: 7 additions & 4 deletions src/components/QueryEditor/QueryEditorForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { css } from '@emotion/css';
import { QueryEditorProps } from '@grafana/data';
import { QueryEditorProps, ScopedVars } from '@grafana/data';
import { ButtonCascader, InlineFormLabel, MultiSelect, Segment, stylesFactory, Select } from '@grafana/ui';
import { Group, Region, XrayJsonData, XrayQuery, XrayQueryType } from '../../types';
import { useInitQuery } from './useInitQuery';
Expand All @@ -17,6 +17,7 @@ import {
import { XrayDataSource } from '../../DataSource';
import { QuerySection } from './QuerySection';
import { XrayLinks } from './XrayLinks';
import { getTemplateSrv } from '@grafana/runtime';

function findOptionForQueryType(queryType: XrayQueryType, options: any = queryTypeOptions): QueryTypeOption[] {
for (const option of options) {
Expand Down Expand Up @@ -57,9 +58,10 @@ export function queryTypeToQueryTypeOptions(queryType?: XrayQueryType): QueryTyp
return findOptionForQueryType(queryType);
}

export function queryTypeOptionToQueryType(selected: string[], query: string): XrayQueryType {
export function queryTypeOptionToQueryType(selected: string[], query: string, scopedVars?: ScopedVars): XrayQueryType {
if (selected[0] === traceListOption.value) {
const isTraceIdQuery = /^\d-\w{8}-\w{24}$/.test(query.trim());
const resolvedQuery = getTemplateSrv().replace(query, scopedVars);
const isTraceIdQuery = /^\d-\w{8}-\w{24}$/.test(resolvedQuery.trim());
return isTraceIdQuery ? XrayQueryType.getTrace : XrayQueryType.getTraceSummaries;
} else {
let found: any = undefined;
Expand Down Expand Up @@ -94,6 +96,7 @@ export function QueryEditorForm({
groups,
range,
regions,
data,
}: XrayQueryEditorFormProps) {
const selectedOptions = queryTypeToQueryTypeOptions(query.queryType);
const allRegions = [{ label: 'default', value: 'default', text: 'default' }, ...regions];
Expand Down Expand Up @@ -144,7 +147,7 @@ export function QueryEditorForm({
value={selectedOptions.map((option) => option.value)}
options={queryTypeOptions}
onChange={(value) => {
const newQueryType = queryTypeOptionToQueryType(value, query.query || '');
const newQueryType = queryTypeOptionToQueryType(value, query.query || '', data?.request?.scopedVars);
onChange({
...query,
queryType: newQueryType,
Expand Down