diff --git a/detectors/gcp/detector.go b/detectors/gcp/detector.go index b9eb1e1e149..7f6f3dc474b 100644 --- a/detectors/gcp/detector.go +++ b/detectors/gcp/detector.go @@ -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 } @@ -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 { diff --git a/detectors/gcp/detector_test.go b/detectors/gcp/detector_test.go index 1ecc97290e3..ff0123b2955 100644 --- a/detectors/gcp/detector_test.go +++ b/detectors/gcp/detector_test.go @@ -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{ @@ -277,6 +343,7 @@ type fakeGCPDetector struct { gcpGceInstanceHostname string cloudRunJobExecution string cloudRunJobTaskIndex string + managedInstanceGroup gcp.ManagedInstanceGroup } func (f *fakeGCPDetector) ProjectID() (string, error) { @@ -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 +} diff --git a/detectors/gcp/types.go b/detectors/gcp/types.go index 666d82e616c..126f8f74b6d 100644 --- a/detectors/gcp/types.go +++ b/detectors/gcp/types.go @@ -30,4 +30,5 @@ type gcpDetector interface { GCEInstanceName() (string, error) CloudRunJobExecution() (string, error) CloudRunJobTaskIndex() (string, error) + GCEManagedInstanceGroup() (gcp.ManagedInstanceGroup, error) }