Search code examples
angularfirebasegoogle-cloud-firestoreangularfire2

Analysis of Firestore reads


Is there currently any way to see where Firestore reads are coming from?

I have several apps and many background + several scheduled cloud functions and it is going to be a big task to try and tune these to reduce reads if I don't know where the reads are coming from.

Even if I could split reads between the front end and the admin SDK that would help narrow things down.

All the SQL databases I have worked with have some kind of profiler to see which queries are being run and how long they take. I wouldn't mind increasing my Firestore usage while I am running the analysis as it would save me usage in the long run.

I am using Angular with AngularFire on the frontend.

EDIT:

One idea I had was to use google analytics to track the reads by using events for each query. I will update this question once I have tried it.


Solution

  • Since this was posted Firestore added audit logging (in late 2021), which allows you to track document reads on the backend and analyze that data using BigQuery.

    For example, I could get the document read count per user per day with this query:

    SELECT
      JSON_VALUE(proto_payload.audit_log.authentication_info.third_party_principal.payload.user_id) as uid,
      TIMESTAMP_TRUNC(timestamp, DAY) as day,
      SUM(proto_payload.audit_log.num_response_items) as read_count
    FROM `nanochat-20241022-mw8qu9.global._Default._AllLogs`
    WHERE proto_payload.audit_log.authorization_info[0].permission_type IN ('DATA_READ', 'DATA_WRITE')
      AND proto_payload.audit_log.method_name LIKE 'google.firestore.v1.Firestore%'
    GROUP BY 
      JSON_VALUE(proto_payload.audit_log.authentication_info.third_party_principal.payload.user_id), 
      TIMESTAMP_TRUNC(timestamp, DAY)
    

    When charted, that looks like this: A line chart, titled "Daily listener read count per UID," shows a line graph tracking the daily read counts for five unique identifiers (UIDs) from November 5 to November 8 in UTC-8, with the y-axis ranging from 0 to 500. The blue line representing (null) remains mostly flat, while the green line for mziZJtP9p1fxavXdVvuk2qMyIWB3 shows a gradual increase. The purple line for SuURGfFZ7i0dyIFpt3oUX7hsITH2 spikes sharply on November 7 before dropping back to 0. The red line for rt1YXNbHlQgfM3gkkwIZJRpD93 shows a small peak on November 7, and the cyan line for ESSqvCiYZect4IeBA9QPcRG5e892 increases slightly over time. The chart illustrates variations in daily read counts across the different UIDs.

    I wrote a post about this with more examples here: Counting document reads per user in Firestore