Repository: prometheus/prometheus
File: storage/remote/otlptranslator/prometheusremotewrite/metrics_to_prw.go (Go)

Code before:

	ts, _ := c.getOrCreateTimeSeries(lbls)
	ts.Samples = append(ts.Samples, *sample)
	return ts
}

func NewPromoteResourceAttributes(otlpCfg config.OTLPConfig) *PromoteResourceAttributes {
	createAttr := func(attributes []string) map[string]struct{} {
		attr := make(map[string]struct{}, len(attributes))
		for _, s := range attributes {
			attr[s] = struct{}{}
		}
		return attr
	}

	if otlpCfg.PromoteAllResourceAttributes {
		return &PromoteResourceAttributes{
			promoteAll: true,
			attrs:      createAttr(otlpCfg.IgnoreResourceAttributes),
		}
	}

	return &PromoteResourceAttributes{
		promoteAll: false,
		attrs:      createAttr(otlpCfg.PromoteResourceAttributes),
	}
}

// promotedAttributes returns labels for promoted resourceAttributes.
func (s *PromoteResourceAttributes) promotedAttributes(resourceAttributes pcommon.Map) []prompb.Label {
	if s == nil {
		return nil
	}
	var promotedAttrs []prompb.Label
	if s.promoteAll {
		promotedAttrs = make([]prompb.Label, 0, resourceAttributes.Len())
		resourceAttributes.Range(func(name string, value pcommon.Value) bool {
			if _, exists := s.attrs[name]; !exists {
				promotedAttrs = append(promotedAttrs, prompb.Label{Name: name, Value: value.AsString()})
			}
			return true
		})
	} else {
		promotedAttrs = make([]prompb.Label, 0, len(s.attrs))
		resourceAttributes.Range(func(name string, value pcommon.Value) bool {
			if _, exists := s.attrs[name]; exists {
				promotedAttrs = append(promotedAttrs, prompb.Label{Name: name, Value: value.AsString()})
			}
			return true
		})
	}

Review comment (refactor):
Let's simplify:
```suggestion
	attrs := otlpCfg.PromoteResourceAttributes
	if otlpCfg.PromoteAllResourceAttributes {
		attrs = otlpCfg.IgnoreResourceAttributes
	}
	attrsMap := make(map[string]struct{}, len(attrs))
	for _, s := range attrs {
		attrsMap[s] = struct{}{}
	}
	return &PromoteResourceAttributes{
		promoteAll: otlpCfg.PromoteAllResourceAttributes,
		attrs:      attrsMap,
	}
```

Code after:

	ts, _ := c.getOrCreateTimeSeries(lbls)
	ts.Samples = append(ts.Samples, *sample)
	return ts
}

func NewPromoteResourceAttributes(otlpCfg config.OTLPConfig) *PromoteResourceAttributes {
	attrs := otlpCfg.PromoteResourceAttributes
	if otlpCfg.PromoteAllResourceAttributes {
		attrs = otlpCfg.IgnoreResourceAttributes
	}
	attrsMap := make(map[string]struct{}, len(attrs))
	for _, s := range attrs {
		attrsMap[s] = struct{}{}
	}
	return &PromoteResourceAttributes{
		promoteAll: otlpCfg.PromoteAllResourceAttributes,
		attrs:      attrsMap,
	}
}

// promotedAttributes returns labels for promoted resourceAttributes.
func (s *PromoteResourceAttributes) promotedAttributes(resourceAttributes pcommon.Map) []prompb.Label {
	if s == nil {
		return nil
	}

	var promotedAttrs []prompb.Label
	if s.promoteAll {
		promotedAttrs = make([]prompb.Label, 0, resourceAttributes.Len())
		resourceAttributes.Range(func(name string, value pcommon.Value) bool {
			if _, exists := s.attrs[name]; !exists {
				promotedAttrs = append(promotedAttrs, prompb.Label{Name: name, Value: value.AsString()})
			}
			return true
		})
	} else {
		promotedAttrs = make([]prompb.Label, 0, len(s.attrs))
		resourceAttributes.Range(func(name string, value pcommon.Value) bool {
			if _, exists := s.attrs[name]; exists {
				promotedAttrs = append(promotedAttrs, prompb.Label{Name: name, Value: value.AsString()})
			}
			return true
		})
	}
	sort.Stable(ByLabelName(promotedAttrs))
	return promotedAttrs
}