metrics.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 = 6
  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. deliveryLatencies.totalDeliveryTime = Math.ceil(performance.now() - t0)
  29. deliveryLatencies.latencyFetch = latencyFetch
  30. if (latencyRender) {
  31. deliveryLatencies.latencyRender = latencyRender
  32. }
  33. if (trackPdfDownloadEnabled) {
  34. // Submit latency along with compile context.
  35. submitCompileMetrics({
  36. ...deliveryLatencies,
  37. ...pdfCachingMetrics,
  38. })
  39. }
  40. }
  41. return {
  42. deliveryLatencies,
  43. firstRenderDone,
  44. }
  45. }
  46. function submitCompileMetrics(metrics) {
  47. const leanMetrics = {
  48. version: VERSION,
  49. ...metrics,
  50. id: EDITOR_SESSION_ID,
  51. }
  52. sl_console.log('/event/compile-metrics', JSON.stringify(leanMetrics))
  53. sendMB('compile-metrics-v6', leanMetrics)
  54. }