Skip to content

Commit

Permalink
enhancement(agentctl): add inspect flag in test-logs cmd
Browse files Browse the repository at this point in the history
Signed-off-by: François Gouteroux <[email protected]>
  • Loading branch information
fgouteroux committed Apr 9, 2024
1 parent 70f5f04 commit 43038c1
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/grafana-agent/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/grafana-agentctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/component/loki/source/file/legacy_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/static/logs/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down
6 changes: 5 additions & 1 deletion internal/static/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions internal/static/logs/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 43038c1

Please sign in to comment.