Skip to content

Commit

Permalink
Add new gcp host attributes (#4263)
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole authored Sep 6, 2023
1 parent aab5f49 commit 7a8f53c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Added

- Add `gcp.gce.instance.name` and `gcp.gce.instance.hostname` resource attributes to `go.opentelemetry.io/contrib/detectors/gcp`. (#4263)
- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068, #3108)

### Changed
Expand Down
4 changes: 3 additions & 1 deletion detectors/gcp/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
)

// NewDetector returns a resource detector which detects resource attributes on:
Expand Down Expand Up @@ -87,6 +87,8 @@ func (d *detector) Detect(ctx context.Context) (*resource.Resource, error) {
b.add(semconv.HostTypeKey, d.detector.GCEHostType)
b.add(semconv.HostIDKey, d.detector.GCEHostID)
b.add(semconv.HostNameKey, d.detector.GCEHostName)
b.add(semconv.GCPGceInstanceNameKey, d.detector.GCEInstanceName)
b.add(semconv.GCPGceInstanceHostnameKey, d.detector.GCEInstanceHostname)
default:
// We don't support this platform yet, so just return with what we have
}
Expand Down
36 changes: 28 additions & 8 deletions detectors/gcp/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
)

func TestDetect(t *testing.T) {
Expand Down Expand Up @@ -77,20 +77,24 @@ func TestDetect(t *testing.T) {
{
desc: "GCE",
detector: &detector{detector: &fakeGCPDetector{
projectID: "my-project",
cloudPlatform: gcp.GCE,
gceHostID: "1472385723456792345",
gceHostName: "my-gke-node-1234",
gceHostType: "n1-standard1",
gceAvailabilityZone: "us-central1-c",
gceRegion: "us-central1",
projectID: "my-project",
cloudPlatform: gcp.GCE,
gceHostID: "1472385723456792345",
gceHostName: "my-gke-node-1234",
gceHostType: "n1-standard1",
gceAvailabilityZone: "us-central1-c",
gceRegion: "us-central1",
gcpGceInstanceName: "my-gke-node-1234",
gcpGceInstanceHostname: "hostname",
}},
expectedResource: resource.NewWithAttributes(semconv.SchemaURL,
semconv.CloudProviderGCP,
semconv.CloudAccountID("my-project"),
semconv.CloudPlatformGCPComputeEngine,
semconv.HostID("1472385723456792345"),
semconv.HostName("my-gke-node-1234"),
semconv.GCPGceInstanceNameKey.String("my-gke-node-1234"),
semconv.GCPGceInstanceHostnameKey.String("hostname"),
semconv.HostType("n1-standard1"),
semconv.CloudRegion("us-central1"),
semconv.CloudAvailabilityZone("us-central1-c"),
Expand Down Expand Up @@ -238,6 +242,8 @@ type fakeGCPDetector struct {
gceHostType string
gceHostID string
gceHostName string
gcpGceInstanceName string
gcpGceInstanceHostname string
}

func (f *fakeGCPDetector) ProjectID() (string, error) {
Expand Down Expand Up @@ -379,3 +385,17 @@ func (f *fakeGCPDetector) GCEHostName() (string, error) {
}
return f.gceHostName, nil
}

func (f *fakeGCPDetector) GCEInstanceName() (string, error) {
if f.err != nil {
return "", f.err
}
return f.gcpGceInstanceName, nil
}

func (f *fakeGCPDetector) GCEInstanceHostname() (string, error) {
if f.err != nil {
return "", f.err
}
return f.gcpGceInstanceHostname, nil
}
2 changes: 2 additions & 0 deletions detectors/gcp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ type gcpDetector interface {
GCEHostType() (string, error)
GCEHostID() (string, error)
GCEHostName() (string, error)
GCEInstanceHostname() (string, error)
GCEInstanceName() (string, error)
}

0 comments on commit 7a8f53c

Please sign in to comment.