2022-09-01 22:49:27 +01:00
|
|
|
import { css } from '@emotion/css';
|
2022-08-24 17:57:59 +01:00
|
|
|
import { defaults } from 'lodash';
|
2023-12-15 12:46:32 +00:00
|
|
|
import React, { useState } from 'react';
|
2022-08-24 17:57:59 +01:00
|
|
|
|
|
|
|
|
import { QueryEditorProps } from '@grafana/data';
|
2023-12-15 12:46:32 +00:00
|
|
|
import { config, reportInteraction } from '@grafana/runtime';
|
|
|
|
|
import { Button, InlineLabel, useStyles2 } from '@grafana/ui';
|
2022-08-24 17:57:59 +01:00
|
|
|
|
2023-12-15 12:46:32 +00:00
|
|
|
import { generateQueryFromFilters } from '../SearchTraceQLEditor/utils';
|
2022-09-05 15:46:10 +01:00
|
|
|
import { TempoDatasource } from '../datasource';
|
|
|
|
|
import { defaultQuery, MyDataSourceOptions, TempoQuery } from '../types';
|
2022-08-24 17:57:59 +01:00
|
|
|
|
2022-09-01 22:49:27 +01:00
|
|
|
import { TempoQueryBuilderOptions } from './TempoQueryBuilderOptions';
|
2022-08-24 17:57:59 +01:00
|
|
|
import { TraceQLEditor } from './TraceQLEditor';
|
|
|
|
|
|
2023-12-15 12:46:32 +00:00
|
|
|
type EditorProps = {
|
|
|
|
|
onClearResults: () => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Props = EditorProps & QueryEditorProps<TempoDatasource, TempoQuery, MyDataSourceOptions>;
|
2022-08-24 17:57:59 +01:00
|
|
|
|
|
|
|
|
export function QueryEditor(props: Props) {
|
2022-09-01 22:49:27 +01:00
|
|
|
const styles = useStyles2(getStyles);
|
|
|
|
|
const query = defaults(props.query, defaultQuery);
|
2023-12-15 12:46:32 +00:00
|
|
|
const [showCopyFromSearchButton, setShowCopyFromSearchButton] = useState(() => {
|
|
|
|
|
const genQuery = generateQueryFromFilters(query.filters || []);
|
|
|
|
|
return genQuery === query.query || genQuery === '{}';
|
|
|
|
|
});
|
2022-08-24 17:57:59 +01:00
|
|
|
|
2022-09-01 22:49:27 +01:00
|
|
|
const onEditorChange = (value: string) => {
|
|
|
|
|
props.onChange({ ...query, query: value });
|
|
|
|
|
};
|
2022-08-24 17:57:59 +01:00
|
|
|
|
|
|
|
|
return (
|
2022-09-01 22:49:27 +01:00
|
|
|
<>
|
2022-09-09 19:00:35 +01:00
|
|
|
<InlineLabel>
|
|
|
|
|
Build complex queries using TraceQL to select a list of traces.{' '}
|
2023-01-23 11:59:55 -05:00
|
|
|
<a rel="noreferrer" target="_blank" href="https://grafana.com/docs/tempo/latest/traceql/">
|
2022-09-09 19:00:35 +01:00
|
|
|
Documentation
|
|
|
|
|
</a>
|
|
|
|
|
</InlineLabel>
|
2023-12-15 12:46:32 +00:00
|
|
|
{!showCopyFromSearchButton && (
|
|
|
|
|
<InlineLabel>
|
|
|
|
|
<div>
|
|
|
|
|
Continue editing the query from the Search tab?
|
|
|
|
|
<Button
|
|
|
|
|
variant="secondary"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
reportInteraction('grafana_traces_copy_to_traceql_clicked', {
|
|
|
|
|
app: props.app ?? '',
|
|
|
|
|
grafana_version: config.buildInfo.version,
|
2024-01-04 16:10:09 +00:00
|
|
|
location: 'traceql_tab',
|
2023-12-15 12:46:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
props.onClearResults();
|
|
|
|
|
props.onChange({
|
|
|
|
|
...query,
|
|
|
|
|
query: generateQueryFromFilters(query.filters || []),
|
|
|
|
|
});
|
|
|
|
|
setShowCopyFromSearchButton(true);
|
|
|
|
|
}}
|
|
|
|
|
style={{ marginLeft: '10px' }}
|
|
|
|
|
>
|
|
|
|
|
Copy query from Search
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</InlineLabel>
|
|
|
|
|
)}
|
2022-09-09 19:00:35 +01:00
|
|
|
<TraceQLEditor
|
2022-12-13 13:27:45 +00:00
|
|
|
placeholder="Enter a TraceQL query or trace ID (run with Shift+Enter)"
|
2023-10-09 14:22:39 +01:00
|
|
|
value={query.query || ''}
|
2022-09-09 19:00:35 +01:00
|
|
|
onChange={onEditorChange}
|
|
|
|
|
datasource={props.datasource}
|
|
|
|
|
onRunQuery={props.onRunQuery}
|
|
|
|
|
/>
|
2022-09-01 22:49:27 +01:00
|
|
|
<div className={styles.optionsContainer}>
|
|
|
|
|
<TempoQueryBuilderOptions query={query} onChange={props.onChange} />
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
2022-08-24 17:57:59 +01:00
|
|
|
);
|
|
|
|
|
}
|
2022-09-01 22:49:27 +01:00
|
|
|
|
|
|
|
|
const getStyles = () => ({
|
|
|
|
|
optionsContainer: css`
|
|
|
|
|
margin-top: 10px;
|
|
|
|
|
`,
|
|
|
|
|
});
|