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
23 changes: 18 additions & 5 deletions genkit-tools/telemetry-server/src/localFileTraceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,31 @@ export class LocalFileTraceStore implements TraceStore {

async save(id: string, rawTrace: TraceData): Promise<void> {
let trace = this.filter(rawTrace);
if (Object.keys(trace.spans).length === 0) {
return;
}
// if everything is filtered, it's probably the root.
const possibleRoot = Object.keys(trace.spans).length === 0;
const mutex = this.getMutex(id);
await mutex.waitForUnlock();
const release = await mutex.acquire();
try {
const existing = await this.load(id);
const existing = (await this.load(id)) || trace;
if (existing) {
Object.keys(trace.spans).forEach(
(spanId) => (existing.spans[spanId] = trace.spans[spanId])
);
// If it's one of those weird roots (internal span that we filter) we try to fix
// whoever was referencing it by making them root.
if (possibleRoot) {
Object.keys(existing.spans).forEach((spanId) => {
const span = existing.spans[spanId];
if (
possibleRoot &&
span.parentSpanId &&
!existing.spans[span.parentSpanId]
) {
delete span.parentSpanId;
}
});
}
existing.displayName = trace.displayName;
existing.startTime = trace.startTime;
existing.endTime = trace.endTime;
Expand All @@ -136,7 +149,7 @@ export class LocalFileTraceStore implements TraceStore {
path.resolve(this.storeRoot, `${id}`),
JSON.stringify(trace)
);
const hasRootSpan = !!Object.values(rawTrace.spans).find(
const hasRootSpan = !!Object.values(trace.spans).find(
(s) => !s.parentSpanId
);
if (this.index && hasRootSpan) {
Expand Down
7 changes: 7 additions & 0 deletions genkit-tools/telemetry-server/tests/file_store_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,15 @@ describe('index', () => {
name: 'spanB',
start: 2345,
end: 3456,
status: 0,
},
{
id: TRACE_ID_1,
type: 'flow',
name: 'spanA',
start: 1234,
end: 2345,
status: 0,
},
]);
});
Expand Down Expand Up @@ -374,13 +376,15 @@ describe('index', () => {
name: 'flowA',
start: 1,
end: 2,
status: 0,
},
{
id: TRACE_ID_1,
type: 'banana',
name: 'flowA',
start: 1,
end: 2,
status: 0,
},
]
);
Expand All @@ -399,6 +403,7 @@ describe('index', () => {
name: 'flowA',
start: 1,
end: 2,
status: 0,
},
]
);
Expand All @@ -418,6 +423,7 @@ describe('index', () => {
name: 'flowA',
start: 1,
end: 2,
status: 0,
},
]
);
Expand Down Expand Up @@ -469,6 +475,7 @@ describe('index', () => {
name: 'flowA',
start: 1,
end: 2,
status: 0,
},
]
);
Expand Down
1 change: 1 addition & 0 deletions genkit-tools/telemetry-server/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function span(
instrumentationLibrary: { name: 'genkit' },
spanKind: 'INTERNAL',
attributes,
status: { code: 0 },
} as SpanData;
}

Expand Down
Loading