diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f42be9913a..73f285c97db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,12 +23,14 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm This option function is used as a replacement of `WithInstrumentationScope` to specify the logged package version. (#5588) - The `WithSchemaURL` option function in `go.opentelemetry.io/contrib/bridges/otelslog`. This option function is used as a replacement of `WithInstrumentationScope` to specify the semantic convention schema URL for the logged records. (#5588) +- Add support for Cloud Run jobs in `go.opentelemetry.io/contrib/detectors/gcp`. (#5559) ### Changed - The gRPC trace `Filter` for interceptor is renamed to `InterceptorFilter`. (#5196) - The gRPC trace filter functions `Any`, `All`, `None`, `Not`, `MethodName`, `MethodPrefix`, `FullMethodName`, `ServiceName`, `ServicePrefix` and `HealthCheck` for interceptor are moved to `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/filters/interceptor`. With this change, the filters in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc` are now working for stats handler. (#5196) + - `NewLogger` now accepts a `name` `string` as the first argument. This parameter is used as a replacement of `WithInstrumentationScope` to specify the name of the logger backing the underlying `Handler`. (#5588) - `NewHandler` now accepts a `name` `string` as the first argument. diff --git a/detectors/gcp/README.md b/detectors/gcp/README.md index ed235f6b483..ec35080b4ec 100644 --- a/detectors/gcp/README.md +++ b/detectors/gcp/README.md @@ -6,6 +6,7 @@ The GCP resource detector supports detecting resources on: * Google Kubernetes Engine (GKE) * Google App Engine (GAE) * Cloud Run + * Cloud Run jobs * Cloud Functions ## Usage diff --git a/detectors/gcp/detector.go b/detectors/gcp/detector.go index f7040e3d199..9dabfe3d041 100644 --- a/detectors/gcp/detector.go +++ b/detectors/gcp/detector.go @@ -6,6 +6,7 @@ package gcp // import "go.opentelemetry.io/contrib/detectors/gcp" import ( "context" "fmt" + "strconv" "cloud.google.com/go/compute/metadata" "github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp" @@ -51,6 +52,13 @@ func (d *detector) Detect(ctx context.Context) (*resource.Resource, error) { b.add(semconv.FaaSVersionKey, d.detector.FaaSVersion) b.add(semconv.FaaSInstanceKey, d.detector.FaaSID) b.add(semconv.CloudRegionKey, d.detector.FaaSCloudRegion) + case gcp.CloudRunJob: + b.attrs = append(b.attrs, semconv.CloudPlatformGCPCloudRun) + b.add(semconv.FaaSNameKey, d.detector.FaaSName) + b.add(semconv.FaaSInstanceKey, d.detector.FaaSID) + b.add(semconv.GCPCloudRunJobExecutionKey, d.detector.CloudRunJobExecution) + b.addInt(semconv.GCPCloudRunJobTaskIndexKey, d.detector.CloudRunJobTaskIndex) + b.add(semconv.CloudRegionKey, d.detector.FaaSCloudRegion) case gcp.CloudFunctions: b.attrs = append(b.attrs, semconv.CloudPlatformGCPCloudFunctions) b.add(semconv.FaaSNameKey, d.detector.FaaSName) @@ -99,6 +107,18 @@ func (r *resourceBuilder) add(key attribute.Key, detect func() (string, error)) } } +func (r *resourceBuilder) addInt(key attribute.Key, detect func() (string, error)) { + if v, err := detect(); err == nil { + if vi, err := strconv.Atoi(v); err == nil { + r.attrs = append(r.attrs, key.Int(vi)) + } else { + r.errs = append(r.errs, err) + } + } else { + r.errs = append(r.errs, err) + } +} + // zoneAndRegion functions are expected to return zone, region, err. func (r *resourceBuilder) addZoneAndRegion(detect func() (string, string, error)) { if zone, region, err := detect(); err == nil { diff --git a/detectors/gcp/detector_test.go b/detectors/gcp/detector_test.go index 405022f48c2..9522698c91a 100644 --- a/detectors/gcp/detector_test.go +++ b/detectors/gcp/detector_test.go @@ -107,6 +107,50 @@ func TestDetect(t *testing.T) { semconv.FaaSInstance("1472385723456792345"), ), }, + { + desc: "Cloud Run Job", + detector: &detector{detector: &fakeGCPDetector{ + projectID: "my-project", + cloudPlatform: gcp.CloudRunJob, + faaSID: "1472385723456792345", + faaSCloudRegion: "us-central1", + faaSName: "my-service", + cloudRunJobExecution: "my-service-ekdih", + cloudRunJobTaskIndex: "0", + }}, + expectedResource: resource.NewWithAttributes(semconv.SchemaURL, + semconv.CloudProviderGCP, + semconv.CloudAccountID("my-project"), + semconv.CloudPlatformGCPCloudRun, + semconv.CloudRegion("us-central1"), + semconv.FaaSName("my-service"), + semconv.GCPCloudRunJobExecution("my-service-ekdih"), + semconv.GCPCloudRunJobTaskIndex(0), + semconv.FaaSInstance("1472385723456792345"), + ), + }, + { + desc: "Cloud Run Job Bad Index", + detector: &detector{detector: &fakeGCPDetector{ + projectID: "my-project", + cloudPlatform: gcp.CloudRunJob, + faaSID: "1472385723456792345", + faaSCloudRegion: "us-central1", + faaSName: "my-service", + cloudRunJobExecution: "my-service-ekdih", + cloudRunJobTaskIndex: "bad-value", + }}, + expectedResource: resource.NewWithAttributes(semconv.SchemaURL, + semconv.CloudProviderGCP, + semconv.CloudAccountID("my-project"), + semconv.CloudPlatformGCPCloudRun, + semconv.CloudRegion("us-central1"), + semconv.FaaSName("my-service"), + semconv.GCPCloudRunJobExecution("my-service-ekdih"), + semconv.FaaSInstance("1472385723456792345"), + ), + expectErr: true, + }, { desc: "Cloud Functions", detector: &detector{detector: &fakeGCPDetector{ @@ -231,6 +275,8 @@ type fakeGCPDetector struct { gceHostName string gcpGceInstanceName string gcpGceInstanceHostname string + cloudRunJobExecution string + cloudRunJobTaskIndex string } func (f *fakeGCPDetector) ProjectID() (string, error) { @@ -386,3 +432,17 @@ func (f *fakeGCPDetector) GCEInstanceHostname() (string, error) { } return f.gcpGceInstanceHostname, nil } + +func (f *fakeGCPDetector) CloudRunJobExecution() (string, error) { + if f.err != nil { + return "", f.err + } + return f.cloudRunJobExecution, nil +} + +func (f *fakeGCPDetector) CloudRunJobTaskIndex() (string, error) { + if f.err != nil { + return "", f.err + } + return f.cloudRunJobTaskIndex, nil +} diff --git a/detectors/gcp/types.go b/detectors/gcp/types.go index d56f4cf384a..666d82e616c 100644 --- a/detectors/gcp/types.go +++ b/detectors/gcp/types.go @@ -28,4 +28,6 @@ type gcpDetector interface { GCEHostName() (string, error) GCEInstanceHostname() (string, error) GCEInstanceName() (string, error) + CloudRunJobExecution() (string, error) + CloudRunJobTaskIndex() (string, error) }