Skip to content

Commit

Permalink
Detect cloud run for stackdriver logging (#234)
Browse files Browse the repository at this point in the history
* Added cloud run detection

* Refactored GetServiceInfo, added Run resource type

* Removed debug code

* Updated docs

* Updated naming to be more clear

* Updated readme

* Renamed func
  • Loading branch information
darrenmcc authored and jprobinson committed Oct 30, 2019
1 parent 478145f commit d0e7717
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)

Expand Down
37 changes: 26 additions & 11 deletions observe/observe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions server/kit/sd_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d0e7717

Please sign in to comment.