metrics.js 1.9 KB

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