-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathhubextensions.ts
More file actions
154 lines (142 loc) · 5.2 KB
/
Copy pathhubextensions.ts
File metadata and controls
154 lines (142 loc) · 5.2 KB
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
import type { ClientOptions, CustomSamplingContext, TransactionContext } from '@sentry/types';
import { logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../debug-build';
import type { Hub } from '../hub';
import { getMainCarrier } from '../hub';
import { spanToTraceHeader } from '../utils/spanUtils';
import { registerErrorInstrumentation } from './errors';
import { IdleTransaction } from './idletransaction';
import { sampleTransaction } from './sampling';
import { Transaction } from './transaction';
/** Returns all trace headers that are currently on the top scope. */
// eslint-disable-next-line deprecation/deprecation
function traceHeaders(this: Hub): { [key: string]: string } {
// eslint-disable-next-line deprecation/deprecation
const scope = this.getScope();
// eslint-disable-next-line deprecation/deprecation
const span = scope.getSpan();
return span
? {
'sentry-trace': spanToTraceHeader(span),
}
: {};
}
/**
* Creates a new transaction and adds a sampling decision if it doesn't yet have one.
*
* The Hub.startTransaction method delegates to this method to do its work, passing the Hub instance in as `this`, as if
* it had been called on the hub directly. Exists as a separate function so that it can be injected into the class as an
* "extension method."
*
* @param this: The Hub starting the transaction
* @param transactionContext: Data used to configure the transaction
* @param CustomSamplingContext: Optional data to be provided to the `tracesSampler` function (if any)
*
* @returns The new transaction
*
* @see {@link Hub.startTransaction}
*/
function _startTransaction(
// eslint-disable-next-line deprecation/deprecation
this: Hub,
transactionContext: TransactionContext,
customSamplingContext?: CustomSamplingContext,
): Transaction {
// eslint-disable-next-line deprecation/deprecation
const client = this.getClient();
const options: Partial<ClientOptions> = (client && client.getOptions()) || {};
const configInstrumenter = options.instrumenter || 'sentry';
const transactionInstrumenter = transactionContext.instrumenter || 'sentry';
if (configInstrumenter !== transactionInstrumenter) {
DEBUG_BUILD &&
logger.error(
`A transaction was started with instrumenter=\`${transactionInstrumenter}\`, but the SDK is configured with the \`${configInstrumenter}\` instrumenter.
The transaction will not be sampled. Please use the ${configInstrumenter} instrumentation to start transactions.`,
);
// eslint-disable-next-line deprecation/deprecation
transactionContext.sampled = false;
}
// eslint-disable-next-line deprecation/deprecation
let transaction = new Transaction(transactionContext, this);
transaction = sampleTransaction(transaction, options, {
name: transactionContext.name,
parentSampled: transactionContext.parentSampled,
transactionContext,
attributes: {
// eslint-disable-next-line deprecation/deprecation
...transactionContext.data,
...transactionContext.attributes,
},
...customSamplingContext,
});
if (transaction.isRecording()) {
transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans as number));
}
if (client && client.emit) {
client.emit('startTransaction', transaction);
}
return transaction;
}
/**
* Create new idle transaction.
*/
export function startIdleTransaction(
// eslint-disable-next-line deprecation/deprecation
hub: Hub,
transactionContext: TransactionContext,
idleTimeout: number,
finalTimeout: number,
onScope?: boolean,
customSamplingContext?: CustomSamplingContext,
heartbeatInterval?: number,
delayAutoFinishUntilSignal: boolean = false,
): IdleTransaction {
// eslint-disable-next-line deprecation/deprecation
const client = hub.getClient();
const options: Partial<ClientOptions> = (client && client.getOptions()) || {};
// eslint-disable-next-line deprecation/deprecation
let transaction = new IdleTransaction(
transactionContext,
hub,
idleTimeout,
finalTimeout,
heartbeatInterval,
onScope,
delayAutoFinishUntilSignal,
);
transaction = sampleTransaction(transaction, options, {
name: transactionContext.name,
parentSampled: transactionContext.parentSampled,
transactionContext,
attributes: {
// eslint-disable-next-line deprecation/deprecation
...transactionContext.data,
...transactionContext.attributes,
},
...customSamplingContext,
});
if (transaction.isRecording()) {
transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans as number));
}
if (client && client.emit) {
client.emit('startTransaction', transaction);
}
return transaction;
}
/**
* Adds tracing extensions to the global hub.
*/
export function addTracingExtensions(): void {
const carrier = getMainCarrier();
if (!carrier.__SENTRY__) {
return;
}
carrier.__SENTRY__.extensions = carrier.__SENTRY__.extensions || {};
if (!carrier.__SENTRY__.extensions.startTransaction) {
carrier.__SENTRY__.extensions.startTransaction = _startTransaction;
}
if (!carrier.__SENTRY__.extensions.traceHeaders) {
carrier.__SENTRY__.extensions.traceHeaders = traceHeaders;
}
registerErrorInstrumentation();
}