Firestore Metrics
This is a library which uses the Cloud Monitoring API v3 to view Firestore usage metrics.
Pre-requisites
- A service account with permission to access the API.
-
Viewer
role should suffice.
-
- Project must be billing enabled.
- This might be more of a bug as requests that use the bearer token obtained from a service account results in a 403 error when the project is not billing enabled.
How to use
- Create a service account.
- Follow steps here to create a service account.
- Select the
Console
tab. - When selecting a role, under
Quick access
>Basic
, pickViewer
.
- Download the service account keys.
- Follow the steps here to download the service account key.
- Pass the service-account key file path or service-account credentials to
FirestoreMetrics
Method references
Usage metrics
- getRequestCount() - Get Firestore API calls count
- getReadCount() - Get Firestore read count metrics
- getWriteCount() - Get Firestore write count metrics
- getDeleteCount() - Get Firestore delete count metrics
- getSnapshotListeners() - Get Firestore snapshot listeners count metrics
- getActiveConnections() - Get Firestore active connections count metrics
- getTTLDeletionCount() - Get Firestore documents deleted by TTL services count
- getRulesEvaluationCount() - Get Firestore Security Rule evaluations count performed in response to write read requests
Misc.
- getProjectId() - Gets the project ID and sets its value if it is still null
- generateToken() - Generate an access token to be used to authenticate requests
- setAccessToken() - Overwrites the access token used to authenticate requests
Get read metrics
import { FirestoreMetrics } from "firestore-metrics";
async function testApi() {
const firestoreMetrics = new FirestoreMetrics({
serviceAccountPath: "./service-account.json",
});
const readUsage = await firestoreMetrics.getReadCount(
"2023-07-22T08:00:00Z",
"2023-07-22T22:42:15Z"
);
console.log(JSON.stringify(readUsage, null, 4));
}
testApi();
Output would look like:
[
{
"type": "QUERY",
"interval": {
"startTime": "2023-08-21T16:15:00Z",
"endTime": "2023-08-21T16:16:00Z"
},
"count": 26
},
{
"type": "QUERY",
"interval": {
"startTime": "2023-08-21T16:14:00Z",
"endTime": "2023-08-21T16:15:00Z"
},
"count": 15
},
{
"type": "QUERY",
"interval": {
"startTime": "2023-08-21T15:51:00Z",
"endTime": "2023-08-21T15:52:00Z"
},
"count": 11
}
]
Get write metrics
import { FirestoreMetrics } from "firestore-metrics";
async function testApi() {
const firestoreMetrics = new FirestoreMetrics({
serviceAccountPath: "./service-account.json",
});
const writeUsage = await firestoreMetrics.getWriteCount(
"2023-08-21T15:00:00Z",
"2023-08-21T20:00:00Z"
);
console.log(JSON.stringify(writeUsage, null, 4));
}
testApi();
Output would look like:
[
{
"op": "CREATE",
"interval": {
"startTime": "2023-08-21T16:09:00Z",
"endTime": "2023-08-21T16:10:00Z"
},
"count": 1
},
{
"op": "UPDATE",
"interval": {
"startTime": "2023-08-21T16:15:00Z",
"endTime": "2023-08-21T16:16:00Z"
},
"count": 1
},
{
"op": "UPDATE",
"interval": {
"startTime": "2023-08-21T16:09:00Z",
"endTime": "2023-08-21T16:10:00Z"
},
"count": 1
}
]
Ways to authenticate request
Using a service-account file
Just provide the path to your service-account
file
const firestoreMetrics = new FirestoreMetrics({
serviceAccountPath: "./service-account.json",
});
Using service-account credentials
Just provide the JSON object of your service-account
credentials
const firestoreMetrics = new FirestoreMetrics({
credentials: {
type: "service_account",
project_id: "<PROJECT_ID>",
private_key_id: "<PRIVATE_KEY_ID>",
private_key: "<PRIVATE_KEY>",
client_email: "<CLIENT_EMAIL>",
client_id: "<CLIENT_ID>",
auth_uri: "<AUTH_URI>",
token_uri: "<TOKEN_URI>",
auth_provider_x509_cert_url: "<AUTH_PROVIDER_X509_CERT_URL>",
client_x509_cert_url: "<CLIENT_X509_CERT_URL>",
universe_domain: "<UNIVERSE_DOMAIN>",
},
});