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.

2.7 KB
Raw Blame
import { ReactNode } from 'react';
import { Badge, type BadgeColorType } from '@signozhq/ui/badge';
import { LogType } from 'components/Logs/LogStateIndicator/LogStateIndicator';
import { getLogIndicatorType } from 'components/Logs/LogStateIndicator/utils';
import { ILog } from 'types/api/logs/log';

import styles from './LogHighlights.module.scss';
import TraceIdField from './TraceIdField';

// Severity badge color mirrors the LogStateIndicator bar
const SEVERITY_COLOR: Record<string, BadgeColorType> = {
	[LogType.TRACE]: 'success',
	[LogType.DEBUG]: 'info',
	[LogType.INFO]: 'primary',
	[LogType.WARN]: 'warning',
	[LogType.ERROR]: 'danger',
	[LogType.FATAL]: 'highlight-danger',
};

export interface LogHighlightConfig {
	key: string;
	label: string;
	render: (log: ILog) => ReactNode | null;
}

// Resource/attribute lookup (keys like `service.name` live in resources_string,
// occasionally attributes_string). Typed loosely as these are string maps.
const getAttr = (log: ILog, key: string): string =>
	(log.resources_string as unknown as Record<string, string>)?.[key] ||
	(log.attributes_string as unknown as Record<string, string>)?.[key] ||
	'';

const valueBadge = (
	value: string,
	options?: { prefix?: ReactNode; color?: BadgeColorType },
): ReactNode => (
	<Badge
		variant="solid"
		color={options?.color ?? 'secondary'}
		textTransform="none"
	>
		{options?.prefix}
		<span className={styles.badgeText} title={value}>
			{value}
		</span>
	</Badge>
);

export const LOG_HIGHLIGHTS: LogHighlightConfig[] = [
	{
		key: 'service',
		label: 'SERVICE',
		render: (log): ReactNode | null => {
			const value = getAttr(log, 'service.name');
			return value
				? valueBadge(value, {
						prefix: <span className={styles.serviceDot} />,
					})
				: null;
		},
	},
	{
		key: 'severity',
		label: 'SEVERITY',
		render: (log): ReactNode | null => {
			if (!log.severity_text) {
				return null;
			}
			return valueBadge(log.severity_text, {
				color: SEVERITY_COLOR[getLogIndicatorType(log)] ?? 'vanilla',
			});
		},
	},
	{
		key: 'namespace',
		label: 'NAMESPACE',
		render: (log): ReactNode | null => {
			const value = getAttr(log, 'service.namespace');
			return value ? valueBadge(value) : null;
		},
	},
	{
		key: 'environment',
		label: 'ENVIRONMENT',
		render: (log): ReactNode | null => {
			const value = getAttr(log, 'deployment.environment');
			return value ? valueBadge(value) : null;
		},
	},
	{
		key: 'traceId',
		label: 'TRACE ID',
		render: (log): ReactNode | null => {
			const traceId = log.trace_id || log.traceId;
			return traceId ? <TraceIdField traceId={traceId} /> : null;
		},
	},
	{
		key: 'spanId',
		label: 'SPAN ID',
		render: (log): ReactNode | null => {
			const spanId = log.span_id || log.spanID;
			return spanId ? valueBadge(spanId) : null;
		},
	},
];