Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix: remove spanScopeSelector key to sync scope filters (#11355)

Aditya Singh6094a5eee29a
diff --git a/frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/SpanScopeSelector.tsx b/frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/SpanScopeSelector.tsx
index e0dc8c5389..1dc504eb5e 100644
--- a/frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/SpanScopeSelector.tsx
+++ b/frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/SpanScopeSelector.tsx
@@ -16,35 +16,31 @@ enum SpanScope {
ENTRYPOINT_SPANS = 'entrypoint_spans',
}
-interface SpanFilterConfig {
- key: string;
- type: string;
-}
-
interface SpanScopeSelectorProps {
onChange?: (value: TagFilter) => void;
query?: IBuilderQuery;
skipQueryBuilderRedirect?: boolean;
}
-const SPAN_FILTER_CONFIG: Record<SpanScope, SpanFilterConfig | null> = {
+const SPAN_FILTER_KEY: Record<SpanScope, string | null> = {
[SpanScope.ALL_SPANS]: null,
- [SpanScope.ROOT_SPANS]: {
- key: 'isRoot',
- type: 'spanSearchScope',
- },
- [SpanScope.ENTRYPOINT_SPANS]: {
- key: 'isEntryPoint',
- type: 'spanSearchScope',
- },
+ [SpanScope.ROOT_SPANS]: 'isRoot',
+ [SpanScope.ENTRYPOINT_SPANS]: 'isEntryPoint',
};
-const createFilterItem = (config: SpanFilterConfig): TagFilterItem => ({
+const SCOPE_FILTER_KEYS = Object.values(SPAN_FILTER_KEY).filter(
+ (key): key is string => key !== null,
+);
+
+const isScopeFilter = (filter: TagFilterItem, key: string): boolean =>
+ filter.key?.key === key && String(filter.value) === 'true';
+
+const createFilterItem = (key: string): TagFilterItem => ({
id: uuid().slice(0, 8),
key: {
- key: config.key,
+ key,
dataType: undefined,
- type: config?.type,
+ type: '',
},
op: '=',
value: 'true',
@@ -70,12 +66,7 @@ function SpanScopeSelector({
filters: TagFilterItem[] = [],
): SpanScope => {
const hasFilter = (key: string): boolean =>
- filters?.some(
- (filter) =>
- filter.key?.type === 'spanSearchScope' &&
- filter.key.key === key &&
- filter.value === 'true',
- );
+ filters?.some((filter) => isScopeFilter(filter, key));
if (hasFilter('isRoot')) {
return SpanScope.ROOT_SPANS;
@@ -113,28 +104,21 @@ function SpanScopeSelector({
const nonScopeFilters = currentFilters.filter(
(filter) =>
- !(
- filter.key?.type === 'spanSearchScope' &&
- (filter.key.key === 'isRoot' || filter.key.key === 'isEntryPoint')
- ),
+ !SCOPE_FILTER_KEYS.some((scopeKey) => isScopeFilter(filter, scopeKey)),
);
- const config = SPAN_FILTER_CONFIG[newScope];
- const newScopeFilter = config !== null ? [createFilterItem(config)] : [];
+ const scopeKey = SPAN_FILTER_KEY[newScope];
+ const newScopeFilter = scopeKey !== null ? [createFilterItem(scopeKey)] : [];
return [...nonScopeFilters, ...newScopeFilter];
};
- const keysToRemove = Object.values(SPAN_FILTER_CONFIG)
- .map((config) => config?.key)
- .filter((key): key is string => typeof key === 'string');
-
newQuery.builder.queryData = newQuery.builder.queryData.map((item) => ({
...item,
filter: {
expression: removeKeysFromExpression(
item.filter?.expression ?? '',
- keysToRemove,
+ SCOPE_FILTER_KEYS,
),
},
filters: {
diff --git a/frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/__test__/SpanScopeSelector.test.tsx b/frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/__test__/SpanScopeSelector.test.tsx
index c804ac7ec3..494ef19d7e 100644
--- a/frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/__test__/SpanScopeSelector.test.tsx
+++ b/frontend/src/container/QueryBuilder/filters/QueryBuilderSearchV2/__test__/SpanScopeSelector.test.tsx
@@ -20,12 +20,16 @@ import SpanScopeSelector from '../SpanScopeSelector';
const mockRedirectWithQueryBuilderData = jest.fn();
+const SCOPE_KEYS = ['isRoot', 'isEntryPoint'];
+const isScopeFilter = (filter: TagFilterItem): boolean =>
+ SCOPE_KEYS.includes(filter.key?.key ?? '') && String(filter.value) === 'true';
+
// Helper to create filter items
const createSpanScopeFilter = (key: string): TagFilterItem => ({
id: 'span-filter',
key: {
key,
- type: 'spanSearchScope',
+ type: '',
},
op: '=',
value: 'true',
@@ -143,7 +147,6 @@ describe('SpanScopeSelector', () => {
expect.objectContaining({
key: expect.objectContaining({
key: expectedKey,
- type: 'spanSearchScope',
}),
op: '=',
value: 'true',
@@ -162,11 +165,7 @@ describe('SpanScopeSelector', () => {
expect(mockRedirectWithQueryBuilderData).toHaveBeenCalled();
const updatedQuery = mockRedirectWithQueryBuilderData.mock.calls[0][0];
const filters = updatedQuery.builder.queryData[0].filters.items;
- expect(filters).not.toContainEqual(
- expect.objectContaining({
- key: expect.objectContaining({ type: 'spanSearchScope' }),
- }),
- );
+ expect(filters.some(isScopeFilter)).toBe(false);
});
it('should add isRoot filter when selecting ROOT_SPANS', async () => {
@@ -206,6 +205,27 @@ describe('SpanScopeSelector', () => {
await expect(screen.findByText(expectedText)).resolves.toBeInTheDocument();
},
);
+
+ // Round-trip from filter.expression can deserialize the value as a boolean
+ // `true` (unquoted in the expression) instead of the string `'true'` produced
+ // by the dropdown. The dropdown must still recognize that as the scope filter.
+ it.each([
+ ['Root Spans', 'isRoot'],
+ ['Entrypoint Spans', 'isEntryPoint'],
+ ])(
+ 'should initialize with %s selected when %s = true (boolean value)',
+ async (expectedText, filterKey) => {
+ const booleanScopeFilter: TagFilterItem = {
+ id: 'span-filter',
+ key: { key: filterKey, type: '' },
+ op: '=',
+ value: true as unknown as string,
+ };
+ const queryWithFilter = createQueryWithFilters([booleanScopeFilter]);
+ renderWithContext(queryWithFilter, undefined, defaultQueryBuilderQuery);
+ await expect(screen.findByText(expectedText)).resolves.toBeInTheDocument();
+ },
+ );
});
describe('when onChange and query props are provided', () => {
@@ -233,9 +253,7 @@ describe('SpanScopeSelector', () => {
expect(items).toContainEqual(nonScopeItem);
});
- const scopeFiltersInPayload = items.filter(
- (filter) => filter.key?.type === 'spanSearchScope',
- );
+ const scopeFiltersInPayload = items.filter(isScopeFilter);
if (expectedScopeKey) {
expect(scopeFiltersInPayload).toHaveLength(1);
@@ -434,9 +452,7 @@ describe('SpanScopeSelector', () => {
items: [],
};
// Count non-scope filters
- const nonScopeFilters = items.filter(
- (filter) => filter.key?.type !== 'spanSearchScope',
- );
+ const nonScopeFilters = items.filter((filter) => !isScopeFilter(filter));
expect(nonScopeFilters).toHaveLength(1);
expect(nonScopeFilters).toContainEqual(