import { css } from '@emotion/css'; import React from 'react'; import useAsync from 'react-use/lib/useAsync'; import { QueryEditorProps, SelectableValue } from '@grafana/data'; import { config, reportInteraction } from '@grafana/runtime'; import { Button, FileDropzone, HorizontalGroup, InlineField, InlineFieldRow, InlineLabel, Modal, RadioButtonGroup, Themeable2, withTheme2, } from '@grafana/ui'; import { LokiQueryField } from '../../loki/components/LokiQueryField'; import { LokiDatasource } from '../../loki/datasource'; import { LokiQuery } from '../../loki/types'; import TraceQLSearch from '../SearchTraceQLEditor/TraceQLSearch'; import { TempoQueryType } from '../dataquery.gen'; import { TempoDatasource } from '../datasource'; import { QueryEditor } from '../traceql/QueryEditor'; import { TempoQuery } from '../types'; import NativeSearch from './NativeSearch'; import { ServiceGraphSection } from './ServiceGraphSection'; import { getDS } from './utils'; interface Props extends QueryEditorProps, Themeable2 {} interface State { uploadModalOpen: boolean; } const DEFAULT_QUERY_TYPE: TempoQueryType = config.featureToggles.traceqlSearch ? 'traceqlSearch' : 'traceql'; class TempoQueryFieldComponent extends React.PureComponent { constructor(props: Props) { super(props); this.state = { uploadModalOpen: false, }; } // Set the default query type when the component mounts. // Also do this if queryType is 'clear' (which is the case when the user changes the query type) // otherwise if the user changes the query type and refreshes the page, no query type will be selected // which is inconsistent with how the UI was originally when they selected the Tempo data source. async componentDidMount() { if (!this.props.query.queryType || this.props.query.queryType === 'clear') { this.props.onChange({ ...this.props.query, queryType: DEFAULT_QUERY_TYPE, }); } } onChangeLinkedQuery = (value: LokiQuery) => { const { query, onChange } = this.props; onChange({ ...query, linkedQuery: { ...value, refId: 'linked' }, }); }; onRunLinkedQuery = () => { this.props.onRunQuery(); }; onClearResults = () => { // Run clear query to clear results const { onChange, query, onRunQuery } = this.props; onChange({ ...query, queryType: 'clear', }); onRunQuery(); }; render() { const { query, onChange, datasource, app } = this.props; const logsDatasourceUid = datasource.getLokiSearchDS(); const graphDatasourceUid = datasource.serviceMap?.datasourceUid; let queryTypeOptions: Array> = [ { value: 'traceql', label: 'TraceQL' }, { value: 'serviceMap', label: 'Service Graph' }, ]; if (config.featureToggles.traceqlSearch) { queryTypeOptions.unshift({ value: 'traceqlSearch', label: 'Search' }); } if (!config.featureToggles.traceqlSearch && !datasource?.search?.hide) { queryTypeOptions.unshift({ value: 'nativeSearch', label: 'Search' }); } if (logsDatasourceUid) { if (datasource?.search?.hide) { // Place at beginning as Search if no native search queryTypeOptions.unshift({ value: 'search', label: 'Search' }); } else { // Place at end as Loki Search if native search is enabled queryTypeOptions.push({ value: 'search', label: 'Loki Search' }); } } return ( <> this.setState({ uploadModalOpen: false })} >
{ this.props.datasource.uploadedJson = result; onChange({ ...query, queryType: 'upload', }); this.setState({ uploadModalOpen: false }); this.props.onRunQuery(); }} />
options={queryTypeOptions} value={query.queryType} onChange={(v) => { reportInteraction('grafana_traces_query_type_changed', { datasourceType: 'tempo', app: app ?? '', grafana_version: config.buildInfo.version, newQueryType: v, previousQueryType: query.queryType ?? '', }); this.onClearResults(); onChange({ ...query, queryType: v, }); }} size="md" /> {query.queryType === 'search' && ( )} {query.queryType === 'nativeSearch' && ( )} {query.queryType === 'traceqlSearch' && ( )} {query.queryType === 'serviceMap' && ( )} {query.queryType === 'traceql' && ( )} ); } } interface SearchSectionProps { logsDatasourceUid?: string; onChange: (value: LokiQuery) => void; onRunQuery: () => void; query: TempoQuery; } function SearchSection({ logsDatasourceUid, onChange, onRunQuery, query }: SearchSectionProps) { const dsState = useAsync(() => getDS(logsDatasourceUid), [logsDatasourceUid]); if (dsState.loading) { return null; } const ds = dsState.value as LokiDatasource; if (ds) { return ( <> Tempo uses {ds.name} to find traces. ); } if (!logsDatasourceUid) { return
Please set up a Loki search datasource in the datasource settings.
; } if (logsDatasourceUid && !ds) { return (
Loki search datasource is configured but the data source no longer exists. Please configure existing data source to use the search.
); } return null; } export const TempoQueryField = withTheme2(TempoQueryFieldComponent);