2

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.

Sniggerfardimungus
  • 11,583
  • 10
  • 52
  • 97

4 Answers4

4

You should also be able to use the recently GA'd App Engine Admin API to figure this out. The nice thing about the admin API is that it's going to work for both standard and flexible: https://cloud.google.com/appengine/docs/admin-api/

Here's the endpoint that returns all of the instances for a given service/version:

https://cloud.google.com/appengine/docs/admin-api/reference/rest/v1/apps.services.versions.instances/list

Depending on the language you're using, there's usually a nice wrapper in the form of a "Google API client" + language library.

Hope this helps!

Justin Beckwith
  • 7,686
  • 1
  • 33
  • 55
  • Ugh. Under "AppsId," they have the example, "apps/myapp/services/default/versions/v1". What they actually mean is that they want your application name in that field, "default" (or, if you've deployed other services under your app, that service's name), and the serving version of your application in the third field. The doc also fails to mention which permission you need. You'll get that in the "Permission Denied" response, though. Very typical of the sloppy docs style for Cloud Services. – Sniggerfardimungus Aug 17 '16 at 19:01
  • Glad to hear you were able to work through this to figure it out. We'll take a look at updating the docs. – Justin Beckwith Aug 17 '16 at 22:25
  • Thanks! I just posted a complete working-code example, but I'll be damned if I can figure out how to get a list of, either all instances across all versions, or even just the default version. I recently noticed that the concept of a 'default' version seems to have disappeared from the admin console. When that happened, maybe the alias disappeared, as well? – Sniggerfardimungus Aug 17 '16 at 22:49
  • There technically never was a 'default' version (at least that I know of). There is a default 'service' though. To get all instances across all versions, you'd need to start with the versions list API :) We use these APIs to build `gcloud app versions list` and `gcloud app instances list` in the Cloud SDK. – Justin Beckwith Aug 18 '16 at 01:15
  • There are remnants of the default version still lying around (something similar to DEFAULT_VERSION_URL is a key in os.environ, for example.) As a feature request: that I could use '*' for either the versionsID or the servicesID in he instances list api call and get back the appropriate lists. (And the documentation to this effect on the api page.) – Sniggerfardimungus Aug 19 '16 at 05:06
1

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)
Sniggerfardimungus
  • 11,583
  • 10
  • 52
  • 97
0

I agree it's hard to find, the closest one I can find is this

google.appengine.api.modules.modules.get_num_instances(module=None, version=None)source

Return the number of instances that are set for the given module version.

This is only valid for fixed modules, an error will be raised for automatically-scaled modules. Support for automatically-scaled modules may be supported in the future.

https://cloud.google.com/appengine/docs/python/refdocs/google.appengine.api.modules.modules

Btw you can also have monitoring from the StackDriver which has metric for total instance

marcadian
  • 2,608
  • 13
  • 20
0

If you're trying to collect stats, you might want to use the Stackdriver Monitoring API to collect the timeseries values that Google has already aggregated.

In particular, the list of App Engine Metrics is here. For example, system/instance_count is the metric indicating the number of instances App Engine is running.

E. Anderson
  • 3,405
  • 1
  • 16
  • 19