2021-05-10 10:12:19 -05:00
|
|
|
import { DataQuery, DataSourceApi, ExploreQueryFieldProps } from '@grafana/data';
|
2021-02-02 03:17:38 -06:00
|
|
|
import { selectors } from '@grafana/e2e-selectors';
|
2021-05-10 10:12:19 -05:00
|
|
|
import { getDataSourceSrv } from '@grafana/runtime';
|
|
|
|
import { InlineField, InlineFieldRow, InlineLabel, LegacyForms, RadioButtonGroup } from '@grafana/ui';
|
|
|
|
import { TraceToLogsOptions } from 'app/core/components/TraceToLogsSettings';
|
2021-02-02 03:17:38 -06:00
|
|
|
import React from 'react';
|
2021-05-10 10:12:19 -05:00
|
|
|
import { LokiQueryField } from '../loki/components/LokiQueryField';
|
|
|
|
import { TempoDatasource, TempoQuery, TempoQueryType } from './datasource';
|
2020-10-13 12:12:49 -05:00
|
|
|
|
|
|
|
type Props = ExploreQueryFieldProps<TempoDatasource, TempoQuery>;
|
2021-05-10 10:12:19 -05:00
|
|
|
const DEFAULT_QUERY_TYPE: TempoQueryType = 'traceId';
|
|
|
|
interface State {
|
|
|
|
linkedDatasource?: DataSourceApi;
|
|
|
|
}
|
|
|
|
export class TempoQueryField extends React.PureComponent<Props, State> {
|
|
|
|
state = {
|
|
|
|
linkedDatasource: undefined,
|
|
|
|
};
|
2021-06-02 09:37:36 -05:00
|
|
|
|
2021-05-10 10:12:19 -05:00
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
|
|
|
const { datasource } = this.props;
|
|
|
|
// Find query field from linked datasource
|
|
|
|
const tracesToLogsOptions: TraceToLogsOptions = datasource.tracesToLogs || {};
|
|
|
|
const linkedDatasourceUid = tracesToLogsOptions.datasourceUid;
|
|
|
|
if (linkedDatasourceUid) {
|
|
|
|
const dsSrv = getDataSourceSrv();
|
|
|
|
const linkedDatasource = await dsSrv.get(linkedDatasourceUid);
|
|
|
|
this.setState({
|
|
|
|
linkedDatasource,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeLinkedQuery = (value: DataQuery) => {
|
2020-10-13 12:12:49 -05:00
|
|
|
const { query, onChange } = this.props;
|
2021-05-10 10:12:19 -05:00
|
|
|
onChange({
|
|
|
|
...query,
|
2021-06-02 09:37:36 -05:00
|
|
|
linkedQuery: { ...value, refId: 'linked' },
|
2021-05-10 10:12:19 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onRunLinkedQuery = () => {
|
|
|
|
this.props.onRunQuery();
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2021-05-19 09:24:27 -05:00
|
|
|
const { query, onChange } = this.props;
|
2021-05-10 10:12:19 -05:00
|
|
|
const { linkedDatasource } = this.state;
|
|
|
|
|
2020-10-13 12:12:49 -05:00
|
|
|
return (
|
2021-05-10 10:12:19 -05:00
|
|
|
<>
|
|
|
|
<InlineFieldRow>
|
|
|
|
<InlineField label="Query type">
|
|
|
|
<RadioButtonGroup<TempoQueryType>
|
|
|
|
options={[
|
|
|
|
{ value: 'search', label: 'Search' },
|
|
|
|
{ value: 'traceId', label: 'TraceID' },
|
|
|
|
]}
|
|
|
|
value={query.queryType || DEFAULT_QUERY_TYPE}
|
|
|
|
onChange={(v) =>
|
|
|
|
onChange({
|
|
|
|
...query,
|
|
|
|
queryType: v,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
size="md"
|
|
|
|
/>
|
|
|
|
</InlineField>
|
|
|
|
</InlineFieldRow>
|
|
|
|
{query.queryType === 'search' && linkedDatasource && (
|
|
|
|
<>
|
|
|
|
<InlineLabel>
|
|
|
|
Tempo uses {((linkedDatasource as unknown) as DataSourceApi).name} to find traces.
|
|
|
|
</InlineLabel>
|
|
|
|
|
|
|
|
<LokiQueryField
|
|
|
|
datasource={linkedDatasource!}
|
|
|
|
onChange={this.onChangeLinkedQuery}
|
|
|
|
onRunQuery={this.onRunLinkedQuery}
|
2021-06-02 09:37:36 -05:00
|
|
|
query={this.props.query.linkedQuery ?? ({ refId: 'linked' } as any)}
|
2021-05-10 10:12:19 -05:00
|
|
|
history={[]}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{query.queryType === 'search' && !linkedDatasource && (
|
|
|
|
<div className="text-warning">Please set up a Traces-to-logs datasource in the datasource settings.</div>
|
|
|
|
)}
|
|
|
|
{query.queryType !== 'search' && (
|
|
|
|
<LegacyForms.FormField
|
|
|
|
label="Trace ID"
|
|
|
|
labelWidth={4}
|
|
|
|
inputEl={
|
|
|
|
<div className="slate-query-field__wrapper">
|
|
|
|
<div className="slate-query-field" aria-label={selectors.components.QueryField.container}>
|
|
|
|
<input
|
|
|
|
style={{ width: '100%' }}
|
|
|
|
value={query.query || ''}
|
|
|
|
onChange={(e) =>
|
|
|
|
onChange({
|
|
|
|
...query,
|
|
|
|
query: e.currentTarget.value,
|
|
|
|
queryType: 'traceId',
|
|
|
|
linkedQuery: undefined,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
2020-10-13 12:12:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|