Skip to content

Commit f655b29

Browse files
committed
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> kokoro-CI: kokoro <[email protected]>
1 parent 52bba2a commit f655b29

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

cmd/internal/cmdconfig/cmdconfig.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ func Logger(ctx context.Context, cfg *config.Config, logName string) middleware.
3636
Type: cfg.MonitoredResource.Type,
3737
Labels: cfg.MonitoredResource.Labels,
3838
})}
39+
if serverconfig.OnCloudRun() {
40+
opts = append(opts, logging.CommonLabels(map[string]string{
41+
// Standard label for Cloud Run instance ID.
42+
// See https://cloud.google.com/run/docs/logging#service-fields.
43+
"instanceId": cfg.InstanceID,
44+
}))
45+
}
3946
if serverconfig.OnGKE() {
4047
opts = append(opts, logging.CommonLabels(map[string]string{
4148
"k8s-pod/env": cfg.DeploymentEnvironment(),

internal/config/serverconfig/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func OnGKE() bool {
8585
return os.Getenv("GO_DISCOVERY_ON_GKE") == "true"
8686
}
8787

88-
// onCloudRun reports whether the current process is running on Cloud Run.
89-
func onCloudRun() bool {
88+
// OnCloudRun reports whether the current process is running on Cloud Run.
89+
func OnCloudRun() bool {
9090
// Use the presence of the environment variables provided by Cloud Run.
9191
// See https://cloud.google.com/run/docs/reference/container-contract.
9292
for _, ev := range []string{"K_SERVICE", "K_REVISION", "K_CONFIGURATION"} {
@@ -100,7 +100,7 @@ func onCloudRun() bool {
100100
// OnGCP reports whether the current process is running on Google Cloud
101101
// Platform.
102102
func OnGCP() bool {
103-
return OnAppEngine() || OnGKE() || onCloudRun()
103+
return OnAppEngine() || OnGKE() || OnCloudRun()
104104
}
105105

106106
// configOverride holds selected config settings that can be dynamically overridden.
@@ -212,7 +212,7 @@ func Init(ctx context.Context) (_ *config.Config, err error) {
212212
"zone": cfg.ZoneID,
213213
},
214214
}
215-
case onCloudRun():
215+
case OnCloudRun():
216216
cfg.MonitoredResource = &config.MonitoredResource{
217217
Type: "cloud_run_revision",
218218
Labels: map[string]string{

0 commit comments

Comments
 (0)