diff --git a/README.md b/README.md index d2fdfc6ad..1b55cefbd 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ The `server/kit` package embodies Gizmo's goals to combine with go-kit. * In this package you'll find: * A more opinionated server with fewer choices. * go-kit used for serving HTTP/JSON & gRPC used for serving HTTP2/RPC - * Monitoring, traces and metrics are automatically registered if running within App Engine, Kubernetes Engine, Compute Engine or AWS EC2 Instances. + * Monitoring, traces and metrics are automatically registered if running within App Engine, Cloud Run, Kubernetes Engine, Compute Engine or AWS EC2 Instances. * to change the name and version for Error reporting and Traces use `SERVICE_NAME` and `SERVICE_VERSION` environment variables. * Logs go to stdout locally or directly to Stackdriver when in GCP. * Using Go's 1.8 graceful HTTP shutdown. @@ -54,7 +54,7 @@ The `server/kit` package embodies Gizmo's goals to combine with go-kit. The `observe` package provides observability helpers for metrics and tracing through OpenCensus * `server/kit` (and soon SimpleServer) utilizes this package to create a StackDriver exporter with sane defaults -* `GoogleProjectID` and `IsGAE` can help you make decisions about the underlying platform +* `GoogleProjectID`, `IsGAE`, and `IsCloudRun` can help you make decisions about the underlying platform #### [`auth`](https://godoc.org/github.com/NYTimes/gizmo/auth) diff --git a/observe/observe.go b/observe/observe.go index 749952530..68a4b2357 100644 --- a/observe/observe.go +++ b/observe/observe.go @@ -74,12 +74,22 @@ func IsGAE() bool { return os.Getenv("GAE_DEPLOYMENT_ID") != "" } -// GetGAEInfo returns the GCP Project ID, -// the service, and the version of the application. -func GetGAEInfo() (projectID, service, version string) { - return GoogleProjectID(), - os.Getenv("GAE_SERVICE"), - os.Getenv("GAE_VERSION") +// GetGAEInfo returns the service and the version of the +// GAE application. +func GetGAEInfo() (service, version string) { + return os.Getenv("GAE_SERVICE"), os.Getenv("GAE_VERSION") +} + +// IsCloudRun tells you whether your program is running +// within the Cloud Run platform. +func IsCloudRun() bool { + return os.Getenv("K_CONFIGURATION") != "" +} + +// GetCloudRunInfo returns the service and the version of the +// Cloud Run application. +func GetCloudRunInfo() (service, version, config string) { + return os.Getenv("K_SERVICE"), os.Getenv("K_REVISION"), os.Getenv("K_CONFIGURATION") } // GetServiceInfo returns the GCP Project ID, @@ -89,10 +99,15 @@ func GetGAEInfo() (projectID, service, version string) { // your application can pass them in as variables // to be included in your trace attributes func GetServiceInfo() (projectID, service, version string) { - if IsGAE() { - return GetGAEInfo() + switch { + case IsGAE(): + service, version = GetGAEInfo() + case IsCloudRun(): + service, version, _ = GetCloudRunInfo() + default: + service, version = os.Getenv("SERVICE_NAME"), os.Getenv("SERVICE_VERSION") } - return GoogleProjectID(), os.Getenv("SERVICE_NAME"), os.Getenv("SERVICE_VERSION") + return GoogleProjectID(), service, version } // getSDOpts returns Stack Driver Options that you can pass directly @@ -105,7 +120,7 @@ func getSDOpts(projectID, service, version string, onErr func(err error)) *stack if err != nil { return nil } - canExport := IsGAE() + canExport := IsGAE() || IsCloudRun() if m := monitoredresource.Autodetect(); m != nil { mr = m canExport = true @@ -134,7 +149,7 @@ func getSDOpts(projectID, service, version string, onErr func(err error)) *stack // IsGCPEnabled returns whether the running application // is inside GCP or has access to its products. func IsGCPEnabled() bool { - return IsGAE() || monitoredresource.Autodetect() != nil + return IsGAE() || IsCloudRun() || monitoredresource.Autodetect() != nil } // SkipObserve checks if the GIZMO_SKIP_OBSERVE environment variable has been populated. diff --git a/server/kit/sd_log.go b/server/kit/sd_log.go index d3f1e278a..87325210f 100644 --- a/server/kit/sd_log.go +++ b/server/kit/sd_log.go @@ -37,6 +37,11 @@ func newStackdriverLogger(ctx context.Context, logID, projectID, service, versio if logID == "" { logID = "app_logs" } + } else if observe.IsCloudRun() { + resource.Type = "cloud_run_revision" + if logID == "" { + logID = "stdout" + } } else if mr := monitoredresource.Autodetect(); mr != nil { typ, lbls := mr.MonitoredResource() for f, v := range lbls {