traceProfilingLink.ts 3.6 KB

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