Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions pkg/metric/family.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package metric

import (
"strings"
"bytes"
)

// FamilyInterface interface for a family
Expand All @@ -40,11 +40,11 @@ func (f Family) Inspect(inspect func(Family)) {

// ByteSlice returns the given Family in its string representation.
func (f Family) ByteSlice() []byte {
b := strings.Builder{}
b := bytes.Buffer{}
for _, m := range f.Metrics {
b.WriteString(f.Name)
m.Write(&b)
}

return []byte(b.String())
return b.Bytes()
}
9 changes: 5 additions & 4 deletions pkg/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package metric

import (
"bytes"
"fmt"
"math"
"strconv"
Expand Down Expand Up @@ -64,7 +65,7 @@ type Metric struct {
Value float64
}

func (m *Metric) Write(s *strings.Builder) {
func (m *Metric) Write(s *bytes.Buffer) {
if len(m.LabelKeys) != len(m.LabelValues) {
panic(fmt.Sprintf(
"expected labelKeys %q to be of same length as labelValues %q",
Expand All @@ -78,7 +79,7 @@ func (m *Metric) Write(s *strings.Builder) {
s.WriteByte('\n')
}

func labelsToString(m *strings.Builder, keys, values []string) {
func labelsToString(m *bytes.Buffer, keys, values []string) {
if len(keys) > 0 {
var separator byte = '{'

Expand All @@ -102,15 +103,15 @@ var (
// escapeString replaces '\' by '\\', new line character by '\n', and '"' by
// '\"'.
// Taken from github.com/prometheus/common/expfmt/text_create.go.
func escapeString(m *strings.Builder, v string) {
func escapeString(m *bytes.Buffer, v string) {
escapeWithDoubleQuote.WriteString(m, v)
}

// writeFloat is equivalent to fmt.Fprint with a float64 argument but hardcodes
// a few common cases for increased efficiency. For non-hardcoded cases, it uses
// strconv.AppendFloat to avoid allocations, similar to writeInt.
// Taken from github.com/prometheus/common/expfmt/text_create.go.
func writeFloat(w *strings.Builder, f float64) {
func writeFloat(w *bytes.Buffer, f float64) {
switch {
case f == 1:
w.WriteByte('1')
Expand Down
5 changes: 3 additions & 2 deletions pkg/metric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package metric

import (
"bytes"
"strings"
"testing"
)
Expand Down Expand Up @@ -70,11 +71,11 @@ func BenchmarkMetricWrite(b *testing.B) {
for _, test := range tests {
b.Run(test.testName, func(b *testing.B) {
for i := 0; i < b.N; i++ {
builder := strings.Builder{}
builder := bytes.Buffer{}

test.metric.Write(&builder)

s := builder.String()
s := builder.Bytes()

// Ensuring that the string is actually build, not optimized
// away by compilation.
Expand Down