aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Pratt <mpratt@google.com>2025-09-02 12:10:26 -0400
committerMichael Pratt <mpratt@google.com>2025-09-08 06:49:23 -0700
commitf655b297736ff88a3f37c952fb49266ab92fa130 (patch)
tree730c8f314a9c831ea1edfadf6e94c755e9fd2dfb
parent52bba2a3258a2d3623a8723127da80fd383aff10 (diff)
downloadgo-x-pkgsite-f655b297736ff88a3f37c952fb49266ab92fa130.tar.xz
cmd/internal/cmdconfig: include Cloud Run instance ID in logs
This aids in diagnosing per-instance issues. The instance ID seems like it would belong in the MonitoredResource labels (configured in serverconfig), but the top-level instanceId field is the standard name for this field on Cloud Run. Log messages going directly to stderr (before we configure logging) get this label automatically from the Cloud Run infra. When we use stackdriver directly we apparently need to add this ourselves. See https://cloud.google.com/run/docs/logging#service-fields. Change-Id: I6a6a636cb11a2abc00347e0679605ebbdb9995f4 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/700396 Reviewed-by: Jonathan Amsterdam <jba@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> kokoro-CI: kokoro <noreply+kokoro@google.com>
-rw-r--r--cmd/internal/cmdconfig/cmdconfig.go7
-rw-r--r--internal/config/serverconfig/config.go8
2 files changed, 11 insertions, 4 deletions
diff --git a/cmd/internal/cmdconfig/cmdconfig.go b/cmd/internal/cmdconfig/cmdconfig.go
index 6d46e094..1e08cd25 100644
--- a/cmd/internal/cmdconfig/cmdconfig.go
+++ b/cmd/internal/cmdconfig/cmdconfig.go
@@ -36,6 +36,13 @@ func Logger(ctx context.Context, cfg *config.Config, logName string) middleware.
Type: cfg.MonitoredResource.Type,
Labels: cfg.MonitoredResource.Labels,
})}
+ if serverconfig.OnCloudRun() {
+ opts = append(opts, logging.CommonLabels(map[string]string{
+ // Standard label for Cloud Run instance ID.
+ // See https://cloud.google.com/run/docs/logging#service-fields.
+ "instanceId": cfg.InstanceID,
+ }))
+ }
if serverconfig.OnGKE() {
opts = append(opts, logging.CommonLabels(map[string]string{
"k8s-pod/env": cfg.DeploymentEnvironment(),
diff --git a/internal/config/serverconfig/config.go b/internal/config/serverconfig/config.go
index 7b37a70d..196e9e93 100644
--- a/internal/config/serverconfig/config.go
+++ b/internal/config/serverconfig/config.go
@@ -85,8 +85,8 @@ func OnGKE() bool {
return os.Getenv("GO_DISCOVERY_ON_GKE") == "true"
}
-// onCloudRun reports whether the current process is running on Cloud Run.
-func onCloudRun() bool {
+// OnCloudRun reports whether the current process is running on Cloud Run.
+func 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"} {
@@ -100,7 +100,7 @@ func onCloudRun() bool {
// OnGCP reports whether the current process is running on Google Cloud
// Platform.
func OnGCP() bool {
- return OnAppEngine() || OnGKE() || onCloudRun()
+ return OnAppEngine() || OnGKE() || OnCloudRun()
}
// configOverride holds selected config settings that can be dynamically overridden.
@@ -212,7 +212,7 @@ func Init(ctx context.Context) (_ *config.Config, err error) {
"zone": cfg.ZoneID,
},
}
- case onCloudRun():
+ case OnCloudRun():
cfg.MonitoredResource = &config.MonitoredResource{
Type: "cloud_run_revision",
Labels: map[string]string{