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(logs): dedupe body/timestamp columns in explorer picker (#11553)

Aditya Singh2e60ab0f8105
diff --git a/frontend/src/components/FieldsSelector/AddedFields.tsx b/frontend/src/components/FieldsSelector/AddedFields.tsx
index 1e06eec0b0..6b9c3a33ba 100644
--- a/frontend/src/components/FieldsSelector/AddedFields.tsx
+++ b/frontend/src/components/FieldsSelector/AddedFields.tsx
@@ -144,10 +144,7 @@ function AddedFields({
field={field}
onRemove={handleRemove}
allowDrag={allowDrag}
- isRequired={
- requiredFields.includes(field.name) ||
- requiredFields.includes(field.key as string)
- }
+ isRequired={requiredFields.includes(field.key as string)}
/>
))}
</SortableContext>
diff --git a/frontend/src/components/FieldsSelector/__tests__/AddedFields.test.tsx b/frontend/src/components/FieldsSelector/__tests__/AddedFields.test.tsx
index a276ad5062..bf4697ee43 100644
--- a/frontend/src/components/FieldsSelector/__tests__/AddedFields.test.tsx
+++ b/frontend/src/components/FieldsSelector/__tests__/AddedFields.test.tsx
@@ -25,7 +25,7 @@ describe('AddedFields — requiredFields', () => {
expect(screen.getAllByRole('button', { name: /remove/i })).toHaveLength(3);
});
- it('hides the Remove button for fields whose name is in requiredFields', () => {
+ it('hides the Remove button for fields whose composite key is in requiredFields', () => {
const fields = [makeField('a'), makeField('b'), makeField('c')];
render(
@@ -33,7 +33,7 @@ describe('AddedFields — requiredFields', () => {
inputValue=""
fields={fields}
onFieldsChange={jest.fn()}
- requiredFields={['a', 'c']}
+ requiredFields={['log.a', 'log.c']}
/>,
);
@@ -50,7 +50,7 @@ describe('AddedFields — requiredFields', () => {
inputValue=""
fields={fields}
onFieldsChange={jest.fn()}
- requiredFields={['a']}
+ requiredFields={['log.a']}
/>,
);
@@ -58,9 +58,9 @@ describe('AddedFields — requiredFields', () => {
expect(screen.getByText('b')).toBeInTheDocument();
});
- it('locks all variants of a required name regardless of fieldContext', () => {
- // Two `body` fields with different contexts — both should lock when
- // `body` is in requiredFields.
+ it('locks ONLY the canonical variant — a same-name field from another context stays removable', () => {
+ // Two `body` fields with different contexts. requiredFields holds the
+ // canonical composite key only, so the attribute variant is deletable.
const fields = [makeField('body', 'log'), makeField('body', 'attribute')];
render(
@@ -68,16 +68,17 @@ describe('AddedFields — requiredFields', () => {
inputValue=""
fields={fields}
onFieldsChange={jest.fn()}
- requiredFields={['body']}
+ requiredFields={['log.body']}
/>,
);
- // Both 'body' variants locked → zero Remove buttons.
- expect(screen.queryAllByRole('button', { name: /remove/i })).toHaveLength(0);
+ // One Remove button: the attribute variant. log variant is locked.
+ expect(screen.getAllByRole('button', { name: /remove/i })).toHaveLength(1);
});
- it('treats requiredFields as exact-name match (substring does not lock)', () => {
- const fields = [makeField('body'), makeField('body_extra')];
+ it('does not lock anything when a bare name is passed (composite key required)', () => {
+ // Bare `body` no longer matches — matching is composite-key only now.
+ const fields = [makeField('body', 'log'), makeField('body', 'attribute')];
render(
<AddedFields
@@ -88,26 +89,23 @@ describe('AddedFields — requiredFields', () => {
/>,
);
- // 'body' locked, 'body_extra' removable.
- expect(screen.getAllByRole('button', { name: /remove/i })).toHaveLength(1);
+ // Neither variant locked → both removable.
+ expect(screen.getAllByRole('button', { name: /remove/i })).toHaveLength(2);
});
- it('also accepts composite IDs in requiredFields (locks a specific variant)', () => {
- // Two `body` fields with different contexts.
- const fields = [makeField('body', 'log'), makeField('body', 'attribute')];
+ it('treats requiredFields as exact composite-key match (substring does not lock)', () => {
+ const fields = [makeField('body'), makeField('body_extra')];
render(
<AddedFields
inputValue=""
fields={fields}
onFieldsChange={jest.fn()}
- // Composite ID — locks ONLY the log variant, attribute variant stays
- // removable.
requiredFields={['log.body']}
/>,
);
- // One Remove button: the attribute variant. log variant is locked.
+ // 'log.body' locked, 'log.body_extra' removable.
expect(screen.getAllByRole('button', { name: /remove/i })).toHaveLength(1);
});
});
diff --git a/frontend/src/components/Logs/TableView/__tests__/useLogsTableColumns.test.tsx b/frontend/src/components/Logs/TableView/__tests__/useLogsTableColumns.test.tsx
index 69b4d27503..41acebeef0 100644
--- a/frontend/src/components/Logs/TableView/__tests__/useLogsTableColumns.test.tsx
+++ b/frontend/src/components/Logs/TableView/__tests__/useLogsTableColumns.test.tsx
@@ -10,9 +10,9 @@ jest.mock('providers/Timezone', () => ({
}),
}));
-const field = (name: string): IField => ({
+const field = (name: string, type = ''): IField => ({
name,
- type: '',
+ type,
dataType: 'string',
});
@@ -38,18 +38,16 @@ describe('useLogsTableColumns — selectColumns-order respected', () => {
useLogsTableColumns({
fields: [
field('service.name'),
- field('body'),
+ field('body', 'log'),
field('request.id'),
- field('timestamp'),
+ field('timestamp', 'log'),
],
fontSize: FontSize.SMALL,
}),
);
- // body/timestamp are NOT pinned to fixed positions — they appear where the
- // caller placed them in `fields`. body/timestamp use composite IDs
- // ('log.body', 'log.timestamp') since their fieldContext is fixed; user
- // fields here have empty `type` so their composite collapses to bare name.
+ // body/timestamp appear where the caller placed them, keyed by their
+ // composite IDs ('log.*'); contextless user fields collapse to bare name.
expect(result.current.map((c) => c.id)).toStrictEqual([
'state-indicator',
'service.name',
@@ -59,6 +57,43 @@ describe('useLogsTableColumns — selectColumns-order respected', () => {
]);
});
+ it('renders a same-name field from another context as a DISTINCT column (no collision)', () => {
+ const { result } = renderHook(() =>
+ useLogsTableColumns({
+ fields: [field('body', 'log'), field('body', 'attribute')],
+ fontSize: FontSize.SMALL,
+ }),
+ );
+
+ const byId = new Map(result.current.map((c) => [c.id, c]));
+ // Attribute variant is its own column, not a duplicate 'log.body'.
+ expect(result.current.map((c) => c.id)).toStrictEqual([
+ 'state-indicator',
+ 'log.body',
+ 'attribute.body',
+ ]);
+ expect(byId.get('log.body')?.enableRemove).toBe(false);
+ expect(byId.get('attribute.body')?.enableRemove).toBe(true);
+ });
+
+ it('applies the same distinct-column treatment to timestamp variants', () => {
+ const { result } = renderHook(() =>
+ useLogsTableColumns({
+ fields: [field('timestamp', 'log'), field('timestamp', 'attribute')],
+ fontSize: FontSize.SMALL,
+ }),
+ );
+
+ const byId = new Map(result.current.map((c) => [c.id, c]));
+ expect(result.current.map((c) => c.id)).toStrictEqual([
+ 'state-indicator',
+ 'log.timestamp',
+ 'attribute.timestamp',
+ ]);
+ expect(byId.get('log.timestamp')?.enableRemove).toBe(false);
+ expect(byId.get('attribute.timestamp')?.enableRemove).toBe(true);
+ });
+
it('skips the synthetic "id" field name', () => {
const { result } = renderHook(() =>
useLogsTableColumns({
@@ -77,7 +112,11 @@ describe('useLogsTableColumns — selectColumns-order respected', () => {
it('uses the special body/timestamp coldefs (canBeHidden=false), not the generic user field def', () => {
const { result } = renderHook(() =>
useLogsTableColumns({
- fields: [field('body'), field('timestamp'), field('user_field')],
+ fields: [
+ field('body', 'log'),
+ field('timestamp', 'log'),
+ field('user_field'),
+ ],
fontSize: FontSize.SMALL,
}),
);
diff --git a/frontend/src/components/Logs/TableView/useLogsTableColumns.tsx b/frontend/src/components/Logs/TableView/useLogsTableColumns.tsx
index 8be971afe9..4e93217bc3 100644
--- a/frontend/src/components/Logs/TableView/useLogsTableColumns.tsx
+++ b/frontend/src/components/Logs/TableView/useLogsTableColumns.tsx
@@ -96,15 +96,18 @@ export function useLogsTableColumns({
),
});
+ // Match body/timestamp by composite key, not bare name — else a variant
+ // like `attribute.body` collapses onto `log.body`, duplicating the column.
const fieldCols = fields
.map((f): TableColumnDef<ILog> | null => {
if (f.name === 'id') {
return null;
}
- if (f.name === 'timestamp') {
+ const compositeKey = buildCompositeKey(f.name, f.type);
+ if (compositeKey === timestampCol.id) {
return timestampCol;
}
- if (f.name === 'body') {
+ if (compositeKey === bodyCol.id) {
return bodyCol;
}
return makeUserFieldCol(f);
diff --git a/frontend/src/container/OptionsMenu/__test__/ensureLogsRequiredColumns.test.ts b/frontend/src/container/OptionsMenu/__test__/ensureLogsRequiredColumns.test.ts
index ec7f11d643..20febba520 100644
--- a/frontend/src/container/OptionsMenu/__test__/ensureLogsRequiredColumns.test.ts
+++ b/frontend/src/container/OptionsMenu/__test__/ensureLogsRequiredColumns.test.ts
@@ -67,13 +67,23 @@ describe('ensureLogsRequiredColumns', () => {
]);
});
- it('does not duplicate if a required column appears twice in the input', () => {
- // Tolerant of malformed input — invariant only adds *missing* required
- // columns; it does not deduplicate existing entries (that's a separate
- // concern, not its job).
+ it('collapses composite-key duplicates in the input', () => {
+ // Two identical `body` entries → deduped to one, then timestamp prepended.
const input = [BODY, BODY, ATTR_A];
const result = ensureLogsRequiredColumns(input);
- expect(result.filter((c) => c.name === 'timestamp')).toHaveLength(1);
- expect(result[0]).toStrictEqual(TIMESTAMP);
+ expect(result).toStrictEqual([TIMESTAMP, BODY, ATTR_A]);
+ expect(result.filter((c) => c.name === 'body')).toHaveLength(1);
+ });
+
+ it('keeps same-name fields with different contexts as distinct columns', () => {
+ // Different composite keys → both legitimate, neither deduped.
+ const ATTR_BODY: TelemetryFieldKey = {
+ name: 'body',
+ signal: 'logs',
+ fieldContext: 'attribute',
+ fieldDataType: 'string',
+ };
+ const input = [TIMESTAMP, BODY, ATTR_BODY];
+ expect(ensureLogsRequiredColumns(input)).toStrictEqual(input);
});
});
diff --git a/frontend/src/container/OptionsMenu/constants.ts b/frontend/src/container/OptionsMenu/constants.ts
index 7a5fd43f34..86148e1525 100644
--- a/frontend/src/container/OptionsMenu/constants.ts
+++ b/frontend/src/container/OptionsMenu/constants.ts
@@ -2,6 +2,7 @@ import { TelemetryFieldKey } from 'api/v5/v5';
import { DataSource } from 'types/common/queryBuilder';
import { FontSize, OptionsQuery } from './types';
+import { buildCompositeKey } from './utils';
export const URL_OPTIONS = 'options';
@@ -35,22 +36,48 @@ export const defaultLogsSelectedColumns: TelemetryFieldKey[] = [
},
];
-export const LOGS_REQUIRED_COLUMNS = ['timestamp', 'body'] as const;
+// Names that must always be present in logs selectColumns (writer invariant).
+const LOGS_REQUIRED_COLUMN_NAMES = defaultLogsSelectedColumns.map(
+ (c) => c.name,
+);
-/**
- * Always-on invariant: every logs selectColumns array must contain `body` and
- * `timestamp`. Applied at both loader and writer boundaries so the picker, the
- * table, and persisted state can never diverge into a "missing required
- * column" state.
- */
+// Composite keys (not bare names) so the picker locks ONLY the canonical
+// `log.body`/`log.timestamp` — a same-name variant like `attribute.body` stays
+// removable.
+export const LOGS_REQUIRED_COLUMNS = defaultLogsSelectedColumns.map((c) =>
+ buildCompositeKey(c.name, c.fieldContext),
+);
+
+// Drop composite-key duplicates (never legitimate — they only come from
+// corrupted state). Returns the same array reference when nothing to dedupe.
+export function dedupeColumnsByCompositeKey(
+ columns: TelemetryFieldKey[],
+): TelemetryFieldKey[] {
+ const seen = new Set<string>();
+ let hasDuplicate = false;
+ const deduped = columns.filter((c) => {
+ const key = buildCompositeKey(c.name, c.fieldContext);
+ if (seen.has(key)) {
+ hasDuplicate = true;
+ return false;
+ }
+ seen.add(key);
+ return true;
+ });
+ return hasDuplicate ? deduped : columns;
+}
+
+// Logs selectColumns invariant: no composite-key duplicates, and body +
+// timestamp always present. Applied at loader + writer boundaries.
export function ensureLogsRequiredColumns(
columns: TelemetryFieldKey[],
): TelemetryFieldKey[] {
- const missing = LOGS_REQUIRED_COLUMNS.filter(
- (name) => !columns.some((c) => c.name === name),
+ const deduped = dedupeColumnsByCompositeKey(columns);
+ const missing = LOGS_REQUIRED_COLUMN_NAMES.filter(
+ (name) => !deduped.some((c) => c.name === name),
);
if (missing.length === 0) {
- return columns;
+ return deduped;
}
const defaultsByName = new Map(
defaultLogsSelectedColumns.map((c) => [c.name, c]),
@@ -58,7 +85,7 @@ export function ensureLogsRequiredColumns(
const prepended = missing
.map((name) => defaultsByName.get(name))
.filter((c): c is TelemetryFieldKey => c !== undefined);
- return [...prepended, ...columns];
+ return [...prepended, ...deduped];
}
export const defaultTraceSelectedColumns: TelemetryFieldKey[] = [