From 580a7dd67f73c1aea576c07151ceecc41ca59133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20SZKIBA?= Date: Wed, 22 May 2024 14:12:12 +0200 Subject: [PATCH] fix: An empty array is placed in the place of the missing metric in the snapshot event. Since the metric event and the snapshot event use parallel arrays, the two parallel arrays cannot be allowed to slip due to a missing metric. --- dashboard/extension.go | 8 ++++---- dashboard/extension_test.go | 4 ++-- dashboard/meter.go | 29 ++++++++++++++++++++++++++++- dashboard/meter_test.go | 4 ++-- scripts/demo/demo-rest.js | 6 +++--- 5 files changed, 39 insertions(+), 12 deletions(-) diff --git a/dashboard/extension.go b/dashboard/extension.go index 4e4a5ea..e02d5ff 100644 --- a/dashboard/extension.go +++ b/dashboard/extension.go @@ -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( diff --git a/dashboard/extension_test.go b/dashboard/extension_test.go index 1ef8efb..abcd494 100644 --- a/dashboard/extension_test.go +++ b/dashboard/extension_test.go @@ -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]) } diff --git a/dashboard/meter.go b/dashboard/meter.go index 4ac2972..9ccef4a 100644 --- a/dashboard/meter.go +++ b/dashboard/meter.go @@ -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, @@ -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 { @@ -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 { diff --git a/dashboard/meter_test.go b/dashboard/meter_test.go index 5a0c53e..9002c96 100644 --- a/dashboard/meter_test.go +++ b/dashboard/meter_test.go @@ -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) } diff --git a/scripts/demo/demo-rest.js b/scripts/demo/demo-rest.js index 7819fe0..87a934d 100644 --- a/scripts/demo/demo-rest.js +++ b/scripts/demo/demo-rest.js @@ -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; @@ -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,