Skip to content

Commit 54ab8eb

Browse files
authored
fix(cli/telemetry-server): fixed trace duplication in the index which resulted in duplication in trace list in dev ui (#3619)
1 parent 437c85b commit 54ab8eb

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

genkit-tools/telemetry-server/src/localFileTraceStore.ts renamed to genkit-tools/telemetry-server/src/file-trace-store.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import {
18+
SpanData,
1819
TraceDataSchema,
1920
type TraceData,
2021
type TraceQueryFilter,
@@ -29,6 +30,7 @@ import { version as currentVersion } from './utils/version';
2930

3031
const MAX_TRACES = 1000;
3132
const MAX_INDEX_FILES = 10;
33+
const MAX_LIST_ATTR_LENGTH = 1000;
3234

3335
/**
3436
* Implementation of trace store that persists traces on local disk.
@@ -148,7 +150,7 @@ export class LocalFileTraceStore implements TraceStore {
148150
path.resolve(this.storeRoot, `${id}`),
149151
JSON.stringify(trace)
150152
);
151-
const hasRootSpan = !!Object.values(trace.spans).find(
153+
const hasRootSpan = !!Object.values(rawTrace.spans).find(
152154
(s) => !s.parentSpanId
153155
);
154156
if (this.index && hasRootSpan) {
@@ -176,7 +178,7 @@ export class LocalFileTraceStore implements TraceStore {
176178
});
177179

178180
const loadedTraces = await Promise.all(
179-
searchResult.data.map((d) => this.load(d['id']))
181+
searchResult.data.map((d) => this.load(d['id']).then(trucateTraceDetails))
180182
);
181183

182184
return {
@@ -238,6 +240,48 @@ export class LocalFileTraceStore implements TraceStore {
238240
}
239241
}
240242

243+
function trucateTraceDetails(t?: TraceData): TraceData | undefined {
244+
if (!t) return t;
245+
246+
const { spans: originalSpans, ...restOfTrace } = t;
247+
248+
const spans = {} as Record<string, SpanData>;
249+
for (const spanId of Object.keys(originalSpans)) {
250+
if (!originalSpans[spanId].parentSpanId) {
251+
const { attributes: originalAttributes, ...restOfSpan } =
252+
originalSpans[spanId];
253+
spans[spanId] = {
254+
attributes: trucateLargeAttrs(originalAttributes),
255+
...restOfSpan,
256+
} as SpanData;
257+
break;
258+
}
259+
}
260+
261+
return { spans, ...restOfTrace };
262+
}
263+
264+
export function trucateLargeAttrs<T>(input: T): T {
265+
if (
266+
input === undefined ||
267+
input === null ||
268+
Array.isArray(input) ||
269+
typeof input !== 'object'
270+
) {
271+
return input;
272+
}
273+
for (const key in input) {
274+
if (
275+
typeof input[key] === 'string' &&
276+
(input[key] as string).length > MAX_LIST_ATTR_LENGTH
277+
) {
278+
input[key] = ((input[key] as string).substring(0, MAX_LIST_ATTR_LENGTH) +
279+
'...') as any;
280+
}
281+
}
282+
return input;
283+
}
284+
241285
export interface IndexSearchResult {
242286
pageLastIndex?: number;
243287
data: Record<string, string>[];

genkit-tools/telemetry-server/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import express from 'express';
2323
import type * as http from 'http';
2424
import type { TraceStore } from './types';
2525

26-
export { LocalFileTraceStore } from './localFileTraceStore.js';
26+
export { LocalFileTraceStore } from './file-trace-store.js';
2727
export { TraceQuerySchema, type TraceQuery, type TraceStore } from './types';
2828

2929
let server: http.Server;

0 commit comments

Comments
 (0)