Skip to content

Commit 1ed3a6a

Browse files
authored
feat: association property in workflows (#36)
1 parent 018b242 commit 1ed3a6a

File tree

3 files changed

+82
-30
lines changed

3 files changed

+82
-30
lines changed

packages/sample-app/src/sample_with.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ traceloop.initialize({
99
const openai = new OpenAI();
1010

1111
async function chat() {
12-
return await traceloop.withWorkflow("sample_chat", async () => {
12+
return await traceloop.withWorkflow("sample_chat", {}, async () => {
1313
const chatCompletion = await openai.chat.completions.create({
1414
messages: [
1515
{ role: "user", content: "Tell me a joke about OpenTelemetry" },
@@ -22,7 +22,7 @@ async function chat() {
2222
}
2323

2424
async function completion() {
25-
return await traceloop.withWorkflow("sample_completion", async () => {
25+
return await traceloop.withWorkflow("sample_completion", {}, async () => {
2626
const completion = await openai.completions.create({
2727
prompt: "Tell me a joke about TypeScript",
2828
model: "gpt-3.5-turbo-instruct",

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export function withAssociationProperties<
1010
thisArg?: ThisParameterType<F>,
1111
...args: A
1212
) {
13+
if (Object.keys(properties).length === 0) {
14+
return fn.apply(thisArg, args);
15+
}
16+
1317
const newContext = context
1418
.active()
1519
.setValue(ASSOCATION_PROPERTIES_KEY, properties);

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

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import {
44
SpanAttributes,
55
TraceloopSpanKindValues,
66
} from "@traceloop/ai-semantic-conventions";
7+
import { withAssociationProperties } from "./association";
78

89
function withEntity<
910
A extends unknown[],
1011
F extends (...args: A) => ReturnType<F>,
1112
>(
1213
type: TraceloopSpanKindValues,
1314
name: string,
15+
associationProperties: { [name: string]: string },
1416
fn: F,
1517
thisArg?: ThisParameterType<F>,
1618
...args: A
@@ -22,46 +24,57 @@ function withEntity<
2224
: context.active();
2325

2426
if (fn.constructor.name === "AsyncFunction") {
25-
return getTracer().startActiveSpan(
27+
return withAssociationProperties(associationProperties, () =>
28+
getTracer().startActiveSpan(
29+
`${name}.${type}`,
30+
{},
31+
workflowContext,
32+
async (span: Span) => {
33+
if (
34+
type === TraceloopSpanKindValues.WORKFLOW ||
35+
type === TraceloopSpanKindValues.AGENT
36+
) {
37+
span.setAttribute(SpanAttributes.TRACELOOP_WORKFLOW_NAME, name);
38+
}
39+
span.setAttribute(SpanAttributes.TRACELOOP_SPAN_KIND, type);
40+
span.setAttribute(SpanAttributes.TRACELOOP_ENTITY_NAME, name);
41+
const res = await fn.apply(thisArg, args);
42+
span.end();
43+
return res;
44+
},
45+
),
46+
);
47+
}
48+
return withAssociationProperties(associationProperties, () =>
49+
getTracer().startActiveSpan(
2650
`${name}.${type}`,
2751
{},
2852
workflowContext,
29-
async (span: Span) => {
30-
if (
31-
type === TraceloopSpanKindValues.WORKFLOW ||
32-
type === TraceloopSpanKindValues.AGENT
33-
) {
34-
span.setAttribute(SpanAttributes.TRACELOOP_WORKFLOW_NAME, name);
35-
}
53+
(span) => {
3654
span.setAttribute(SpanAttributes.TRACELOOP_SPAN_KIND, type);
3755
span.setAttribute(SpanAttributes.TRACELOOP_ENTITY_NAME, name);
38-
const res = await fn.apply(thisArg, args);
56+
const res = fn.apply(thisArg, args);
3957
span.end();
4058
return res;
4159
},
42-
);
43-
}
44-
return getTracer().startActiveSpan(
45-
`${name}.${type}`,
46-
{},
47-
workflowContext,
48-
(span) => {
49-
span.setAttribute(SpanAttributes.TRACELOOP_SPAN_KIND, type);
50-
span.setAttribute(SpanAttributes.TRACELOOP_ENTITY_NAME, name);
51-
const res = fn.apply(thisArg, args);
52-
span.end();
53-
return res;
54-
},
60+
),
5561
);
5662
}
5763

5864
export function withWorkflow<
5965
A extends unknown[],
6066
F extends (...args: A) => ReturnType<F>,
61-
>(name: string, fn: F, thisArg?: ThisParameterType<F>, ...args: A) {
67+
>(
68+
name: string,
69+
associationProperties: { [name: string]: string },
70+
fn: F,
71+
thisArg?: ThisParameterType<F>,
72+
...args: A
73+
) {
6274
return withEntity(
6375
TraceloopSpanKindValues.WORKFLOW,
6476
name,
77+
associationProperties,
6578
fn,
6679
thisArg,
6780
...args,
@@ -72,21 +85,48 @@ export function withTask<
7285
A extends unknown[],
7386
F extends (...args: A) => ReturnType<F>,
7487
>(name: string, fn: F, thisArg?: ThisParameterType<F>, ...args: A) {
75-
return withEntity(TraceloopSpanKindValues.TASK, name, fn, thisArg, ...args);
88+
return withEntity(
89+
TraceloopSpanKindValues.TASK,
90+
name,
91+
{},
92+
fn,
93+
thisArg,
94+
...args,
95+
);
7696
}
7797

7898
export function withAgent<
7999
A extends unknown[],
80100
F extends (...args: A) => ReturnType<F>,
81-
>(name: string, fn: F, thisArg?: ThisParameterType<F>, ...args: A) {
82-
return withEntity(TraceloopSpanKindValues.AGENT, name, fn, thisArg, ...args);
101+
>(
102+
name: string,
103+
associationProperties: { [name: string]: string },
104+
fn: F,
105+
thisArg?: ThisParameterType<F>,
106+
...args: A
107+
) {
108+
return withEntity(
109+
TraceloopSpanKindValues.AGENT,
110+
name,
111+
associationProperties,
112+
fn,
113+
thisArg,
114+
...args,
115+
);
83116
}
84117

85118
export function withTool<
86119
A extends unknown[],
87120
F extends (...args: A) => ReturnType<F>,
88121
>(name: string, fn: F, thisArg?: ThisParameterType<F>, ...args: A) {
89-
return withEntity(TraceloopSpanKindValues.TOOL, name, fn, thisArg, ...args);
122+
return withEntity(
123+
TraceloopSpanKindValues.TOOL,
124+
name,
125+
{},
126+
fn,
127+
thisArg,
128+
...args,
129+
);
90130
}
91131

92132
function entity(type: TraceloopSpanKindValues, name?: string) {
@@ -103,14 +143,22 @@ function entity(type: TraceloopSpanKindValues, name?: string) {
103143
return await withEntity(
104144
type,
105145
entityName,
146+
{},
106147
originalMethod,
107148
target,
108149
...args,
109150
);
110151
};
111152
} else {
112153
descriptor.value = function (...args: any[]) {
113-
return withEntity(type, entityName, originalMethod, target, ...args);
154+
return withEntity(
155+
type,
156+
entityName,
157+
{},
158+
originalMethod,
159+
target,
160+
...args,
161+
);
114162
};
115163
}
116164
};

0 commit comments

Comments
 (0)