traceProfilingLink.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import type {Location, LocationDescriptor} from 'history';
  2. import {getDateFromTimestamp} from 'sentry/utils/dates';
  3. import {generateContinuousProfileFlamechartRouteWithQuery} from 'sentry/utils/profiling/routes';
  4. import {isSpanNode, isTransactionNode} from '../traceGuards';
  5. import {TraceTree} from '../traceModels/traceTree';
  6. import type {TraceTreeNode} from '../traceModels/traceTreeNode';
  7. function getNodeId(node: TraceTreeNode<TraceTree.NodeValue>): string | undefined {
  8. if (isTransactionNode(node)) {
  9. return node.value.event_id;
  10. }
  11. if (isSpanNode(node)) {
  12. return node.value.span_id;
  13. }
  14. return undefined;
  15. }
  16. // In the current version, a segment is a parent transaction
  17. function getEventId(node: TraceTreeNode<TraceTree.NodeValue>): string | undefined {
  18. if (isTransactionNode(node)) {
  19. return node.value.event_id;
  20. }
  21. return TraceTree.ParentTransaction(node)?.value?.event_id;
  22. }
  23. /**
  24. * Generates a link to a continuous profile for a given trace element type
  25. */
  26. export function makeTraceContinuousProfilingLink(
  27. node: TraceTreeNode<TraceTree.NodeValue>,
  28. profilerId: string,
  29. options: {
  30. orgSlug: string;
  31. projectSlug: string;
  32. threadId: string | undefined;
  33. traceId: string;
  34. },
  35. query: Location['query'] = {}
  36. ): LocationDescriptor | null {
  37. if (!options.projectSlug || !options.orgSlug) {
  38. return null;
  39. }
  40. // We compute a time offset based on the duration of the span so that
  41. // users can see some context of things that occurred before and after the span.
  42. const transaction = isTransactionNode(node) ? node : TraceTree.ParentTransaction(node);
  43. if (!transaction) {
  44. return null;
  45. }
  46. let start: Date | null = getDateFromTimestamp(transaction.space[0]);
  47. let end: Date | null = getDateFromTimestamp(
  48. transaction.space[0] + transaction.space[1]
  49. );
  50. // End timestamp is required to generate a link
  51. if (end === null || typeof profilerId !== 'string' || profilerId === '') {
  52. return null;
  53. }
  54. // If we have an end, but no start, then we'll generate a window of time around end timestamp
  55. // so that we can show context around the event.
  56. if (end && end.getTime() === start?.getTime()) {
  57. const PRE_CONTEXT_WINDOW_MS = 100;
  58. const POST_CONTEXT_WINDOW_MS = 100;
  59. start = new Date(start.getTime() - PRE_CONTEXT_WINDOW_MS);
  60. end = new Date(end.getTime() + POST_CONTEXT_WINDOW_MS);
  61. }
  62. // We require a full time range to open a flamechart
  63. if (start === null) {
  64. return null;
  65. }
  66. // TransactionId is required to generate a link because
  67. // we need to link to the segment of the trace and fetch its spans
  68. const eventId = getEventId(node);
  69. if (!eventId) {
  70. return null;
  71. }
  72. const queryWithEventData: Record<string, string> = {
  73. ...query,
  74. eventId,
  75. traceId: options.traceId,
  76. };
  77. if (typeof options.threadId === 'string') {
  78. queryWithEventData.tid = options.threadId;
  79. }
  80. const spanId = getNodeId(node);
  81. if (spanId) {
  82. queryWithEventData.spanId = spanId;
  83. }
  84. return generateContinuousProfileFlamechartRouteWithQuery({
  85. orgSlug: options.orgSlug,
  86. projectSlug: options.projectSlug,
  87. profilerId: profilerId,
  88. start: start.toISOString(),
  89. end: end.toISOString(),
  90. query: queryWithEventData,
  91. });
  92. }