Skip to content

Commit ec227b1

Browse files
Merge pull request #16 from istreamlabs/optimize-tag-building
refactor(perf): improve tag serialization
2 parents 19917a8 + 0acad4e commit ec227b1

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

metrics/util.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func cloneTagsWithMap(original []string, newTags map[string]string) []string {
3232

3333
i := len(original)
3434
for k, v := range newTags {
35-
combined[i] = fmt.Sprintf("%s:%s", k, v)
35+
combined[i] = buildTag(k, v)
3636
i++
3737
}
3838

@@ -44,12 +44,21 @@ func mapToStrings(tagMap map[string]string) []string {
4444
tags := make([]string, 0, len(tagMap))
4545

4646
for k, v := range tagMap {
47-
tags = append(tags, fmt.Sprintf("%s:%s", k, v))
47+
tags = append(tags, buildTag(k, v))
4848
}
4949

5050
return tags
5151
}
5252

53+
func buildTag(k, v string) string {
54+
var b strings.Builder
55+
b.Grow(len(k) + len(v) + 1)
56+
b.WriteString(k)
57+
b.WriteByte(':')
58+
b.WriteString(v)
59+
return b.String()
60+
}
61+
5362
// convertType converts a value into an specific type if possible, otherwise
5463
// panics. The returned interface is guaranteed to cast properly.
5564
func convertType(value interface{}, toType reflect.Type) interface{} {

0 commit comments

Comments
 (0)