Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import {
SpanData,
TraceDataSchema,
type TraceData,
type TraceQueryFilter,
Expand All @@ -29,6 +30,7 @@ import { version as currentVersion } from './utils/version';

const MAX_TRACES = 1000;
const MAX_INDEX_FILES = 10;
const MAX_LIST_ATTR_LENGTH = 1000;

/**
* Implementation of trace store that persists traces on local disk.
Expand Down Expand Up @@ -148,7 +150,7 @@ export class LocalFileTraceStore implements TraceStore {
path.resolve(this.storeRoot, `${id}`),
JSON.stringify(trace)
);
const hasRootSpan = !!Object.values(trace.spans).find(
const hasRootSpan = !!Object.values(rawTrace.spans).find(
(s) => !s.parentSpanId
);
if (this.index && hasRootSpan) {
Expand Down Expand Up @@ -176,7 +178,7 @@ export class LocalFileTraceStore implements TraceStore {
});

const loadedTraces = await Promise.all(
searchResult.data.map((d) => this.load(d['id']))
searchResult.data.map((d) => this.load(d['id']).then(trucateTraceDetails))
);

return {
Expand Down Expand Up @@ -238,6 +240,48 @@ export class LocalFileTraceStore implements TraceStore {
}
}

function trucateTraceDetails(t?: TraceData): TraceData | undefined {
if (!t) return t;

const { spans: originalSpans, ...restOfTrace } = t;

const spans = {} as Record<string, SpanData>;
for (const spanId of Object.keys(originalSpans)) {
if (!originalSpans[spanId].parentSpanId) {
const { attributes: originalAttributes, ...restOfSpan } =
originalSpans[spanId];
spans[spanId] = {
attributes: trucateLargeAttrs(originalAttributes),
...restOfSpan,
} as SpanData;
break;
}
}

return { spans, ...restOfTrace };
}

export function trucateLargeAttrs<T>(input: T): T {
if (
input === undefined ||
input === null ||
Array.isArray(input) ||
typeof input !== 'object'
) {
return input;
}
for (const key in input) {
if (
typeof input[key] === 'string' &&
(input[key] as string).length > MAX_LIST_ATTR_LENGTH
) {
input[key] = ((input[key] as string).substring(0, MAX_LIST_ATTR_LENGTH) +
'...') as any;
}
}
return input;
}

export interface IndexSearchResult {
pageLastIndex?: number;
data: Record<string, string>[];
Expand Down
2 changes: 1 addition & 1 deletion genkit-tools/telemetry-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import express from 'express';
import type * as http from 'http';
import type { TraceStore } from './types';

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

let server: http.Server;
Expand Down
Loading