@@ -488,4 +488,61 @@ export interface Metrics {
488488 * method on the returned summary group object
489489 */
490490 registerSummaryGroup : ( ( name : string , options ?: SummaryOptions ) => SummaryGroup ) & ( ( name : string , options : CalculatedSummaryOptions < Record < string , number > > ) => void )
491+
492+ /**
493+ * Wrap a function for tracing purposes.
494+ *
495+ * All functions wrapped like this should accept a final optional options arg.
496+ *
497+ * In order to pass an execution context along to create a multi-layered
498+ * trace, the index of the options arg must be specified.
499+ */
500+ traceFunction < F extends ( ...args : any [ ] ) => AsyncIterator < any > > ( name : string , fn : F , options ?: TraceGeneratorFunctionOptions < Parameters < F > , ReturnType < F > , YieldType < ReturnType < F > > > ) : F
501+ traceFunction < F extends ( ...args : any [ ] ) => Iterator < any > > ( name : string , fn : F , options ?: TraceGeneratorFunctionOptions < Parameters < F > , ReturnType < F > , YieldType < ReturnType < F > > > ) : F
502+ traceFunction < F extends ( ...args : any [ ] ) => any = ( ...args : any [ ] ) => any > ( name : string , fn : F , options ?: TraceFunctionOptions < Parameters < F > , ReturnType < F > > ) : F
503+
504+ /**
505+ * Creates a tracing context that can be used to trace a method call
506+ */
507+ createTraceContext ( ) : any
508+ }
509+
510+ /**
511+ * Infer the yielded type of an (async)iterable
512+ */
513+ type YieldType < T extends AsyncIterator < any > | Iterator < any > > = T extends AsyncIterator < infer Y > ? Y : T extends Iterator < infer Y , any , any > ? Y : never
514+
515+ export type TraceAttributes = Record < string , number | string | boolean | number [ ] | string [ ] | boolean [ ] >
516+
517+ export interface TraceFunctionOptions < A , B > {
518+ /**
519+ * To construct a trace that spans multiple method invocations, it's necessary
520+ * to pass the trace context onwards as part of the options object.
521+ *
522+ * Specify the index of the options object in the args array here.
523+ *
524+ * @default 0
525+ */
526+ optionsIndex ?: number
527+
528+ /**
529+ * Set attributes on the trace by modifying the passed attributes object.
530+ */
531+ getAttributesFromArgs ?( args : A , attributes : TraceAttributes ) : TraceAttributes
532+
533+ /**
534+ * Set attributes on the trace by modifying the passed attributes object. The
535+ * object will have previously been passed to `appendAttributesFromArgs`
536+ * and/or `appendAttributesFromYieldedValue` (if defined)
537+ */
538+ getAttributesFromReturnValue ?( value : B , attributes : TraceAttributes ) : TraceAttributes
539+ }
540+
541+ export interface TraceGeneratorFunctionOptions < A , B , C = any > extends TraceFunctionOptions < A , B > {
542+ /**
543+ * Set attributes on the trace by modifying the passed attributes object. The
544+ * object will have previously been passed to `appendAttributesFromArgs` (if
545+ * defined)
546+ */
547+ getAttributesFromYieldedValue ? ( value : C , attributes : TraceAttributes , index : number ) : TraceAttributes
491548}
0 commit comments