Skip to content

Commit

Permalink
Merge pull request #174 from grafana/feature/skip-report-if-not-enoug…
Browse files Browse the repository at this point in the history
…ht-data

fix: Skipping HTML report file generation if there is not enough data.
  • Loading branch information
szkiba authored Feb 20, 2024
2 parents 5a730f7 + 5603cde commit b53c0df
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
32 changes: 32 additions & 0 deletions dashboard/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ func TestExtension_report(t *testing.T) {
ext.AddMetricSamples(testSampleContainer(t, sample).toArray())
}()

time.Sleep(100 * time.Millisecond)

assert.NoError(t, ext.Stop())

st, err := osFS.Stat(file.Name() + ".gz")
Expand All @@ -282,6 +284,36 @@ func TestExtension_report(t *testing.T) {
assert.NoError(t, osFS.Remove(file.Name()+".gz"))
}

func TestExtension_skip_report(t *testing.T) {
t.Parallel()

osFS := fsext.NewMemMapFs()

file, err := osFS.Create("temp")

assert.NoError(t, err)
assert.NoError(t, file.Close())

var params output.Params

params.Logger = logrus.StandardLogger()
params.ConfigArgument = "period=10ms&port=-1&report=" + file.Name() + ".gz"
params.FS = osFS

ext, err := New(params)

assert.NoError(t, err)
assert.NotNil(t, ext)

assert.NoError(t, ext.Start())

assert.NoError(t, ext.Stop())

_, err = osFS.Stat(file.Name() + ".gz")

assert.Error(t, err)
}

func Test_newParamData(t *testing.T) {
t.Parallel()

Expand Down
15 changes: 15 additions & 0 deletions dashboard/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type reporter struct {
data *reportData
output string
mu sync.RWMutex

snapshotCount int
}

var (
Expand Down Expand Up @@ -51,6 +53,13 @@ func (rep *reporter) onStop(_ error) error {
return nil
}

if rep.snapshotCount < 2 {
rep.proc.logger.Warn(
"The test run was short, report generation was skipped (not enough data)",
)
return nil
}

file, err := rep.proc.fs.Create(rep.output)
if err != nil {
return err
Expand Down Expand Up @@ -105,6 +114,12 @@ func (rep *reporter) onEvent(name string, data interface{}) {
}

rep.proc.logger.Error(err)

return
}

if name == snapshotEvent {
rep.snapshotCount++
}
}

Expand Down

0 comments on commit b53c0df

Please sign in to comment.