Search code examples
apigoogle-app-engineinstances

Is there an API call I can make to get the number of App Engine Instances I'm running?


It's a pretty simple question, really. I want to report the number of running instances to datadog, along with a bunch of my other stats.

There's an irony to the fact that I search Google Web Search for how to do something in Google App Engine and get the crappiest possible result, every time: The Google App Engine documentation pages.


Solution

  • I hate it when SO questions only end up with partial answers, so here's a complete, working example. If you paste it into your interactive console, it should work for you. (Don't forget to set the versionsId to whatever your default app version is. If you know how I can get it to use the default version, please post a comment. 'default', '*', 'any', etc. all no da workie.)

    Strictly achieved by trial and error:

    import httplib2
    import logging
    import time
    import webapp2
    
    from google.appengine.api.app_identity import app_identity
    from googleapiclient.discovery import build
    from oauth2client.client import GoogleCredentials
    
    credentials = GoogleCredentials.get_application_default()
    service =  build('appengine', 'v1', credentials=credentials)
    appsId = app_identity.get_application_id()
    version_list = service.apps().services().versions().list(
            servicesId='default', appsId=appsId).execute()
    
    for version in version_list['versions']:
        if not version['id'].startswith('ah-builtin'):
            rpc_result = service.apps().services().versions().instances().list(
                    versionsId=version['id'], servicesId='default',
                    appsId=appsId).execute()
    
            if rpc_result:
                instance_list = rpc_result['instances']
            else:
                instance_list = []
            print version['id'], len(instance_list)