trackResponse.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import {useEffect, useState} from 'react';
  2. import {trackAnalytics} from 'sentry/utils/analytics';
  3. import EventView from 'sentry/utils/discover/eventView';
  4. import useOrganization from 'sentry/utils/useOrganization';
  5. export function TrackResponse(
  6. eventView: EventView,
  7. {isLoading, statusCode}: {isLoading: boolean; statusCode?: string}
  8. ) {
  9. // Get current timestamp
  10. const [startTimestamp, setStartTimestamp] = useState<number | null>(null);
  11. const [initiallyLoaded, setInitiallyLoaded] = useState(false);
  12. const organization = useOrganization();
  13. useEffect(() => {
  14. if (isLoading) {
  15. if (startTimestamp === null) {
  16. setStartTimestamp(Date.now());
  17. setInitiallyLoaded(false);
  18. }
  19. return;
  20. }
  21. if (initiallyLoaded) {
  22. return;
  23. }
  24. setInitiallyLoaded(true);
  25. if (startTimestamp) {
  26. const now = Date.now();
  27. trackAnalytics('starfish.request', {
  28. organization,
  29. duration: now - startTimestamp,
  30. statusCode,
  31. });
  32. setStartTimestamp(null);
  33. }
  34. }, [initiallyLoaded, eventView, startTimestamp, organization, isLoading, statusCode]);
  35. }