import { DataQuery, DataSourceApi, ExploreQueryFieldProps } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { getDataSourceSrv } from '@grafana/runtime'; import { InlineField, InlineFieldRow, InlineLabel, LegacyForms, RadioButtonGroup } from '@grafana/ui'; import { TraceToLogsOptions } from 'app/core/components/TraceToLogsSettings'; import React from 'react'; import { LokiQueryField } from '../loki/components/LokiQueryField'; import { TempoDatasource, TempoQuery, TempoQueryType } from './datasource'; type Props = ExploreQueryFieldProps; const DEFAULT_QUERY_TYPE: TempoQueryType = 'traceId'; interface State { linkedDatasource?: DataSourceApi; } export class TempoQueryField extends React.PureComponent { state = { linkedDatasource: undefined, }; linkedQuery: DataQuery; constructor(props: Props) { super(props); this.linkedQuery = { refId: 'linked' }; } 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) => { const { query, onChange } = this.props; this.linkedQuery = value; onChange({ ...query, linkedQuery: this.linkedQuery, }); }; onRunLinkedQuery = () => { this.props.onRunQuery(); }; render() { const { query, onChange } = this.props; const { linkedDatasource } = this.state; return ( <> options={[ { value: 'search', label: 'Search' }, { value: 'traceId', label: 'TraceID' }, ]} value={query.queryType || DEFAULT_QUERY_TYPE} onChange={(v) => onChange({ ...query, queryType: v, }) } size="md" /> {query.queryType === 'search' && linkedDatasource && ( <> Tempo uses {((linkedDatasource as unknown) as DataSourceApi).name} to find traces. )} {query.queryType === 'search' && !linkedDatasource && (
Please set up a Traces-to-logs datasource in the datasource settings.
)} {query.queryType !== 'search' && (
onChange({ ...query, query: e.currentTarget.value, queryType: 'traceId', linkedQuery: undefined, }) } />
} /> )} ); } }