Skip to content

Commit 7a8f53c

Browse files
authored
Add new gcp host attributes (#4263)
1 parent aab5f49 commit 7a8f53c

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1010

1111
### Added
1212

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

1516
### Changed

detectors/gcp/detector.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
"go.opentelemetry.io/otel/attribute"
2525
"go.opentelemetry.io/otel/sdk/resource"
26-
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
26+
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
2727
)
2828

2929
// NewDetector returns a resource detector which detects resource attributes on:
@@ -87,6 +87,8 @@ func (d *detector) Detect(ctx context.Context) (*resource.Resource, error) {
8787
b.add(semconv.HostTypeKey, d.detector.GCEHostType)
8888
b.add(semconv.HostIDKey, d.detector.GCEHostID)
8989
b.add(semconv.HostNameKey, d.detector.GCEHostName)
90+
b.add(semconv.GCPGceInstanceNameKey, d.detector.GCEInstanceName)
91+
b.add(semconv.GCPGceInstanceHostnameKey, d.detector.GCEInstanceHostname)
9092
default:
9193
// We don't support this platform yet, so just return with what we have
9294
}

detectors/gcp/detector_test.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/stretchr/testify/assert"
2525

2626
"go.opentelemetry.io/otel/sdk/resource"
27-
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
27+
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
2828
)
2929

3030
func TestDetect(t *testing.T) {
@@ -77,20 +77,24 @@ func TestDetect(t *testing.T) {
7777
{
7878
desc: "GCE",
7979
detector: &detector{detector: &fakeGCPDetector{
80-
projectID: "my-project",
81-
cloudPlatform: gcp.GCE,
82-
gceHostID: "1472385723456792345",
83-
gceHostName: "my-gke-node-1234",
84-
gceHostType: "n1-standard1",
85-
gceAvailabilityZone: "us-central1-c",
86-
gceRegion: "us-central1",
80+
projectID: "my-project",
81+
cloudPlatform: gcp.GCE,
82+
gceHostID: "1472385723456792345",
83+
gceHostName: "my-gke-node-1234",
84+
gceHostType: "n1-standard1",
85+
gceAvailabilityZone: "us-central1-c",
86+
gceRegion: "us-central1",
87+
gcpGceInstanceName: "my-gke-node-1234",
88+
gcpGceInstanceHostname: "hostname",
8789
}},
8890
expectedResource: resource.NewWithAttributes(semconv.SchemaURL,
8991
semconv.CloudProviderGCP,
9092
semconv.CloudAccountID("my-project"),
9193
semconv.CloudPlatformGCPComputeEngine,
9294
semconv.HostID("1472385723456792345"),
9395
semconv.HostName("my-gke-node-1234"),
96+
semconv.GCPGceInstanceNameKey.String("my-gke-node-1234"),
97+
semconv.GCPGceInstanceHostnameKey.String("hostname"),
9498
semconv.HostType("n1-standard1"),
9599
semconv.CloudRegion("us-central1"),
96100
semconv.CloudAvailabilityZone("us-central1-c"),
@@ -238,6 +242,8 @@ type fakeGCPDetector struct {
238242
gceHostType string
239243
gceHostID string
240244
gceHostName string
245+
gcpGceInstanceName string
246+
gcpGceInstanceHostname string
241247
}
242248

243249
func (f *fakeGCPDetector) ProjectID() (string, error) {
@@ -379,3 +385,17 @@ func (f *fakeGCPDetector) GCEHostName() (string, error) {
379385
}
380386
return f.gceHostName, nil
381387
}
388+
389+
func (f *fakeGCPDetector) GCEInstanceName() (string, error) {
390+
if f.err != nil {
391+
return "", f.err
392+
}
393+
return f.gcpGceInstanceName, nil
394+
}
395+
396+
func (f *fakeGCPDetector) GCEInstanceHostname() (string, error) {
397+
if f.err != nil {
398+
return "", f.err
399+
}
400+
return f.gcpGceInstanceHostname, nil
401+
}

detectors/gcp/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ type gcpDetector interface {
3737
GCEHostType() (string, error)
3838
GCEHostID() (string, error)
3939
GCEHostName() (string, error)
40+
GCEInstanceHostname() (string, error)
41+
GCEInstanceName() (string, error)
4042
}

0 commit comments

Comments
 (0)