Skip to content

Commit

Permalink
Prevent prompt rendering from spamming local trace store (#60)
Browse files Browse the repository at this point in the history
- Provides attribute-based filtering and default filters
  in the LocalFileTraceStore implementation.
  • Loading branch information
maxl0rd authored May 7, 2024
1 parent 5f1658c commit ecfa890
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions js/core/src/tracing/localFileTraceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ import {
export class LocalFileTraceStore implements TraceStore {
private readonly storeRoot;
private mutexes: Record<string, Mutex> = {};
private filters: Record<string, string>;

constructor() {
static defaultFilters: Record<string, string> = {
// Prevent prompt rendering from spamming local trace store
'genkit:metadata:subtype': 'prompt',
};

constructor(filters = LocalFileTraceStore.defaultFilters) {
const rootHash = crypto
.createHash('md5')
.update(require?.main?.filename || 'unknown')
Expand All @@ -45,6 +51,7 @@ export class LocalFileTraceStore implements TraceStore {
logger.info(
`Initialized local file trace store at root: ${this.storeRoot}`
);
this.filters = filters;
}

async load(id: string): Promise<TraceData | undefined> {
Expand All @@ -68,7 +75,11 @@ export class LocalFileTraceStore implements TraceStore {
return this.mutexes[id];
}

async save(id: string, trace: TraceData): Promise<void> {
async save(id: string, rawTrace: TraceData): Promise<void> {
let trace = this.filter(rawTrace);
if (Object.keys(trace.spans).length === 0) {
return;
}
const mutex = this.getMutex(id);
await mutex.waitForUnlock();
const release = await mutex.acquire();
Expand Down Expand Up @@ -126,4 +137,26 @@ export class LocalFileTraceStore implements TraceStore {
continuationToken: files.length > stopAt ? stopAt.toString() : undefined,
};
}

private filter(trace: TraceData): TraceData {
// Delete any spans that match the filter criteria
Object.keys(trace.spans).forEach((spanId) => {
const span = trace.spans[spanId];
Object.keys(this.filters).forEach((f) => {
if (span.attributes[f] === this.filters[f]) {
delete trace.spans[spanId];
}
});
});
// Delete the root wrapper if it's the only span left
if (Object.keys(trace.spans).length === 1) {
Object.keys(trace.spans).forEach((spanId) => {
const span = trace.spans[spanId];
if (span.attributes['genkit:name'] === 'dev-run-action-wrapper') {
delete trace.spans[spanId];
}
});
}
return trace;
}
}

0 comments on commit ecfa890

Please sign in to comment.