Skip to content

Commit bde94ec

Browse files
authored
Allow for fallback env variables to handle missing K_<X> env var from Cloud Run (#460)
1 parent 088df59 commit bde94ec

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

perf/exporters.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,21 @@ func NewCloudExporter(opts CloudOptions) (Exporter, error) {
5151
sdOpts.ProjectID = os.Getenv(envVarStackDriverProjectID)
5252
}
5353

54-
// For Cloud Run applications use
54+
// For Cloud Run Services applications use
5555
// See: https://cloud.google.com/run/docs/container-contract#env-vars
56-
if os.Getenv("K_SERVICE") != "" {
56+
// Cloud Run Worker Pools do NOT have those env variable automatically injected
57+
// We manually add our own
58+
module := os.Getenv("K_SERVICE")
59+
if module == "" {
60+
// Check fallback env variable for Cloud Run Worker Pool
61+
module = os.Getenv("GAE_SERVICE")
62+
}
63+
version := os.Getenv("K_REVISION")
64+
if version == "" {
65+
// Check fallback env variable for Cloud Run Worker Pool
66+
version = os.Getenv("CRWP_VERSION")
67+
}
68+
if module != "" {
5769
// Allow for local testing with GCP_COMPUTE_ZONE
5870
var err error
5971
zone := os.Getenv("GCP_COMPUTE_ZONE")
@@ -84,8 +96,8 @@ func NewCloudExporter(opts CloudOptions) (Exporter, error) {
8496
// See: https://cloud.google.com/monitoring/custom-metrics/creating-metrics#create-metric-desc
8597
sdOpts.MonitoredResource = &gaeResource{
8698
projectID: os.Getenv(envVarStackDriverProjectID),
87-
module: os.Getenv("K_SERVICE"),
88-
version: os.Getenv("K_REVISION"),
99+
module: module,
100+
version: version,
89101
instanceID: instanceID,
90102
location: zone,
91103
}

0 commit comments

Comments
 (0)