Skip to content

Commit

Permalink
fix GAE region detection
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Jun 3, 2022
1 parent 16663a2 commit 738ae4a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 56 deletions.
11 changes: 8 additions & 3 deletions detectors/gcp/app_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ func (d *Detector) AppEngineServiceInstance() (string, error) {
return "", errEnvVarNotFound
}

// AppEngineAvailabilityZoneAndRegion returns the zone and region in which this program is running
func (d *Detector) AppEngineAvailabilityZoneAndRegion() (string, string, error) {
return d.GCEAvailabilityZoneAndRegion()
// AppEngineCloudRegion returns the zone the app engine service is running in.
func (d *Detector) AppEngineAvailabilityZone() (string, error) {
return d.metadata.Zone()
}

// AppEngineCloudRegion returns the region the app engine service is running in.
func (d *Detector) AppEngineCloudRegion() (string, error) {
return d.FaaSCloudRegion()
}
27 changes: 8 additions & 19 deletions detectors/gcp/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

package gcp

import (
"fmt"
"strings"
)

// See the available GCE instance metadata:
// https://cloud.google.com/compute/docs/metadata/default-metadata-values#vm_instance_metadata
const machineTypeMetadataAttr = "instance/machine-type"
Expand All @@ -43,18 +38,12 @@ func (d *Detector) GCEHostName() (string, error) {
return d.metadata.InstanceName()
}

// GCEAvailabilityZoneAndRegion returns the zone and region in which this program is running
func (d *Detector) GCEAvailabilityZoneAndRegion() (string, string, error) {
zone, err := d.metadata.Zone()
if err != nil {
return "", "", err
}
if zone == "" {
return "", "", fmt.Errorf("no zone detected from GCE metadata server")
}
splitZone := strings.SplitN(zone, "-", 3)
if len(splitZone) != 3 {
return "", "", fmt.Errorf("zone was not in the expected format: country-region-zone. Got %v", zone)
}
return zone, strings.Join(splitZone[0:2], "-"), nil
// GCEAvailabilityZone returns the zone and region in which this program is running
func (d *Detector) GCEAvailabilityZone() (string, error) {
return d.metadata.Zone()
}

// GCECloudRegion returns the region in which this program is running
func (d *Detector) GCECloudRegion() (string, error) {
return d.FaaSCloudRegion()
}
28 changes: 12 additions & 16 deletions detectors/gcp/gce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,42 +75,38 @@ func TestGCEHostNameErr(t *testing.T) {
assert.Equal(t, hostName, "")
}

func TestGCEAvaiabilityZoneAndRegion(t *testing.T) {
func TestGCEAvaiabilityZone(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{
FakeZone: "us-central1-c",
}, &FakeOSProvider{})
zone, region, err := d.GCEAvailabilityZoneAndRegion()
zone, err := d.GCEAvailabilityZone()
assert.NoError(t, err)
assert.Equal(t, zone, "us-central1-c")
assert.Equal(t, region, "us-central1")
}

func TestGCEAvaiabilityZoneAndRegionMalformedZone(t *testing.T) {
func TestGCEAvaiabilityZoneErr(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{
FakeZone: "us-central1",
Err: fmt.Errorf("fake error"),
}, &FakeOSProvider{})
zone, region, err := d.GCEAvailabilityZoneAndRegion()
zone, err := d.GCEAvailabilityZone()
assert.Error(t, err)
assert.Equal(t, zone, "")
assert.Equal(t, region, "")
}

func TestGCEAvaiabilityZoneAndRegionNoZone(t *testing.T) {
func TestGCECloudRegion(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{
FakeZone: "",
Attributes: map[string]string{regionMetadataAttr: "/projects/123/regions/us-central1"},
}, &FakeOSProvider{})
zone, region, err := d.GCEAvailabilityZoneAndRegion()
assert.Error(t, err)
assert.Equal(t, zone, "")
assert.Equal(t, region, "")
region, err := d.GCECloudRegion()
assert.NoError(t, err)
assert.Equal(t, region, "us-central1")
}

func TestGCEAvaiabilityZoneAndRegionErr(t *testing.T) {
func TestGCECloudRegionErr(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{
Err: fmt.Errorf("fake error"),
}, &FakeOSProvider{})
zone, region, err := d.GCEAvailabilityZoneAndRegion()
region, err := d.GCECloudRegion()
assert.Error(t, err)
assert.Equal(t, zone, "")
assert.Equal(t, region, "")
}
28 changes: 10 additions & 18 deletions e2e-test-server/endtoendserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,29 +201,21 @@ func (d *testDetector) Detect(ctx context.Context) (*resource.Resource, error) {
})
case gcp.AppEngine:
attributes = append(attributes, semconv.CloudPlatformGCPAppEngine)
zone, region, err := detector.AppEngineAvailabilityZoneAndRegion()
if err != nil {
return nil, err
}
attributes = append(attributes, semconv.CloudAvailabilityZoneKey.String(zone))
attributes = append(attributes, semconv.CloudRegionKey.String(region))
return detectWithFuncs(attributes, map[attribute.Key]detectionFunc{
semconv.FaaSNameKey: detector.AppEngineServiceName,
semconv.FaaSVersionKey: detector.AppEngineServiceVersion,
semconv.FaaSIDKey: detector.AppEngineServiceInstance,
semconv.FaaSNameKey: detector.AppEngineServiceName,
semconv.FaaSVersionKey: detector.AppEngineServiceVersion,
semconv.FaaSIDKey: detector.AppEngineServiceInstance,
semconv.CloudRegionKey: detector.AppEngineCloudRegion,
semconv.CloudAvailabilityZoneKey: detector.AppEngineAvailabilityZone,
})
case gcp.GCE:
attributes = append(attributes, semconv.CloudPlatformGCPComputeEngine)
zone, region, err := detector.GCEAvailabilityZoneAndRegion()
if err != nil {
return nil, err
}
attributes = append(attributes, semconv.CloudAvailabilityZoneKey.String(zone))
attributes = append(attributes, semconv.CloudRegionKey.String(region))
return detectWithFuncs(attributes, map[attribute.Key]detectionFunc{
semconv.HostTypeKey: detector.GCEHostType,
semconv.HostIDKey: detector.GCEHostID,
semconv.HostNameKey: detector.GCEHostName,
semconv.HostTypeKey: detector.GCEHostType,
semconv.HostIDKey: detector.GCEHostID,
semconv.HostNameKey: detector.GCEHostName,
semconv.CloudRegionKey: detector.GCEAvailabilityZone,
semconv.CloudAvailabilityZoneKey: detector.GCECloudRegion,
})
default:
return resource.NewWithAttributes(semconv.SchemaURL, attributes...), nil
Expand Down

0 comments on commit 738ae4a

Please sign in to comment.