Skip to content

Commit

Permalink
Limit the size of the warning from metrics's Add
Browse files Browse the repository at this point in the history
fixes #2217
  • Loading branch information
mstoykov committed Nov 2, 2021
1 parent b8164ee commit f48e588
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion js/modules/k6/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"errors"
"fmt"
"math"
"strconv"
"strings"
"time"

"github.com/dop251/goja"
Expand Down Expand Up @@ -75,6 +77,21 @@ func (mi *ModuleInstance) newMetric(call goja.ConstructorCall, t stats.MetricTyp
return v.ToObject(rt), nil
}

const warnMessageValueMaxSize = 100

func limitValue(v string) string {
if len([]rune(v)) < warnMessageValueMaxSize {
return v
}
vRunes := []rune(v)
difference := int64(len(vRunes) - warnMessageValueMaxSize)
omitMsg := append(strconv.AppendInt([]byte("... omitting "), difference, 10), " characters ..."...)
return strings.Join([]string{
string(vRunes[:warnMessageValueMaxSize/2]),
string(vRunes[len(vRunes)-warnMessageValueMaxSize/2:]),
}, string(omitMsg))
}

func (m Metric) add(v goja.Value, addTags ...map[string]string) (bool, error) {
state := m.core.GetState()
if state == nil {
Expand All @@ -84,7 +101,7 @@ func (m Metric) add(v goja.Value, addTags ...map[string]string) (bool, error) {
// return/throw exception if throw enabled, otherwise just log
raiseNan := func() (bool, error) {
err := fmt.Errorf("'%s' is an invalid value for metric '%s', a number or a boolean value is expected",
v, m.metric.Name)
limitValue(v.String()), m.metric.Name)
if state.Options.Throw.Bool {
return false, err
}
Expand Down

0 comments on commit f48e588

Please sign in to comment.