metrics.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 = 9
  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, pdfCachingMinChunkSize } = 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. pdfCachingMinChunkSize,
  37. ...deliveryLatencies,
  38. ...pdfCachingMetrics,
  39. })
  40. }
  41. }
  42. return {
  43. deliveryLatencies,
  44. firstRenderDone,
  45. }
  46. }
  47. function submitCompileMetrics(metrics) {
  48. const leanMetrics = {
  49. version: VERSION,
  50. ...metrics,
  51. id: EDITOR_SESSION_ID,
  52. }
  53. sl_console.log('/event/compile-metrics', JSON.stringify(leanMetrics))
  54. sendMB('compile-metrics-v6', leanMetrics)
  55. }