|
| 1 | +/** |
| 2 | + * Maximum length for string values in display output |
| 3 | + * Prevents database storage issues with very large trace spans |
| 4 | + */ |
| 5 | +const MAX_STRING_LENGTH = 10000 |
| 6 | + |
| 7 | +/** |
| 8 | + * Truncates a string if it exceeds the maximum length |
| 9 | + */ |
| 10 | +function truncateString(value: string, maxLength = MAX_STRING_LENGTH): string { |
| 11 | + if (value.length <= maxLength) { |
| 12 | + return value |
| 13 | + } |
| 14 | + return `${value.substring(0, maxLength)}... [truncated ${value.length - maxLength} chars]` |
| 15 | +} |
| 16 | + |
1 | 17 | /** |
2 | 18 | * Type guard to check if an object is a UserFile |
3 | 19 | */ |
@@ -48,16 +64,28 @@ const DISPLAY_FILTERS = [ |
48 | 64 | /** |
49 | 65 | * Generic helper to filter internal/technical fields from data for cleaner display in logs and console. |
50 | 66 | * Applies all registered filters recursively to the data structure. |
| 67 | + * Also truncates long strings to prevent database storage issues. |
51 | 68 | * |
52 | 69 | * To add a new filter: |
53 | 70 | * 1. Create a filter function that checks and transforms a specific data type |
54 | 71 | * 2. Add it to the DISPLAY_FILTERS array above |
55 | 72 | * |
56 | 73 | * @param data - Data to filter (objects, arrays, primitives) |
57 | | - * @returns Filtered data with internal fields removed |
| 74 | + * @returns Filtered data with internal fields removed and long strings truncated |
58 | 75 | */ |
59 | 76 | export function filterForDisplay(data: any): any { |
60 | | - if (!data || typeof data !== 'object') { |
| 77 | + // Handle null/undefined |
| 78 | + if (data === null || data === undefined) { |
| 79 | + return data |
| 80 | + } |
| 81 | + |
| 82 | + // Truncate long strings |
| 83 | + if (typeof data === 'string') { |
| 84 | + return truncateString(data) |
| 85 | + } |
| 86 | + |
| 87 | + // Return primitives as-is |
| 88 | + if (typeof data !== 'object') { |
61 | 89 | return data |
62 | 90 | } |
63 | 91 |
|
|
0 commit comments