traceProfilingLink.ts 3.2 KB

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