Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit the size of the warning from metrics' Add() methods #2219

Merged
merged 1 commit into from
Nov 3, 2021
Merged
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
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 {
vRunes := []rune(v)
if len(vRunes) < warnMessageValueMaxSize {
return 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