traceProfilingLink.ts 3.2 KB

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