Skip to content

Commit

Permalink
Merge pull request #186 from grafana/185-dasboard-does-not-display-da…
Browse files Browse the repository at this point in the history
…ta-using-setup-function

fix #185
  • Loading branch information
szkiba authored May 22, 2024
2 parents e977608 + 580a7dd commit 37acd38
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
8 changes: 4 additions & 4 deletions dashboard/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ func (ext *extension) flush() {
samples := ext.buffer.GetBufferedSamples()
now := time.Now()

if !ext.noFlush.Load() { // skip the last fraction period for sanpshot (called when flusher stops)
ext.updateAndSend(samples, newMeter(ext.period, now, ext.options.Tags), snapshotEvent, now)
}

ext.updateAndSend(samples, ext.cumulative, cumulativeEvent, now)
ext.evaluateAndSend(ext.cumulative, now)

if !ext.noFlush.Load() { // skip the last fraction period for sanpshot (called when flusher stops)
ext.updateAndSend(samples, ext.cumulative.toSnapshot(ext.period, now), snapshotEvent, now)
}
}

func (ext *extension) updateAndSend(
Expand Down
4 changes: 2 additions & 2 deletions dashboard/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ func TestExtension(t *testing.T) {

assert.True(t, strings.HasPrefix(lines[20], idPrefix))
assert.True(t, strings.HasPrefix(lines[21], aggregateDataPrefix))
assert.Equal(t, "event: snapshot", lines[22])
assert.Equal(t, "event: cumulative", lines[22])
assert.Empty(t, lines[23])

assert.True(t, strings.HasPrefix(lines[24], idPrefix))
assert.True(t, strings.HasPrefix(lines[25], aggregateDataPrefix))
assert.Equal(t, "event: cumulative", lines[26])
assert.Equal(t, "event: snapshot", lines[26])
assert.Empty(t, lines[27])
}

Expand Down
29 changes: 28 additions & 1 deletion dashboard/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newMeter(period time.Duration, now time.Time, tags []string) *meter {
clock, _ := metric.Sink.(*metrics.GaugeSink)

start := now
clock.Value = float64(start.UnixMilli())
clock.Add(metrics.Sample{Time: now, Value: float64(start.UnixMilli())})

return &meter{
registry: registry,
Expand All @@ -41,6 +41,29 @@ func newMeter(period time.Duration, now time.Time, tags []string) *meter {
}
}

func (m *meter) toSnapshot(period time.Duration, now time.Time) *meter {
meter := newMeter(period, now, m.tags)

for _, met := range m.registry.All() {
if meter.registry.Get(met.Name) != nil {
continue
}

clone, _ := meter.registry.getOrNew(
met.Name,
met.Type,
met.Contains,
thresholdsSources(met.Thresholds),
)

for _, sub := range met.Submetrics {
clone.AddSubmetric(sub.Suffix) //nolint:errcheck,gosec
}
}

return meter
}

func (m *meter) update(containers []metrics.SampleContainer, now time.Time) ([]sampleData, error) {
dur := m.period
if dur == 0 {
Expand Down Expand Up @@ -96,6 +119,10 @@ func (m *meter) add(sample metrics.Sample) error {

func (m *meter) format(dur time.Duration) []sampleData {
fmt := func(met *metrics.Metric) sampleData {
if met.Sink.IsEmpty() {
return sampleData{}
}

sample := met.Sink.Format(dur)

if sink, ok := met.Sink.(*metrics.TrendSink); ok {
Expand Down
4 changes: 2 additions & 2 deletions dashboard/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func Test_meter_format(t *testing.T) {
assert.Equal(t, 3, len(data))

assert.Equal(t, []sampleData{
[]float64{0, 0},
[]float64{0, 0},
[]float64{},
[]float64{},
[]float64{float64(now.UnixMilli())},
}, data)
}
Expand Down
6 changes: 3 additions & 3 deletions scripts/demo/demo-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export let options = {
},
};

export function setup() {} // to have "setup" group, which only has metric value at the begining of the test run

export default function () {
let crocodiles, ok;

Expand All @@ -47,9 +49,7 @@ export default function () {

group("get crocodile", () => {
for (var i = 0; i < crocodiles.length; i++) {
let response = http.get(
http.url`https://test-api.k6.io/public/crocodiles/${crocodiles[i].id}`
);
let response = http.get(http.url`https://test-api.k6.io/public/crocodiles/${crocodiles[i].id}`);

check(response, {
"status is OK": (r) => r && r.status == 200,
Expand Down

0 comments on commit 37acd38

Please sign in to comment.