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

Add support for detecting MIG attributes on GCE VMs #6538

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions detectors/gcp/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
b.add(semconv.HostNameKey, d.detector.GCEHostName)
b.add(semconv.GCPGceInstanceNameKey, d.detector.GCEInstanceName)
b.add(semconv.GCPGceInstanceHostnameKey, d.detector.GCEInstanceHostname)
b.addMIG(d.detector.GCEManagedInstanceGroup)
default:
// We don't support this platform yet, so just return with what we have
}
Expand Down Expand Up @@ -144,6 +145,31 @@
}
}

var (
// TODO: semconv.GCPGceInstanceGroupManagerNameKey
gcpGceInstanceGroupManagerNameKey = attribute.Key("gcp.gce.instance_group_manager.name")
// TODO: semconv.GCPGceInstanceGroupManagerZoneKey
gcpGceInstanceGroupManagerZoneKey = attribute.Key("gcp.gce.instance_group_manager.zone")
// TODO: semconv.GCPGceInstanceGroupManagerRegionKey
gcpGceInstanceGroupManagerRegionKey = attribute.Key("gcp.gce.instance_group_manager.region")
Comment on lines +149 to +154
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please open an issue to track the work needed to resolve these TODOs and reference the issue in this comment.

)

func (r *resourceBuilder) addMIG(detect func() (gcp.ManagedInstanceGroup, error)) {
if mig, err := detect(); err == nil {
if mig.Name != "" {
r.attrs = append(r.attrs, gcpGceInstanceGroupManagerNameKey.String(mig.Name))
}
switch mig.Type {
case gcp.Zone:
r.attrs = append(r.attrs, gcpGceInstanceGroupManagerZoneKey.String(mig.Location))
case gcp.Region:
r.attrs = append(r.attrs, gcpGceInstanceGroupManagerRegionKey.String(mig.Location))
}
} else {
r.errs = append(r.errs, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test for this error.

}

Check warning on line 170 in detectors/gcp/detector.go

View check run for this annotation

Codecov / codecov/patch

detectors/gcp/detector.go#L168-L170

Added lines #L168 - L170 were not covered by tests
Comment on lines +158 to +170
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation can be reduced by restructuring:

Suggested change
if mig, err := detect(); err == nil {
if mig.Name != "" {
r.attrs = append(r.attrs, gcpGceInstanceGroupManagerNameKey.String(mig.Name))
}
switch mig.Type {
case gcp.Zone:
r.attrs = append(r.attrs, gcpGceInstanceGroupManagerZoneKey.String(mig.Location))
case gcp.Region:
r.attrs = append(r.attrs, gcpGceInstanceGroupManagerRegionKey.String(mig.Location))
}
} else {
r.errs = append(r.errs, err)
}
mig, err := detect()
if err != nil {
r.errs = append(r.errs, err)
return
}
if mig.Name != "" {
r.attrs = append(r.attrs, gcpGceInstanceGroupManagerNameKey.String(mig.Name))
}
switch mig.Type {
case gcp.Zone:
r.attrs = append(r.attrs, gcpGceInstanceGroupManagerZoneKey.String(mig.Location))
case gcp.Region:
r.attrs = append(r.attrs, gcpGceInstanceGroupManagerRegionKey.String(mig.Location))
}

}

func (r *resourceBuilder) build() (*resource.Resource, error) {
var err error
if len(r.errs) > 0 {
Expand Down
74 changes: 74 additions & 0 deletions detectors/gcp/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,72 @@ func TestDetect(t *testing.T) {
semconv.CloudAvailabilityZone("us-central1-c"),
),
},
{
desc: "GCE with zonal MIG",
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",
gcpGceInstanceName: "my-gke-node-1234",
gcpGceInstanceHostname: "hostname",
managedInstanceGroup: gcp.ManagedInstanceGroup{
Name: "my-instance-group",
Location: "us-central1-c",
Type: gcp.Zone,
},
}},
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"),
gcpGceInstanceGroupManagerNameKey.String("my-instance-group"),
gcpGceInstanceGroupManagerZoneKey.String("us-central1-c"),
),
},
{
desc: "GCE with regional MIG",
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",
gcpGceInstanceName: "my-gke-node-1234",
gcpGceInstanceHostname: "hostname",
managedInstanceGroup: gcp.ManagedInstanceGroup{
Name: "my-instance-group",
Location: "us-central1",
Type: gcp.Region,
},
}},
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"),
gcpGceInstanceGroupManagerNameKey.String("my-instance-group"),
gcpGceInstanceGroupManagerRegionKey.String("us-central1"),
),
},
{
desc: "Cloud Run",
detector: &detector{detector: &fakeGCPDetector{
Expand Down Expand Up @@ -277,6 +343,7 @@ type fakeGCPDetector struct {
gcpGceInstanceHostname string
cloudRunJobExecution string
cloudRunJobTaskIndex string
managedInstanceGroup gcp.ManagedInstanceGroup
}

func (f *fakeGCPDetector) ProjectID() (string, error) {
Expand Down Expand Up @@ -446,3 +513,10 @@ func (f *fakeGCPDetector) CloudRunJobTaskIndex() (string, error) {
}
return f.cloudRunJobTaskIndex, nil
}

func (f *fakeGCPDetector) GCEManagedInstanceGroup() (gcp.ManagedInstanceGroup, error) {
if f.err != nil {
return gcp.ManagedInstanceGroup{}, f.err
}
return f.managedInstanceGroup, nil
}
1 change: 1 addition & 0 deletions detectors/gcp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ type gcpDetector interface {
GCEInstanceName() (string, error)
CloudRunJobExecution() (string, error)
CloudRunJobTaskIndex() (string, error)
GCEManagedInstanceGroup() (gcp.ManagedInstanceGroup, error)
}
Loading