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 { useCallback, useMemo, useState } from 'react';
import { AppLink } from 'lib/router/AppLink';
import { buildRoutePath } from 'lib/router/buildRoutePath';
import { ChevronDown, ChevronRight } from '@signozhq/icons';
import { Badge } from '@signozhq/ui/badge';
import ROUTES from 'constants/routes';
import KeyValueLabel from 'periscope/components/KeyValueLabel';

import styles from './LinkedSpans.module.scss';

interface SpanReference {
	traceId: string;
	spanId: string;
	refType: string;
}

interface LinkedSpansProps {
	// eslint-disable-next-line @typescript-eslint/no-explicit-any
	references: any;
}

interface LinkedSpansState {
	linkedSpans: SpanReference[];
	count: number;
	isOpen: boolean;
	toggleOpen: () => void;
}

export function useLinkedSpans(references: any): LinkedSpansState {
	const [isOpen, setIsOpen] = useState(false);

	const linkedSpans: SpanReference[] = useMemo(
		() =>
			(references || []).filter(
				(ref: SpanReference) => ref.refType !== 'CHILD_OF',
			),
		[references],
	);

	const toggleOpen = useCallback(() => setIsOpen((prev) => !prev), []);

	return {
		linkedSpans,
		count: linkedSpans.length,
		isOpen,
		toggleOpen,
	};
}

export function LinkedSpansToggle({
	count,
	isOpen,
	toggleOpen,
}: {
	count: number;
	isOpen: boolean;
	toggleOpen: () => void;
}): JSX.Element {
	if (count === 0) {
		return <span className={styles.label}>0 linked spans</span>;
	}

	return (
		<button type="button" className={styles.toggle} onClick={toggleOpen}>
			<span className={styles.label}>
				{count} linked span{count !== 1 ? 's' : ''}
			</span>
			{isOpen ? <ChevronDown size={12} /> : <ChevronRight size={12} />}
		</button>
	);
}

export function LinkedSpansPanel({
	linkedSpans,
	isOpen,
}: {
	linkedSpans: SpanReference[];
	isOpen: boolean;
}): JSX.Element | null {
	const getLink = useCallback(
		(item: SpanReference): string =>
			`${buildRoutePath(ROUTES.TRACE_DETAIL, { id: item.traceId })}?spanId=${item.spanId}`,
		[],
	);

	if (!isOpen || linkedSpans.length === 0) {
		return null;
	}

	return (
		<div className={styles.list}>
			{linkedSpans.map((item) => (
				<KeyValueLabel
					key={item.spanId}
					badgeKey="Linked Span ID"
					badgeValue={
						<AppLink to={getLink(item)}>
							<Badge textTransform="none" variant="solid" color="secondary">
								{item.spanId}
							</Badge>
						</AppLink>
					}
					direction="column"
				/>
			))}
		</div>
	);
}

function LinkedSpans({ references }: LinkedSpansProps): JSX.Element {
	const { linkedSpans, count, isOpen, toggleOpen } = useLinkedSpans(references);

	return (
		<div className={styles.root}>
			<LinkedSpansToggle count={count} isOpen={isOpen} toggleOpen={toggleOpen} />
			<LinkedSpansPanel linkedSpans={linkedSpans} isOpen={isOpen} />
		</div>
	);
}

export default LinkedSpans;