Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for span scope #6910

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
.query-builder-search-container {
position: relative;
display: flex;
align-items: center;
gap: 12px;
}

.selectOptionContainer {
display: flex;
gap: 8px;
Expand Down Expand Up @@ -410,3 +417,6 @@
}
}
}
.span-scope-selector {
width: 160px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { Select } from 'antd';
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
import { cloneDeep } from 'lodash-es';
import { useEffect, useState } from 'react';
import { TagFilterItem } from 'types/api/queryBuilder/queryBuilderData';
import { v4 as uuid } from 'uuid';

enum SpanScope {
ALL_SPANS = 'all_spans',
ROOT_SPANS = 'root_spans',
ENTRYPOINT_SPANS = 'entrypoint_spans',
}

interface SpanFilterConfig {
key: string;
type: string;
}

interface SpanScopeSelectorProps {
queryName: string;
}

const SPAN_FILTER_CONFIG: Record<SpanScope, SpanFilterConfig | null> = {
[SpanScope.ALL_SPANS]: null,
[SpanScope.ROOT_SPANS]: {
key: 'isRoot',
type: 'spanSearchScope',
},
[SpanScope.ENTRYPOINT_SPANS]: {
key: 'isEntryPoint',
type: 'spanSearchScope',
},
};

const createFilterItem = (config: SpanFilterConfig): TagFilterItem => ({
id: uuid().slice(0, 8),
key: {
key: config.key,
dataType: undefined,
isColumn: false,
type: config?.type,
},
op: '=',
value: 'true',
});

const SELECT_OPTIONS = [
{ value: SpanScope.ALL_SPANS, label: 'All Spans' },
{ value: SpanScope.ROOT_SPANS, label: 'Root Spans' },
{ value: SpanScope.ENTRYPOINT_SPANS, label: 'Entrypoint Spans' },
];

function SpanScopeSelector({ queryName }: SpanScopeSelectorProps): JSX.Element {
const { currentQuery, redirectWithQueryBuilderData } = useQueryBuilder();
const [selectedScope, setSelectedScope] = useState<SpanScope>(
SpanScope.ALL_SPANS,
);

const getCurrentScopeFromFilters = (
filters: TagFilterItem[] = [],
): SpanScope => {
const hasFilter = (key: string): boolean =>
filters.some(
(filter) =>
filter.key?.type === 'spanSearchScope' &&
filter.key.key === key &&
filter.value === 'true',
);

if (hasFilter('isRoot')) return SpanScope.ROOT_SPANS;
if (hasFilter('isEntryPoint')) return SpanScope.ENTRYPOINT_SPANS;
return SpanScope.ALL_SPANS;
};

useEffect(() => {
const queryData = currentQuery.builder.queryData.find(
(item) => item.queryName === queryName,
);
const filters = queryData?.filters?.items;
const currentScope = getCurrentScopeFromFilters(filters);
setSelectedScope(currentScope);
}, [currentQuery, queryName]);

const handleScopeChange = (newScope: SpanScope): void => {
const newQuery = cloneDeep(currentQuery);

const getUpdatedFilters = (
currentFilters: TagFilterItem[] = [],
isTargetQuery: boolean,
): TagFilterItem[] => {
if (!isTargetQuery) return currentFilters;

const nonScopeFilters = currentFilters.filter(
(filter) =>
!(
filter.key?.type === 'spanSearchScope' &&
(filter.key.key === 'isRoot' || filter.key.key === 'isEntryPoint')
),
);

const config = SPAN_FILTER_CONFIG[newScope];
const newScopeFilter = config !== null ? [createFilterItem(config)] : [];

return [...nonScopeFilters, ...newScopeFilter];
};

newQuery.builder.queryData = newQuery.builder.queryData.map((item) => ({
...item,
filters: {
...item.filters,
items: getUpdatedFilters(item.filters?.items, item.queryName === queryName),
},
}));

redirectWithQueryBuilderData(newQuery);
};

//
return (
<Select
value={selectedScope}
className="span-scope-selector"
onChange={handleScopeChange}
options={SELECT_OPTIONS}
/>
);
}

export default SpanScopeSelector;
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { PLACEHOLDER } from './constant';
import ExampleQueriesRendererForLogs from './ExampleQueriesRendererForLogs';
import OptionRenderer from './OptionRenderer';
import OptionRendererForLogs from './OptionRendererForLogs';
import SpanScopeSelector from './SpanScopeSelector';
import { StyledCheckOutlined, TypographyText } from './style';
import {
convertExampleQueriesToOptions,
Expand All @@ -82,6 +83,11 @@ function QueryBuilderSearch({
pathname,
]);

const isTracesExplorerPage = useMemo(
() => pathname === ROUTES.TRACES_EXPLORER,
[pathname],
);

const {
updateTag,
handleClearTag,
Expand Down Expand Up @@ -319,11 +325,7 @@ function QueryBuilderSearch({
));

return (
<div
style={{
position: 'relative',
}}
>
<div className="query-builder-search-container">
<Select
ref={selectRef}
getPopupContainer={popupContainer}
Expand Down Expand Up @@ -449,6 +451,7 @@ function QueryBuilderSearch({
</Select.Option>
))}
</Select>
{isTracesExplorerPage && <SpanScopeSelector queryName={query.queryName} />}
</div>
);
}
Expand Down
Loading