-1

I need to make decisions in an external system based on the current CPU utilization of my App Engine Flexible service. I can see the exact values / metrics I need to use in the dashboard charting in my Google Cloud Console, but I don't see a direct, easy way to get this information from something like a gcloud command.

I also need to know the count of running instances, but I think I can use gcloud app instances list -s default to get a list of my running instances in the default service, and then I can use a count of lines approach to get this info easily. I intend to make a python function which returns a tuple like (instance_count, cpu_utilization).

I'd appreciate if anyone can direct me to an easy way to get this. I am currently exploring the StackDriver Monitoring service to get this same information, but as of now it is looking super-complicated to me.

Dhiraj Gupta
  • 9,704
  • 8
  • 49
  • 54

1 Answers1

1

You can use the gcloud app instances list -s default command to get the running instances list, as you said. To retrieve CPU utilization, have a look on this Python Client for Stackdriver Monitoring. To list available metric types:

from google.cloud import monitoring
client = monitoring.Client()
for descriptor in client.list_metric_descriptors():
   print(descriptor.type)

Metric descriptors are described here. To display utilization across your GCE instances during the last five minutes:

metric = 'compute.googleapis.com/instance/cpu/utilization'
query = client.query(metric, minutes=5)
print(query.as_dataframe())

Do not forget to add google-cloud-monitoring==0.28.1 to “requirements.txt” before installing it.

Check this code that locally runs for me:

import logging

from flask import Flask
from google.cloud import monitoring as mon

app = Flask(__name__)

@app.route('/')
def list_metric_descriptors():
    """Return all metric descriptors"""
    # Instantiate client 
    client = mon.Client()

    for descriptor in client.list_metric_descriptors():
        print(descriptor.type)
    return descriptor.type

@app.route('/CPU')
def cpuUtilization():
    """Return CPU utilization"""
    client = mon.Client()
    metric = 'compute.googleapis.com/instance/cpu/utilization'
    query = client.query(metric, minutes=5)
    print(type(query.as_dataframe()))    
    print(query.as_dataframe())    
    data=str(query.as_dataframe())
    return data

@app.errorhandler(500)
def server_error(e):
    logging.exception('An error occurred during a request.')
    return """
    An internal error occurred: <pre>{}</pre>
    See logs for full stacktrace.
    """.format(e), 500


if __name__ == '__main__':
    # This is used when running locally. Gunicorn is used to run the
    # application on Google App Engine. See entrypoint in app.yaml.
    app.run(host='127.0.0.1', port=8080, debug=True)
Rubén C.
  • 1,098
  • 6
  • 16