8000 fix(sdk): decorator bug with passing this parameter · traceloop/openllmetry-js@f8edc41 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit f8edc41

Browse files
committed
fix(sdk): decorator bug with passing this parameter
1 parent 923df7f commit f8edc41

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
lines changed

packages/sample-app/src/sample_decorators.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ traceloop.initialize({
99
const openai = new OpenAI();
1010

1111
class SampleOpenAI {
12+
constructor(private model = "gpt-3.5-turbo") {}
13+
1214
@traceloop.workflow({ name: "sample_chat" })
1315
async chat() {
1416
const chatCompletion = await openai.chat.completions.create({
1517
messages: [
1618
{ role: "user", content: "Tell me a joke about OpenTelemetry" },
1719
],
18-
model: "gpt-3.5-turbo",
20+
model: this.model,
1921
});
2022

2123
return chatCompletion.choices[0].message.content;

packages/traceloop-sdk/src/lib/tracing/decorators.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -187,30 +187,18 @@ function entity(
187187
propertyKey: string,
188188
descriptor: PropertyDescriptor,
189189
) {
190-
const originalMethod: () => any = descriptor.value;
190+
const originalMethod = descriptor.value;
191191
const entityName = config.name ?? originalMethod.name;
192192

193-
if (originalMethod.constructor.name === "AsyncFunction") {
194-
descriptor.value = async function (...args: any[]) {
195-
return await withEntity(
196-
type,
197-
{ ...config, name: entityName },
198-
originalMethod,
199-
target,
200-
...args,
201-
);
202-
};
203-
} else {
204-
descriptor.value = function (...args: any[]) {
205-
return withEntity(
206-
type,
207-
{ ...config, name: entityName },
208-
originalMethod,
209-
target,
210-
...args,
211-
);
212-
};
213-
}
193+
descriptor.value = function (...args: any[]) {
194+
return withEntity(
195+
type,
196+
{ ...config, name: entityName },
197+
originalMethod,
198+
this,
199+
...args,
200+
);
201+
};
214202
};
215203
}
216204

packages/traceloop-sdk/test/decorators.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ describe("Test SDK Decorators", () => {
134134

135135
it("should create spans for workflows using decoration syntax", async () => {
136136
class TestOpenAI {
137+
constructor(private model = "gpt-3.5-turbo") {}
138+
137139
@traceloop.workflow({ name: "sample_chat" })
138140
async chat(things: Map<string, string>) {
139141
const generations: Map<string, string> = new Map();
@@ -142,7 +144,7 @@ describe("Test SDK Decorators", () => {
142144
messages: [
143145
{ role: "user", content: `Tell me a ${key} about ${value}` },
144146
],
145-
model: "gpt-3.5-turbo",
147+
model: this.model,
146148
});
147149

148150
if (cha 38D6 tCompletion.choices[0].message.content) {

0 commit comments

Comments
 (0)
0