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
14 changes: 7 additions & 7 deletions packages/sdk/server-ai/__tests__/LDGraphTrackerImpl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,25 @@ it('drops trackInvocationFailure after trackInvocationSuccess and warns', () =>
});

// ---------------------------------------------------------------------------
// trackLatency – at-most-once
// trackDuration – at-most-once
// ---------------------------------------------------------------------------

it('trackLatency sets durationMs and emits event', () => {
it('trackDuration sets durationMs and emits event', () => {
const tracker = makeTracker('r');
tracker.trackLatency(1234);
tracker.trackDuration(1234);
expect(tracker.getSummary().durationMs).toBe(1234);
expect(mockTrack).toHaveBeenCalledWith(
'$ld:ai:graph:latency',
'$ld:ai:graph:duration:total',
testContext,
tracker.getTrackData(),
1234,
);
});

it('drops second trackLatency call and warns', () => {
it('drops second trackDuration call and warns', () => {
const tracker = makeTracker('r');
tracker.trackLatency(100);
tracker.trackLatency(200);
tracker.trackDuration(100);
tracker.trackDuration(200);
expect(mockTrack).toHaveBeenCalledTimes(1);
expect(tracker.getSummary().durationMs).toBe(100);
expect(mockWarn).toHaveBeenCalled();
Expand Down
11 changes: 8 additions & 3 deletions packages/sdk/server-ai/src/LDGraphTrackerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,20 @@ export class LDGraphTrackerImpl implements LDGraphTracker {
this._ldClient.track('$ld:ai:graph:invocation_failure', this._context, this.getTrackData(), 1);
}

trackLatency(durationMs: number): void {
trackDuration(durationMs: number): void {
if (this._summary.durationMs !== undefined) {
this._ldClient.logger?.warn(
'LDGraphTracker: trackLatency already called for this run — dropping duplicate call.',
'LDGraphTracker: trackDuration already called for this run — dropping duplicate call.',
);
return;
}
this._summary.durationMs = durationMs;
this._ldClient.track('$ld:ai:graph:latency', this._context, this.getTrackData(), durationMs);
this._ldClient.track(
'$ld:ai:graph:duration:total',
this._context,
this.getTrackData(),
durationMs,
);
}

trackTotalTokens(tokens: LDTokenUsage): void {
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/server-ai/src/api/graph/LDGraphTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { LDGraphMetricSummary, LDGraphTrackData } from './types';
* try {
* // ... execute graph ...
* tracker.trackInvocationSuccess();
* tracker.trackLatency(durationMs);
* tracker.trackDuration(durationMs);
* } catch {
* tracker.trackInvocationFailure();
* }
Expand Down Expand Up @@ -62,13 +62,13 @@ export interface LDGraphTracker {
trackInvocationFailure(): void;

/**
* Tracks the total latency of the graph execution in milliseconds.
* Emits event `$ld:ai:graph:latency` with the duration as the metric value.
* Tracks the total duration of the graph execution in milliseconds.
* Emits event `$ld:ai:graph:duration:total` with the duration as the metric value.
* At-most-once: subsequent calls are dropped with a warning.
*
* @param durationMs Duration in milliseconds.
*/
trackLatency(durationMs: number): void;
trackDuration(durationMs: number): void;

/**
* Tracks aggregate token usage across the entire graph invocation.
Expand Down
Loading