span.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const utils = require('@sentry/utils');
  3. const debugBuild = require('../debug-build.js');
  4. const metricSummary = require('../metrics/metric-summary.js');
  5. const semanticAttributes = require('../semanticAttributes.js');
  6. const getRootSpan = require('../utils/getRootSpan.js');
  7. const spanUtils = require('../utils/spanUtils.js');
  8. const spanstatus = require('./spanstatus.js');
  9. /**
  10. * Keeps track of finished spans for a given transaction
  11. * @internal
  12. * @hideconstructor
  13. * @hidden
  14. */
  15. class SpanRecorder {
  16. constructor(maxlen = 1000) {
  17. this._maxlen = maxlen;
  18. this.spans = [];
  19. }
  20. /**
  21. * This is just so that we don't run out of memory while recording a lot
  22. * of spans. At some point we just stop and flush out the start of the
  23. * trace tree (i.e.the first n spans with the smallest
  24. * start_timestamp).
  25. */
  26. add(span) {
  27. if (this.spans.length > this._maxlen) {
  28. // eslint-disable-next-line deprecation/deprecation
  29. span.spanRecorder = undefined;
  30. } else {
  31. this.spans.push(span);
  32. }
  33. }
  34. }
  35. /**
  36. * Span contains all data about a span
  37. */
  38. class Span {
  39. /**
  40. * Tags for the span.
  41. * @deprecated Use `spanToJSON(span).atttributes` instead.
  42. */
  43. /**
  44. * Data for the span.
  45. * @deprecated Use `spanToJSON(span).atttributes` instead.
  46. */
  47. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  48. /**
  49. * List of spans that were finalized
  50. *
  51. * @deprecated This property will no longer be public. Span recording will be handled internally.
  52. */
  53. /**
  54. * @inheritDoc
  55. * @deprecated Use top level `Sentry.getRootSpan()` instead
  56. */
  57. /**
  58. * The instrumenter that created this span.
  59. *
  60. * TODO (v8): This can probably be replaced by an `instanceOf` check of the span class.
  61. * the instrumenter can only be sentry or otel so we can check the span instance
  62. * to verify which one it is and remove this field entirely.
  63. *
  64. * @deprecated This field will be removed.
  65. */
  66. /** Epoch timestamp in seconds when the span started. */
  67. /** Epoch timestamp in seconds when the span ended. */
  68. /** Internal keeper of the status */
  69. /**
  70. * You should never call the constructor manually, always use `Sentry.startTransaction()`
  71. * or call `startChild()` on an existing span.
  72. * @internal
  73. * @hideconstructor
  74. * @hidden
  75. */
  76. constructor(spanContext = {}) {
  77. this._traceId = spanContext.traceId || utils.uuid4();
  78. this._spanId = spanContext.spanId || utils.uuid4().substring(16);
  79. this._startTime = spanContext.startTimestamp || utils.timestampInSeconds();
  80. // eslint-disable-next-line deprecation/deprecation
  81. this.tags = spanContext.tags ? { ...spanContext.tags } : {};
  82. // eslint-disable-next-line deprecation/deprecation
  83. this.data = spanContext.data ? { ...spanContext.data } : {};
  84. // eslint-disable-next-line deprecation/deprecation
  85. this.instrumenter = spanContext.instrumenter || 'sentry';
  86. this._attributes = {};
  87. this.setAttributes({
  88. [semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: spanContext.origin || 'manual',
  89. [semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_OP]: spanContext.op,
  90. ...spanContext.attributes,
  91. });
  92. // eslint-disable-next-line deprecation/deprecation
  93. this._name = spanContext.name || spanContext.description;
  94. if (spanContext.parentSpanId) {
  95. this._parentSpanId = spanContext.parentSpanId;
  96. }
  97. // We want to include booleans as well here
  98. if ('sampled' in spanContext) {
  99. this._sampled = spanContext.sampled;
  100. }
  101. if (spanContext.status) {
  102. this._status = spanContext.status;
  103. }
  104. if (spanContext.endTimestamp) {
  105. this._endTime = spanContext.endTimestamp;
  106. }
  107. }
  108. // This rule conflicts with another eslint rule :(
  109. /* eslint-disable @typescript-eslint/member-ordering */
  110. /**
  111. * An alias for `description` of the Span.
  112. * @deprecated Use `spanToJSON(span).description` instead.
  113. */
  114. get name() {
  115. return this._name || '';
  116. }
  117. /**
  118. * Update the name of the span.
  119. * @deprecated Use `spanToJSON(span).description` instead.
  120. */
  121. set name(name) {
  122. this.updateName(name);
  123. }
  124. /**
  125. * Get the description of the Span.
  126. * @deprecated Use `spanToJSON(span).description` instead.
  127. */
  128. get description() {
  129. return this._name;
  130. }
  131. /**
  132. * Get the description of the Span.
  133. * @deprecated Use `spanToJSON(span).description` instead.
  134. */
  135. set description(description) {
  136. this._name = description;
  137. }
  138. /**
  139. * The ID of the trace.
  140. * @deprecated Use `spanContext().traceId` instead.
  141. */
  142. get traceId() {
  143. return this._traceId;
  144. }
  145. /**
  146. * The ID of the trace.
  147. * @deprecated You cannot update the traceId of a span after span creation.
  148. */
  149. set traceId(traceId) {
  150. this._traceId = traceId;
  151. }
  152. /**
  153. * The ID of the span.
  154. * @deprecated Use `spanContext().spanId` instead.
  155. */
  156. get spanId() {
  157. return this._spanId;
  158. }
  159. /**
  160. * The ID of the span.
  161. * @deprecated You cannot update the spanId of a span after span creation.
  162. */
  163. set spanId(spanId) {
  164. this._spanId = spanId;
  165. }
  166. /**
  167. * @inheritDoc
  168. *
  169. * @deprecated Use `startSpan` functions instead.
  170. */
  171. set parentSpanId(string) {
  172. this._parentSpanId = string;
  173. }
  174. /**
  175. * @inheritDoc
  176. *
  177. * @deprecated Use `spanToJSON(span).parent_span_id` instead.
  178. */
  179. get parentSpanId() {
  180. return this._parentSpanId;
  181. }
  182. /**
  183. * Was this span chosen to be sent as part of the sample?
  184. * @deprecated Use `isRecording()` instead.
  185. */
  186. get sampled() {
  187. return this._sampled;
  188. }
  189. /**
  190. * Was this span chosen to be sent as part of the sample?
  191. * @deprecated You cannot update the sampling decision of a span after span creation.
  192. */
  193. set sampled(sampled) {
  194. this._sampled = sampled;
  195. }
  196. /**
  197. * Attributes for the span.
  198. * @deprecated Use `spanToJSON(span).atttributes` instead.
  199. */
  200. get attributes() {
  201. return this._attributes;
  202. }
  203. /**
  204. * Attributes for the span.
  205. * @deprecated Use `setAttributes()` instead.
  206. */
  207. set attributes(attributes) {
  208. this._attributes = attributes;
  209. }
  210. /**
  211. * Timestamp in seconds (epoch time) indicating when the span started.
  212. * @deprecated Use `spanToJSON()` instead.
  213. */
  214. get startTimestamp() {
  215. return this._startTime;
  216. }
  217. /**
  218. * Timestamp in seconds (epoch time) indicating when the span started.
  219. * @deprecated In v8, you will not be able to update the span start time after creation.
  220. */
  221. set startTimestamp(startTime) {
  222. this._startTime = startTime;
  223. }
  224. /**
  225. * Timestamp in seconds when the span ended.
  226. * @deprecated Use `spanToJSON()` instead.
  227. */
  228. get endTimestamp() {
  229. return this._endTime;
  230. }
  231. /**
  232. * Timestamp in seconds when the span ended.
  233. * @deprecated Set the end time via `span.end()` instead.
  234. */
  235. set endTimestamp(endTime) {
  236. this._endTime = endTime;
  237. }
  238. /**
  239. * The status of the span.
  240. *
  241. * @deprecated Use `spanToJSON().status` instead to get the status.
  242. */
  243. get status() {
  244. return this._status;
  245. }
  246. /**
  247. * The status of the span.
  248. *
  249. * @deprecated Use `.setStatus()` instead to set or update the status.
  250. */
  251. set status(status) {
  252. this._status = status;
  253. }
  254. /**
  255. * Operation of the span
  256. *
  257. * @deprecated Use `spanToJSON().op` to read the op instead.
  258. */
  259. get op() {
  260. return this._attributes[semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_OP] ;
  261. }
  262. /**
  263. * Operation of the span
  264. *
  265. * @deprecated Use `startSpan()` functions to set or `span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'op')
  266. * to update the span instead.
  267. */
  268. set op(op) {
  269. this.setAttribute(semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_OP, op);
  270. }
  271. /**
  272. * The origin of the span, giving context about what created the span.
  273. *
  274. * @deprecated Use `spanToJSON().origin` to read the origin instead.
  275. */
  276. get origin() {
  277. return this._attributes[semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] ;
  278. }
  279. /**
  280. * The origin of the span, giving context about what created the span.
  281. *
  282. * @deprecated Use `startSpan()` functions to set the origin instead.
  283. */
  284. set origin(origin) {
  285. this.setAttribute(semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, origin);
  286. }
  287. /* eslint-enable @typescript-eslint/member-ordering */
  288. /** @inheritdoc */
  289. spanContext() {
  290. const { _spanId: spanId, _traceId: traceId, _sampled: sampled } = this;
  291. return {
  292. spanId,
  293. traceId,
  294. traceFlags: sampled ? spanUtils.TRACE_FLAG_SAMPLED : spanUtils.TRACE_FLAG_NONE,
  295. };
  296. }
  297. /**
  298. * Creates a new `Span` while setting the current `Span.id` as `parentSpanId`.
  299. * Also the `sampled` decision will be inherited.
  300. *
  301. * @deprecated Use `startSpan()`, `startSpanManual()` or `startInactiveSpan()` instead.
  302. */
  303. startChild(
  304. spanContext,
  305. ) {
  306. const childSpan = new Span({
  307. ...spanContext,
  308. parentSpanId: this._spanId,
  309. sampled: this._sampled,
  310. traceId: this._traceId,
  311. });
  312. // eslint-disable-next-line deprecation/deprecation
  313. childSpan.spanRecorder = this.spanRecorder;
  314. // eslint-disable-next-line deprecation/deprecation
  315. if (childSpan.spanRecorder) {
  316. // eslint-disable-next-line deprecation/deprecation
  317. childSpan.spanRecorder.add(childSpan);
  318. }
  319. const rootSpan = getRootSpan.getRootSpan(this);
  320. // TODO: still set span.transaction here until we have a more permanent solution
  321. // Probably similarly to the weakmap we hold in node-experimental
  322. // eslint-disable-next-line deprecation/deprecation
  323. childSpan.transaction = rootSpan ;
  324. if (debugBuild.DEBUG_BUILD && rootSpan) {
  325. const opStr = (spanContext && spanContext.op) || '< unknown op >';
  326. const nameStr = spanUtils.spanToJSON(childSpan).description || '< unknown name >';
  327. const idStr = rootSpan.spanContext().spanId;
  328. const logMessage = `[Tracing] Starting '${opStr}' span on transaction '${nameStr}' (${idStr}).`;
  329. utils.logger.log(logMessage);
  330. this._logMessage = logMessage;
  331. }
  332. return childSpan;
  333. }
  334. /**
  335. * Sets the tag attribute on the current span.
  336. *
  337. * Can also be used to unset a tag, by passing `undefined`.
  338. *
  339. * @param key Tag key
  340. * @param value Tag value
  341. * @deprecated Use `setAttribute()` instead.
  342. */
  343. setTag(key, value) {
  344. // eslint-disable-next-line deprecation/deprecation
  345. this.tags = { ...this.tags, [key]: value };
  346. return this;
  347. }
  348. /**
  349. * Sets the data attribute on the current span
  350. * @param key Data key
  351. * @param value Data value
  352. * @deprecated Use `setAttribute()` instead.
  353. */
  354. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  355. setData(key, value) {
  356. // eslint-disable-next-line deprecation/deprecation
  357. this.data = { ...this.data, [key]: value };
  358. return this;
  359. }
  360. /** @inheritdoc */
  361. setAttribute(key, value) {
  362. if (value === undefined) {
  363. // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
  364. delete this._attributes[key];
  365. } else {
  366. this._attributes[key] = value;
  367. }
  368. }
  369. /** @inheritdoc */
  370. setAttributes(attributes) {
  371. Object.keys(attributes).forEach(key => this.setAttribute(key, attributes[key]));
  372. }
  373. /**
  374. * @inheritDoc
  375. */
  376. setStatus(value) {
  377. this._status = value;
  378. return this;
  379. }
  380. /**
  381. * @inheritDoc
  382. * @deprecated Use top-level `setHttpStatus()` instead.
  383. */
  384. setHttpStatus(httpStatus) {
  385. spanstatus.setHttpStatus(this, httpStatus);
  386. return this;
  387. }
  388. /**
  389. * @inheritdoc
  390. *
  391. * @deprecated Use `.updateName()` instead.
  392. */
  393. setName(name) {
  394. this.updateName(name);
  395. }
  396. /**
  397. * @inheritDoc
  398. */
  399. updateName(name) {
  400. this._name = name;
  401. return this;
  402. }
  403. /**
  404. * @inheritDoc
  405. *
  406. * @deprecated Use `spanToJSON(span).status === 'ok'` instead.
  407. */
  408. isSuccess() {
  409. return this._status === 'ok';
  410. }
  411. /**
  412. * @inheritDoc
  413. *
  414. * @deprecated Use `.end()` instead.
  415. */
  416. finish(endTimestamp) {
  417. return this.end(endTimestamp);
  418. }
  419. /** @inheritdoc */
  420. end(endTimestamp) {
  421. // If already ended, skip
  422. if (this._endTime) {
  423. return;
  424. }
  425. const rootSpan = getRootSpan.getRootSpan(this);
  426. if (
  427. debugBuild.DEBUG_BUILD &&
  428. // Don't call this for transactions
  429. rootSpan &&
  430. rootSpan.spanContext().spanId !== this._spanId
  431. ) {
  432. const logMessage = this._logMessage;
  433. if (logMessage) {
  434. utils.logger.log((logMessage ).replace('Starting', 'Finishing'));
  435. }
  436. }
  437. this._endTime = spanUtils.spanTimeInputToSeconds(endTimestamp);
  438. }
  439. /**
  440. * @inheritDoc
  441. *
  442. * @deprecated Use `spanToTraceHeader()` instead.
  443. */
  444. toTraceparent() {
  445. return spanUtils.spanToTraceHeader(this);
  446. }
  447. /**
  448. * @inheritDoc
  449. *
  450. * @deprecated Use `spanToJSON()` or access the fields directly instead.
  451. */
  452. toContext() {
  453. return utils.dropUndefinedKeys({
  454. data: this._getData(),
  455. description: this._name,
  456. endTimestamp: this._endTime,
  457. // eslint-disable-next-line deprecation/deprecation
  458. op: this.op,
  459. parentSpanId: this._parentSpanId,
  460. sampled: this._sampled,
  461. spanId: this._spanId,
  462. startTimestamp: this._startTime,
  463. status: this._status,
  464. // eslint-disable-next-line deprecation/deprecation
  465. tags: this.tags,
  466. traceId: this._traceId,
  467. });
  468. }
  469. /**
  470. * @inheritDoc
  471. *
  472. * @deprecated Update the fields directly instead.
  473. */
  474. updateWithContext(spanContext) {
  475. // eslint-disable-next-line deprecation/deprecation
  476. this.data = spanContext.data || {};
  477. // eslint-disable-next-line deprecation/deprecation
  478. this._name = spanContext.name || spanContext.description;
  479. this._endTime = spanContext.endTimestamp;
  480. // eslint-disable-next-line deprecation/deprecation
  481. this.op = spanContext.op;
  482. this._parentSpanId = spanContext.parentSpanId;
  483. this._sampled = spanContext.sampled;
  484. this._spanId = spanContext.spanId || this._spanId;
  485. this._startTime = spanContext.startTimestamp || this._startTime;
  486. this._status = spanContext.status;
  487. // eslint-disable-next-line deprecation/deprecation
  488. this.tags = spanContext.tags || {};
  489. this._traceId = spanContext.traceId || this._traceId;
  490. return this;
  491. }
  492. /**
  493. * @inheritDoc
  494. *
  495. * @deprecated Use `spanToTraceContext()` util function instead.
  496. */
  497. getTraceContext() {
  498. return spanUtils.spanToTraceContext(this);
  499. }
  500. /**
  501. * Get JSON representation of this span.
  502. *
  503. * @hidden
  504. * @internal This method is purely for internal purposes and should not be used outside
  505. * of SDK code. If you need to get a JSON representation of a span,
  506. * use `spanToJSON(span)` instead.
  507. */
  508. getSpanJSON() {
  509. return utils.dropUndefinedKeys({
  510. data: this._getData(),
  511. description: this._name,
  512. op: this._attributes[semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_OP] ,
  513. parent_span_id: this._parentSpanId,
  514. span_id: this._spanId,
  515. start_timestamp: this._startTime,
  516. status: this._status,
  517. // eslint-disable-next-line deprecation/deprecation
  518. tags: Object.keys(this.tags).length > 0 ? this.tags : undefined,
  519. timestamp: this._endTime,
  520. trace_id: this._traceId,
  521. origin: this._attributes[semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] ,
  522. _metrics_summary: metricSummary.getMetricSummaryJsonForSpan(this),
  523. });
  524. }
  525. /** @inheritdoc */
  526. isRecording() {
  527. return !this._endTime && !!this._sampled;
  528. }
  529. /**
  530. * Convert the object to JSON.
  531. * @deprecated Use `spanToJSON(span)` instead.
  532. */
  533. toJSON() {
  534. return this.getSpanJSON();
  535. }
  536. /**
  537. * Get the merged data for this span.
  538. * For now, this combines `data` and `attributes` together,
  539. * until eventually we can ingest `attributes` directly.
  540. */
  541. _getData()
  542. {
  543. // eslint-disable-next-line deprecation/deprecation
  544. const { data, _attributes: attributes } = this;
  545. const hasData = Object.keys(data).length > 0;
  546. const hasAttributes = Object.keys(attributes).length > 0;
  547. if (!hasData && !hasAttributes) {
  548. return undefined;
  549. }
  550. if (hasData && hasAttributes) {
  551. return {
  552. ...data,
  553. ...attributes,
  554. };
  555. }
  556. return hasData ? data : attributes;
  557. }
  558. }
  559. exports.Span = Span;
  560. exports.SpanRecorder = SpanRecorder;
  561. //# sourceMappingURL=span.js.map