Jonathan Amsterdam submitted this change.
internal/config: support Cloud Run
Add support for services on Cloud Run by looking for Cloud-Run-specific
environment variables.
Change-Id: I0877bc90ab9b8b9d7a960f82d85a015afdf22d2e
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/353809
Trust: Jonathan Amsterdam <j...@google.com>
Run-TryBot: Jonathan Amsterdam <j...@google.com>
Reviewed-by: Julie Qiu <ju...@golang.org>
TryBot-Result: kokoro <noreply...@google.com>
---
M internal/config/config.go
1 file changed, 47 insertions(+), 5 deletions(-)
diff --git a/internal/config/config.go b/internal/config/config.go
index 3f243db..d078278 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -143,7 +143,7 @@
GoogleTagManagerID string
// MonitoredResource represents the resource that is running the current binary.
- // It might be a Google AppEngine app or a Kubernetes pod.
+ // It might be a Google AppEngine app, a Cloud Run service, or a Kubernetes pod.
// See https://cloud.google.com/monitoring/api/resources for more
// details:
// "An object representing a resource that can be used for monitoring, logging,
@@ -208,10 +208,22 @@
return os.Getenv("GO_DISCOVERY_ON_GKE") == "true"
}
+// OnCloudRun reports whether the current process is running on Cloud Run.
+func (c *Config) OnCloudRun() bool {
+ // Use the presence of the environment variables provided by Cloud Run.
+ // See https://cloud.google.com/run/docs/reference/container-contract.
+ for _, ev := range []string{"K_SERVICE", "K_REVISION", "K_CONFIGURATION"} {
+ if os.Getenv(ev) == "" {
+ return false
+ }
+ }
+ return true
+}
+
// OnGCP reports whether the current process is running on Google Cloud
// Platform.
func (c *Config) OnGCP() bool {
- return c.OnAppEngine() || c.OnGKE()
+ return c.OnAppEngine() || c.OnGKE() || c.OnCloudRun()
}
// StatementTimeout is the value of the Postgres statement_timeout parameter.
@@ -345,9 +357,12 @@
Port: os.Getenv("PORT"),
DebugPort: os.Getenv("DEBUG_PORT"),
// Resolve AppEngine identifiers
- ProjectID: os.Getenv("GOOGLE_CLOUD_PROJECT"),
- ServiceID: GetEnv("GAE_SERVICE", os.Getenv("GO_DISCOVERY_SERVICE")),
- VersionID: GetEnv("GAE_VERSION", os.Getenv("DOCKER_IMAGE")),
+ ProjectID: os.Getenv("GOOGLE_CLOUD_PROJECT"),
+ ServiceID: GetEnv("GAE_SERVICE", os.Getenv("GO_DISCOVERY_SERVICE")),
+ // Version ID from either AppEngine, Cloud Run (see
+ // https://cloud.google.com/run/docs/reference/container-contract) or
+ // GKE (set by our own config).
+ VersionID: GetEnv("GAE_VERSION", GetEnv("K_REVISION", os.Getenv("DOCKER_IMAGE"))),
InstanceID: GetEnv("GAE_INSTANCE", os.Getenv("GO_DISCOVERY_INSTANCE")),
GoogleTagManagerID: os.Getenv("GO_DISCOVERY_GOOGLE_TAG_MANAGER_ID"),
QueueURL: os.Getenv("GO_DISCOVERY_QUEUE_URL"),
@@ -423,6 +438,16 @@
"zone": cfg.ZoneID,
},
}
+ case cfg.OnCloudRun():
+ cfg.MonitoredResource = &mrpb.MonitoredResource{
+ Type: "cloud_run_revision",
+ Labels: map[string]string{
+ "project_id": cfg.ProjectID,
+ "service_name": cfg.ServiceID,
+ "revision_name": cfg.VersionID,
+ "configuration_name": os.Getenv("K_CONFIGURATION"),
+ },
+ }
case cfg.OnGKE():
cfg.MonitoredResource = &mrpb.MonitoredResource{
Type: "k8s_container",
To view, visit change 353809. To unsubscribe, or for help writing mail filters, visit settings.