Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detectors/gcp: Add support for Cloud Run jobs #5559

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
nvx marked this conversation as resolved.
Show resolved Hide resolved
}},
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)
}