@@ -4,13 +4,15 @@ import {
4
4
SpanAttributes ,
5
5
TraceloopSpanKindValues ,
6
6
} from "@traceloop/ai-semantic-conventions" ;
7
+ import { withAssociationProperties } from "./association" ;
7
8
8
9
function withEntity <
9
10
A extends unknown [ ] ,
10
11
F extends ( ...args : A ) => ReturnType < F > ,
11
12
> (
12
13
type : TraceloopSpanKindValues ,
13
14
name : string ,
15
+ associationProperties : { [ name : string ] : string } ,
14
16
fn : F ,
15
17
thisArg ?: ThisParameterType < F > ,
16
18
...args : A
@@ -22,46 +24,57 @@ function withEntity<
22
24
: context . active ( ) ;
23
25
24
26
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 (
26
50
`${ name } .${ type } ` ,
27
51
{ } ,
28
52
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 ) => {
36
54
span . setAttribute ( SpanAttributes . TRACELOOP_SPAN_KIND , type ) ;
37
55
span . setAttribute ( SpanAttributes . TRACELOOP_ENTITY_NAME , name ) ;
38
- const res = await fn . apply ( thisArg , args ) ;
56
+ const res = fn . apply ( thisArg , args ) ;
39
57
span . end ( ) ;
40
58
return res ;
41
59
} ,
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
+ ) ,
55
61
) ;
56
62
}
57
63
58
64
export function withWorkflow <
59
65
A extends unknown [ ] ,
60
66
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
+ ) {
62
74
return withEntity (
63
75
TraceloopSpanKindValues . WORKFLOW ,
64
76
name ,
77
+ associationProperties ,
65
78
fn ,
66
79
thisArg ,
67
80
...args ,
@@ -72,21 +85,48 @@ export function withTask<
72
85
A extends unknown [ ] ,
73
86
F extends ( ...args : A ) => ReturnType < F > ,
74
87
> ( 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
+ ) ;
76
96
}
77
97
78
98
export function withAgent <
79
99
A extends unknown [ ] ,
80
100
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
+ ) ;
83
116
}
84
117
85
118
export function withTool <
86
119
A extends unknown [ ] ,
87
120
F extends ( ...args : A ) => ReturnType < F > ,
88
121
> ( 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
+ ) ;
90
130
}
91
131
92
132
function entity ( type : TraceloopSpanKindValues , name ?: string ) {
@@ -103,14 +143,22 @@ function entity(type: TraceloopSpanKindValues, name?: string) {
103
143
return await withEntity (
104
144
type ,
105
145
entityName ,
146
+ { } ,
106
147
originalMethod ,
107
148
target ,
108
149
...args ,
109
150
) ;
110
151
} ;
111
152
} else {
112
153
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
+ ) ;
114
162
} ;
115
163
}
116
164
} ;
0 commit comments