From 43038c184222a97d44189d62dd5f5785e6467e9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Gouteroux?= Date: Tue, 9 Apr 2024 14:30:11 +0200 Subject: [PATCH] enhancement(agentctl): add inspect flag in test-logs cmd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: François Gouteroux --- CHANGELOG.md | 2 ++ cmd/grafana-agent/entrypoint.go | 2 +- cmd/grafana-agentctl/main.go | 4 +++- internal/component/loki/source/file/legacy_file_test.go | 2 +- internal/static/logs/http_test.go | 4 ++-- internal/static/logs/logs.go | 6 +++++- internal/static/logs/logs_test.go | 6 +++--- 7 files changed, 17 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9822e24ee3f6..2081ca42f56d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,8 @@ Main (unreleased) ### Enhancements +- Add `--inspect` flag in ``grafana-agentctl test-logs` cmd. (@fgouteroux) + - Add support for importing folders as single module to `import.file`. (@wildum) - Add support for importing directories as single module to `import.git`. (@wildum) diff --git a/cmd/grafana-agent/entrypoint.go b/cmd/grafana-agent/entrypoint.go index ea7b9a0eb3e2..6f2bc359631a 100644 --- a/cmd/grafana-agent/entrypoint.go +++ b/cmd/grafana-agent/entrypoint.go @@ -79,7 +79,7 @@ func NewEntrypoint(logger *server.Logger, cfg *config.Config, reloader Reloader) return nil, err } - ep.lokiLogs, err = logs.New(reg, cfg.Logs, logger, false) + ep.lokiLogs, err = logs.New(reg, cfg.Logs, logger, false, false) if err != nil { return nil, err } diff --git a/cmd/grafana-agentctl/main.go b/cmd/grafana-agentctl/main.go index 5022e50770e9..319abb2cce11 100644 --- a/cmd/grafana-agentctl/main.go +++ b/cmd/grafana-agentctl/main.go @@ -434,6 +434,7 @@ func filterAgentOwners(refs []meta_v1.OwnerReference) (filtered []meta_v1.OwnerR } func testLogs() *cobra.Command { + var inspect bool cmd := &cobra.Command{ Use: "test-logs [config file]", Short: "Collect logs but print entries instead of sending them to Loki.", @@ -452,7 +453,7 @@ func testLogs() *cobra.Command { } logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)) - l, err := logs.New(prometheus.NewRegistry(), cfg.Logs, logger, true) + l, err := logs.New(prometheus.NewRegistry(), cfg.Logs, logger, true, inspect) if err != nil { fmt.Fprintf(os.Stderr, "failed to start log collection: %s\n", err) os.Exit(1) @@ -466,6 +467,7 @@ func testLogs() *cobra.Command { fmt.Fprintf(os.Stderr, "received shutdown %v signal, stopping...", sig) }, } + cmd.Flags().BoolVarP(&inspect, "inspect", "i", false, "Allows for detailed inspection of pipeline stages.") return cmd } diff --git a/internal/component/loki/source/file/legacy_file_test.go b/internal/component/loki/source/file/legacy_file_test.go index c73d46f432d7..435750e69732 100644 --- a/internal/component/loki/source/file/legacy_file_test.go +++ b/internal/component/loki/source/file/legacy_file_test.go @@ -84,7 +84,7 @@ configs: require.NoError(t, dec.Decode(&cfg)) require.NoError(t, cfg.ApplyDefaults()) logger := logkit.NewNopLogger() - l, err := logs.New(prometheus.NewRegistry(), &cfg, logger, false) + l, err := logs.New(prometheus.NewRegistry(), &cfg, logger, false, false) require.NoError(t, err) // // Write a log line and wait for it to come through. diff --git a/internal/static/logs/http_test.go b/internal/static/logs/http_test.go index e37110f2058e..9830eb25ddd3 100644 --- a/internal/static/logs/http_test.go +++ b/internal/static/logs/http_test.go @@ -27,7 +27,7 @@ configs: var cfg Config logger := util.TestLogger(t) - l, err := New(prometheus.NewRegistry(), &cfg, logger, false) + l, err := New(prometheus.NewRegistry(), &cfg, logger, false, false) require.NoError(t, err) defer l.Stop() @@ -72,7 +72,7 @@ configs: require.NoError(t, dec.Decode(&cfg)) logger := util.TestLogger(t) - l, err := New(prometheus.NewRegistry(), &cfg, logger, false) + l, err := New(prometheus.NewRegistry(), &cfg, logger, false, false) require.NoError(t, err) defer l.Stop() diff --git a/internal/static/logs/logs.go b/internal/static/logs/logs.go index 2d6c478fe510..667abf3f853a 100644 --- a/internal/static/logs/logs.go +++ b/internal/static/logs/logs.go @@ -14,6 +14,7 @@ import ( "github.com/grafana/agent/internal/agentseed" "github.com/grafana/agent/internal/useragent" "github.com/grafana/agent/internal/util" + "github.com/grafana/loki/clients/pkg/logentry/stages" "github.com/grafana/loki/clients/pkg/promtail" "github.com/grafana/loki/clients/pkg/promtail/api" "github.com/grafana/loki/clients/pkg/promtail/client" @@ -40,12 +41,15 @@ type Logs struct { } // New creates and starts Loki log collection. -func New(reg prometheus.Registerer, c *Config, l log.Logger, dryRun bool) (*Logs, error) { +func New(reg prometheus.Registerer, c *Config, l log.Logger, dryRun, inspect bool) (*Logs, error) { logs := &Logs{ instances: make(map[string]*Instance), reg: reg, l: log.With(l, "component", "logs"), } + + stages.Inspect = inspect + if err := logs.ApplyConfig(c, dryRun); err != nil { return nil, err } diff --git a/internal/static/logs/logs_test.go b/internal/static/logs/logs_test.go index 255c99b55f59..4134a2a02f9c 100644 --- a/internal/static/logs/logs_test.go +++ b/internal/static/logs/logs_test.go @@ -23,7 +23,7 @@ import ( ) func TestLogs_NilConfig(t *testing.T) { - l, err := New(prometheus.NewRegistry(), nil, util.TestLogger(t), false) + l, err := New(prometheus.NewRegistry(), nil, util.TestLogger(t), false, false) require.NoError(t, err) require.NoError(t, l.ApplyConfig(nil, false)) @@ -88,7 +88,7 @@ configs: require.NoError(t, dec.Decode(&cfg)) require.NoError(t, cfg.ApplyDefaults()) logger := log.NewSyncLogger(log.NewNopLogger()) - l, err := New(prometheus.NewRegistry(), &cfg, logger, false) + l, err := New(prometheus.NewRegistry(), &cfg, logger, false, false) require.NoError(t, err) defer l.Stop() @@ -195,7 +195,7 @@ configs: require.NoError(t, dec.Decode(&cfg)) require.NoError(t, cfg.ApplyDefaults()) logger := util.TestLogger(t) - l, err := New(prometheus.NewRegistry(), &cfg, logger, false) + l, err := New(prometheus.NewRegistry(), &cfg, logger, false, false) require.NoError(t, err) defer l.Stop()