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

[chore] resourcedetectionprocessor: Remove usage of insert in gcp detector, fix error orders #13912

Merged
merged 1 commit into from
Sep 6, 2022
Merged
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
45 changes: 25 additions & 20 deletions processor/resourcedetectionprocessor/internal/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,44 +119,49 @@ type resourceBuilder struct {
}

func (r *resourceBuilder) add(key string, detect func() (string, error)) {
if v, err := detect(); err == nil {
r.attrs.InsertString(key, v)
} else {
v, err := detect()
if err != nil {
r.errs = append(r.errs, err)
return
}
r.attrs.UpsertString(key, v)
}

// addFallible adds a detect function whose failures should be ignored
func (r *resourceBuilder) addFallible(key string, detect func() (string, error)) {
if v, err := detect(); err == nil {
r.attrs.InsertString(key, v)
} else {
v, err := detect()
if err != nil {
r.logger.Info("Fallible detector failed. This attribute will not be available.", zap.String("key", key), zap.Error(err))
return
}
r.attrs.UpsertString(key, v)
}

// zoneAndRegion functions are expected to return zone, region, err.
func (r *resourceBuilder) addZoneAndRegion(detect func() (string, string, error)) {
if zone, region, err := detect(); err == nil {
r.attrs.InsertString(conventions.AttributeCloudAvailabilityZone, zone)
r.attrs.InsertString(conventions.AttributeCloudRegion, region)
} else {
zone, region, err := detect()
if err != nil {
r.errs = append(r.errs, err)
return
}
r.attrs.UpsertString(conventions.AttributeCloudAvailabilityZone, zone)
r.attrs.UpsertString(conventions.AttributeCloudRegion, region)
}

func (r *resourceBuilder) addZoneOrRegion(detect func() (string, gcp.LocationType, error)) {
if v, locType, err := detect(); err == nil {
switch locType {
case gcp.Zone:
r.attrs.InsertString(conventions.AttributeCloudAvailabilityZone, v)
case gcp.Region:
r.attrs.InsertString(conventions.AttributeCloudRegion, v)
default:
r.errs = append(r.errs, fmt.Errorf("location must be zone or region. Got %v", locType))
}
} else {
v, locType, err := detect()
if err != nil {
r.errs = append(r.errs, err)
return
}

switch locType {
case gcp.Zone:
r.attrs.UpsertString(conventions.AttributeCloudAvailabilityZone, v)
case gcp.Region:
r.attrs.UpsertString(conventions.AttributeCloudRegion, v)
default:
r.errs = append(r.errs, fmt.Errorf("location must be zone or region. Got %v", locType))
}
}

Expand Down