Skip to content
Permalink
Browse files
fix(logging): appengine zone label (#3998)
Fixes: #3919

**Before:** 
```json
resource: {
  type: "gae_app"
  labels: {
    project_id: "log-bench"
    module_id: "default"
    zone: ""
    version_id: "20201113t135702"
  }
}
```
**After:** 
```json
resource: {
  type: "gae_app"
  labels: {
    project_id: "log-bench"
    module_id: "default"
    zone: "us-west-a"
    version_id: "20201113t135702"
  }
}
```

e2e environment tests for this change passing at [env-tests](https://github.com/googleapis/env-tests-logging)

=========================================== test session starts ===========================================
platform darwin -- Python 3.7.9, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: /Users/nicolezhu/Desktop/google-cloud-go/internal/env-tests-logging
collected 3 items

tests/go/test_appengine_standard.py ['/Users/nicolezhu/Desktop/google-cloud-go/internal/env-tests-logging/envctl/envctl', 'go', 'appengine_standard', 'verify']
['/Users/nicolezhu/Desktop/google-cloud-go/internal/env-tests-logging/envctl/envctl', 'go', 'appengine_standard', 'deploy']
['/Users/nicolezhu/Desktop/google-cloud-go/internal/env-tests-logging/envctl/envctl', 'go', 'appengine_standard', 'verify']
['/Users/nicolezhu/Desktop/google-cloud-go/internal/env-tests-logging/envctl/envctl', 'go', 'appengine_standard', 'trigger', 'simplelog', 'log_text="test_monitored_resource d3654c60-a3d4-11eb-bb1c-acde48001122"']
.['/Users/nicolezhu/Desktop/google-cloud-go/internal/env-tests-logging/envctl/envctl', 'go', 'appengine_standard', 'trigger', 'simplelog', 'log_text="test_receive_log de86ea86-a3d4-11eb-bb1c-acde48001122"']
..

====================================== 3 passed in 176.73s (0:02:56) ======================================
nox > Session tests(language='go', platform='appengine_standard') was successful.
  • Loading branch information
0xSage committed May 3, 2021
1 parent aded90b commit 394a586bac04953e92a6496a7ca3b61bd64155ab
Showing 1 changed file with 37 additions and 15 deletions.
@@ -285,19 +285,6 @@ func detectGCEResource() *mrpb.MonitoredResource {
}
}

func detectGAEResource() *mrpb.MonitoredResource {
return &mrpb.MonitoredResource{
Type: "gae_app",
Labels: map[string]string{
"project_id": os.Getenv("GOOGLE_CLOUD_PROJECT"),
"module_id": os.Getenv("GAE_SERVICE"),
"version_id": os.Getenv("GAE_VERSION"),
"instance_id": os.Getenv("GAE_INSTANCE"),
"runtime": os.Getenv("GAE_RUNTIME"),
},
}
}

func isCloudRun() bool {
_, config := os.LookupEnv("K_CONFIGURATION")
_, service := os.LookupEnv("K_SERVICE")
@@ -363,13 +350,48 @@ func detectCloudFunction() *mrpb.MonitoredResource {
}
}

// isAppEngine returns true for both standard and flex
func isAppEngine() bool {
_, service := os.LookupEnv("GAE_SERVICE")
_, version := os.LookupEnv("GAE_VERSION")
_, instance := os.LookupEnv("GAE_INSTANCE")

return service && version && instance
}

func detectAppEngineResource() *mrpb.MonitoredResource {
projectID, err := metadata.ProjectID()
if err != nil {
return nil
}
if projectID == "" {
projectID = os.Getenv("GOOGLE_CLOUD_PROJECT")
}
zone, err := metadata.Zone()
if err != nil {
return nil
}

return &mrpb.MonitoredResource{
Type: "gae_app",
Labels: map[string]string{
"project_id": projectID,
"module_id": os.Getenv("GAE_SERVICE"),
"version_id": os.Getenv("GAE_VERSION"),
"instance_id": os.Getenv("GAE_INSTANCE"),
"runtime": os.Getenv("GAE_RUNTIME"),
"zone": zone,
},
}
}

func detectResource() *mrpb.MonitoredResource {
detectedResource.once.Do(func() {
switch {
// AppEngine, Functions, CloudRun are detected first, as metadata.OnGCE()
// erroneously returns true on these runtimes.
case os.Getenv("GAE_ENV") == "standard":
detectedResource.pb = detectGAEResource()
case isAppEngine():
detectedResource.pb = detectAppEngineResource()
case isCloudFunction():
detectedResource.pb = detectCloudFunction()
case isCloudRun():

0 comments on commit 394a586

Please sign in to comment.