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 @@ func (d *detector) Detect(ctx context.Context) (*resource.Resource, error) {
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 @@ func (r *resourceBuilder) addZoneOrRegion(detect func() (string, gcp.LocationTyp
}
}

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")
)

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)
}
}

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