11import { AppError } from "./error.js" ;
22import { isNil } from "./lodash.js" ;
3+ import { _compasSentryExport } from "./sentry.js" ;
34
45/**
56 * @typedef {object } InsightEventSpan
@@ -13,8 +14,8 @@ import { isNil } from "./lodash.js";
1314
1415/**
1516 * The insight event is a tool for tracking the duration of (async) functions manually.
16- * By utilizing the insight event, you can gain access to a task or request-specific logger and
17- * obtain insights into the execution time of your functions.
17+ * By utilizing the insight event, you can gain access to a task or request-specific
18+ * logger and obtain insights into the execution time of your functions.
1819 *
1920 * How to use the Insight Event:
2021 *
@@ -24,7 +25,8 @@ import { isNil } from "./lodash.js";
2425 * In your tests you can use {@link newTestEvent}.
2526 *
2627 * You could pass the event object down through your (async) functions as an argument.
27- * This allows the insight event to associate the event with the specific task or request.
28+ * This allows the insight event to associate the event with the specific task or
29+ * request.
2830 *
2931 * Finally, you should stop the event for correct logging by calling {@link eventStop}.
3032 * When the root event is stopped via {@link eventStop} it calculates the duration
@@ -64,6 +66,7 @@ import { isNil } from "./lodash.js";
6466 * @property {InsightEvent } [rootEvent]
6567 * @property {string } [name]
6668 * @property {InsightEventSpan } span
69+ * @property {import("@sentry/node").Span } [_compasSentrySpan]
6770 */
6871
6972/**
@@ -91,6 +94,8 @@ function InsightEventConstructor(logger, signal) {
9194 abortedTime : undefined ,
9295 children : [ ] ,
9396 } ,
97+
98+ _compasSentrySpan : undefined ,
9499 } ;
95100}
96101
@@ -120,6 +125,10 @@ export function newEventFromEvent(event) {
120125 if ( event . signal ?. aborted ) {
121126 event . span . abortedTime = Date . now ( ) ;
122127
128+ if ( event . _compasSentrySpan ) {
129+ event . _compasSentrySpan . end ( ) ;
130+ }
131+
123132 throw AppError . serverError ( {
124133 message : "Operation aborted" ,
125134 span : getEventRoot ( event ) . span ,
@@ -151,9 +160,21 @@ export function eventStart(event, name) {
151160 event . span . name = name ;
152161 event . span . startTime = Date . now ( ) ;
153162
163+ if ( typeof _compasSentryExport ?. startInactiveSpan === "function" ) {
164+ event . _compasSentrySpan = _compasSentryExport . startInactiveSpan ( {
165+ op : "event" ,
166+ name : name ,
167+ description : name ,
168+ } ) ;
169+ }
170+
154171 if ( event . signal ?. aborted ) {
155172 event . span . abortedTime = Date . now ( ) ;
156173
174+ if ( event . _compasSentrySpan ) {
175+ event . _compasSentrySpan . end ( ) ;
176+ }
177+
157178 throw AppError . serverError ( {
158179 message : "Operation aborted" ,
159180 span : getEventRoot ( event ) . span ,
@@ -174,9 +195,18 @@ export function eventRename(event, name) {
174195 event . name = name ;
175196 event . span . name = name ;
176197
198+ if ( event . _compasSentrySpan ) {
199+ event . _compasSentrySpan . description = name ;
200+ event . _compasSentrySpan . updateName ( name ) ;
201+ }
202+
177203 if ( event . signal ?. aborted ) {
178204 event . span . abortedTime = Date . now ( ) ;
179205
206+ if ( event . _compasSentrySpan ) {
207+ event . _compasSentrySpan . end ( ) ;
208+ }
209+
180210 throw AppError . serverError ( {
181211 message : "Operation aborted" ,
182212 span : getEventRoot ( event ) . span ,
@@ -199,6 +229,10 @@ export function eventStop(event) {
199229 event . span . duration = event . span . stopTime - event . span . startTime ;
200230 }
201231
232+ if ( event . _compasSentrySpan ) {
233+ event . _compasSentrySpan . end ( ) ;
234+ }
235+
202236 if ( isNil ( event . rootEvent ) ) {
203237 event . log . info ( {
204238 type : "event_span" ,
0 commit comments