Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 25x 12x 25x 13x 25x 25x 25x 25x 25x 1x 1x 1x 1x 23x 23x 23x 1x 23x 22x 23x 23x 23x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 21x 21x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 11x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 13x 13x 13x 13x 13x 13x 2x 2x 2x 13x 13x 13x 13x | 'use client';
import React, { useState } from 'react';
import { formatDuration } from '@/lib/monitoring/percentiles';
export interface SpanNode {
id: string;
parentId: string | null;
name: string;
kind: string;
startTime: string;
endTime: string;
duration: number;
status: string;
attributes: Record<string, unknown> | null;
events: Array<{
name: string;
timestamp: string;
attributes?: Record<string, unknown>;
}> | null;
relativeStart: number;
depth: number;
children: SpanNode[];
}
export interface TraceInfo {
id: string;
name: string;
serviceName: string;
startTime: string;
endTime: string;
duration: number;
status: string;
userId?: number | null;
requestId?: string | null;
}
export interface TraceStats {
totalSpans: number;
errorCount: number;
dbSpanCount: number;
httpSpanCount: number;
avgSpanDuration: number;
maxSpanDuration: number;
dbTime: number;
httpTime: number;
}
export interface TraceWaterfallProps {
/** Trace information */
trace: TraceInfo;
/** Flat list of spans for waterfall rendering */
flatSpans: SpanNode[];
/** Trace statistics */
stats?: TraceStats;
/** Loading state */
isLoading?: boolean;
/** Callback when a span is selected */
onSelectSpan?: (span: SpanNode) => void;
/** Currently selected span ID */
selectedSpanId?: string;
/** Additional CSS classes */
className?: string;
}
/**
* Get kind badge color
*/
function getKindColor(kind: string): string {
switch (kind) {
case 'server':
return 'bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-400';
case 'client':
return 'bg-purple-100 text-purple-800 dark:bg-purple-900/30 dark:text-purple-400';
case 'producer':
return 'bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400';
case 'consumer':
return 'bg-orange-100 text-orange-800 dark:bg-orange-900/30 dark:text-orange-400';
default:
return 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300';
}
}
/**
* Get bar color based on status
*/
function getBarColor(status: string): string {
switch (status) {
case 'error':
return 'bg-red-500';
case 'ok':
return 'bg-blue-500';
default:
return 'bg-gray-400';
}
}
/**
* SpanDetailPanel - Shows details for a selected span
*/
function SpanDetailPanel({ span }: { span: SpanNode }) {
return (
<div className="bg-gray-50 dark:bg-gray-900/50 rounded-lg p-4 border border-gray-200 dark:border-gray-700">
<h4 className="font-semibold text-gray-900 dark:text-white mb-3">{span.name}</h4>
<div className="grid grid-cols-2 gap-4 text-sm mb-4">
<div>
<span className="text-gray-500 dark:text-gray-400">Kind:</span>
<span className={`ml-2 px-2 py-0.5 text-xs font-medium rounded ${getKindColor(span.kind)}`}>
{span.kind}
</span>
</div>
<div>
<span className="text-gray-500 dark:text-gray-400">Status:</span>
<span className={`ml-2 ${span.status === 'error' ? 'text-red-600' : 'text-green-600'}`}>
{span.status}
</span>
</div>
<div>
<span className="text-gray-500 dark:text-gray-400">Duration:</span>
<span className="ml-2 text-gray-900 dark:text-white font-medium">
{formatDuration(span.duration)}
</span>
</div>
<div>
<span className="text-gray-500 dark:text-gray-400">Span ID:</span>
<span className="ml-2 font-mono text-xs text-gray-600 dark:text-gray-400">
{span.id}
</span>
</div>
</div>
{/* Attributes */}
{span.attributes && Object.keys(span.attributes).length > 0 && (
<div className="mb-4">
<h5 className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Attributes</h5>
<div className="bg-white dark:bg-gray-800 rounded border border-gray-200 dark:border-gray-700 p-2 max-h-32 overflow-auto">
<pre className="text-xs text-gray-600 dark:text-gray-400 whitespace-pre-wrap">
{JSON.stringify(span.attributes, null, 2)}
</pre>
</div>
</div>
)}
{/* Events */}
{span.events && span.events.length > 0 && (
<div>
<h5 className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Events</h5>
<div className="space-y-2">
{span.events.map((event, idx) => (
<div
key={idx}
className="bg-white dark:bg-gray-800 rounded border border-gray-200 dark:border-gray-700 p-2"
>
<div className="flex items-center justify-between text-xs mb-1">
<span className="font-medium text-gray-900 dark:text-white">{event.name}</span>
<span className="text-gray-500 dark:text-gray-400">
{new Date(event.timestamp).toLocaleTimeString()}
</span>
</div>
{event.attributes && (
<pre className="text-xs text-gray-600 dark:text-gray-400 whitespace-pre-wrap">
{JSON.stringify(event.attributes, null, 2)}
</pre>
)}
</div>
))}
</div>
</div>
)}
</div>
);
}
/**
* TraceWaterfall - Visualizes spans in a waterfall diagram
*/
export function TraceWaterfall({
trace,
flatSpans,
stats,
isLoading = false,
onSelectSpan,
selectedSpanId,
className = '',
}: TraceWaterfallProps) {
const [hoveredSpanId, setHoveredSpanId] = useState<string | null>(null);
const selectedSpan = flatSpans.find((s) => s.id === selectedSpanId);
if (isLoading) {
return (
<div
className={`bg-white dark:bg-gray-800 rounded-lg shadow p-4 ${className}`}
data-testid="trace-waterfall"
>
<div className="h-8 w-64 bg-gray-200 dark:bg-gray-700 rounded animate-pulse mb-4" />
<div className="space-y-2">
{[1, 2, 3, 4, 5].map((i) => (
<div key={i} className="h-10 bg-gray-200 dark:bg-gray-700 rounded animate-pulse" />
))}
</div>
</div>
);
}
return (
<div
className={`bg-white dark:bg-gray-800 rounded-lg shadow ${className}`}
data-testid="trace-waterfall"
>
{/* Header */}
<div className="p-4 border-b border-gray-200 dark:border-gray-700">
<div className="flex items-start justify-between">
<div>
<h3 className="text-lg font-semibold text-gray-900 dark:text-white">{trace.name}</h3>
<div className="flex items-center gap-4 text-sm text-gray-500 dark:text-gray-400 mt-1">
<span>{trace.serviceName}</span>
<span
className={`px-2 py-0.5 text-xs font-medium rounded ${
trace.status === 'error'
? 'bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400'
: 'bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400'
}`}
>
{trace.status}
</span>
<span className="font-mono text-xs">{trace.id}</span>
</div>
</div>
<div className="text-right">
<div className="text-2xl font-bold text-gray-900 dark:text-white">
{formatDuration(trace.duration)}
</div>
<div className="text-sm text-gray-500 dark:text-gray-400">
{flatSpans.length} spans
</div>
</div>
</div>
{/* Stats */}
{stats && (
<div className="grid grid-cols-4 gap-4 mt-4 pt-4 border-t border-gray-200 dark:border-gray-700">
<div>
<div className="text-xs text-gray-500 dark:text-gray-400">DB Time</div>
<div className="font-medium text-gray-900 dark:text-white">
{formatDuration(stats.dbTime)} ({stats.dbSpanCount} queries)
</div>
</div>
<div>
<div className="text-xs text-gray-500 dark:text-gray-400">HTTP Time</div>
<div className="font-medium text-gray-900 dark:text-white">
{formatDuration(stats.httpTime)} ({stats.httpSpanCount} calls)
</div>
</div>
<div>
<div className="text-xs text-gray-500 dark:text-gray-400">Errors</div>
<div
className={`font-medium ${
stats.errorCount > 0 ? 'text-red-600 dark:text-red-400' : 'text-green-600'
}`}
>
{stats.errorCount}
</div>
</div>
<div>
<div className="text-xs text-gray-500 dark:text-gray-400">Max Span</div>
<div className="font-medium text-gray-900 dark:text-white">
{formatDuration(stats.maxSpanDuration)}
</div>
</div>
</div>
)}
</div>
{/* Waterfall visualization */}
<div className="p-4">
<div className="space-y-1">
{flatSpans.map((span) => {
const widthPercent = (span.duration / trace.duration) * 100;
const leftPercent = (span.relativeStart / trace.duration) * 100;
const isSelected = selectedSpanId === span.id;
const isHovered = hoveredSpanId === span.id;
return (
<button
key={span.id}
onClick={() => onSelectSpan?.(span)}
onMouseEnter={() => setHoveredSpanId(span.id)}
onMouseLeave={() => setHoveredSpanId(null)}
className={`w-full text-left transition-colors ${
isSelected
? 'bg-blue-50 dark:bg-blue-900/20'
: isHovered
? 'bg-gray-50 dark:bg-gray-700/50'
: ''
}`}
data-testid={`span-${span.id}`}
>
<div className="flex items-center gap-2 py-1">
{/* Span name with indentation */}
<div
className="flex items-center gap-1 min-w-[200px] max-w-[300px]"
style={{ paddingLeft: `${span.depth * 16}px` }}
>
{span.depth > 0 && (
<span className="text-gray-300 dark:text-gray-600">{'└─'}</span>
)}
<span
className={`px-1.5 py-0.5 text-xs font-medium rounded ${getKindColor(span.kind)}`}
>
{span.kind.charAt(0).toUpperCase()}
</span>
<span
className={`text-sm truncate ${
span.status === 'error' ? 'text-red-600 dark:text-red-400' : 'text-gray-900 dark:text-white'
}`}
title={span.name}
>
{span.name}
</span>
</div>
{/* Timeline bar */}
<div className="flex-1 h-6 bg-gray-100 dark:bg-gray-700 rounded relative overflow-hidden">
<div
className={`absolute h-full rounded ${getBarColor(span.status)} ${
isSelected || isHovered ? 'opacity-100' : 'opacity-75'
}`}
style={{
left: `${leftPercent}%`,
width: `${Math.max(widthPercent, 0.5)}%`,
}}
/>
</div>
{/* Duration */}
<div className="text-xs font-mono text-gray-600 dark:text-gray-400 w-16 text-right">
{formatDuration(span.duration)}
</div>
</div>
</button>
);
})}
</div>
</div>
{/* Selected span details */}
{selectedSpan && (
<div className="border-t border-gray-200 dark:border-gray-700 p-4">
<SpanDetailPanel span={selectedSpan} />
</div>
)}
</div>
);
}
|