googleapis / googleapis/google-cloud-go
logging: Resource Labels not set for Cloud Run Function (Gen 2) / Cloud Run Revision
- Dominant language
- Go
- Stars
- 4.5k
- Forks
- 1.6k
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 109
Description
## Client
Logging
## Environment
Tested both locally (using the Cloud Functions Framework) and deployed, using Cloud Build, to Google Cloud Run Functions.
Local info:
```text
go version go1.24.6 darwin/amd64
```
This was the buildInfo for the log
```text
buildConfig:
automaticUpdatePolicy: {}
build: projects/857264307371/locations/asia-southeast1/builds/770b4b91-7255-4830-b575-4d60f72fd1ee
dockerRegistry: ARTIFACT_REGISTRY
dockerRepository: projects/redacted/locations/asia-southeast1/repositories/gcf-artifacts
entryPoint: CloudRunFunctionLogTest
runtime: go124
source:
storageSource:
bucket: gcf-v2-sources-857264307371-asia-southeast1
generation: '1756806556452354'
object: logging-resource-test/function-source.zip
sourceProvenance:
resolvedStorageSource:
bucket: gcf-v2-sources-857264307371-asia-southeast1
generation: '1756806556452354'
object: logging-resource-test/function-source.zip
createTime: '2025-09-02T09:49:17.113692395Z'
environment: GEN_2
eventTrigger:
eventType: google.cloud.pubsub.topic.v1.messagePublished
pubsubTopic: projects/redacted/topics/logging-resource-test
retryPolicy: RETRY_POLICY_DO_NOT_RETRY
serviceAccountEmail: 1234562437623-compute@developer.gserviceaccount.com
trigger: projects/redacted/locations/asia-southeast1/triggers/logging-resource-test-352021
triggerRegion: asia-southeast1
labels:
deployment-tool: cli-gcloud
name: projects/redacted/locations/asia-southeast1/functions/logging-resource-test
serviceConfig:
allTrafficOnLatestRevision: true
availableCpu: '0.0833'
availableMemory: 128Mi
environmentVariables:
GCP_PROJECT: redacted
LOG_EXECUTION_ID: 'true'
ingressSettings: ALLOW_ALL
maxInstanceCount: 1
maxInstanceRequestConcurrency: 1
revision: logging-resource-test-00001-neh
service: projects/redacted/locations/asia-southeast1/services/logging-resource-test
serviceAccountEmail: 1234562437623-compute@developer.gserviceaccount.com
timeoutSeconds: 60
uri: https://logging-resource-test-qnubfm7itq-as.a.run.app
state: ACTIVE
updateTime: '2025-09-02T09:51:44.081311229Z'
url: https://asia-southeast1-redacted.cloudfunctions.net/logging-resource-test
```
## Code and Dependencies
This is the basic code that I used for testing
```go
package main
import (
"context"
"os"
"cloud.google.com/go/logging"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/cloudevents/sdk-go/v2/event"
mrpb "google.golang.org/genproto/googleapis/api/monitoredres"
)
func init() {
// This is used for local testing with the Google Functions Framework.
functions.CloudEvent("CloudRunFunctionLogTest", CloudRunFunctionLogTest)
}
func CloudRunFunctionLogTest(ctx context.Context, e event.Event) error {
// Create Client
client, err := logging.NewClient(ctx, os.Getenv("GCP_PROJECT"))
if err != nil {
return err
}
// Create a *logging.Logger with just a logID set
testLogger := client.Logger("testLogger")
resourceCloudRunRevison := &mrpb.MonitoredResource{
Type: "cloud_run_revision",
Labels: map[string]string{
"project_id": "dummy_project",
"service_name": "dummy_service_name",
"revision_name": "dummy_rev_name",
"location": "asia-southeast1",
"configuration_name": "dummy_config_name",
},
}
// Create a *logging.Logger with manual setting for a Cloud Run Revision
testLoggerCloudRunRevision := client.Logger("testLoggerCloudRunRevision", logging.CommonResource(resourceCloudRunRevison))
resourceCloudFunction := &mrpb.MonitoredResource{
Type: "cloud_function",
Labels: map[string]string{
"function_name": "dummy_func_name",
"project_id": "dummy_project",
"region": "asia-southeast1",
},
}
// Create a *logging.Logger with manual setting for a Cloud Function
testLoggerCloudFunction := client.Logger("testLoggerCloudFunction", logging.CommonResource(resourceCloudFunction))
// Create 3 *log.Loggers
stdLogger1 := testLogger.StandardLogger(logging.Notice)
stdLogger2 := testLoggerCloudRunRevision.StandardLogger(logging.Notice)
stdLogger3 := testLoggerCloudFunction.StandardLogger(logging.Notice)
// Log Test Messages from each of the *log.Loggers
stdLogger1.Printf("Log Test 1 - No CommonResource added")
stdLogger2.Printf("Log Test 2 - Cloud Run Revision CommonResource manually added")
stdLogger3.Printf("Log Test 3 - Cloud Function CommonResource manually added")
return nil
}
```
go.mod
```text
module example.com/logtest
go 1.24.0
require (
cloud.google.com/go/logging v1.13.0
github.com/GoogleCloudPlatform/functions-framework-go v1.9.2
github.com/cloudevents/sdk-go/v2 v2.16.1
google.golang.org/genproto/googleapis/api v0.0.0-20250826171959-ef028d996bc1
)
require (
cloud.google.com/go v0.117.0 // indirect
cloud.google.com/go/auth v0.13.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.6 // indirect
cloud.google.com/go/compute/metadata v0.6.0 // indirect
cloud.google.com/go/functions v1.19.3 // indirect
cloud.google.com/go/longrunning v0.6.2 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.14.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/net v0.37.0 // indirect
golang.org/x/oauth2 v0.25.0 // indirect
golang.org/x/sync v0.15.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.12.0 // indirect
google.golang.org/api v0.214.0 // indirect
google.golang.org/genproto v0.0.0-20241118233622-e639e219e697 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250818200422-3122310a409c // indirect
google.golang.org/grpc v1.71.0 // indirect
google.golang.org/protobuf v1.36.8 // indirect
)
```
## Expected behavior
The expected behaviour is that for a `*logger.Logger`, without any specific `logging.LoggerOptions` set, the `resource.type` and `resource.labels` should be detected and set based on where the library is running from. This does not appear to be working for Cloud Run Function (Gen 2).
As this is a Gen 2 function, it's not exactly clear is the detected `resource.type` should be `cloud_function` or `cloud_run_revision` (although I suspect Cloud Run Revision would make the most sense).
So an example log message, with appropriate `resource` settings would look like:
```text
insertId: 1nzkf4qf8zd3ji
logName: projects/redacted/logs/testLoggerCloudRunRevision
receiveTimestamp: '2025-09-02T09:32:27.629960961Z'
resource:
labels:
configuration_name: dummy_config_name
location: asia-southeast1
project_id: dummy_project
revision_name: dummy_rev_name
service_name: dummy_service_name
type: cloud_run_revision
severity: NOTICE
textPayload: |
Log Test 2 - Cloud Run Revision CommonResource manually added
timestamp: '2025-09-02T09:32:26.036192Z'
```
My understanding is that the schema for the Monitored Resource (Cloud Run Revision) is defined here:
## Actual behavior
The log messages have a `resource.type` of `project` and only the `resource.label.project_id` set.
For example:
```text
insertId: 1h23reof8v9cp3
logName: projects/redacted/logs/testLogger
receiveTimestamp: '2025-09-02T09:32:27.599605557Z'
resource:
labels:
project_id: redacted
type: project
severity: NOTICE
textPayload: |
Log Test 1 - No CommonResource added
timestamp: '2025-09-02T09:32:26.035482Z'
```
## Screenshots
Contributor guide
Assessment
This issue has not been assessed yet.