Skip to content

Instantly share code, notes, and snippets.

@rockwotj

rockwotj/rtdb_load.py

Last active Jun 28, 2017
Embed
What would you like to do?
An example in python on using the Stackdriver Monitoring API to query your Firebase Realtime Database's average load over the past day
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This is an example script that will download the latest 24 hours of
your realtime database load and tell you the average utilization
of you database instance.
The metric for your database's load is a percentage of the last
1 minute that it was busy processing requests.
"""
try:
from google.cloud import monitoring
from google.cloud.monitoring.metric import MetricKind, ValueType
except ImportError:
print "Please run 'pip install google-cloud'"
import sys
sys.exit(1)
def main():
"""
See: https://googlecloudplatform.github.io/google-cloud-python/stable/monitoring-usage.html
for more example and documentation on how the monitoring API for python.
"""
# This is the GCP project tied to your firebase project
client = monitoring.Client(project = 'rockwood-test')
# This is from the following link:
# https://cloud.google.com/monitoring/api/metrics#gcp-firebasedatabase
METRIC = 'firebasedatabase.googleapis.com/io/utilization'
query = client.query(METRIC, days=1)
# We should only get back a single timeseries from
# this query, with the metric that we want.
for timeseries in query:
assert timeseries.metric_kind == MetricKind.GAUGE
assert timeseries.value_type == ValueType.DOUBLE
total = 0
size = 0
for point in timeseries.points:
total += point.value * 100.0 # Convert to percent
size += 1
average = round(total / size, 2)
print "Your database's average load is", average, "%"
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment