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 NewResourceAttributesSetting(otlpCfg config.OTLPConfig) ResourceAttributesSetting {
	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 ResourceAttributesSetting{
			Action: PromoteAllExceptIgnoreResourceAttributeAction,
			Attr:   createAttr(otlpCfg.IgnoreResourceAttributes),
		}
	}

	return ResourceAttributesSetting{
		Action: PromoteSpecificResourceAttributeAction,
		Attr:   createAttr(otlpCfg.PromoteResourceAttributes),
	}
}

func (s *ResourceAttributesSetting) shouldPromote(name string) bool {
	switch s.Action {
	case PromoteAllExceptIgnoreResourceAttributeAction:
		_, exist := s.Attr[name]
		return !exist
	case PromoteSpecificResourceAttributeAction:
		_, exist := s.Attr[name]
		return exist
	default:
		return false
	}
}

// Not promote anything.
func (s *ResourceAttributesSetting) reset() {
	s.Action = PromoteSpecificResourceAttributeAction
	s.Attr = make(map[string]struct{})
}


Review comment (suggestion):
This can be made more readable, and by returning a pointer, we can use nil to signal that there's nothing to promote as before this PR:
```suggestion
func NewPromoteResourceAttributes(otlpCfg config.OTLPConfig) *PromoteResourceAttributes {
	if otlpCfg.PromoteAllResourceAttributes {
		attrs := make(map[string]struct{}, len(otlpCfg.IgnoreResourceAttributes))
		for _, s := range otlpCfg.IgnoreResourceAttributes {
			attrs[s] = struct{}{}
		}
		return &PromoteResourceAttributes{
			promoteAll: true,
			attrs:      attrs,
		}
	}

	attrs := make(map[string]struct{}, len(otlpCfg.PromoteResourceAttributes))
	for _, s := range otlpCfg.PromoteResourceAttributes {
		attrs[s] = struct{}{}
	}
	return &PromoteResourceAttributes{
		promoteAll: false,
		attrs:      attrs,
	}
}
```

Code after:
func (c *PrometheusConverter) addSample(sample *prompb.Sample, lbls []prompb.Label) *prompb.TimeSeries {
	if sample == nil || len(lbls) == 0 {
		// This shouldn't happen
		return nil
	}

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