Skip to content

Commit

Permalink
test: add test cases covering builder options
Browse files Browse the repository at this point in the history
  • Loading branch information
wazazaby committed Sep 16, 2024
1 parent 2c17d6f commit 973ad08
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
10 changes: 6 additions & 4 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func WithLabelNameMaxLen(maxLen int) BuilderOption {
// - [Builder.LabelStringQuote]
// - [Builder.LabelError]
// - [Builder.LabelErrorQuote]
// - [Builder.LabelNamedError]
// - [Builder.LabelNamedErrorQuote]
//
// Zero means no length limit.
//
Expand Down Expand Up @@ -139,7 +141,7 @@ func (b *Builder) labelString(name, value string, escapeQuotes bool) *Builder {
if !b.isValidLabelName(name) {
return b
}
if !b.isValidLabelValue(value) {
if !b.isValidLabelValue(name, value) {
return b
}
b.buf = b.appendCommaOrLeftBracket()
Expand Down Expand Up @@ -429,14 +431,14 @@ func (b *Builder) isValidLabelName(name string) bool {
// label value len must also be less than the provided max len value.
//
// In case of an invalid label value, a log line containing the reasons will be output to [os.Stderr].
func (b *Builder) isValidLabelValue(value string) bool {
func (b *Builder) isValidLabelValue(name, value string) bool {
lv := len(value)
if lv == 0 {
log.Printf("vimebu: metric %q, label name: %q, received empty label value - skipping", b.buf, value)
log.Printf("vimebu: metric %q, label name: %q, received empty label value - skipping", b.buf, name)
return false
}
if b.labelValueMaxLen > 0 && lv > b.labelValueMaxLen {
log.Printf("vimebu: metric %q, label name %q, label value %q len exceeds set limit of %d - skipping", b.buf, value, value, b.labelNameMaxLen)
log.Printf("vimebu: metric %q, label name %q, label value %q len exceeds set limit of %d - skipping", b.buf, name, value, b.labelNameMaxLen)
return false
}
return true
Expand Down
50 changes: 49 additions & 1 deletion builder_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package vimebu

import (
"bytes"
"fmt"
"log"
"os"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -308,6 +312,50 @@ func TestBuilderParallel(t *testing.T) {
require.NoError(t, eg.Wait())
}

func captureLogOutput(f func()) []string {
var buf bytes.Buffer
log.SetOutput(&buf)
f()
log.SetOutput(os.Stderr)
return strings.FieldsFunc(buf.String(), func(c rune) bool {
return c == '\n' || c == '\r'
})
}

func TestBuilderOptionsWithLabelNameMaxLen(t *testing.T) {
logLines := captureLogOutput(func() {
metric := Metric("test_options", WithLabelNameMaxLen(3)).
LabelString("one", "one").
LabelString("two", "two").
LabelString("three", "three"). // Will be skipped.
LabelStringQuote("four", `fo"ur`). // Will be skipped.
LabelError(fmt.Errorf("something went wrong")). // Will be skipped.
LabelErrorQuote(fmt.Errorf(`"something" went wrong`)). // Will be skipped.
LabelNamedError("err", fmt.Errorf("mayday")).
String()
require.Equal(t, `test_options{one="one",two="two",err="mayday"}`, metric)
})

require.Len(t, logLines, 4) // One log line for each label name exceeding the limit of 3 bytes, thus getting skipped.
}

func TestBuilderOptionsWithLabelValueMaxLen(t *testing.T) {
logLines := captureLogOutput(func() {
metric := Metric("test_options", WithLabelValueMaxLen(5)).
LabelString("one", "one").
LabelString("two", "two").
LabelString("three", "three").
LabelStringQuote("four", `fo"ur`).
LabelError(fmt.Errorf("something went wrong")). // Will be skipped.
LabelErrorQuote(fmt.Errorf(`"something" went wrong`)). // Will be skipped.
LabelNamedError("err", fmt.Errorf("mayday")). // Will be skipped.
String()
require.Equal(t, `test_options{one="one",two="two",three="three",four="fo\"ur"}`, metric)
})

require.Len(t, logLines, 3) // One log line for each label value exceeding the limit of 5 bytes, thus getting skipped.
}

func BenchmarkBuilderTestCasesParallel(b *testing.B) {
b.ReportAllocs()

Expand Down Expand Up @@ -341,7 +389,7 @@ func BenchmarkBuilderTestCasesSequential(b *testing.B) {
}

func doBenchmarkCase(in input) {
builder := Metric(in.name)
builder := Metric(in.name, WithLabelNameMaxLen(100), WithLabelValueMaxLen(100))
for _, label := range in.labels {
addLabelAnyToBuilder(builder, label)
}
Expand Down

0 comments on commit 973ad08

Please sign in to comment.