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 (#36142)

<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description
This adds resource detection for the GCE "instance group manager" that
created an instance, allowing like VMs to be grouped using a resource
attribute.

I also have corresponding branches prepared for opentelemetry-go and
semantic-conventions

<!--Describe what testing was performed and which tests were added.-->
#### Testing
This PR includes unit tests to validate that the correct resource
attributes are added, and I manually tested a collector built with this
change on a MIG VM and it generates the expected resource attributes.

<!--Describe the documentation added.-->
#### Documentation
Documented with the other resource attributes in the existing
documentation.

<!--Please delete paragraphs that you did not use before submitting.-->

---------

Co-authored-by: David Ashpole <[email protected]>
Co-authored-by: Tyler Helmuth <[email protected]>
Co-authored-by: Pablo Baeyens <[email protected]>
Co-authored-by: Antoine Toulme <[email protected]>
Co-authored-by: Yang Song <[email protected]>
Co-authored-by: Sean Marciniak <[email protected]>
  • Loading branch information
7 people authored Jan 25, 2025
1 parent 9841fe1 commit 7c32a5d
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 53 deletions.
27 changes: 27 additions & 0 deletions .chloggen/gcp-gce-mig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: resourcedetectionprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: The `gcp` resource detector will now detect resource attributes identifying a GCE instance's managed instance group.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [36142]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
| gcp.cloud_run.job.task_index | The Job execution task index | 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 |
| 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 |
| host.id | The host.id | Any Str | true |
| host.name | The host.name | Any Str | true |
| host.type | The host.type | 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 @@ -27,6 +27,12 @@ all_set:
enabled: true
gcp.gce.instance.name:
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
host.id:
enabled: true
host.name:
Expand Down Expand Up @@ -63,6 +69,12 @@ none_set:
enabled: false
gcp.gce.instance.name:
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
host.id:
enabled: false
host.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 @@ -71,3 +71,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 7c32a5d

Please sign in to comment.