Skip to content

Commit

Permalink
Change SetDebugMode by a more flexible ExternalLogger functionality (#…
Browse files Browse the repository at this point in the history
…1069)

* Change SetDebugMode by a more flexible ExternalLogger functionality

* enable tracing as different argument
  • Loading branch information
mariomac authored Jul 30, 2024
1 parent 7e6c73d commit 9d07bfb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
22 changes: 11 additions & 11 deletions pkg/beyla/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,17 @@ func (c *Config) Enabled(feature Feature) bool {
return false
}

// SetDebugMode sets the debug mode for Beyla
func (c *Config) SetDebugMode() {
lvl := slog.LevelVar{}
lvl.Set(slog.LevelDebug)
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: &lvl,
})))
c.TracePrinter = debug.TracePrinterText
c.EBPF.BpfDebug = true
if c.NetworkFlows.Enable {
c.NetworkFlows.Print = true
// ExternalLogger sets the logging capabilities of Beyla.
// Used for integrating Beyla with an external logging system (for example Alloy)
// TODO: maybe this method has too many responsibilities, as it affects the global logger.
func (c *Config) ExternalLogger(handler slog.Handler, tracing bool) {
slog.SetDefault(slog.New(handler))
if tracing {
c.TracePrinter = debug.TracePrinterText
c.EBPF.BpfDebug = true
if c.NetworkFlows.Enable {
c.NetworkFlows.Print = true
}
}
}

Expand Down
60 changes: 60 additions & 0 deletions pkg/beyla/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package beyla
import (
"bytes"
"fmt"
"io"
"log/slog"
"os"
"regexp"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -376,6 +379,63 @@ func TestConfig_NetworkImplicitProm(t *testing.T) {
assert.True(t, cfg.Enabled(FeatureNetO11y)) // Net o11y should be on
}

func TestConfig_ExternalLogger(t *testing.T) {
type testCase struct {
name string
handler func(out io.Writer) slog.Handler
expectedText *regexp.Regexp
expectedCfg Config
tracing bool
networkEnable bool
}
for _, tc := range []testCase{{
name: "default info log",
handler: func(out io.Writer) slog.Handler {
return slog.NewTextHandler(out, &slog.HandlerOptions{Level: slog.LevelInfo})
},
expectedText: regexp.MustCompile(
`^time=\S+ level=INFO msg=information arg=info$`),
}, {
name: "default debug log",
handler: func(out io.Writer) slog.Handler {
return slog.NewTextHandler(out, &slog.HandlerOptions{Level: slog.LevelDebug})
},
expectedText: regexp.MustCompile(
`^time=\S+ level=INFO msg=information arg=info
time=\S+ level=DEBUG msg=debug arg=debug$`),
tracing: true,
expectedCfg: Config{
TracePrinter: debug.TracePrinterText,
EBPF: ebpfcommon.TracerConfig{BpfDebug: true},
},
}, {
name: "debug log with network flows",
handler: func(out io.Writer) slog.Handler {
return slog.NewTextHandler(out, &slog.HandlerOptions{Level: slog.LevelDebug})
},
networkEnable: true,
expectedText: regexp.MustCompile(
`^time=\S+ level=INFO msg=information arg=info
time=\S+ level=DEBUG msg=debug arg=debug$`),
tracing: true,
expectedCfg: Config{
TracePrinter: debug.TracePrinterText,
EBPF: ebpfcommon.TracerConfig{BpfDebug: true},
NetworkFlows: NetworkConfig{Enable: true, Print: true},
},
}} {
t.Run(tc.name, func(t *testing.T) {
cfg := Config{NetworkFlows: NetworkConfig{Enable: tc.networkEnable}}
out := &bytes.Buffer{}
cfg.ExternalLogger(tc.handler(out), tc.tracing)
slog.Info("information", "arg", "info")
slog.Debug("debug", "arg", "debug")
assert.Regexp(t, tc.expectedText, strings.TrimSpace(out.String()))
assert.Equal(t, tc.expectedCfg, cfg)
})
}
}

func loadConfig(t *testing.T, env envMap) *Config {
for k, v := range env {
require.NoError(t, os.Setenv(k, v))
Expand Down

0 comments on commit 9d07bfb

Please sign in to comment.