func handler(w http.ResponseWriter, r *http.Request) { ctx := appengine.NewContext(r) module := appengine.ModuleName(ctx) instance := appengine.InstanceID() log.Infof(ctx, "Received on module %s; instance %s", module, instance) }
//Diagnostics shows a bunch of app engine's information for the app/project //useful for figuring out which version of an app is serving func Diagnostics(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) out := map[string]interface{}{ "App ID": appengine.AppID(c), "Instance ID": appengine.InstanceID(), "Default Version Hostname": appengine.DefaultVersionHostname(c), "Version ID": appengine.VersionID(c), "Datacenter": appengine.Datacenter(c), "Module Name": appengine.ModuleName(c), "Server Software": appengine.ServerSoftware(), } templates.Load(w, "diagnostics", out) return }
// Status gathers a quick overview of the system state // and dumps it in JSON format. func Status(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { return } ctx := appengine.NewContext(r) m := new(runtime.MemStats) runtime.ReadMemStats(m) s := &status{ initTime, appengineStatus{ appengine.InstanceID(), appengine.AppID(ctx), appengine.Datacenter(ctx), appengine.DefaultVersionHostname(ctx), appengine.ModuleName(ctx), appengine.IsDevAppServer(), }, runtimeStatus{ runtime.GOMAXPROCS(0), runtime.GOARCH, runtime.GOOS, runtime.GOROOT(), runtime.NumCPU(), runtime.NumCgoCall(), runtime.Version(), *m, }, os.Environ(), } b, err := json.Marshal(s) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json; encoding=utf-8") w.Write(b) }
func (l *GCLLogger) Log(res *responseLogger, r *http.Request) (err error) { if res.Status() == http.StatusOK { context := appengine.NewContext(r) client, err := google.DefaultClient(appengine.NewContext(r), logging.CloudPlatformScope) if err != nil { return err } service, err := logging.New(client) if err != nil { return err } projectId := appengine.AppID(context) labels := map[string]string{ "compute.googleapis.com/resource_type": "instance", "compute.googleapis.com/resource_id": appengine.InstanceID(), } meta := &logging.LogEntryMetadata{ Severity: l.Severity, ProjectId: projectId, ServiceName: "compute.googleapis.com", Zone: appengine.Datacenter(context), } entry := &logging.LogEntry{Metadata: meta, TextPayload: string(res.Body())} e := &logging.WriteLogEntriesRequest{ CommonLabels: labels, Entries: []*logging.LogEntry{entry}, } _, err = service.Projects.Logs.Entries.Write(projectId, l.Target, e).Do() } return }
// Todo: When c==nil we are in a non-appengine environment. // We still want to return at least ii.PureHostname func GetByContext(c context.Context) *Instance { tstart := time.Now() if !ii.LastUpdated.IsZero() { age := tstart.Sub(ii.LastUpdated) if age < 200*time.Millisecond { aelog.Infof(c, "instance info update too recently: %v, skipping.\n", age) return ii } if age < 1*time.Hour { if len(ii.Hostname) > 2 { return ii } } aelog.Infof(c, "instance info update too old: %v, recomputing.\n", age) } ii.ModuleName = appengine.ModuleName(c) ii.InstanceID = appengine.InstanceID() ii.VersionFull = appengine.VersionID(c) majorMinor := strings.Split(ii.VersionFull, ".") if len(majorMinor) != 2 { panic("we need a version string of format X.Y") } ii.VersionMajor = majorMinor[0] ii.VersionMinor = majorMinor[1] var err = errors.New("dummy creation error message") ii.NumInstances, err = module.NumInstances(c, ii.ModuleName, ii.VersionFull) if err != nil { // this never works with version full // we do not log this - but try version major err = nil if !util_appengine.IsLocalEnviron() { ii.NumInstances, err = module.NumInstances(c, ii.ModuleName, ii.VersionMajor) if err != nil { eStr := err.Error() eCmp1, eCmp2, eCmp3 := "API error", "INVALID_VERSION)", "Could not find the given version" if strings.Contains(eStr, eCmp1) && strings.Contains(eStr, eCmp2) && strings.Contains(eStr, eCmp3) { aelog.Infof(c, "get num instances works only live and without autoscale; %v", err) } else { aelog.Errorf(c, "get num instances error; %v", err) } } } } // in auto scaling, google reports "zero" - which can not be true // we assume at least 1 if ii.NumInstances == 0 { ii.NumInstances = 1 } // http://[0-2].1.default.libertarian-islands.appspot.com/instance-info ii.Hostname, err = appengine.ModuleHostname(c, ii.ModuleName, ii.VersionMajor, "") if err != nil { aelog.Errorf(c, "ModuleHostname1: %v", err) } ii.PureHostname = appengine.DefaultVersionHostname(c) if !appengine.IsDevAppServer() { ii.HostnameInst0, err = appengine.ModuleHostname(c, ii.ModuleName, ii.VersionMajor, "0") if err != nil && (err.Error() == autoScalingErr1 || err.Error() == autoScalingErr2) { aelog.Infof(c, "inst 0: "+autoScalingErrMsg) err = nil } if err != nil { aelog.Errorf(c, "ModuleHostname2: %v", err) } ii.HostnameInst1, err = appengine.ModuleHostname(c, ii.ModuleName, ii.VersionMajor, "1") if err != nil && (err.Error() == autoScalingErr1 || err.Error() == autoScalingErr2) { aelog.Infof(c, "inst 1: "+autoScalingErrMsg) err = nil } if err != nil { aelog.Errorf(c, "ModuleHostname3: %v", err) } ii.HostnameMod02, err = appengine.ModuleHostname(c, "mod02", "", "") if err != nil { aelog.Infof(c, "ModuleHostname4: %v", err) } } ii.LastUpdated = time.Now() aelog.Infof(c, "collectInfo() completed, %v - %v - %v - %v - %v, took %v", stringspb.Ellipsoider(ii.InstanceID, 4), ii.VersionMajor, ii.ModuleName, ii.Hostname, ii.PureHostname, time.Now().Sub(tstart)) return ii }
func (g giImpl) InstanceID() string { return appengine.InstanceID() }