-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathtransaction.ts
176 lines (142 loc) · 4.6 KB
/
transaction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { getCurrentHub, Hub } from '@sentry/hub';
import {
Event,
Measurements,
Outcome,
Transaction as TransactionInterface,
TransactionContext,
TransactionMetadata,
} from '@sentry/types';
import { dropUndefinedKeys, isInstanceOf, logger } from '@sentry/utils';
import { Span as SpanClass, SpanRecorder } from './span';
/** JSDoc */
export class Transaction extends SpanClass implements TransactionInterface {
public name: string;
public metadata: TransactionMetadata;
private _measurements: Measurements = {};
/**
* The reference to the current hub.
*/
private readonly _hub: Hub = (getCurrentHub() as unknown) as Hub;
private _trimEnd?: boolean;
/**
* This constructor should never be called manually. Those instrumenting tracing should use
* `Sentry.startTransaction()`, and internal methods should use `hub.startTransaction()`.
* @internal
* @hideconstructor
* @hidden
*/
public constructor(transactionContext: TransactionContext, hub?: Hub) {
super(transactionContext);
if (isInstanceOf(hub, Hub)) {
this._hub = hub as Hub;
}
this.name = transactionContext.name || '';
this.metadata = transactionContext.metadata || {};
this._trimEnd = transactionContext.trimEnd;
// this is because transactions are also spans, and spans have a transaction pointer
this.transaction = this;
}
/**
* JSDoc
*/
public setName(name: string): void {
this.name = name;
}
/**
* Attaches SpanRecorder to the span itself
* @param maxlen maximum number of spans that can be recorded
*/
public initSpanRecorder(maxlen: number = 1000): void {
if (!this.spanRecorder) {
this.spanRecorder = new SpanRecorder(maxlen);
}
this.spanRecorder.add(this);
}
/**
* Set observed measurements for this transaction.
* @hidden
*/
public setMeasurements(measurements: Measurements): void {
this._measurements = { ...measurements };
}
/**
* Set metadata for this transaction.
* @hidden
*/
public setMetadata(newMetadata: TransactionMetadata): void {
this.metadata = { ...this.metadata, ...newMetadata };
}
/**
* @inheritDoc
*/
public finish(endTimestamp?: number): string | undefined {
// This transaction is already finished, so we should not flush it again.
if (this.endTimestamp !== undefined) {
return undefined;
}
if (!this.name) {
logger.warn('Transaction has no name, falling back to `<unlabeled transaction>`.');
this.name = '<unlabeled transaction>';
}
// just sets the end timestamp
super.finish(endTimestamp);
if (this.sampled !== true) {
// At this point if `sampled !== true` we want to discard the transaction.
logger.log('[Tracing] Discarding transaction because its trace was not chosen to be sampled.');
this._hub
.getClient()
?.getTransport()
.recordLostEvent?.(Outcome.SampleRate, 'transaction');
return undefined;
}
const finishedSpans = this.spanRecorder ? this.spanRecorder.spans.filter(s => s !== this && s.endTimestamp) : [];
if (this._trimEnd && finishedSpans.length > 0) {
this.endTimestamp = finishedSpans.reduce((prev: SpanClass, current: SpanClass) => {
if (prev.endTimestamp && current.endTimestamp) {
return prev.endTimestamp > current.endTimestamp ? prev : current;
}
return prev;
}).endTimestamp;
}
const transaction: Event = {
contexts: {
trace: this.getTraceContext(),
},
spans: finishedSpans,
start_timestamp: this.startTimestamp,
tags: this.tags,
timestamp: this.endTimestamp,
transaction: this.name,
type: 'transaction',
debug_meta: this.metadata,
};
const hasMeasurements = Object.keys(this._measurements).length > 0;
if (hasMeasurements) {
logger.log('[Measurements] Adding measurements to transaction', JSON.stringify(this._measurements, undefined, 2));
transaction.measurements = this._measurements;
}
logger.log(`[Tracing] Finishing ${this.op} transaction: ${this.name}.`);
return this._hub.captureEvent(transaction);
}
/**
* @inheritDoc
*/
public toContext(): TransactionContext {
const spanContext = super.toContext();
return dropUndefinedKeys({
...spanContext,
name: this.name,
trimEnd: this._trimEnd,
});
}
/**
* @inheritDoc
*/
public updateWithContext(transactionContext: TransactionContext): this {
super.updateWithContext(transactionContext);
this.name = transactionContext.name ?? '';
this._trimEnd = transactionContext.trimEnd;
return this;
}
}