66 * Side Public License, v 1.
77 */
88
9+ import { ApmError } from './apm_error' ;
910import { BaseSpan } from './base_span' ;
1011import { Fields } from './entity' ;
11- import { generateEventId } from './utils/generate_id' ;
12+ import { generateShortId } from './utils/generate_id' ;
1213
1314export class Transaction extends BaseSpan {
1415 private _sampled : boolean = true ;
16+ private readonly _errors : ApmError [ ] = [ ] ;
1517
1618 constructor ( fields : Fields ) {
1719 super ( {
1820 ...fields ,
1921 'processor.event' : 'transaction' ,
20- 'transaction.id' : generateEventId ( ) ,
22+ 'transaction.id' : generateShortId ( ) ,
2123 'transaction.sampled' : true ,
2224 } ) ;
2325 }
2426
27+ parent ( span : BaseSpan ) {
28+ super . parent ( span ) ;
29+
30+ this . _errors . forEach ( ( error ) => {
31+ error . fields [ 'trace.id' ] = this . fields [ 'trace.id' ] ;
32+ error . fields [ 'transaction.id' ] = this . fields [ 'transaction.id' ] ;
33+ error . fields [ 'transaction.type' ] = this . fields [ 'transaction.type' ] ;
34+ } ) ;
35+
36+ return this ;
37+ }
38+
39+ errors ( ...errors : ApmError [ ] ) {
40+ errors . forEach ( ( error ) => {
41+ error . fields [ 'trace.id' ] = this . fields [ 'trace.id' ] ;
42+ error . fields [ 'transaction.id' ] = this . fields [ 'transaction.id' ] ;
43+ error . fields [ 'transaction.type' ] = this . fields [ 'transaction.type' ] ;
44+ } ) ;
45+
46+ this . _errors . push ( ...errors ) ;
47+
48+ return this ;
49+ }
50+
2551 duration ( duration : number ) {
2652 this . fields [ 'transaction.duration.us' ] = duration * 1000 ;
2753 return this ;
@@ -35,11 +61,13 @@ export class Transaction extends BaseSpan {
3561 serialize ( ) {
3662 const [ transaction , ...spans ] = super . serialize ( ) ;
3763
64+ const errors = this . _errors . flatMap ( ( error ) => error . serialize ( ) ) ;
65+
3866 const events = [ transaction ] ;
3967 if ( this . _sampled ) {
4068 events . push ( ...spans ) ;
4169 }
4270
43- return events ;
71+ return events . concat ( errors ) ;
4472 }
4573}
0 commit comments