Skip to content

Commit

Permalink
[processor/resourcedetection] Add support for detecting GCE VMs in Ma…
Browse files Browse the repository at this point in the history
…naged Instance Groups
  • Loading branch information
quentinmit committed Oct 25, 2024
1 parent 1d555e9 commit 6fc73b1
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
| faas.version | The faas.version | Any Str | true |
| gcp.cloud_run.job.execution | The Job execution name | Any Str | true |
| gcp.cloud_run.job.task_index | The Job execution task index | Any Str | true |
| gcp.gce.instance.group.manager.name | The name of an instanceGroupManager. | Any Str | true |
| gcp.gce.instance.group.manager.region | The region of a regional instanceGroupManager. | Any Str | true |
| gcp.gce.instance.group.manager.zone | The zone of a zonal instanceGroupManager. | Any Str | true |
| gcp.gce.instance.hostname | The hostname of the GCE instance. | Any Str | false |
| gcp.gce.instance.name | The name of the GCE instance. | Any Str | false |
| host.id | The host.id | Any Str | true |
Expand Down
1 change: 1 addition & 0 deletions processor/resourcedetectionprocessor/internal/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (d *detector) Detect(context.Context) (resource pcommon.Resource, schemaURL
d.rb.SetFromCallable(d.rb.SetHostName, d.detector.GCEHostName),
d.rb.SetFromCallable(d.rb.SetGcpGceInstanceHostname, d.detector.GCEInstanceHostname),
d.rb.SetFromCallable(d.rb.SetGcpGceInstanceName, d.detector.GCEInstanceName),
d.rb.SetManagedInstanceGroup(d.detector.GCEManagedInstanceGroup),
)
default:
// We don't support this platform yet, so just return with what we have
Expand Down
39 changes: 39 additions & 0 deletions processor/resourcedetectionprocessor/internal/gcp/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,37 @@ func TestDetect(t *testing.T) {
"gcp.gce.instance.name": "my-gke-node-1234",
},
},
{
desc: "GCE with MIG",
detector: newTestDetector(&fakeGCPDetector{
projectID: "my-project",
cloudPlatform: gcp.GCE,
gceHostID: "1472385723456792345",
gceHostName: "my-gke-node-1234",
gceHostType: "n1-standard1",
gceAvailabilityZone: "us-central1-c",
gceRegion: "us-central1",
gcpGceInstanceHostname: "custom.dns.example.com",
gcpGceInstanceName: "my-gke-node-1234",
gcpGceManagedInstanceGroup: gcp.ManagedInstanceGroup{
Name: "my-gke-node",
Location: "us-central1",
Type: gcp.Region,
},
}),
expectedResource: map[string]any{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderGCP,
conventions.AttributeCloudAccountID: "my-project",
conventions.AttributeCloudPlatform: conventions.AttributeCloudPlatformGCPComputeEngine,
conventions.AttributeHostID: "1472385723456792345",
conventions.AttributeHostName: "my-gke-node-1234",
conventions.AttributeHostType: "n1-standard1",
conventions.AttributeCloudRegion: "us-central1",
conventions.AttributeCloudAvailabilityZone: "us-central1-c",
"gcp.gce.instance.group.manager.name": "my-gke-node",
"gcp.gce.instance.group.manager.region": "us-central1",
},
},
{
desc: "Cloud Run",
detector: newTestDetector(&fakeGCPDetector{
Expand Down Expand Up @@ -456,6 +487,7 @@ type fakeGCPDetector struct {
gcpCloudRunJobTaskIndex string
gcpGceInstanceName string
gcpGceInstanceHostname string
gcpGceManagedInstanceGroup gcp.ManagedInstanceGroup
gcpBareMetalSolutionInstanceID string
gcpBareMetalSolutionCloudRegion string
gcpBareMetalSolutionProjectID string
Expand Down Expand Up @@ -622,6 +654,13 @@ func (f *fakeGCPDetector) GCEInstanceHostname() (string, error) {
return f.gcpGceInstanceHostname, nil
}

func (f *fakeGCPDetector) GCEManagedInstanceGroup() (gcp.ManagedInstanceGroup, error) {
if f.err != nil {
return gcp.ManagedInstanceGroup{}, f.err
}
return f.gcpGceManagedInstanceGroup, nil
}

func (f *fakeGCPDetector) BareMetalSolutionInstanceID() (string, error) {
if f.err != nil {
return "", f.err
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ func (rb *ResourceBuilder) SetZoneOrRegion(detect func() (string, gcp.LocationTy
}
return nil
}

func (rb *ResourceBuilder) SetManagedInstanceGroup(detect func() (gcp.ManagedInstanceGroup, error)) error {
v, err := detect()
if err != nil {
return err
}
if v.Name != "" {
rb.SetGcpGceInstanceGroupManagerName(v.Name)
}
switch v.Type {
case gcp.Zone:
rb.SetGcpGceInstanceGroupManagerZone(v.Location)
case gcp.Region:
rb.SetGcpGceInstanceGroupManagerRegion(v.Location)
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ all_set:
enabled: true
gcp.cloud_run.job.task_index:
enabled: true
gcp.gce.instance.group.manager.name:
enabled: true
gcp.gce.instance.group.manager.region:
enabled: true
gcp.gce.instance.group.manager.zone:
enabled: true
gcp.gce.instance.hostname:
enabled: true
gcp.gce.instance.name:
Expand Down Expand Up @@ -59,6 +65,12 @@ none_set:
enabled: false
gcp.cloud_run.job.task_index:
enabled: false
gcp.gce.instance.group.manager.name:
enabled: false
gcp.gce.instance.group.manager.region:
enabled: false
gcp.gce.instance.group.manager.zone:
enabled: false
gcp.gce.instance.hostname:
enabled: false
gcp.gce.instance.name:
Expand Down
12 changes: 12 additions & 0 deletions processor/resourcedetectionprocessor/internal/gcp/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ resource_attributes:
description: The hostname of the GCE instance.
type: string
enabled: false
gcp.gce.instance.group.manager.name:
description: The name of an instanceGroupManager.
type: string
enabled: true
gcp.gce.instance.group.manager.zone:
description: The zone of a zonal instanceGroupManager.
type: string
enabled: true
gcp.gce.instance.group.manager.region:
description: The region of a regional instanceGroupManager.
type: string
enabled: true
1 change: 1 addition & 0 deletions processor/resourcedetectionprocessor/internal/gcp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type gcpDetector interface {
CloudRunJobTaskIndex() (string, error)
GCEInstanceHostname() (string, error)
GCEInstanceName() (string, error)
GCEManagedInstanceGroup() (gcp.ManagedInstanceGroup, error)
BareMetalSolutionInstanceID() (string, error)
BareMetalSolutionCloudRegion() (string, error)
BareMetalSolutionProjectID() (string, error)
Expand Down

0 comments on commit 6fc73b1

Please sign in to comment.