123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- import {useQuery} from '@tanstack/react-query';
- import {NewQuery} from 'sentry/types';
- import EventView from 'sentry/utils/discover/eventView';
- import {DiscoverDatasets} from 'sentry/utils/discover/types';
- import {DefinedUseQueryResult} from 'sentry/utils/queryClient';
- import {useLocation} from 'sentry/utils/useLocation';
- import usePageFilters from 'sentry/utils/usePageFilters';
- import {HOST} from 'sentry/views/starfish/utils/constants';
- import {
- datetimeToClickhouseFilterTimestamps,
- getDateFilters,
- } from 'sentry/views/starfish/utils/dates';
- import {getDateQueryFilter} from 'sentry/views/starfish/utils/getDateQueryFilter';
- import {useWrappedDiscoverTimeseriesQuery} from 'sentry/views/starfish/utils/useSpansQuery';
- export const getHostListQuery = ({datetime}) => {
- const {start_timestamp, end_timestamp} = datetimeToClickhouseFilterTimestamps(datetime);
- return `SELECT
- domain,
- toStartOfInterval(start_timestamp, INTERVAL 12 HOUR) as interval,
- quantile(0.99)(exclusive_time) as p99,
- count() as count,
- countIf(greaterOrEquals(status, 400) AND lessOrEquals(status, 599)) as failure_count,
- failure_count / count as failure_rate
- FROM spans_experimental_starfish
- WHERE module = 'http'
- AND domain != ''
- ${start_timestamp ? `AND greaterOrEquals(start_timestamp, '${start_timestamp}')` : ''}
- ${end_timestamp ? `AND lessOrEquals(start_timestamp, '${end_timestamp}')` : ''}
- GROUP BY domain, interval
- ORDER BY domain, interval asc
- `;
- };
- export const getHostListEventView = ({datetime}) => {
- return EventView.fromSavedQuery({
- name: '',
- fields: ['domain'],
- yAxis: ['p99(span.self_time)', 'count()'],
- query: 'module:http',
- topEvents: '10',
- start: datetime.start,
- end: datetime.end,
- range: datetime.period,
- dataset: DiscoverDatasets.SPANS_INDEXED,
- projects: [1],
- version: 2,
- });
- };
- export const getEndpointListQuery = ({domain, action, datetime, transaction}) => {
- const {start_timestamp, end_timestamp} = datetimeToClickhouseFilterTimestamps(datetime);
- return `SELECT
- description,
- group_id,
- domain,
- action,
- quantile(0.5)(exclusive_time) AS "p50(span.self_time)",
- quantile(0.95)(exclusive_time) AS "p95(span.self_time)",
- sum(exclusive_time) AS "sum(span.self_time)",
- uniq(user) AS "count_unique(user)", uniq(transaction) AS "count_unique(transaction)",
- count() AS count,
- countIf(greaterOrEquals(status, 400) AND lessOrEquals(status, 599)) AS failure_count,
- failure_count / count AS failure_rate
- FROM spans_experimental_starfish
- WHERE module = 'http'
- ${domain ? `AND domain = '${domain}'` : ''}
- ${action ? `AND action = '${action}'` : ''}
- ${transaction ? `AND transaction = '${transaction}'` : ''}
- ${start_timestamp ? `AND greaterOrEquals(start_timestamp, '${start_timestamp}')` : ''}
- ${end_timestamp ? `AND lessOrEquals(start_timestamp, '${end_timestamp}')` : ''}
- GROUP BY description, domain, action, group_id
- ORDER BY count DESC
- LIMIT 10
- `;
- };
- export const getEndpointListEventView = ({domain, action, datetime, transaction}) => {
- return EventView.fromSavedQuery({
- name: '',
- fields: [
- 'description',
- 'group_id',
- 'domain',
- 'action',
- 'p50(span.self_time)',
- 'p95(span.self_time)',
- 'sum(span.self_time)',
- 'count()',
- 'count_unique(user)',
- 'count_unique(transaction)',
- ],
- orderby: '-count',
- query: `module:http ${domain ? `domain:${domain}` : ''} ${
- action ? `action:${action}` : ''
- } ${transaction ? `transaction:${transaction}` : ''}`,
- start: datetime.start,
- end: datetime.end,
- range: datetime.period,
- dataset: DiscoverDatasets.SPANS_INDEXED,
- projects: [1],
- version: 2,
- });
- };
- export const getEndpointDomainsQuery = ({datetime}) => {
- const {start_timestamp, end_timestamp} = datetimeToClickhouseFilterTimestamps(datetime);
- return `SELECT domain, count(),
- sum(exclusive_time) as "sum(span.self_time)",
- max(exclusive_time) as "p100(span.self_time)",
- quantile(0.99)(exclusive_time) as "p99(span.self_time)",
- quantile(0.95)(exclusive_time) as "p95(span.self_time)",
- quantile(0.50)(exclusive_time) as "p50(span.self_time)"
- FROM spans_experimental_starfish
- WHERE module = 'http'
- ${start_timestamp ? `AND greaterOrEquals(start_timestamp, '${start_timestamp}')` : ''}
- ${end_timestamp ? `AND lessOrEquals(start_timestamp, '${end_timestamp}')` : ''}
- GROUP BY domain
- ORDER BY count() DESC
- `;
- };
- export const getEndpointDomainsEventView = ({datetime}) => {
- return EventView.fromSavedQuery({
- name: '',
- fields: [
- 'domain',
- 'count()',
- 'sum(span.self_time)',
- 'p100(span.self_time)',
- 'p99(span.self_time)',
- 'p95(span.self_time)',
- 'p50(span.self_time)',
- ],
- orderby: '-count',
- query: 'module:http',
- start: datetime.start,
- end: datetime.end,
- range: datetime.period,
- dataset: DiscoverDatasets.SPANS_INDEXED,
- projects: [1],
- version: 2,
- });
- };
- export const getEndpointGraphQuery = ({datetime}) => {
- const {start_timestamp, end_timestamp} = datetimeToClickhouseFilterTimestamps(datetime);
- return `SELECT
- toStartOfInterval(start_timestamp, INTERVAL 12 HOUR) as interval,
- quantile(0.5)(exclusive_time) as "p50(span.self_time)",
- quantile(0.75)(exclusive_time) as "p75(span.self_time)",
- quantile(0.95)(exclusive_time) as "p95(span.self_time)",
- quantile(0.99)(exclusive_time) as "p99(span.self_time)",
- count() as "count()",
- countIf(greaterOrEquals(status, 400) AND lessOrEquals(status, 599)) as "failure_count()",
- "failure_count()" / "count()" as "failure_rate()"
- FROM spans_experimental_starfish
- WHERE module = 'http'
- ${start_timestamp ? `AND greaterOrEquals(start_timestamp, '${start_timestamp}')` : ''}
- ${end_timestamp ? `AND lessOrEquals(start_timestamp, '${end_timestamp}')` : ''}
- GROUP BY interval
- ORDER BY interval asc
- `;
- };
- export const getEndpointGraphEventView = ({datetime}) => {
- return EventView.fromSavedQuery({
- name: '',
- fields: [
- 'count()',
- 'p50(span.self_time)',
- 'p75(span.self_time)',
- 'p95(span.self_time)',
- 'p99(span.self_time)',
- ],
- yAxis: [
- 'count()',
- 'p50(span.self_time)',
- 'p75(span.self_time)',
- 'p95(span.self_time)',
- 'p99(span.self_time)',
- ],
- query: 'module:http',
- start: datetime.start,
- end: datetime.end,
- range: datetime.period,
- dataset: DiscoverDatasets.SPANS_INDEXED,
- projects: [1],
- version: 2,
- });
- };
- export const getEndpointDetailSeriesQuery = ({
- description,
- transactionName,
- datetime,
- groupId,
- }) => {
- const {start_timestamp, end_timestamp} = datetimeToClickhouseFilterTimestamps(datetime);
- const interval = 12;
- return `SELECT
- toStartOfInterval(start_timestamp, INTERVAL ${interval} HOUR) as interval,
- quantile(0.5)(exclusive_time) as p50,
- quantile(0.95)(exclusive_time) as p95,
- divide(count(), multiply(${interval}, 60)) as spm,
- count() as count,
- countIf(greaterOrEquals(status, 400) AND lessOrEquals(status, 599)) as failure_count,
- failure_count / count as failure_rate
- FROM spans_experimental_starfish
- WHERE module = 'http'
- ${description ? `AND description = '${description}'` : ''}
- ${groupId ? `AND group_id = '${groupId}'` : ''}
- ${transactionName ? `AND transaction = '${transactionName}'` : ''}
- ${
- start_timestamp ? `AND greaterOrEquals(start_timestamp, '${start_timestamp}')` : ''
- }
- ${end_timestamp ? `AND lessOrEquals(start_timestamp, '${end_timestamp}')` : ''}
- GROUP BY interval
- ORDER BY interval asc
- `;
- };
- export const getEndpointDetailTableQuery = ({
- description,
- transactionName,
- datetime,
- groupId,
- }) => {
- const {start_timestamp, end_timestamp} = datetimeToClickhouseFilterTimestamps(datetime);
- return `
- SELECT transaction,
- count(),
- quantile(0.5)(exclusive_time) as "p50(span.self_time)",
- quantile(0.95)(exclusive_time) as "p95(span.self_time)",
- countIf(greaterOrEquals(status, 400) AND lessOrEquals(status, 599)) as failure_count,
- failure_count / count() as failure_rate,
- sum(exclusive_time) as "sum(span.self_time)",
- count(DISTINCT transaction_id) as "count_unique(transaction)"
- FROM spans_experimental_starfish
- WHERE module = 'http'
- ${description ? `AND description = '${description}'` : ''}
- ${groupId ? `AND group_id = '${groupId}'` : ''}
- ${transactionName ? `AND transaction = '${transactionName}'` : ''}
- ${start_timestamp ? `AND greaterOrEquals(start_timestamp, '${start_timestamp}')` : ''}
- ${end_timestamp ? `AND lessOrEquals(start_timestamp, '${end_timestamp}')` : ''}
- GROUP BY transaction
- ORDER BY count() DESC
- LIMIT 5
- `;
- };
- export const getEndpointDetailTableEventView = ({
- description,
- transactionName,
- datetime,
- groupId,
- }) => {
- return EventView.fromSavedQuery({
- name: '',
- fields: [
- 'transaction',
- 'count()',
- 'p50(span.self_time)',
- 'p95(span.self_time)',
- 'sum(span.self_time)',
- 'count_unique(transaction)',
- ],
- orderby: '-count',
- query: `module:http ${description ? `description:${description}` : ''} ${
- transactionName ? `transaction:${transactionName}` : ''
- } ${groupId ? `group_id:${groupId}` : ''}`,
- start: datetime.start,
- end: datetime.end,
- range: datetime.period,
- dataset: DiscoverDatasets.SPANS_INDEXED,
- projects: [1],
- version: 2,
- });
- };
- export const getHostStatusBreakdownQuery = ({domain, datetime}) => {
- const {start_timestamp, end_timestamp} = datetimeToClickhouseFilterTimestamps(datetime);
- return `
- SELECT count() as count, status
- FROM spans_experimental_starfish
- WHERE module = 'http'
- AND domain = '${domain}'
- ${start_timestamp ? `AND greaterOrEquals(start_timestamp, '${start_timestamp}')` : ''}
- ${end_timestamp ? `AND lessOrEquals(start_timestamp, '${end_timestamp}')` : ''}
- GROUP BY status
- ORDER BY count DESC
- `;
- };
- export const getHostStatusBreakdownEventView = ({domain, datetime}) => {
- return EventView.fromSavedQuery({
- name: '',
- fields: ['status', 'count()'],
- orderby: '-count',
- query: `module:http domain:${domain}`,
- start: datetime.start,
- end: datetime.end,
- range: datetime.period,
- dataset: DiscoverDatasets.SPANS_INDEXED,
- projects: [1],
- version: 2,
- });
- };
- export const getEndpointAggregatesQuery = ({datetime, transaction}) => {
- const {start_timestamp, end_timestamp} = datetimeToClickhouseFilterTimestamps(datetime);
- return `
- SELECT
- description,
- toStartOfInterval(start_timestamp, INTERVAL 12 HOUR) as interval,
- count() AS count,
- quantile(0.5)(exclusive_time) as p50,
- quantile(0.95)(exclusive_time) as p95
- FROM spans_experimental_starfish
- WHERE module = 'http'
- ${transaction ? `AND transaction = '${transaction}'` : ''}
- ${start_timestamp ? `AND greaterOrEquals(start_timestamp, '${start_timestamp}')` : ''}
- ${end_timestamp ? `AND lessOrEquals(start_timestamp, '${end_timestamp}')` : ''}
- GROUP BY description, interval
- ORDER BY interval asc
- `;
- };
- const ORDERBY = `
- -power(10, floor(log10(count()))), -quantile(0.75)(exclusive_time)
- `;
- const getTransactionsFromHostSubquery = (hostNames: string[], dateFilters: string) => {
- const hostFilter = `domain IN ('${hostNames.join(`', '`)}')`;
- return `
- SELECT
- transaction
- FROM default.spans_experimental_starfish
- WHERE
- startsWith(span_operation, 'http')
- AND ${hostFilter}
- ${dateFilters}
- GROUP BY transaction
- ORDER BY ${ORDERBY}
- LIMIT 5
- `;
- };
- type TopTransactionData = {
- interval: string;
- transaction: string;
- epm?: number;
- p75?: number;
- };
- export const useGetTransactionsForHosts = (
- hostNames: string[],
- interval: string
- ): DefinedUseQueryResult<TopTransactionData[]> => {
- const pageFilter = usePageFilters();
- const location = useLocation();
- const {startTime, endTime} = getDateFilters(pageFilter);
- const dateFilters = getDateQueryFilter(startTime, endTime);
- const transactionNameQuery = getTransactionsFromHostSubquery(hostNames, dateFilters);
- const {start, end, period} = pageFilter.selection.datetime;
- const {isLoading: isTopTransactionNamesLoading, data: topTransactionNamesData} =
- useQuery<{transaction: string}[]>({
- enabled: !!hostNames?.length,
- queryKey: ['topTransactionNames', hostNames.join(','), start, end],
- queryFn: () =>
- fetch(`${HOST}/?query=${transactionNameQuery}`).then(res => res.json()),
- retry: false,
- refetchOnWindowFocus: false,
- initialData: [],
- });
- const query: NewQuery = {
- id: undefined,
- name: '',
- query: `transaction:[${topTransactionNamesData
- ?.map(d => `"${d.transaction}"`)
- .join(',')}]`,
- projects: [1],
- fields: ['transaction', 'epm()', 'p75(transaction.duration)'],
- version: 1,
- topEvents: '5',
- start: start?.toString(),
- end: end?.toString(),
- dataset: DiscoverDatasets.METRICS_ENHANCED,
- interval,
- yAxis: ['epm()', 'p75(transaction.duration)'],
- };
- const eventView = EventView.fromNewQueryWithLocation(query, location);
- eventView.statsPeriod = period ?? undefined;
- const {
- isLoading: isTopTransactionSeriesLoading,
- data: topTransactionSeriesData,
- ...rest
- } = useWrappedDiscoverTimeseriesQuery({
- eventView,
- initialData: [],
- enabled: !isTopTransactionNamesLoading && !!topTransactionNamesData.length,
- });
- return {
- ...rest,
- isLoading: isTopTransactionSeriesLoading,
- data: topTransactionSeriesData,
- } as DefinedUseQueryResult<TopTransactionData[]>;
- };
|