@sentry/opentelemetry 7.105.0 → 8.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/README.md +8 -14
  2. package/cjs/asyncContextStrategy.js +87 -19
  3. package/cjs/asyncContextStrategy.js.map +1 -1
  4. package/cjs/constants.js +16 -4
  5. package/cjs/constants.js.map +1 -1
  6. package/cjs/contextManager.js +42 -15
  7. package/cjs/contextManager.js.map +1 -1
  8. package/cjs/custom/client.js +0 -28
  9. package/cjs/custom/client.js.map +1 -1
  10. package/cjs/custom/getCurrentHub.js +106 -0
  11. package/cjs/custom/getCurrentHub.js.map +1 -0
  12. package/cjs/custom/hub.js +3 -136
  13. package/cjs/custom/hub.js.map +1 -1
  14. package/cjs/custom/transaction.js +3 -32
  15. package/cjs/custom/transaction.js.map +1 -1
  16. package/cjs/index.js +9 -9
  17. package/cjs/index.js.map +1 -1
  18. package/cjs/propagator.js +125 -40
  19. package/cjs/propagator.js.map +1 -1
  20. package/cjs/sampler.js +7 -6
  21. package/cjs/sampler.js.map +1 -1
  22. package/cjs/semanticAttributes.js +0 -9
  23. package/cjs/semanticAttributes.js.map +1 -1
  24. package/cjs/setupEventContextTrace.js +12 -7
  25. package/cjs/setupEventContextTrace.js.map +1 -1
  26. package/cjs/spanExporter.js +62 -58
  27. package/cjs/spanExporter.js.map +1 -1
  28. package/cjs/spanProcessor.js +16 -18
  29. package/cjs/spanProcessor.js.map +1 -1
  30. package/cjs/trace.js +120 -23
  31. package/cjs/trace.js.map +1 -1
  32. package/cjs/utils/addOriginToSpan.js +2 -2
  33. package/cjs/utils/addOriginToSpan.js.map +1 -1
  34. package/cjs/utils/contextData.js +35 -15
  35. package/cjs/utils/contextData.js.map +1 -1
  36. package/cjs/utils/dynamicSamplingContext.js +64 -0
  37. package/cjs/utils/dynamicSamplingContext.js.map +1 -0
  38. package/cjs/utils/mapStatus.js +5 -4
  39. package/cjs/utils/mapStatus.js.map +1 -1
  40. package/cjs/utils/setupCheck.js +17 -0
  41. package/cjs/utils/setupCheck.js.map +1 -0
  42. package/cjs/utils/spanData.js +23 -23
  43. package/cjs/utils/spanData.js.map +1 -1
  44. package/esm/asyncContextStrategy.js +89 -18
  45. package/esm/asyncContextStrategy.js.map +1 -1
  46. package/esm/constants.js +11 -4
  47. package/esm/constants.js.map +1 -1
  48. package/esm/contextManager.js +41 -17
  49. package/esm/contextManager.js.map +1 -1
  50. package/esm/custom/client.js +0 -28
  51. package/esm/custom/client.js.map +1 -1
  52. package/esm/custom/getCurrentHub.js +101 -0
  53. package/esm/custom/getCurrentHub.js.map +1 -0
  54. package/esm/custom/hub.js +4 -133
  55. package/esm/custom/hub.js.map +1 -1
  56. package/esm/custom/transaction.js +4 -32
  57. package/esm/custom/transaction.js.map +1 -1
  58. package/esm/index.js +7 -5
  59. package/esm/index.js.map +1 -1
  60. package/esm/propagator.js +128 -45
  61. package/esm/propagator.js.map +1 -1
  62. package/esm/sampler.js +7 -6
  63. package/esm/sampler.js.map +1 -1
  64. package/esm/semanticAttributes.js +0 -9
  65. package/esm/semanticAttributes.js.map +1 -1
  66. package/esm/setupEventContextTrace.js +14 -9
  67. package/esm/setupEventContextTrace.js.map +1 -1
  68. package/esm/spanExporter.js +63 -59
  69. package/esm/spanExporter.js.map +1 -1
  70. package/esm/spanProcessor.js +12 -14
  71. package/esm/spanProcessor.js.map +1 -1
  72. package/esm/trace.js +122 -26
  73. package/esm/trace.js.map +1 -1
  74. package/esm/utils/addOriginToSpan.js +2 -2
  75. package/esm/utils/addOriginToSpan.js.map +1 -1
  76. package/esm/utils/contextData.js +33 -15
  77. package/esm/utils/contextData.js.map +1 -1
  78. package/esm/utils/dynamicSamplingContext.js +59 -0
  79. package/esm/utils/dynamicSamplingContext.js.map +1 -0
  80. package/esm/utils/mapStatus.js +5 -4
  81. package/esm/utils/mapStatus.js.map +1 -1
  82. package/esm/utils/setupCheck.js +14 -0
  83. package/esm/utils/setupCheck.js.map +1 -0
  84. package/esm/utils/spanData.js +22 -20
  85. package/esm/utils/spanData.js.map +1 -1
  86. package/package.json +5 -5
  87. package/types/asyncContextStrategy.d.ts.map +1 -1
  88. package/types/constants.d.ts +6 -2
  89. package/types/constants.d.ts.map +1 -1
  90. package/types/contextManager.d.ts +1 -1
  91. package/types/contextManager.d.ts.map +1 -1
  92. package/types/custom/client.d.ts.map +1 -1
  93. package/types/custom/getCurrentHub.d.ts +7 -0
  94. package/types/custom/getCurrentHub.d.ts.map +1 -0
  95. package/types/custom/hub.d.ts +2 -33
  96. package/types/custom/hub.d.ts.map +1 -1
  97. package/types/custom/transaction.d.ts +1 -10
  98. package/types/custom/transaction.d.ts.map +1 -1
  99. package/types/index.d.ts +7 -5
  100. package/types/index.d.ts.map +1 -1
  101. package/types/propagator.d.ts +11 -2
  102. package/types/propagator.d.ts.map +1 -1
  103. package/types/sampler.d.ts.map +1 -1
  104. package/types/semanticAttributes.d.ts +0 -9
  105. package/types/semanticAttributes.d.ts.map +1 -1
  106. package/types/setupEventContextTrace.d.ts.map +1 -1
  107. package/types/spanExporter.d.ts.map +1 -1
  108. package/types/spanProcessor.d.ts +1 -5
  109. package/types/spanProcessor.d.ts.map +1 -1
  110. package/types/trace.d.ts +14 -3
  111. package/types/trace.d.ts.map +1 -1
  112. package/types/types.d.ts +7 -12
  113. package/types/types.d.ts.map +1 -1
  114. package/types/utils/addOriginToSpan.d.ts.map +1 -1
  115. package/types/utils/contextData.d.ts +21 -10
  116. package/types/utils/contextData.d.ts.map +1 -1
  117. package/types/utils/dynamicSamplingContext.d.ts +11 -0
  118. package/types/utils/dynamicSamplingContext.d.ts.map +1 -0
  119. package/types/utils/mapStatus.d.ts +2 -2
  120. package/types/utils/mapStatus.d.ts.map +1 -1
  121. package/types/utils/setupCheck.d.ts +9 -0
  122. package/types/utils/setupCheck.d.ts.map +1 -0
  123. package/types/utils/spanData.d.ts +14 -8
  124. package/types/utils/spanData.d.ts.map +1 -1
  125. package/types-ts3.8/constants.d.ts +6 -2
  126. package/types-ts3.8/contextManager.d.ts +1 -1
  127. package/types-ts3.8/custom/getCurrentHub.d.ts +7 -0
  128. package/types-ts3.8/custom/hub.d.ts +2 -33
  129. package/types-ts3.8/custom/transaction.d.ts +1 -10
  130. package/types-ts3.8/index.d.ts +7 -5
  131. package/types-ts3.8/propagator.d.ts +11 -2
  132. package/types-ts3.8/semanticAttributes.d.ts +0 -9
  133. package/types-ts3.8/spanProcessor.d.ts +1 -5
  134. package/types-ts3.8/trace.d.ts +14 -3
  135. package/types-ts3.8/types.d.ts +7 -12
  136. package/types-ts3.8/utils/contextData.d.ts +21 -10
  137. package/types-ts3.8/utils/dynamicSamplingContext.d.ts +11 -0
  138. package/types-ts3.8/utils/mapStatus.d.ts +2 -2
  139. package/types-ts3.8/utils/setupCheck.d.ts +9 -0
  140. package/types-ts3.8/utils/spanData.d.ts +14 -8
  141. package/cjs/custom/scope.js +0 -202
  142. package/cjs/custom/scope.js.map +0 -1
  143. package/esm/custom/scope.js +0 -200
  144. package/esm/custom/scope.js.map +0 -1
  145. package/types/custom/scope.d.ts +0 -42
  146. package/types/custom/scope.d.ts.map +0 -1
  147. package/types-ts3.8/custom/scope.d.ts +0 -42
package/cjs/custom/hub.js CHANGED
@@ -1,146 +1,13 @@
1
1
  Object.defineProperty(exports, '__esModule', { value: true });
2
2
 
3
- const core = require('@sentry/core');
4
- const utils = require('@sentry/utils');
5
- const scope = require('./scope.js');
6
-
7
- /**
8
- * A custom hub that ensures we always creat an OTEL scope.
9
- * Exported only for testing
10
- */
11
- class OpenTelemetryHub extends core.Hub {
12
- constructor(client, scope$1 = new scope.OpenTelemetryScope()) {
13
- super(client, scope$1);
14
- }
15
- }
16
-
17
- /** Custom getClient method that uses the custom hub. */
18
- function getClient() {
19
- // eslint-disable-next-line deprecation/deprecation
20
- return getCurrentHub().getClient();
21
- }
22
-
23
- /**
24
- * *******************************************************************************
25
- * Everything below here is a copy of the stuff from core's hub.ts,
26
- * only that we make sure to create our custom NodeExperimentalScope instead of the default Scope.
27
- * This is necessary to get the correct breadcrumbs behavior.
28
- *
29
- * Basically, this overwrites all places that do `new Scope()` with `new NodeExperimentalScope()`.
30
- * Which in turn means overwriting all places that do `new Hub()` and make sure to pass in a NodeExperimentalScope instead.
31
- * *******************************************************************************
32
- */
33
-
34
3
  /**
35
- * API compatibility version of this hub.
4
+ * Ensure the global hub is setup.
36
5
  *
37
- * WARNING: This number should only be increased when the global interface
38
- * changes and new methods are introduced.
39
- *
40
- * @hidden
41
- */
42
- const API_VERSION = 4;
43
-
44
- /**
45
- * Returns the default hub instance.
46
- *
47
- * If a hub is already registered in the global carrier but this module
48
- * contains a more recent version, it replaces the registered version.
49
- * Otherwise, the currently registered hub will be returned.
50
- */
51
- function getCurrentHub() {
52
- // Get main carrier (global for every environment)
53
- const registry = getMainCarrier();
54
-
55
- if (registry.__SENTRY__ && registry.__SENTRY__.acs) {
56
- const hub = registry.__SENTRY__.acs.getCurrentHub();
57
-
58
- if (hub) {
59
- return hub;
60
- }
61
- }
62
-
63
- // Return hub that lives on a global object
64
- return getGlobalHub(registry);
65
- }
66
-
67
- /**
68
- * Ensure the global hub is an OpenTelemetryHub.
6
+ * @deprecated This will be removed in the next major version.
69
7
  */
70
8
  function setupGlobalHub() {
71
- const globalRegistry = getMainCarrier();
72
-
73
- if (getGlobalHub(globalRegistry) instanceof OpenTelemetryHub) {
74
- return;
75
- }
76
-
77
- // If the current global hub is not correct, ensure we overwrite it
78
- setHubOnCarrier(globalRegistry, new OpenTelemetryHub());
79
- }
80
-
81
- /**
82
- * This will create a new {@link Hub} and add to the passed object on
83
- * __SENTRY__.hub.
84
- * @param carrier object
85
- * @hidden
86
- */
87
- function getHubFromCarrier(carrier) {
88
- return utils.getGlobalSingleton('hub', () => new OpenTelemetryHub(), carrier);
89
- }
90
-
91
- function getGlobalHub(registry = getMainCarrier()) {
92
- // If there's no hub, or its an old API, assign a new one
93
- if (
94
- !hasHubOnCarrier(registry) ||
95
- // eslint-disable-next-line deprecation/deprecation
96
- getHubFromCarrier(registry).isOlderThan(API_VERSION)
97
- ) {
98
- setHubOnCarrier(registry, new OpenTelemetryHub());
99
- }
100
-
101
- // Return hub that lives on a global object
102
- return getHubFromCarrier(registry);
103
- }
104
-
105
- /**
106
- * This will tell whether a carrier has a hub on it or not
107
- * @param carrier object
108
- */
109
- function hasHubOnCarrier(carrier) {
110
- return !!(carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub);
111
- }
112
-
113
- /**
114
- * Returns the global shim registry.
115
- *
116
- * FIXME: This function is problematic, because despite always returning a valid Carrier,
117
- * it has an optional `__SENTRY__` property, which then in turn requires us to always perform an unnecessary check
118
- * at the call-site. We always access the carrier through this function, so we can guarantee that `__SENTRY__` is there.
119
- **/
120
- function getMainCarrier() {
121
- utils.GLOBAL_OBJ.__SENTRY__ = utils.GLOBAL_OBJ.__SENTRY__ || {
122
- extensions: {},
123
- hub: undefined,
124
- };
125
- return utils.GLOBAL_OBJ;
126
- }
127
-
128
- /**
129
- * This will set passed {@link Hub} on the passed object's __SENTRY__.hub attribute
130
- * @param carrier object
131
- * @param hub Hub
132
- * @returns A boolean indicating success or failure
133
- */
134
- function setHubOnCarrier(carrier, hub) {
135
- if (!carrier) return false;
136
- const __SENTRY__ = (carrier.__SENTRY__ = carrier.__SENTRY__ || {});
137
- __SENTRY__.hub = hub;
138
- return true;
9
+ // noop
139
10
  }
140
11
 
141
- exports.OpenTelemetryHub = OpenTelemetryHub;
142
- exports.getClient = getClient;
143
- exports.getCurrentHub = getCurrentHub;
144
- exports.getHubFromCarrier = getHubFromCarrier;
145
12
  exports.setupGlobalHub = setupGlobalHub;
146
13
  //# sourceMappingURL=hub.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"hub.js","sources":["../../../src/custom/hub.ts"],"sourcesContent":["import type { Carrier, Scope } from '@sentry/core';\nimport { Hub } from '@sentry/core';\nimport type { Client } from '@sentry/types';\nimport { GLOBAL_OBJ, getGlobalSingleton } from '@sentry/utils';\n\nimport { OpenTelemetryScope } from './scope';\n\n/**\n * A custom hub that ensures we always creat an OTEL scope.\n * Exported only for testing\n */\nexport class OpenTelemetryHub extends Hub {\n public constructor(client?: Client, scope: Scope = new OpenTelemetryScope()) {\n super(client, scope);\n }\n}\n\n/** Custom getClient method that uses the custom hub. */\nexport function getClient<C extends Client>(): C | undefined {\n // eslint-disable-next-line deprecation/deprecation\n return getCurrentHub().getClient<C>();\n}\n\n/**\n * *******************************************************************************\n * Everything below here is a copy of the stuff from core's hub.ts,\n * only that we make sure to create our custom NodeExperimentalScope instead of the default Scope.\n * This is necessary to get the correct breadcrumbs behavior.\n *\n * Basically, this overwrites all places that do `new Scope()` with `new NodeExperimentalScope()`.\n * Which in turn means overwriting all places that do `new Hub()` and make sure to pass in a NodeExperimentalScope instead.\n * *******************************************************************************\n */\n\n/**\n * API compatibility version of this hub.\n *\n * WARNING: This number should only be increased when the global interface\n * changes and new methods are introduced.\n *\n * @hidden\n */\nconst API_VERSION = 4;\n\n/**\n * Returns the default hub instance.\n *\n * If a hub is already registered in the global carrier but this module\n * contains a more recent version, it replaces the registered version.\n * Otherwise, the currently registered hub will be returned.\n */\nexport function getCurrentHub(): Hub {\n // Get main carrier (global for every environment)\n const registry = getMainCarrier();\n\n if (registry.__SENTRY__ && registry.__SENTRY__.acs) {\n const hub = registry.__SENTRY__.acs.getCurrentHub();\n\n if (hub) {\n return hub;\n }\n }\n\n // Return hub that lives on a global object\n return getGlobalHub(registry);\n}\n\n/**\n * Ensure the global hub is an OpenTelemetryHub.\n */\nexport function setupGlobalHub(): void {\n const globalRegistry = getMainCarrier();\n\n if (getGlobalHub(globalRegistry) instanceof OpenTelemetryHub) {\n return;\n }\n\n // If the current global hub is not correct, ensure we overwrite it\n setHubOnCarrier(globalRegistry, new OpenTelemetryHub());\n}\n\n/**\n * This will create a new {@link Hub} and add to the passed object on\n * __SENTRY__.hub.\n * @param carrier object\n * @hidden\n */\nexport function getHubFromCarrier(carrier: Carrier): Hub {\n return getGlobalSingleton<Hub>('hub', () => new OpenTelemetryHub(), carrier);\n}\n\n/**\n * @private Private API with no semver guarantees!\n *\n * If the carrier does not contain a hub, a new hub is created with the global hub client and scope.\n */\nexport function ensureHubOnCarrier(carrier: Carrier, parent: Hub = getGlobalHub()): void {\n // If there's no hub on current domain, or it's an old API, assign a new one\n if (\n !hasHubOnCarrier(carrier) ||\n // eslint-disable-next-line deprecation/deprecation\n getHubFromCarrier(carrier).isOlderThan(API_VERSION)\n ) {\n // eslint-disable-next-line deprecation/deprecation\n const globalHubTopStack = parent.getStackTop();\n setHubOnCarrier(carrier, new OpenTelemetryHub(globalHubTopStack.client, globalHubTopStack.scope.clone()));\n }\n}\n\nfunction getGlobalHub(registry: Carrier = getMainCarrier()): Hub {\n // If there's no hub, or its an old API, assign a new one\n if (\n !hasHubOnCarrier(registry) ||\n // eslint-disable-next-line deprecation/deprecation\n getHubFromCarrier(registry).isOlderThan(API_VERSION)\n ) {\n setHubOnCarrier(registry, new OpenTelemetryHub());\n }\n\n // Return hub that lives on a global object\n return getHubFromCarrier(registry);\n}\n\n/**\n * This will tell whether a carrier has a hub on it or not\n * @param carrier object\n */\nfunction hasHubOnCarrier(carrier: Carrier): boolean {\n return !!(carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub);\n}\n\n/**\n * Returns the global shim registry.\n *\n * FIXME: This function is problematic, because despite always returning a valid Carrier,\n * it has an optional `__SENTRY__` property, which then in turn requires us to always perform an unnecessary check\n * at the call-site. We always access the carrier through this function, so we can guarantee that `__SENTRY__` is there.\n **/\nfunction getMainCarrier(): Carrier {\n GLOBAL_OBJ.__SENTRY__ = GLOBAL_OBJ.__SENTRY__ || {\n extensions: {},\n hub: undefined,\n };\n return GLOBAL_OBJ;\n}\n\n/**\n * This will set passed {@link Hub} on the passed object's __SENTRY__.hub attribute\n * @param carrier object\n * @param hub Hub\n * @returns A boolean indicating success or failure\n */\nfunction setHubOnCarrier(carrier: Carrier, hub: Hub): boolean {\n if (!carrier) return false;\n const __SENTRY__ = (carrier.__SENTRY__ = carrier.__SENTRY__ || {});\n __SENTRY__.hub = hub;\n return true;\n}\n"],"names":["Hub","scope","OpenTelemetryScope","getGlobalSingleton","GLOBAL_OBJ"],"mappings":"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"}
1
+ {"version":3,"file":"hub.js","sources":["../../../src/custom/hub.ts"],"sourcesContent":["/**\n * Ensure the global hub is setup.\n *\n * @deprecated This will be removed in the next major version.\n */\nexport function setupGlobalHub(): void {\n // noop\n}\n"],"names":[],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACO,SAAS,cAAc,GAAS;AACvC;AACA;;;;"}
@@ -1,7 +1,6 @@
1
1
  Object.defineProperty(exports, '__esModule', { value: true });
2
2
 
3
3
  const core = require('@sentry/core');
4
- const utils = require('@sentry/utils');
5
4
 
6
5
  /**
7
6
  * This is a fork of core's tracing/hubextensions.ts _startTransaction,
@@ -10,46 +9,18 @@ const utils = require('@sentry/utils');
10
9
  function startTransaction(hub, transactionContext) {
11
10
  // eslint-disable-next-line deprecation/deprecation
12
11
  const client = hub.getClient();
13
- const options = (client && client.getOptions()) || {};
14
12
 
15
13
  // eslint-disable-next-line deprecation/deprecation
16
- const transaction = new OpenTelemetryTransaction(transactionContext, hub );
14
+ const transaction = new core.Transaction(transactionContext, hub );
17
15
  // Since we do not do sampling here, we assume that this is _always_ sampled
18
16
  // Any sampling decision happens in OpenTelemetry's sampler
19
- transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans ));
17
+ transaction.initSpanRecorder();
20
18
 
21
- if (client && client.emit) {
19
+ if (client) {
22
20
  client.emit('startTransaction', transaction);
23
21
  }
24
22
  return transaction;
25
23
  }
26
24
 
27
- /**
28
- * This is a fork of the base Transaction with OTEL specific stuff added.
29
- */
30
- class OpenTelemetryTransaction extends core.Transaction {
31
- /**
32
- * Finish the transaction, but apply the given scope instead of the current one.
33
- */
34
- finishWithScope(endTimestamp, scope) {
35
- const event = this._finishTransaction(endTimestamp);
36
-
37
- if (!event) {
38
- return undefined;
39
- }
40
-
41
- // eslint-disable-next-line deprecation/deprecation
42
- const client = this._hub.getClient();
43
-
44
- if (!client) {
45
- return undefined;
46
- }
47
-
48
- const eventId = utils.uuid4();
49
- return client.captureEvent(event, { event_id: eventId }, scope);
50
- }
51
- }
52
-
53
- exports.OpenTelemetryTransaction = OpenTelemetryTransaction;
54
25
  exports.startTransaction = startTransaction;
55
26
  //# sourceMappingURL=transaction.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"transaction.js","sources":["../../../src/custom/transaction.ts"],"sourcesContent":["import type { Hub } from '@sentry/core';\nimport { Transaction } from '@sentry/core';\nimport type { ClientOptions, Hub as HubInterface, Scope, TransactionContext } from '@sentry/types';\nimport { uuid4 } from '@sentry/utils';\n\n/**\n * This is a fork of core's tracing/hubextensions.ts _startTransaction,\n * with some OTEL specifics.\n */\nexport function startTransaction(hub: HubInterface, transactionContext: TransactionContext): Transaction {\n // eslint-disable-next-line deprecation/deprecation\n const client = hub.getClient();\n const options: Partial<ClientOptions> = (client && client.getOptions()) || {};\n\n // eslint-disable-next-line deprecation/deprecation\n const transaction = new OpenTelemetryTransaction(transactionContext, hub as Hub);\n // Since we do not do sampling here, we assume that this is _always_ sampled\n // Any sampling decision happens in OpenTelemetry's sampler\n transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans as number));\n\n if (client && client.emit) {\n client.emit('startTransaction', transaction);\n }\n return transaction;\n}\n\n/**\n * This is a fork of the base Transaction with OTEL specific stuff added.\n */\nexport class OpenTelemetryTransaction extends Transaction {\n /**\n * Finish the transaction, but apply the given scope instead of the current one.\n */\n public finishWithScope(endTimestamp?: number, scope?: Scope): string | undefined {\n const event = this._finishTransaction(endTimestamp);\n\n if (!event) {\n return undefined;\n }\n\n // eslint-disable-next-line deprecation/deprecation\n const client = this._hub.getClient();\n\n if (!client) {\n return undefined;\n }\n\n const eventId = uuid4();\n return client.captureEvent(event, { event_id: eventId }, scope);\n }\n}\n"],"names":["Transaction","uuid4"],"mappings":";;;;;AAKA;AACA;AACA;AACA;AACO,SAAS,gBAAgB,CAAC,GAAG,EAAgB,kBAAkB,EAAmC;AACzG;AACA,EAAE,MAAM,MAAO,GAAE,GAAG,CAAC,SAAS,EAAE,CAAA;AAChC,EAAE,MAAM,OAAO,GAA2B,CAAC,MAAO,IAAG,MAAM,CAAC,UAAU,EAAE,KAAK,EAAE,CAAA;AAC/E;AACA;AACA,EAAE,MAAM,cAAc,IAAI,wBAAwB,CAAC,kBAAkB,EAAE,GAAA,EAAW,CAAA;AAClF;AACA;AACA,EAAE,WAAW,CAAC,gBAAgB,CAAC,OAAO,CAAC,YAAA,KAAiB,OAAO,CAAC,YAAY,CAAC,QAAA,EAAmB,CAAC,CAAA;AACjG;AACA,EAAE,IAAI,MAAA,IAAU,MAAM,CAAC,IAAI,EAAE;AAC7B,IAAI,MAAM,CAAC,IAAI,CAAC,kBAAkB,EAAE,WAAW,CAAC,CAAA;AAChD,GAAE;AACF,EAAE,OAAO,WAAW,CAAA;AACpB,CAAA;AACA;AACA;AACA;AACA;AACO,MAAM,wBAAyB,SAAQA,gBAAY,CAAA;AAC1D;AACA;AACA;AACA,GAAS,eAAe,CAAC,YAAY,EAAW,KAAK,EAA8B;AACnF,IAAI,MAAM,QAAQ,IAAI,CAAC,kBAAkB,CAAC,YAAY,CAAC,CAAA;AACvD;AACA,IAAI,IAAI,CAAC,KAAK,EAAE;AAChB,MAAM,OAAO,SAAS,CAAA;AACtB,KAAI;AACJ;AACA;AACA,IAAI,MAAM,SAAS,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAA;AACxC;AACA,IAAI,IAAI,CAAC,MAAM,EAAE;AACjB,MAAM,OAAO,SAAS,CAAA;AACtB,KAAI;AACJ;AACA,IAAI,MAAM,OAAA,GAAUC,WAAK,EAAE,CAAA;AAC3B,IAAI,OAAO,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,OAAA,EAAS,EAAE,KAAK,CAAC,CAAA;AACnE,GAAE;AACF;;;;;"}
1
+ {"version":3,"file":"transaction.js","sources":["../../../src/custom/transaction.ts"],"sourcesContent":["import type { Hub } from '@sentry/core';\nimport { Transaction } from '@sentry/core';\nimport type { Hub as HubInterface, TransactionContext } from '@sentry/types';\n\n/**\n * This is a fork of core's tracing/hubextensions.ts _startTransaction,\n * with some OTEL specifics.\n */\nexport function startTransaction(hub: HubInterface, transactionContext: TransactionContext): Transaction {\n // eslint-disable-next-line deprecation/deprecation\n const client = hub.getClient();\n\n // eslint-disable-next-line deprecation/deprecation\n const transaction = new Transaction(transactionContext, hub as Hub);\n // Since we do not do sampling here, we assume that this is _always_ sampled\n // Any sampling decision happens in OpenTelemetry's sampler\n transaction.initSpanRecorder();\n\n if (client) {\n client.emit('startTransaction', transaction);\n }\n return transaction;\n}\n"],"names":["Transaction"],"mappings":";;;;AAIA;AACA;AACA;AACA;AACO,SAAS,gBAAgB,CAAC,GAAG,EAAgB,kBAAkB,EAAmC;AACzG;AACA,EAAE,MAAM,MAAO,GAAE,GAAG,CAAC,SAAS,EAAE,CAAA;AAChC;AACA;AACA,EAAE,MAAM,cAAc,IAAIA,gBAAW,CAAC,kBAAkB,EAAE,GAAA,EAAW,CAAA;AACrE;AACA;AACA,EAAE,WAAW,CAAC,gBAAgB,EAAE,CAAA;AAChC;AACA,EAAE,IAAI,MAAM,EAAE;AACd,IAAI,MAAM,CAAC,IAAI,CAAC,kBAAkB,EAAE,WAAW,CAAC,CAAA;AAChD,GAAE;AACF,EAAE,OAAO,WAAW,CAAA;AACpB;;;;"}
package/cjs/index.js CHANGED
@@ -12,7 +12,7 @@ const isSentryRequest = require('./utils/isSentryRequest.js');
12
12
  const getActiveSpan = require('./utils/getActiveSpan.js');
13
13
  const trace = require('./trace.js');
14
14
  const hub = require('./custom/hub.js');
15
- const scope = require('./custom/scope.js');
15
+ const getCurrentHub = require('./custom/getCurrentHub.js');
16
16
  const hubextensions = require('./custom/hubextensions.js');
17
17
  const setupEventContextTrace = require('./setupEventContextTrace.js');
18
18
  const asyncContextStrategy = require('./asyncContextStrategy.js');
@@ -20,6 +20,8 @@ const contextManager = require('./contextManager.js');
20
20
  const propagator = require('./propagator.js');
21
21
  const spanProcessor = require('./spanProcessor.js');
22
22
  const sampler = require('./sampler.js');
23
+ const setupCheck = require('./utils/setupCheck.js');
24
+ const core = require('@sentry/core');
23
25
 
24
26
  /**
25
27
  * The following internal utils are not considered public API and are subject to change.
@@ -33,15 +35,12 @@ const _INTERNAL = {
33
35
 
34
36
  exports.wrapClientClass = client.wrapClientClass;
35
37
  exports.getSpanKind = getSpanKind.getSpanKind;
36
- exports.getSpanFinishScope = spanData.getSpanFinishScope;
37
38
  exports.getSpanHub = spanData.getSpanHub;
38
39
  exports.getSpanMetadata = spanData.getSpanMetadata;
39
40
  exports.getSpanParent = spanData.getSpanParent;
40
- exports.getSpanScope = spanData.getSpanScope;
41
+ exports.getSpanScopes = spanData.getSpanScopes;
41
42
  exports.setSpanMetadata = spanData.setSpanMetadata;
42
- exports.getPropagationContextFromContext = contextData.getPropagationContextFromContext;
43
- exports.setHubOnContext = contextData.setHubOnContext;
44
- exports.setPropagationContextOnContext = contextData.setPropagationContextOnContext;
43
+ exports.getScopesFromContext = contextData.getScopesFromContext;
45
44
  exports.spanHasAttributes = spanTypes.spanHasAttributes;
46
45
  exports.spanHasEvents = spanTypes.spanHasEvents;
47
46
  exports.spanHasKind = spanTypes.spanHasKind;
@@ -54,10 +53,9 @@ exports.getRootSpan = getActiveSpan.getRootSpan;
54
53
  exports.startInactiveSpan = trace.startInactiveSpan;
55
54
  exports.startSpan = trace.startSpan;
56
55
  exports.startSpanManual = trace.startSpanManual;
57
- exports.getClient = hub.getClient;
58
- exports.getCurrentHub = hub.getCurrentHub;
56
+ exports.withActiveSpan = trace.withActiveSpan;
59
57
  exports.setupGlobalHub = hub.setupGlobalHub;
60
- exports.OpenTelemetryScope = scope.OpenTelemetryScope;
58
+ exports.getCurrentHub = getCurrentHub.getCurrentHub;
61
59
  exports.addTracingExtensions = hubextensions.addTracingExtensions;
62
60
  exports.setupEventContextTrace = setupEventContextTrace.setupEventContextTrace;
63
61
  exports.setOpenTelemetryContextAsyncContextStrategy = asyncContextStrategy.setOpenTelemetryContextAsyncContextStrategy;
@@ -65,5 +63,7 @@ exports.wrapContextManagerClass = contextManager.wrapContextManagerClass;
65
63
  exports.SentryPropagator = propagator.SentryPropagator;
66
64
  exports.SentrySpanProcessor = spanProcessor.SentrySpanProcessor;
67
65
  exports.SentrySampler = sampler.SentrySampler;
66
+ exports.openTelemetrySetupCheck = setupCheck.openTelemetrySetupCheck;
67
+ exports.getClient = core.getClient;
68
68
  exports._INTERNAL = _INTERNAL;
69
69
  //# sourceMappingURL=index.js.map
package/cjs/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["import { addOriginToSpan } from './utils/addOriginToSpan';\nimport { maybeCaptureExceptionForTimedEvent } from './utils/captureExceptionForTimedEvent';\nimport { getRequestSpanData } from './utils/getRequestSpanData';\n\nexport type { OpenTelemetryClient } from './types';\nexport { wrapClientClass } from './custom/client';\n\nexport { getSpanKind } from './utils/getSpanKind';\nexport {\n getSpanHub,\n getSpanMetadata,\n getSpanParent,\n getSpanScope,\n setSpanMetadata,\n getSpanFinishScope,\n} from './utils/spanData';\n\nexport { getPropagationContextFromContext, setPropagationContextOnContext, setHubOnContext } from './utils/contextData';\n\nexport {\n spanHasAttributes,\n spanHasEvents,\n spanHasKind,\n spanHasName,\n spanHasParentId,\n spanHasStatus,\n} from './utils/spanTypes';\n\nexport { isSentryRequestSpan } from './utils/isSentryRequest';\n\nexport { getActiveSpan, getRootSpan } from './utils/getActiveSpan';\nexport { startSpan, startSpanManual, startInactiveSpan } from './trace';\n\nexport { getCurrentHub, setupGlobalHub, getClient } from './custom/hub';\nexport { OpenTelemetryScope } from './custom/scope';\nexport { addTracingExtensions } from './custom/hubextensions';\nexport { setupEventContextTrace } from './setupEventContextTrace';\n\nexport { setOpenTelemetryContextAsyncContextStrategy } from './asyncContextStrategy';\nexport { wrapContextManagerClass } from './contextManager';\nexport { SentryPropagator } from './propagator';\nexport { SentrySpanProcessor } from './spanProcessor';\nexport { SentrySampler } from './sampler';\n\n/**\n * The following internal utils are not considered public API and are subject to change.\n * @hidden\n */\nconst _INTERNAL = {\n addOriginToSpan,\n maybeCaptureExceptionForTimedEvent,\n getRequestSpanData,\n} as const;\n\nexport { _INTERNAL };\n"],"names":["addOriginToSpan","maybeCaptureExceptionForTimedEvent","getRequestSpanData"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA4CA;AACA;AACA;AACA;AACA,MAAM,YAAY;AAClB,mBAAEA,+BAAe;AACjB,sCAAEC,gEAAkC;AACpC,sBAAEC,qCAAkB;AACpB,CAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["import { addOriginToSpan } from './utils/addOriginToSpan';\nimport { maybeCaptureExceptionForTimedEvent } from './utils/captureExceptionForTimedEvent';\nimport { getRequestSpanData } from './utils/getRequestSpanData';\n\nexport type { OpenTelemetryClient } from './types';\nexport { wrapClientClass } from './custom/client';\n\nexport { getSpanKind } from './utils/getSpanKind';\nexport {\n getSpanHub,\n getSpanMetadata,\n getSpanParent,\n setSpanMetadata,\n getSpanScopes,\n} from './utils/spanData';\n\nexport { getScopesFromContext } from './utils/contextData';\n\nexport {\n spanHasAttributes,\n spanHasEvents,\n spanHasKind,\n spanHasName,\n spanHasParentId,\n spanHasStatus,\n} from './utils/spanTypes';\n\nexport { isSentryRequestSpan } from './utils/isSentryRequest';\n\nexport { getActiveSpan, getRootSpan } from './utils/getActiveSpan';\nexport { startSpan, startSpanManual, startInactiveSpan, withActiveSpan } from './trace';\n\n// eslint-disable-next-line deprecation/deprecation\nexport { setupGlobalHub } from './custom/hub';\n// eslint-disable-next-line deprecation/deprecation\nexport { getCurrentHub } from './custom/getCurrentHub';\nexport { addTracingExtensions } from './custom/hubextensions';\nexport { setupEventContextTrace } from './setupEventContextTrace';\n\nexport { setOpenTelemetryContextAsyncContextStrategy } from './asyncContextStrategy';\nexport { wrapContextManagerClass } from './contextManager';\nexport { SentryPropagator } from './propagator';\nexport { SentrySpanProcessor } from './spanProcessor';\nexport { SentrySampler } from './sampler';\n\nexport { openTelemetrySetupCheck } from './utils/setupCheck';\n\n// Legacy\nexport { getClient } from '@sentry/core';\n\n/**\n * The following internal utils are not considered public API and are subject to change.\n * @hidden\n */\nconst _INTERNAL = {\n addOriginToSpan,\n maybeCaptureExceptionForTimedEvent,\n getRequestSpanData,\n} as const;\n\nexport { _INTERNAL };\n"],"names":["addOriginToSpan","maybeCaptureExceptionForTimedEvent","getRequestSpanData"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAkDA;AACA;AACA;AACA;AACA,MAAM,YAAY;AAClB,mBAAEA,+BAAe;AACjB,sCAAEC,gEAAkC;AACpC,sBAAEC,qCAAkB;AACpB,CAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/cjs/propagator.js CHANGED
@@ -9,14 +9,36 @@ const core = require('@opentelemetry/core');
9
9
  const core$1 = require('@sentry/core');
10
10
  const utils = require('@sentry/utils');
11
11
  const constants = require('./constants.js');
12
- const hub = require('./custom/hub.js');
13
12
  const contextData = require('./utils/contextData.js');
14
- const spanData = require('./utils/spanData.js');
13
+ const setupCheck = require('./utils/setupCheck.js');
14
+
15
+ /** Get the Sentry propagation context from a span context. */
16
+ function getPropagationContextFromSpanContext(spanContext) {
17
+ const { traceId, spanId, traceFlags, traceState } = spanContext;
18
+
19
+ const dscString = traceState ? traceState.get(constants.SENTRY_TRACE_STATE_DSC) : undefined;
20
+ const dsc = dscString ? utils.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
21
+ const parentSpanId = traceState ? traceState.get(constants.SENTRY_TRACE_STATE_PARENT_SPAN_ID) : undefined;
22
+ const sampled = traceFlags === api.TraceFlags.SAMPLED;
23
+
24
+ return {
25
+ traceId,
26
+ spanId,
27
+ sampled,
28
+ parentSpanId,
29
+ dsc,
30
+ };
31
+ }
15
32
 
16
33
  /**
17
34
  * Injects and extracts `sentry-trace` and `baggage` headers from carriers.
18
35
  */
19
36
  class SentryPropagator extends core.W3CBaggagePropagator {
37
+ constructor() {
38
+ super();
39
+ setupCheck.setIsSetup('SentryPropagator');
40
+ }
41
+
20
42
  /**
21
43
  * @inheritDoc
22
44
  */
@@ -27,9 +49,7 @@ class SentryPropagator extends core.W3CBaggagePropagator {
27
49
 
28
50
  let baggage = api.propagation.getBaggage(context) || api.propagation.createBaggage({});
29
51
 
30
- const propagationContext = contextData.getPropagationContextFromContext(context);
31
- const { spanId, traceId, sampled } = getSentryTraceData(context, propagationContext);
32
- const dynamicSamplingContext = propagationContext ? getDsc(context, propagationContext, traceId) : undefined;
52
+ const { dynamicSamplingContext, traceId, spanId, sampled } = getInjectionData(context);
33
53
 
34
54
  if (dynamicSamplingContext) {
35
55
  baggage = Object.entries(dynamicSamplingContext).reduce((b, [dscKey, dscValue]) => {
@@ -60,18 +80,32 @@ class SentryPropagator extends core.W3CBaggagePropagator {
60
80
 
61
81
  const propagationContext = utils.propagationContextFromHeaders(sentryTraceHeader, maybeBaggageHeader);
62
82
 
63
- // Add propagation context to context
64
- const contextWithPropagationContext = contextData.setPropagationContextOnContext(context, propagationContext);
83
+ // We store the DSC as OTEL trace state on the span context
84
+ const traceState = makeTraceState({
85
+ parentSpanId: propagationContext.parentSpanId,
86
+ dsc: propagationContext.dsc,
87
+ });
65
88
 
66
89
  const spanContext = {
67
90
  traceId: propagationContext.traceId,
68
91
  spanId: propagationContext.parentSpanId || '',
69
92
  isRemote: true,
70
93
  traceFlags: propagationContext.sampled === true ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
94
+ traceState,
95
+ };
96
+
97
+ // Add remote parent span context,
98
+ const ctxWithSpanContext = api.trace.setSpanContext(context, spanContext);
99
+
100
+ // Also update the scope on the context (to be sure this is picked up everywhere)
101
+ const scopes = contextData.getScopesFromContext(ctxWithSpanContext);
102
+ const newScopes = {
103
+ scope: scopes ? scopes.scope.clone() : core$1.getCurrentScope().clone(),
104
+ isolationScope: scopes ? scopes.isolationScope : core$1.getIsolationScope(),
71
105
  };
106
+ newScopes.scope.setPropagationContext(propagationContext);
72
107
 
73
- // Add remote parent span context
74
- return api.trace.setSpanContext(contextWithPropagationContext, spanContext);
108
+ return contextData.setScopesOnContext(ctxWithSpanContext, newScopes);
75
109
  }
76
110
 
77
111
  /**
@@ -82,51 +116,102 @@ class SentryPropagator extends core.W3CBaggagePropagator {
82
116
  }
83
117
  }
84
118
 
85
- function getDsc(
86
- context,
87
- propagationContext,
88
- traceId,
89
- ) {
90
- // If we have a DSC on the propagation context, we just use it
91
- if (propagationContext.dsc) {
92
- return propagationContext.dsc;
119
+ /** Exported for tests. */
120
+ function makeTraceState({
121
+ parentSpanId,
122
+ dsc,
123
+ }) {
124
+ if (!parentSpanId && !dsc) {
125
+ return undefined;
93
126
  }
94
127
 
95
- // Else, we try to generate a new one
96
- const client = hub.getClient();
97
- const activeSpan = api.trace.getSpan(context);
98
- const scope = activeSpan ? spanData.getSpanScope(activeSpan) : undefined;
128
+ // We store the DSC as OTEL trace state on the span context
129
+ const dscString = dsc ? utils.dynamicSamplingContextToSentryBaggageHeader(dsc) : undefined;
99
130
 
100
- if (client) {
101
- return core$1.getDynamicSamplingContextFromClient(traceId || propagationContext.traceId, client, scope);
102
- }
131
+ const traceStateBase = parentSpanId
132
+ ? new core.TraceState().set(constants.SENTRY_TRACE_STATE_PARENT_SPAN_ID, parentSpanId)
133
+ : new core.TraceState();
103
134
 
104
- return undefined;
135
+ return dscString ? traceStateBase.set(constants.SENTRY_TRACE_STATE_DSC, dscString) : traceStateBase;
105
136
  }
106
137
 
107
- function getSentryTraceData(
108
- context,
109
- propagationContext,
110
- )
138
+ function getInjectionData(context)
111
139
 
112
140
  {
113
141
  const span = api.trace.getSpan(context);
114
- const spanContext = span && span.spanContext();
142
+ const spanIsRemote = _optionalChain([span, 'optionalAccess', _ => _.spanContext, 'call', _2 => _2(), 'access', _3 => _3.isRemote]);
143
+
144
+ // If we have a local span, we can just pick everything from it
145
+ if (span && !spanIsRemote) {
146
+ const spanContext = span.spanContext();
147
+ const propagationContext = getPropagationContextFromSpanContext(spanContext);
148
+ const dynamicSamplingContext = getDynamicSamplingContext(propagationContext, spanContext.traceId);
149
+ return {
150
+ dynamicSamplingContext,
151
+ traceId: spanContext.traceId,
152
+ spanId: spanContext.spanId,
153
+ sampled: spanContext.traceFlags === api.TraceFlags.SAMPLED,
154
+ };
155
+ }
115
156
 
116
- const traceId = spanContext ? spanContext.traceId : _optionalChain([propagationContext, 'optionalAccess', _ => _.traceId]);
157
+ // Else we try to use the propagation context from the scope
158
+ const scope = _optionalChain([contextData.getScopesFromContext, 'call', _4 => _4(context), 'optionalAccess', _5 => _5.scope]);
159
+ if (scope) {
160
+ const propagationContext = scope.getPropagationContext();
161
+ const dynamicSamplingContext = getDynamicSamplingContext(propagationContext, propagationContext.traceId);
162
+ return {
163
+ dynamicSamplingContext,
164
+ traceId: propagationContext.traceId,
165
+ spanId: propagationContext.spanId,
166
+ sampled: propagationContext.sampled,
167
+ };
168
+ }
169
+
170
+ // Else, we look at the remote span context
171
+ const spanContext = api.trace.getSpanContext(context);
172
+ if (spanContext) {
173
+ const propagationContext = getPropagationContextFromSpanContext(spanContext);
174
+ const dynamicSamplingContext = getDynamicSamplingContext(propagationContext, spanContext.traceId);
175
+
176
+ return {
177
+ dynamicSamplingContext,
178
+ traceId: spanContext.traceId,
179
+ spanId: spanContext.spanId,
180
+ sampled: spanContext.traceFlags === api.TraceFlags.SAMPLED,
181
+ };
182
+ }
183
+
184
+ // If we have neither, there is nothing much we can do, but that should not happen usually
185
+ // Unless there is a detached OTEL context being passed around
186
+ return {
187
+ dynamicSamplingContext: undefined,
188
+ traceId: undefined,
189
+ spanId: undefined,
190
+ sampled: undefined,
191
+ };
192
+ }
193
+
194
+ /** Get the DSC from a context, or fall back to use the one from the client. */
195
+ function getDynamicSamplingContext(
196
+ propagationContext,
197
+ traceId,
198
+ ) {
199
+ // If we have a DSC on the propagation context, we just use it
200
+ if (_optionalChain([propagationContext, 'optionalAccess', _6 => _6.dsc])) {
201
+ return propagationContext.dsc;
202
+ }
117
203
 
118
- // We have a few scenarios here:
119
- // If we have an active span, and it is _not_ remote, we just use the span's ID
120
- // If we have an active span that is remote, we do not want to use the spanId, as we don't want to attach it to the parent span
121
- // If `isRemote === true`, the span is bascially virtual
122
- // If we don't have a local active span, we use the generated spanId from the propagationContext
123
- const spanId = spanContext && !spanContext.isRemote ? spanContext.spanId : _optionalChain([propagationContext, 'optionalAccess', _2 => _2.spanId]);
204
+ // Else, we try to generate a new one
205
+ const client = core$1.getClient();
124
206
 
125
- // eslint-disable-next-line no-bitwise
126
- const sampled = spanContext ? Boolean(spanContext.traceFlags & api.TraceFlags.SAMPLED) : _optionalChain([propagationContext, 'optionalAccess', _3 => _3.sampled]);
207
+ if (client) {
208
+ return core$1.getDynamicSamplingContextFromClient(traceId || propagationContext.traceId, client);
209
+ }
127
210
 
128
- return { traceId, spanId, sampled };
211
+ return undefined;
129
212
  }
130
213
 
131
214
  exports.SentryPropagator = SentryPropagator;
215
+ exports.getPropagationContextFromSpanContext = getPropagationContextFromSpanContext;
216
+ exports.makeTraceState = makeTraceState;
132
217
  //# sourceMappingURL=propagator.js.map