traceProfilingLink.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import type {Location, LocationDescriptor} from 'history';
  2. import {generateContinuousProfileFlamechartRouteWithQuery} from 'sentry/utils/profiling/routes';
  3. import type {
  4. TraceTree,
  5. TraceTreeNode,
  6. } from 'sentry/views/performance/newTraceDetails/traceModels/traceTree';
  7. function toDate(value: unknown): Date | null {
  8. if (typeof value !== 'string' && typeof value !== 'number') {
  9. return null;
  10. }
  11. const dateObj = new Date(value);
  12. if (isNaN(dateObj.getTime())) {
  13. return null;
  14. }
  15. return dateObj;
  16. }
  17. /**
  18. * Generates a link to a continuous profile for a given trace element type
  19. */
  20. export function makeTraceContinuousProfilingLink(
  21. value: TraceTreeNode<TraceTree.NodeValue>,
  22. profilerId: string,
  23. options: {
  24. orgSlug: string;
  25. projectSlug: string;
  26. },
  27. query: Location['query'] = {}
  28. ): LocationDescriptor | null {
  29. if (!options.projectSlug || !options.orgSlug) {
  30. return null;
  31. }
  32. let start: Date | null = toDate(value.space[0]);
  33. let end: Date | null = toDate(value.space[0] + value.space[1]);
  34. // End timestamp is required to generate a link
  35. if (end === null || typeof profilerId !== 'string' || profilerId === '') {
  36. return null;
  37. }
  38. // If we have an end, but no start, then we'll generate a window of time around end timestamp
  39. // so that we can show context around the event.
  40. if (end && end.getTime() === start?.getTime()) {
  41. const PRE_CONTEXT_WINDOW_MS = 100;
  42. const POST_CONTEXT_WINDOW_MS = 100;
  43. start = new Date(start.getTime() - PRE_CONTEXT_WINDOW_MS);
  44. end = new Date(end.getTime() + POST_CONTEXT_WINDOW_MS);
  45. }
  46. // We require a full time range to open a flamechart
  47. if (start === null) {
  48. return null;
  49. }
  50. return generateContinuousProfileFlamechartRouteWithQuery(
  51. options.orgSlug,
  52. options.projectSlug,
  53. profilerId,
  54. start.toISOString(),
  55. end.toISOString(),
  56. query
  57. );
  58. }