Skip to content

Commit 70c273f

Browse files
committed
fix: bedrock traces do not have correct latency in TypeScript client
1 parent f041a6c commit 70c273f

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

src/lib/integrations/bedrockAgentTracer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ function createTracedCompletion(
199199
rawOutput: JSON.stringify(rawOutputChunks, null, 2),
200200
metadata: metadata,
201201
provider: 'Bedrock',
202+
startTime: startTime,
203+
endTime: endTime,
202204
};
203205

204206
addChatCompletionStepToTrace(traceStepData);

src/lib/integrations/langchainCallback.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ export class OpenlayerHandler extends BaseCallbackHandler {
136136
modelParameters: this.modelParameters,
137137
metadata: this.metadata,
138138
provider: this.provider ?? '',
139+
startTime: this.startTime,
140+
endTime: this.endTime,
139141
});
140142
}
141143
}

src/lib/integrations/openAiTracer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ export function traceOpenAI(openai: OpenAI): OpenAI {
6969
completionTokens: completionTokens,
7070
promptTokens: 0,
7171
tokens: completionTokens,
72+
startTime: startTime,
73+
endTime: endTime,
7274
};
7375
addChatCompletionStepToTrace(traceData);
7476
}
@@ -101,6 +103,8 @@ export function traceOpenAI(openai: OpenAI): OpenAI {
101103
rawOutput: JSON.stringify(response, null, 2),
102104
metadata: {},
103105
provider: 'OpenAI',
106+
startTime: startTime,
107+
endTime: endTime,
104108
};
105109
addChatCompletionStepToTrace(traceData);
106110
return response;

src/lib/tracing/steps.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ export class Step {
3939
latency: number | null = null;
4040
steps: Step[] = [];
4141

42-
constructor(name: string, inputs: any = null, output: any = null, metadata: Record<string, any> = {}) {
42+
constructor(name: string, inputs: any = null, output: any = null, metadata: Record<string, any> = {}, startTime?: number | null, endTime?: number | null) {
4343
this.name = name;
4444
this.id = uuidv4();
4545
this.inputs = inputs;
4646
this.output = output;
4747
this.metadata = metadata;
4848

49-
this.startTime = Date.now();
49+
this.startTime = startTime ?? Date.now();
50+
this.endTime = endTime ?? null;
5051
}
5152

5253
addNestedStep(nestedStep: Step): void {
@@ -80,8 +81,8 @@ export class Step {
8081
}
8182

8283
export class UserCallStep extends Step {
83-
constructor(name: string, inputs: any = null, output: any = null, metadata: Record<string, any> = {}) {
84-
super(name, inputs, output, metadata);
84+
constructor(name: string, inputs: any = null, output: any = null, metadata: Record<string, any> = {}, startTime?: number | null, endTime?: number | null) {
85+
super(name, inputs, output, metadata, startTime, endTime);
8586
this.stepType = StepType.USER_CALL;
8687
}
8788
}
@@ -96,8 +97,8 @@ export class ChatCompletionStep extends Step {
9697
modelParameters: Record<string, any> | null = null;
9798
rawOutput: string | null = null;
9899

99-
constructor(name: string, inputs: any = null, output: any = null, metadata: Record<string, any> = {}) {
100-
super(name, inputs, output, metadata);
100+
constructor(name: string, inputs: any = null, output: any = null, metadata: Record<string, any> = {}, startTime?: number | null, endTime?: number | null) {
101+
super(name, inputs, output, metadata, startTime, endTime);
101102
this.stepType = StepType.CHAT_COMPLETION;
102103
}
103104

src/lib/tracing/tracer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ function createStep(
3131
inputs?: any,
3232
output?: any,
3333
metadata: Record<string, any> | null = null,
34+
startTime?: number | null,
35+
endTime?: number | null,
3436
): [Step, () => void] {
3537
metadata = metadata || {};
3638
let newStep: Step;
3739
if (stepType === StepType.CHAT_COMPLETION) {
38-
newStep = new ChatCompletionStep(name, inputs, output, metadata);
40+
newStep = new ChatCompletionStep(name, inputs, output, metadata, startTime, endTime);
3941
} else {
40-
newStep = new UserCallStep(name, inputs, output, metadata);
42+
newStep = new UserCallStep(name, inputs, output, metadata, startTime, endTime);
4143
}
4244

4345
const parentStep = getCurrentStep();
@@ -167,6 +169,8 @@ export function addChatCompletionStepToTrace({
167169
rawOutput = null,
168170
metadata = {},
169171
provider = 'OpenAI',
172+
startTime = null,
173+
endTime = null,
170174
}: {
171175
name: string;
172176
inputs: any;
@@ -180,8 +184,10 @@ export function addChatCompletionStepToTrace({
180184
rawOutput?: string | null;
181185
metadata?: Record<string, any>;
182186
provider?: string;
187+
startTime?: number | null;
188+
endTime?: number | null;
183189
}) {
184-
const [step, endStep] = createStep(name, StepType.CHAT_COMPLETION, inputs, output, metadata);
190+
const [step, endStep] = createStep(name, StepType.CHAT_COMPLETION, inputs, output, metadata, startTime, endTime);
185191

186192
if (step.stepType === StepType.CHAT_COMPLETION) {
187193
(step as ChatCompletionStep).provider = provider;

0 commit comments

Comments
 (0)