Skip to content

Commit

Permalink
feat: show error when no log lines were found
Browse files Browse the repository at this point in the history
To achieve this the log output has been wrapped with a new interface
that needs to implement LineCount(). This is also useful in the tests
where we previously counted the lines in a different way.
  • Loading branch information
ctrox committed Feb 7, 2025
1 parent 79ca991 commit cfc8fd4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
24 changes: 20 additions & 4 deletions api/log/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
)

type filteredOutput struct {
out output.LogOutput
labels map[string]struct{}
out output.LogOutput
labels map[string]struct{}
lineCount int
}

func (o *filteredOutput) FormatAndPrintln(ts time.Time, lbls loghttp.LabelSet, maxLabelsLen int, line string) {
Expand All @@ -22,14 +23,29 @@ func (o *filteredOutput) FormatAndPrintln(ts time.Time, lbls loghttp.LabelSet, m
}
}
o.out.FormatAndPrintln(ts, lbls, maxLabelsLen, line)
o.lineCount++
}

func (o filteredOutput) WithWriter(w io.Writer) output.LogOutput {
return o.out.WithWriter(w)
}

func NewStdOut(mode string, noLabels bool, labels ...string) (output.LogOutput, error) {
out, err := output.NewLogOutput(os.Stdout, mode, &output.LogOutputOptions{
func (o filteredOutput) LineCount() int {
return o.lineCount
}

type Output interface {
output.LogOutput
// LineCount returns the amount of lines the output has processed
LineCount() int
}

func NewStdOut(mode string, noLabels bool, labels ...string) (Output, error) {
return NewOutput(os.Stdout, mode, noLabels, labels...)
}

func NewOutput(w io.Writer, mode string, noLabels bool, labels ...string) (Output, error) {
out, err := output.NewLogOutput(w, mode, &output.LogOutputOptions{
NoLabels: noLabels, ColoredOutput: true, Timezone: time.Local,
})
if err != nil {
Expand Down
12 changes: 9 additions & 3 deletions logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/alecthomas/kong"
"github.com/grafana/loki/pkg/logcli/output"
"github.com/grafana/loki/pkg/logproto"
"github.com/ninech/nctl/api"
"github.com/ninech/nctl/api/log"
Expand All @@ -30,7 +29,7 @@ type logsCmd struct {
To time.Time `help:"Ignore since flag and stop looking for logs at this absolute time (RFC3339)" placeholder:"2025-01-01T15:00:00+01:00"`
Output string `help:"Configures the log output format. ${enum}" short:"o" enum:"default,json" default:"default"`
NoLabels bool `help:"disable labels in log output"`
out output.LogOutput
out log.Output
}

var logRetention = time.Duration(time.Hour * 24 * 30)
Expand Down Expand Up @@ -70,7 +69,14 @@ func (cmd *logsCmd) Run(ctx context.Context, client *api.Client, queryString str
return client.Log.TailQuery(ctx, 0, out, query)
}

return client.Log.QueryRange(ctx, out, query)
if err := client.Log.QueryRange(ctx, out, query); err != nil {
return err
}
if out.LineCount() == 0 {
return fmt.Errorf("no logs found between %s and %s", start.Format(time.RFC3339), end.Format(time.RFC3339))
}

return nil
}

type queryOperator string
Expand Down
8 changes: 2 additions & 6 deletions logs/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"testing"
"time"

"github.com/grafana/loki/pkg/logcli/output"
apps "github.com/ninech/apis/apps/v1alpha1"
"github.com/ninech/nctl/api"
"github.com/ninech/nctl/api/log"
"github.com/ninech/nctl/internal/test"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -99,9 +97,7 @@ func TestRun(t *testing.T) {
tc := tc
t.Run(name, func(t *testing.T) {
var buf bytes.Buffer
out, err := output.NewLogOutput(&buf, log.Mode(tc.cmd.Output), &output.LogOutputOptions{
NoLabels: true, ColoredOutput: false, Timezone: time.Local,
})
out, err := log.NewOutput(&buf, log.Mode(tc.cmd.Output), true)
if err != nil {
t.Fatal(err)
}
Expand All @@ -117,7 +113,7 @@ func TestRun(t *testing.T) {
}

if tc.expectedLines != 0 {
assert.Equal(t, test.CountLines(buf.String()), tc.expectedLines)
assert.Equal(t, out.LineCount(), tc.expectedLines)
}

if tc.cmd.Output == "json" {
Expand Down

0 comments on commit cfc8fd4

Please sign in to comment.