metrics.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { v4 as uuid } from 'uuid'
  2. import { sendMB } from '../../../infrastructure/event-tracking'
  3. import { trackPdfDownloadEnabled } from './pdf-caching-flags'
  4. import { debugConsole } from '@/utils/debugging'
  5. // VERSION should get incremented when making changes to caching behavior or
  6. // adjusting metrics collection.
  7. const VERSION = 9
  8. // editing session id
  9. const EDITOR_SESSION_ID = uuid()
  10. const pdfCachingMetrics = {
  11. viewerId: EDITOR_SESSION_ID,
  12. }
  13. export function getPdfCachingMetrics() {
  14. return pdfCachingMetrics
  15. }
  16. export function trackPdfDownload(response, compileTimeClientE2E, t0) {
  17. const { timings, pdfCachingMinChunkSize } = response
  18. const deliveryLatencies = {
  19. compileTimeClientE2E,
  20. compileTimeServerE2E: timings?.compileE2E,
  21. }
  22. // There can be multiple "first" renderings with two pdf viewers.
  23. // E.g. two pdf detach tabs or pdf detacher plus pdf detach.
  24. // Let the pdfCachingMetrics round trip to account for pdf-detach.
  25. let isFirstRender = true
  26. function firstRenderDone({ latencyFetch, latencyRender, pdfCachingMetrics }) {
  27. if (!isFirstRender) return
  28. isFirstRender = false
  29. deliveryLatencies.totalDeliveryTime = Math.ceil(performance.now() - t0)
  30. deliveryLatencies.latencyFetch = latencyFetch
  31. if (latencyRender) {
  32. deliveryLatencies.latencyRender = latencyRender
  33. }
  34. if (trackPdfDownloadEnabled) {
  35. // Submit latency along with compile context.
  36. submitCompileMetrics({
  37. pdfCachingMinChunkSize,
  38. ...deliveryLatencies,
  39. ...pdfCachingMetrics,
  40. })
  41. }
  42. }
  43. return {
  44. deliveryLatencies,
  45. firstRenderDone,
  46. }
  47. }
  48. function submitCompileMetrics(metrics) {
  49. const leanMetrics = {
  50. version: VERSION,
  51. ...metrics,
  52. id: EDITOR_SESSION_ID,
  53. }
  54. debugConsole.log('/event/compile-metrics', JSON.stringify(leanMetrics))
  55. sendMB('compile-metrics-v6', leanMetrics)
  56. }