aws-observability / aws-rum-web

Amazon CloudWatch RUM Web Client
Apache License 2.0
114 stars 65 forks source link

chore: dispatch all events to EventBus #445

Closed williazz closed 10 months ago

williazz commented 10 months ago

Rewrite of #440

Purpose

This PR creates the structure necessary for sharing information between plugins.

Uses cases:

  1. Have PluginA find eventId's created by PluginB etc. Useful for LCP attribution
  2. WebVitalsPlugin tracks current count of in-flight Xhr and Fetch requests

Revisions

Revision 1

I have created EventBus with topic-based filtering. I implemented our own instead of taking on a dependency because we only need a few extremely simple features.

Revision 2

Revision 3

Revision 4

Use case 1: PluginA finds eventId's of events from PluginB

class WebVitalsPlugin {
   imageEvents = []
   constructor() {
      this.context.bus.subscribe(PERFORMANCE_RESOURCE_EVENT, this.storeResourceEvent)
   }
   storeResourceEvent(event) {
      if (event.fileType === ResourceTypes.IMAGE) {
         this.imageEvents.push(event)
      }
   }

   ...
   onLCP(payload => {
       ...
       findMatchingLCPEvent()
       // teardown
       this.imageEvents = []
       this.context.bus.unsubscribe(...)
   })
}

Use case 2: Count in-flight fetch/xhr requests

// FetchPlugin.ts
...
private fetch() {
   ...
   this.context.bus.dispatch('http_open_count', {payload: 1})
   return original()
   .apply()
   .then(() => {
        this.context.bus.dispatch('http_open_count', {payload: -1})
    })
   .catch(() => {
        this.context.bus.dispatch('http_open_count', {payload: -1})
    })
}

...

// WebVitalsPlugin
class WebVitalsPlugins {
    ...
    http_open_count = 0
    handleHttpCountChange({payload}) { this.http_open_count += payload }
    constructor() {
        this.context.bus.subscribe('http_open_count', this.handleHttpCountChange)
   }
   teardown() {
       this.context.bus.unsubscribe('http_open_count', this.handleHttpCountChange)
   }
   ....
}

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

ps863 commented 10 months ago

high level question: could the addition and usage of an event bus impact the memory/performance footprint of the web client on web applications?

williazz commented 10 months ago

high level question: could the addition and usage of an event bus impact the memory/performance footprint of the web client on web applications?

We are currently only monitoring the size and relying design principles to limit memory and cpu usage. I have also been thinking about how to objectively monitor this performance.

But from a design perspective, I think the impact is minimal.

qhanam commented 10 months ago

My thoughts on (1) vs (2): If doing (1), this topic allows any plugin to listen for any event. Publish all events under an ‘events’ topic. If doing (2), this topic is for the specific purpose of tracking resource/nav event Ids. Return the event id from recordEvent and publish an <event id, url> tuple.