@@ -3,7 +3,13 @@ import { VERSION } from "../../version.js";
33import { ApiError , RateLimitError } from "../apiClient/errors.js" ;
44import { ConsoleInterceptor } from "../consoleInterceptor.js" ;
55import { isInternalError , parseError , sanitizeError , TaskPayloadParsedError } from "../errors.js" ;
6- import { lifecycleHooks , runMetadata , TriggerConfig , waitUntil } from "../index.js" ;
6+ import {
7+ flattenAttributes ,
8+ lifecycleHooks ,
9+ runMetadata ,
10+ TriggerConfig ,
11+ waitUntil ,
12+ } from "../index.js" ;
713import { recordSpanException , TracingSDK } from "../otel/index.js" ;
814import { runTimelineMetrics } from "../run-timeline-metrics-api.js" ;
915import {
@@ -288,58 +294,87 @@ export class TaskExecutor {
288294 return this . _tracer . startActiveSpan (
289295 "hooks.init" ,
290296 async ( span ) => {
291- return await runTimelineMetrics . measureMetric ( "trigger.dev/execution" , "init" , async ( ) => {
292- // Store global hook results in an array
293- const globalResults = [ ] ;
294- for ( const hook of globalInitHooks ) {
295- const result = await this . _tracer . startActiveSpan (
296- hook . name ?? "global" ,
297- async ( span ) => {
298- return await hook . fn ( { payload, ctx, signal, task : this . task . id } ) ;
299- } ,
300- {
301- attributes : {
302- [ SemanticInternalAttributes . STYLE_ICON ] : "function" ,
297+ const result = await runTimelineMetrics . measureMetric (
298+ "trigger.dev/execution" ,
299+ "init" ,
300+ async ( ) => {
301+ // Store global hook results in an array
302+ const globalResults = [ ] ;
303+ for ( const hook of globalInitHooks ) {
304+ const result = await this . _tracer . startActiveSpan (
305+ hook . name ?? "global" ,
306+ async ( span ) => {
307+ const result = await hook . fn ( { payload, ctx, signal, task : this . task . id } ) ;
308+
309+ if ( result && typeof result === "object" && ! Array . isArray ( result ) ) {
310+ span . setAttributes ( flattenAttributes ( result ) ) ;
311+
312+ return result ;
313+ }
314+
315+ return { } ;
303316 } ,
317+ {
318+ attributes : {
319+ [ SemanticInternalAttributes . STYLE_ICON ] : "tabler-function" ,
320+ } ,
321+ }
322+ ) ;
323+ // Only include object results
324+ if ( result && typeof result === "object" && ! Array . isArray ( result ) ) {
325+ globalResults . push ( result ) ;
304326 }
305- ) ;
306- // Only include object results
307- if ( result && typeof result === "object" && ! Array . isArray ( result ) ) {
308- globalResults . push ( result ) ;
309327 }
310- }
311328
312- // Merge all global results into a single object
313- const mergedGlobalResults = Object . assign ( { } , ...globalResults ) ;
329+ // Merge all global results into a single object
330+ const mergedGlobalResults = Object . assign ( { } , ...globalResults ) ;
314331
315- if ( taskInitHook ) {
316- const taskResult = await this . _tracer . startActiveSpan (
317- "task" ,
318- async ( span ) => {
319- return await taskInitHook ( { payload, ctx, signal, task : this . task . id } ) ;
320- } ,
321- {
322- attributes : {
323- [ SemanticInternalAttributes . STYLE_ICON ] : "function" ,
332+ if ( taskInitHook ) {
333+ const taskResult = await this . _tracer . startActiveSpan (
334+ "task" ,
335+ async ( span ) => {
336+ const result = await taskInitHook ( { payload, ctx, signal, task : this . task . id } ) ;
337+
338+ if ( result && typeof result === "object" && ! Array . isArray ( result ) ) {
339+ span . setAttributes ( flattenAttributes ( result ) ) ;
340+
341+ return result ;
342+ }
343+
344+ return { } ;
324345 } ,
346+ {
347+ attributes : {
348+ [ SemanticInternalAttributes . STYLE_ICON ] : "tabler-function" ,
349+ } ,
350+ }
351+ ) ;
352+
353+ // Only merge if taskResult is an object
354+ if ( taskResult && typeof taskResult === "object" && ! Array . isArray ( taskResult ) ) {
355+ return { ...mergedGlobalResults , ...taskResult } ;
325356 }
326- ) ;
327357
328- // Only merge if taskResult is an object
329- if ( taskResult && typeof taskResult === "object" && ! Array . isArray ( taskResult ) ) {
330- return { ...mergedGlobalResults , ...taskResult } ;
358+ // If taskResult isn't an object, return global results
359+ return mergedGlobalResults ;
331360 }
332361
333- // If taskResult isn't an object, return global results
334362 return mergedGlobalResults ;
335363 }
364+ ) ;
336365
337- return mergedGlobalResults ;
338- } ) ;
366+ if ( result && typeof result === "object" && ! Array . isArray ( result ) ) {
367+ span . setAttributes ( flattenAttributes ( result ) ) ;
368+
369+ return result ;
370+ }
371+
372+ return ;
339373 } ,
340374 {
341375 attributes : {
342- [ SemanticInternalAttributes . STYLE_ICON ] : "function" ,
376+ [ SemanticInternalAttributes . STYLE_ICON ] : "tabler-function" ,
377+ [ SemanticInternalAttributes . COLLAPSED ] : true ,
343378 } ,
344379 }
345380 ) ;
0 commit comments