firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

FR: Provide a way to add our own listeners to EventManager (maybe not a FR)

Open Supamiu opened this issue 3 years ago • 2 comments

[REQUIRED] Describe your environment

  • Operating System version: Windows 10
  • Browser version: Latest chrome
  • Firebase SDK version: 9.13.0
  • Firebase Product: Firestore

[REQUIRED] Describe the problem

This is not a bug report so I won't provide steps to reproduce, but I'll explain my need, why and how I want to do what I want to do 😄 .

Context

It all starts with https://groups.google.com/g/google-cloud-firestore-discuss/c/1WrV4sgFoU8/m/ZyI-IdanAgAJ?utm_medium=email&utm_source=footer, where I asked for some kind of requests profiler to optimize Firestore usage, this profiler's MVP features being:

  • Track how much listeners you have active
  • Track how many read operations you're getting with the client (without rules for the first iteration, rules parsing later on for approximate impact)

So my idea was to hook where I could in my Firestore instance (for the context: I'm using this inside of Angular with @angular/fire but that doesn't really matter since we can grab the FirestoreInstance from it anyways) and it looks like the best way to hook on this would be the EventManager instance inside of the FirestoreInstance. With this, I can monitor the amount of listeners and even have basic info for them using their key in the Map you're using.

Now, I wanted to PoC the second feature: monitoring read ops (and write/delete later on ofc). So I tried many approaches and failed, because the system is not designed for that, ofc 👍.

What I tried

  • Iterate on the QueryListener Map to add my own listener: 🚫 This breaks because then the listener is in an unexpected state, blocking the instance entirely.
  • Patch the listener to add my own logic on top of it (basically patch the next method to call my probe then do its normal behavior): 🚫 looks like patching isn't an option, no mater what I do, the probe is never called so I assume it's just not possible.
  • Patch the onListen method in the EventManager: 🚫 Same as above, patching this doesn't seem to be working, probably due to some integrity checks on your end but idk.

The solution(s?)

I think there's two solutions, a quick and a slow one:

  • Quick one: maybe there's a way to do that that I ignore because I only spent half a day digging in the code and I'm not a maintainer here, meaning that I don't know the code enough to find this way.
  • Slow one: there's no ways to do that atm and in that case, this is really a FR because I really think it's important to provide some sort of profiler to help people understand their firestore usage on a single client.

Supamiu avatar Nov 09 '22 08:11 Supamiu

Thanks for the suggestion @Supamiu . I will bring this up internally with the team for discussion, but cannot make any statements about its priority. In the meantime, feel free to fork the repo and start working on a simple implementation. I'd love to see what you come up with!

ehsannas avatar Nov 09 '22 19:11 ehsannas

Thanks 👍 I'll see what I can do but I was kinda hoping for a way that I had missed to hook on this, no big deal, that's a good challenge !

Supamiu avatar Nov 10 '22 08:11 Supamiu