7
7
} from "@traceloop/ai-semantic-conventions" ;
8
8
import { withAssociationProperties } from "./association" ;
9
9
import { shouldSendTraces } from "." ;
10
+ import { Telemetry } from "../telemetry/telemetry" ;
10
11
11
12
export type DecoratorConfig = {
12
13
name : string ;
@@ -82,8 +83,8 @@ function withEntity<
82
83
} ) ,
83
84
) ;
84
85
}
85
- } catch {
86
- /* empty */
86
+ } catch ( error ) {
87
+ Telemetry . getInstance ( ) . logException ( error ) ;
87
88
}
88
89
}
89
90
@@ -92,19 +93,14 @@ function withEntity<
92
93
return res . then ( ( resolvedRes ) => {
93
94
try {
94
95
if ( shouldSendTraces ( ) ) {
95
- if ( resolvedRes instanceof Map ) {
96
- span . setAttribute (
97
- SpanAttributes . TRACELOOP_ENTITY_OUTPUT ,
98
- JSON . stringify ( Array . from ( resolvedRes . entries ( ) ) ) ,
99
- ) ;
100
- } else {
101
- span . setAttribute (
102
- SpanAttributes . TRACELOOP_ENTITY_OUTPUT ,
103
- JSON . stringify ( resolvedRes ) ,
104
- ) ;
105
- }
96
+ span . setAttribute (
97
+ SpanAttributes . TRACELOOP_ENTITY_OUTPUT ,
98
+ serialize ( resolvedRes ) ,
99
+ ) ;
106
100
}
107
101
return resolvedRes ;
102
+ } catch ( error ) {
103
+ Telemetry . getInstance ( ) . logException ( error ) ;
108
104
} finally {
109
105
span . end ( ) ;
110
106
}
@@ -114,10 +110,12 @@ function withEntity<
114
110
if ( shouldSendTraces ( ) ) {
115
111
span . setAttribute (
116
112
SpanAttributes . TRACELOOP_ENTITY_OUTPUT ,
117
- JSON . stringify ( res ) ,
113
+ serialize ( res ) ,
118
114
) ;
119
115
}
120
116
return res ;
117
+ } catch ( error ) {
118
+ Telemetry . getInstance ( ) . logException ( error ) ;
121
119
} finally {
122
120
span . end ( ) ;
123
121
}
@@ -240,3 +238,11 @@ export function tool(
240
238
) {
241
239
return entity ( TraceloopSpanKindValues . TOOL , config ?? { } ) ;
242
240
}
241
+
242
+ function serialize ( input : unknown ) : string {
243
+ if ( input instanceof Map ) {
244
+ return JSON . stringify ( Array . from ( input . entries ( ) ) ) ;
245
+ } else {
246
+ return JSON . stringify ( input ) ;
247
+ }
248
+ }
0 commit comments