Skip to content

Commit

Permalink
detectors/gcp: Add support for Cloud Run jobs (#5559)
Browse files Browse the repository at this point in the history
Adds support for Cloud Run jobs, previously on Cloud Run services were
supported.

I've left the cloud platform as `semconv.CloudPlatformGCPCloudRun` as
it's a part of Cloud Run ecosystem, but have no objections to making a
"gcp_cloud_run_jobs" if needed although that would require an update to
the `semconv` package first and I'm not quite sure I understand how the
folder versioning works for that so would be looking for guidance (do I
just add it to the latest version folder? do I make a new version
copying the latest version folder?)

---------

Co-authored-by: David Ashpole <dashpole@google.com>
  • Loading branch information
nvx and dashpole authored May 17, 2024
1 parent a7bcce4 commit 0812bb2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions detectors/gcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions detectors/gcp/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
60 changes: 60 additions & 0 deletions detectors/gcp/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -231,6 +275,8 @@ type fakeGCPDetector struct {
gceHostName string
gcpGceInstanceName string
gcpGceInstanceHostname string
cloudRunJobExecution string
cloudRunJobTaskIndex string
}

func (f *fakeGCPDetector) ProjectID() (string, error) {
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions detectors/gcp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ type gcpDetector interface {
GCEHostName() (string, error)
GCEInstanceHostname() (string, error)
GCEInstanceName() (string, error)
CloudRunJobExecution() (string, error)
CloudRunJobTaskIndex() (string, error)
}

0 comments on commit 0812bb2

Please sign in to comment.