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.

feat(logs-explorer): resolve body json fields in logs table columns (#12503)

Aditya Singha355996a5d1e
diff --git a/frontend/src/components/Logs/TableView/__tests__/useLogsTableColumns.test.tsx b/frontend/src/components/Logs/TableView/__tests__/useLogsTableColumns.test.tsx
index 41acebeef0..9f433f1ef9 100644
--- a/frontend/src/components/Logs/TableView/__tests__/useLogsTableColumns.test.tsx
+++ b/frontend/src/components/Logs/TableView/__tests__/useLogsTableColumns.test.tsx
@@ -10,6 +10,10 @@ jest.mock('providers/Timezone', () => ({
}),
}));
+jest.mock('providers/App/App', () => ({
+ useAppContext: (): { featureFlags: [] } => ({ featureFlags: [] }),
+}));
+
const field = (name: string, type = ''): IField => ({
name,
type,
diff --git a/frontend/src/components/Logs/TableView/useLogsTableColumns.tsx b/frontend/src/components/Logs/TableView/useLogsTableColumns.tsx
index 4e93217bc3..f3a52bb0fd 100644
--- a/frontend/src/components/Logs/TableView/useLogsTableColumns.tsx
+++ b/frontend/src/components/Logs/TableView/useLogsTableColumns.tsx
@@ -2,13 +2,15 @@ import type { ReactElement } from 'react';
import { useMemo } from 'react';
import TanStackTable from 'components/TanStackTableView';
import { DATE_TIME_FORMATS } from 'constants/dateTimeFormats';
+import { FeatureKeys } from 'constants/features';
import {
getBodyDisplayString,
getSanitizedLogBody,
} from 'container/LogDetailedView/utils';
import { FontSize } from 'container/OptionsMenu/types';
import { buildCompositeKey } from 'container/OptionsMenu/utils';
-import { FlatLogData } from 'lib/logs/flatLogData';
+import { getLogFieldValue } from 'lib/logs/flatLogData';
+import { useAppContext } from 'providers/App/App';
import { useTimezone } from 'providers/Timezone';
import { IField } from 'types/api/logs/fields';
import { ILog } from 'types/api/logs/log';
@@ -26,6 +28,10 @@ export function useLogsTableColumns({
fontSize,
}: UseLogsTableColumnsProps): TableColumnDef<ILog>[] {
const { formatTimezoneAdjustedTimestamp } = useTimezone();
+ const { featureFlags } = useAppContext();
+ const isBodyJsonEnabled =
+ featureFlags?.find((flag) => flag.name === FeatureKeys.USE_JSON_BODY)
+ ?.active || false;
return useMemo<TableColumnDef<ILog>[]>(() => {
const stateIndicatorCol: TableColumnDef<ILog> = {
@@ -88,7 +94,8 @@ export function useLogsTableColumns({
const makeUserFieldCol = (f: IField): TableColumnDef<ILog> => ({
id: buildCompositeKey(f.name, f.type),
header: f.name,
- accessorFn: (log): unknown => FlatLogData(log)[f.name],
+ accessorFn: (log): unknown =>
+ getLogFieldValue(log, f.name, isBodyJsonEnabled),
enableRemove: true,
width: { min: 192 },
cell: ({ value }): ReactElement => (
@@ -115,5 +122,5 @@ export function useLogsTableColumns({
.filter((c): c is TableColumnDef<ILog> => c !== null);
return [stateIndicatorCol, ...fieldCols];
- }, [fields, fontSize, formatTimezoneAdjustedTimestamp]);
+ }, [fields, fontSize, formatTimezoneAdjustedTimestamp, isBodyJsonEnabled]);
}
diff --git a/frontend/src/lib/logs/flatLogData.test.ts b/frontend/src/lib/logs/flatLogData.test.ts
new file mode 100644
index 0000000000..647b9c30c6
--- /dev/null
+++ b/frontend/src/lib/logs/flatLogData.test.ts
@@ -0,0 +1,55 @@
+import { ILog } from 'types/api/logs/log';
+
+import { getLogFieldValue } from './flatLogData';
+
+const asLog = (partial: Partial<ILog>): ILog => partial as unknown as ILog;
+
+describe('getLogFieldValue', () => {
+ it('resolves a nested body field by dotted key when use_json_body is on', () => {
+ const log = asLog({ body: { a: { b: { c: 'deep' } } } });
+ expect(getLogFieldValue(log, 'a.b.c', true)).toBe('deep');
+ });
+
+ it('ignores body when use_json_body is off', () => {
+ const log = asLog({ body: { a: { b: { c: 'deep' } } } });
+ expect(getLogFieldValue(log, 'a.b.c', false)).toBeUndefined();
+ });
+
+ it('ignores a stringified body even when use_json_body is on', () => {
+ const log = asLog({ body: '{"a":{"b":1}}' });
+ expect(getLogFieldValue(log, 'a.b', true)).toBeUndefined();
+ });
+
+ it('prefers the body value over attributes when the key exists in both (body first)', () => {
+ const log = asLog({
+ attributes_string: { 'a.b': 'attr' } as never,
+ body: { a: { b: 'bodyval' } },
+ });
+ expect(getLogFieldValue(log, 'a.b', true)).toBe('bodyval');
+ });
+
+ it('falls back to attributes when the key is not in the body', () => {
+ const log = asLog({
+ attributes_string: { 'x.y': 'attr' } as never,
+ body: { other: 1 },
+ });
+ expect(getLogFieldValue(log, 'x.y', true)).toBe('attr');
+ });
+
+ it('preserves falsy body values (0, false, empty string)', () => {
+ const log = asLog({ body: { n: 0, flag: false, s: '' } });
+ expect(getLogFieldValue(log, 'n', true)).toBe(0);
+ expect(getLogFieldValue(log, 'flag', true)).toBe(false);
+ expect(getLogFieldValue(log, 's', true)).toBe('');
+ });
+
+ it('returns undefined when the body path is missing', () => {
+ const log = asLog({ body: { x: 1 } });
+ expect(getLogFieldValue(log, 'nope', true)).toBeUndefined();
+ });
+
+ it('returns undefined when a mid path segment is not an object', () => {
+ const log = asLog({ body: { a: { b: 'leaf' } } });
+ expect(getLogFieldValue(log, 'a.b.c', true)).toBeUndefined();
+ });
+});
diff --git a/frontend/src/lib/logs/flatLogData.ts b/frontend/src/lib/logs/flatLogData.ts
index dfb59b9c68..f968009100 100644
--- a/frontend/src/lib/logs/flatLogData.ts
+++ b/frontend/src/lib/logs/flatLogData.ts
@@ -1,5 +1,5 @@
import { defaultTo } from 'lodash-es';
-import { ILog } from 'types/api/logs/log';
+import { ILog, ILogBody } from 'types/api/logs/log';
export function FlatLogData(log: ILog): Record<string, string> {
const flattenLogObject: Record<string, string> = {};
@@ -15,3 +15,29 @@ export function FlatLogData(log: ILog): Record<string, string> {
});
return flattenLogObject;
}
+
+function getBodyFieldValue(body: ILogBody, key: string): unknown {
+ return key.split('.').reduce<unknown>((acc, segment) => {
+ if (acc && typeof acc === 'object' && !Array.isArray(acc)) {
+ return (acc as Record<string, unknown>)[segment];
+ }
+ return undefined;
+ }, body);
+}
+
+// Resolve one field for the logs table. A JSON body is checked first (use_json_body
+// only), splitting the key on `.`; otherwise fall back to FlatLogData
+// (attributes/resources/scope/top-level).
+export function getLogFieldValue(
+ log: ILog,
+ fieldName: string,
+ isBodyJsonEnabled: boolean,
+): unknown {
+ if (isBodyJsonEnabled && log.body && typeof log.body === 'object') {
+ const bodyValue = getBodyFieldValue(log.body, fieldName);
+ if (bodyValue !== undefined) {
+ return bodyValue;
+ }
+ }
+ return FlatLogData(log)[fieldName];
+}