1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-12-01 23:12:29 +02:00

Use strings.Cut() instead of string.SplitN() (#4049)

strings.Cut() generates less garbage as it does not allocate the slice to hold parts.
This commit is contained in:
Mikhail Mazurskiy
2023-05-18 02:28:44 +10:00
committed by GitHub
parent 8445f21305
commit f95bee22b9
8 changed files with 64 additions and 68 deletions

View File

@@ -82,23 +82,23 @@ func constructOTResources(s string) (*Resource, error) {
return Empty(), nil
}
pairs := strings.Split(s, ",")
attrs := []attribute.KeyValue{}
var attrs []attribute.KeyValue
var invalid []string
for _, p := range pairs {
field := strings.SplitN(p, "=", 2)
if len(field) != 2 {
k, v, found := strings.Cut(p, "=")
if !found {
invalid = append(invalid, p)
continue
}
k := strings.TrimSpace(field[0])
v, err := url.QueryUnescape(strings.TrimSpace(field[1]))
key := strings.TrimSpace(k)
val, err := url.QueryUnescape(strings.TrimSpace(v))
if err != nil {
// Retain original value if decoding fails, otherwise it will be
// an empty string.
v = field[1]
val = v
otel.Handle(err)
}
attrs = append(attrs, attribute.String(k, v))
attrs = append(attrs, attribute.String(key, val))
}
var err error
if len(invalid) > 0 {