Skip to content

Commit

Permalink
Drop k6's debug output from logs pushed to Loki
Browse files Browse the repository at this point in the history
This is not really helping the user debug anything. If we need to, we
can send it to our own instance, as it's more relevant to us that it's
to the user.

Signed-off-by: Marcelo E. Magallon <[email protected]>
  • Loading branch information
mem committed Oct 5, 2023
1 parent c70a755 commit 8da0088
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
17 changes: 14 additions & 3 deletions internal/k6runner/k6runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,23 @@ func k6LogsToLogger(logs []byte, logger logger.Logger) error {
// labels.
dec := logfmt.NewDecoder(bytes.NewBuffer(logs))

NEXT_RECORD:
for dec.ScanRecord() {
var line []interface{}
var source, level string
for dec.ScanKeyval() {
key := dec.Key()
value := dec.Value()
line = append(line, string(key), string(value))
key := string(dec.Key())
value := string(dec.Value())
switch key {
case "source":
source = value
case "level":
level = value
}
line = append(line, key, value)
}
if level == "debug" && source == "" { // if there's no source, it's probably coming from k6
continue NEXT_RECORD
}
_ = logger.Log(line...)
}
Expand Down
21 changes: 17 additions & 4 deletions internal/prober/multihttp/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"context"
"net/http/httptest"
"path/filepath"
"strings"
"testing"

kitlog "github.com/go-kit/kit/log" //nolint:staticcheck // TODO(mem): replace in BBE
"github.com/go-kit/log/level"
"github.com/grafana/synthetic-monitoring-agent/internal/k6runner"
"github.com/grafana/synthetic-monitoring-agent/internal/testhelper"
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
Expand Down Expand Up @@ -755,23 +757,34 @@ func TestSettingsToScript(t *testing.T) {
require.NotEmpty(t, actual)

check := sm.Check{
Target: settings.Entries[0].Request.Url,
Job: "test",
Settings: sm.CheckSettings{
Multihttp: settings,
},
}

ctx, cancel := testhelper.Context(context.Background(), t)
t.Cleanup(cancel)
logger := zerolog.New(zerolog.NewTestWriter(t))
runner := k6runner.New("k6")
// logger := zerolog.New(zerolog.NewTestWriter(t))
logger := zerolog.Nop()
k6path := filepath.Join(testhelper.ModuleDir(t), "dist", "k6")
runner := k6runner.New(k6path)

prober, err := NewProber(ctx, check, logger, runner)
require.NoError(t, err)

reg := prometheus.NewPedanticRegistry()
require.NotNil(t, reg)

var buf bytes.Buffer
plogger := kitlog.NewLogfmtLogger(&buf)
prober.Probe(ctx, "foo", reg, plogger)
userLogger := level.NewFilter(kitlog.NewLogfmtLogger(&buf), level.AllowInfo(), level.SquelchNoLevel(false))
require.NotNil(t, userLogger)

success := prober.Probe(ctx, check.Target, reg, userLogger)
require.True(t, success)

t.Log(buf.String())
}

func TestReplaceVariablesInString(t *testing.T) {
Expand Down
28 changes: 28 additions & 0 deletions internal/testhelper/testhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package testhelper
import (
"context"
"os"
"path/filepath"
"runtime"
"testing"
"time"

Expand All @@ -27,3 +29,29 @@ func MustReadFile(t *testing.T, filename string) []byte {

return data
}

func ModuleDir(t *testing.T) string {
t.Helper()

_, filename, _, ok := runtime.Caller(1)
if !ok {
// uh?
return ""
}
dir := filepath.Dir(filename)
for dir != "/" {
gomod := filepath.Join(dir, "go.mod")
_, err := os.Stat(gomod)
switch {
case err == nil:
return dir
case os.IsNotExist(err):
dir = filepath.Join(dir, "..")
continue
default:
panic(err)
}
}

return dir
}
5 changes: 4 additions & 1 deletion xk6/sm/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import (
"go.k6.io/k6/output"
)

const ExtensionName = "sm"

func init() {
output.RegisterExtension("sm", New)
output.RegisterExtension(ExtensionName, New)
}

// Output is a k6 output plugin that writes metrics to an io.Writer in
Expand Down Expand Up @@ -118,6 +120,7 @@ func (o *Output) Stop() error {
// might end up with invalid label values. This is probably a job for Loki,
// meaning we need an structured way of storing this information in logs.
fields := logrus.Fields{
"source": ExtensionName,
"metric": metricName,
"scenario": scenario,
"value": value,
Expand Down

0 comments on commit 8da0088

Please sign in to comment.