1
1
import { Scope } from '@sentry/core' ;
2
- import type { Breadcrumb , Transaction } from '@sentry/types' ;
3
- import { dateTimestampInSeconds } from '@sentry/utils' ;
2
+ import type { Breadcrumb } from '@sentry/types' ;
4
3
4
+ import type { TransactionWithBreadcrumbs } from '../types' ;
5
5
import { getActiveSpan } from './trace' ;
6
6
7
- const DEFAULT_MAX_BREADCRUMBS = 100 ;
8
-
9
- /**
10
- * This is a fork of the base Transaction with OTEL specific stuff added.
11
- * Note that we do not solve this via an actual subclass, but by wrapping this in a proxy when we need it -
12
- * as we can't easily control all the places a transaction may be created.
13
- */
14
- interface TransactionWithBreadcrumbs extends Transaction {
15
- _breadcrumbs : Breadcrumb [ ] ;
16
-
17
- /** Get all breadcrumbs added to this transaction. */
18
- getBreadcrumbs ( ) : Breadcrumb [ ] ;
19
-
20
- /** Add a breadcrumb to this transaction. */
21
- addBreadcrumb ( breadcrumb : Breadcrumb , maxBreadcrumbs ?: number ) : void ;
22
- }
23
-
24
7
/** A fork of the classic scope with some otel specific stuff. */
25
8
export class OtelScope extends Scope {
26
9
/**
@@ -54,7 +37,7 @@ export class OtelScope extends Scope {
54
37
public addBreadcrumb ( breadcrumb : Breadcrumb , maxBreadcrumbs ?: number ) : this {
55
38
const transaction = getActiveTransaction ( ) ;
56
39
57
- if ( transaction ) {
40
+ if ( transaction && transaction . addBreadcrumb ) {
58
41
transaction . addBreadcrumb ( breadcrumb , maxBreadcrumbs ) ;
59
42
return this ;
60
43
}
@@ -67,7 +50,7 @@ export class OtelScope extends Scope {
67
50
*/
68
51
protected _getBreadcrumbs ( ) : Breadcrumb [ ] {
69
52
const transaction = getActiveTransaction ( ) ;
70
- const transactionBreadcrumbs = transaction ? transaction . getBreadcrumbs ( ) : [ ] ;
53
+ const transactionBreadcrumbs = transaction && transaction . getBreadcrumbs ? transaction . getBreadcrumbs ( ) : [ ] ;
71
54
72
55
return this . _breadcrumbs . concat ( transactionBreadcrumbs ) ;
73
56
}
@@ -79,60 +62,5 @@ export class OtelScope extends Scope {
79
62
*/
80
63
function getActiveTransaction ( ) : TransactionWithBreadcrumbs | undefined {
81
64
const activeSpan = getActiveSpan ( ) ;
82
- const transaction = activeSpan && activeSpan . transaction ;
83
-
84
- if ( ! transaction ) {
85
- return undefined ;
86
- }
87
-
88
- if ( transactionHasBreadcrumbs ( transaction ) ) {
89
- return transaction ;
90
- }
91
-
92
- return new Proxy ( transaction as TransactionWithBreadcrumbs , {
93
- get ( target , prop , receiver ) {
94
- if ( prop === 'addBreadcrumb' ) {
95
- return addBreadcrumb ;
96
- }
97
- if ( prop === 'getBreadcrumbs' ) {
98
- return getBreadcrumbs ;
99
- }
100
- if ( prop === '_breadcrumbs' ) {
101
- const breadcrumbs = Reflect . get ( target , prop , receiver ) ;
102
- return breadcrumbs || [ ] ;
103
- }
104
- return Reflect . get ( target , prop , receiver ) ;
105
- } ,
106
- } ) ;
107
- }
108
-
109
- function transactionHasBreadcrumbs ( transaction : Transaction ) : transaction is TransactionWithBreadcrumbs {
110
- return (
111
- typeof ( transaction as TransactionWithBreadcrumbs ) . getBreadcrumbs === 'function' &&
112
- typeof ( transaction as TransactionWithBreadcrumbs ) . addBreadcrumb === 'function'
113
- ) ;
114
- }
115
-
116
- /** Add a breadcrumb to a transaction. */
117
- function addBreadcrumb ( this : TransactionWithBreadcrumbs , breadcrumb : Breadcrumb , maxBreadcrumbs ?: number ) : void {
118
- const maxCrumbs = typeof maxBreadcrumbs === 'number' ? maxBreadcrumbs : DEFAULT_MAX_BREADCRUMBS ;
119
-
120
- // No data has been changed, so don't notify scope listeners
121
- if ( maxCrumbs <= 0 ) {
122
- return ;
123
- }
124
-
125
- const mergedBreadcrumb = {
126
- timestamp : dateTimestampInSeconds ( ) ,
127
- ...breadcrumb ,
128
- } ;
129
-
130
- const breadcrumbs = this . _breadcrumbs ;
131
- breadcrumbs . push ( mergedBreadcrumb ) ;
132
- this . _breadcrumbs = breadcrumbs . length > maxCrumbs ? breadcrumbs . slice ( - maxCrumbs ) : breadcrumbs ;
133
- }
134
-
135
- /** Get all breadcrumbs from a transaction. */
136
- function getBreadcrumbs ( this : TransactionWithBreadcrumbs ) : Breadcrumb [ ] {
137
- return this . _breadcrumbs ;
65
+ return activeSpan && ( activeSpan . transaction as TransactionWithBreadcrumbs | undefined ) ;
138
66
}
0 commit comments