Skip to content

Commit ca6c657

Browse files
committed
[+] introduce new type metrics.MetricIntervals
We want to change it to `map[string]int` instead of `map[string]float64` and use the type name everywhere in code base to make the purpose of vars obvious
1 parent 7195aea commit ca6c657

18 files changed

Lines changed: 114 additions & 105 deletions

internal/metrics/postgres_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func TestMetricsToPostgres(t *testing.T) {
196196
}
197197
presetRows := func() *pgxmock.Rows {
198198
return pgxmock.NewRows([]string{"name", "description", "metrics"}).
199-
AddRow("test", "desc", map[string]float64{"metric": 30})
199+
AddRow("test", "desc", metrics.MetricIntervals{"metric": 30})
200200
}
201201

202202
t.Run("GetMetrics", func(t *testing.T) {

internal/metrics/types.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ func (m Metric) GetSQL(version int) string {
6666
return m.SQLs[closestVersion]
6767
}
6868

69+
// MetricIntervals maps metric name to collection interval in seconds
70+
type MetricIntervals map[string]int
71+
72+
// PresetDefs maps preset name to its definition, which includes description and the metrics it contains
6973
type PresetDefs map[string]Preset
7074

75+
// Preset represents a collection of metrics grouped together with a description
7176
type Preset struct {
7277
Description string
73-
Metrics map[string]float64
78+
Metrics MetricIntervals
7479
}
7580

7681
const (

internal/metrics/types_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ func TestFilterByNames(t *testing.T) {
8686
PresetDefs: PresetDefs{
8787
"minimal": Preset{
8888
Description: "Minimal preset",
89-
Metrics: map[string]float64{
89+
Metrics: MetricIntervals{
9090
"cpu_load": 60,
9191
"db_size": 300,
9292
},
9393
},
9494
"standard": Preset{
9595
Description: "Standard preset",
96-
Metrics: map[string]float64{
96+
Metrics: MetricIntervals{
9797
"cpu_load": 60,
9898
"db_size": 300,
9999
"db_stats": 60,
@@ -104,12 +104,12 @@ func TestFilterByNames(t *testing.T) {
104104
}
105105

106106
tests := []struct {
107-
name string
108-
names []string
109-
wantMetrics []string
110-
wantPresets []string
111-
wantErr bool
112-
errContains string
107+
name string
108+
names []string
109+
wantMetrics []string
110+
wantPresets []string
111+
wantErr bool
112+
errContains string
113113
}{
114114
{
115115
name: "empty names returns all",

internal/metrics/yaml_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestWriteMetricsToFile(t *testing.T) {
6161
PresetDefs: metrics.PresetDefs{
6262
"test_preset": metrics.Preset{
6363
Description: "Test preset",
64-
Metrics: map[string]float64{
64+
Metrics: metrics.MetricIntervals{
6565
"test_metric": 1.0,
6666
},
6767
},
@@ -105,7 +105,7 @@ func TestMetricsToFile(t *testing.T) {
105105
PresetDefs: metrics.PresetDefs{
106106
"test_preset": metrics.Preset{
107107
Description: "Test preset",
108-
Metrics: map[string]float64{
108+
Metrics: metrics.MetricIntervals{
109109
"existing_metric": 1.0,
110110
},
111111
},
@@ -164,7 +164,7 @@ func TestPresetsToFile(t *testing.T) {
164164
presetDefs := metrics.PresetDefs{
165165
"existing_preset": metrics.Preset{
166166
Description: "Existing preset",
167-
Metrics: map[string]float64{
167+
Metrics: metrics.MetricIntervals{
168168
"existing_metric": 1.0,
169169
},
170170
},
@@ -186,7 +186,7 @@ func TestPresetsToFile(t *testing.T) {
186186
// Call the function being tested
187187
newPreset := metrics.Preset{
188188
Description: "New preset",
189-
Metrics: map[string]float64{
189+
Metrics: metrics.MetricIntervals{
190190
"new_metric": 1.0,
191191
},
192192
}
@@ -315,7 +315,7 @@ func TestCreateMetricAndPreset(t *testing.T) {
315315
// Create a new preset
316316
testPreset := metrics.Preset{
317317
Description: "Test preset for creation",
318-
Metrics: map[string]float64{"db_stats": 60},
318+
Metrics: metrics.MetricIntervals{"db_stats": 60},
319319
}
320320
err = yamlrw.CreatePreset("test_preset", testPreset)
321321
a.NoError(err)
@@ -346,7 +346,7 @@ func TestMetricsDir(t *testing.T) {
346346
PresetDefs: map[string]metrics.Preset{
347347
"preset1": {
348348
Description: "preset1 description",
349-
Metrics: map[string]float64{
349+
Metrics: metrics.MetricIntervals{
350350
"metric1": 10,
351351
},
352352
},
@@ -363,7 +363,7 @@ func TestMetricsDir(t *testing.T) {
363363
PresetDefs: map[string]metrics.Preset{
364364
"preset2": {
365365
Description: "preset2 description",
366-
Metrics: map[string]float64{
366+
Metrics: metrics.MetricIntervals{
367367
"metric2": 10,
368368
},
369369
},
@@ -460,7 +460,7 @@ func TestConcurrentPresetUpdates(t *testing.T) {
460460
presetName := fmt.Sprintf("preset_%d", id)
461461
testPreset := metrics.Preset{
462462
Description: fmt.Sprintf("Test preset %d", id),
463-
Metrics: map[string]float64{fmt.Sprintf("metric_%d", id): 60},
463+
Metrics: metrics.MetricIntervals{fmt.Sprintf("metric_%d", id): 60},
464464
}
465465
time.Sleep(time.Millisecond * time.Duration(id%3))
466466
err := yamlrw.UpdatePreset(presetName, testPreset)

internal/reaper/logparser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type LogParser struct {
4343
ctx context.Context
4444
LogsMatchRegex *regexp.Regexp
4545
SourceConn *sources.SourceConn
46-
Interval float64
46+
Interval int
4747
StoreCh chan<- metrics.MeasurementEnvelope
4848
eventCounts map[string]int64 // for the specific DB. [WARNING: 34, ERROR: 10, ...], zeroed on storage send
4949
eventCountsTotal map[string]int64 // for the whole instance

internal/reaper/logparser_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestNewLogParser(t *testing.T) {
2828
sourceConn := &sources.SourceConn{
2929
Source: sources.Source{
3030
Name: "test-source",
31-
Metrics: map[string]float64{specialMetricServerLogEventCounts: 60.0},
31+
Metrics: metrics.MetricIntervals{specialMetricServerLogEventCounts: 60.0},
3232
},
3333
Conn: mock,
3434
}
@@ -48,7 +48,7 @@ func TestNewLogParser(t *testing.T) {
4848
assert.Equal(t, tempDir, lp.Directory)
4949
assert.Equal(t, "en", lp.ServerMessagesLang)
5050
assert.Equal(t, false, lp.TruncateOnRotation)
51-
assert.Equal(t, 60.0, lp.Interval)
51+
assert.Equal(t, 60, lp.Interval)
5252
assert.NotNil(t, lp.LogsMatchRegex)
5353
assert.NoError(t, mock.ExpectationsWereMet())
5454
})
@@ -596,7 +596,7 @@ func TestLogParseRemote(t *testing.T) {
596596
sourceConn := &sources.SourceConn{
597597
Source: sources.Source{
598598
Name: "test-source",
599-
Metrics: map[string]float64{specialMetricServerLogEventCounts: 60}, // 60s interval - won't trigger during test
599+
Metrics: metrics.MetricIntervals{specialMetricServerLogEventCounts: 60}, // 60s interval - won't trigger during test
600600
},
601601
Conn: mock,
602602
}
@@ -657,7 +657,7 @@ func TestLogParseRemote(t *testing.T) {
657657
sourceConn := &sources.SourceConn{
658658
Source: sources.Source{
659659
Name: "test-source",
660-
Metrics: map[string]float64{specialMetricServerLogEventCounts: 0.1},
660+
Metrics: metrics.MetricIntervals{specialMetricServerLogEventCounts: 1},
661661
},
662662
Conn: mock,
663663
}
@@ -721,7 +721,7 @@ incomplete line without proper fields
721721
sourceConn := &sources.SourceConn{
722722
Source: sources.Source{
723723
Name: "test-source",
724-
Metrics: map[string]float64{specialMetricServerLogEventCounts: 60}, // Long interval
724+
Metrics: metrics.MetricIntervals{specialMetricServerLogEventCounts: 60}, // Long interval
725725
},
726726
Conn: mock,
727727
}
@@ -790,7 +790,7 @@ incomplete line without proper fields
790790
sourceConn := &sources.SourceConn{
791791
Source: sources.Source{
792792
Name: "test-source",
793-
Metrics: map[string]float64{specialMetricServerLogEventCounts: 0.1},
793+
Metrics: metrics.MetricIntervals{specialMetricServerLogEventCounts: 1},
794794
},
795795
Conn: mock,
796796
}

internal/reaper/metric.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (cmd *ConcurrentMetricDefs) GetPresetDef(name string) (m metrics.Preset, ok
4141
return
4242
}
4343

44-
func (cmd *ConcurrentMetricDefs) GetPresetMetrics(name string) (m map[string]float64) {
44+
func (cmd *ConcurrentMetricDefs) GetPresetMetrics(name string) (m metrics.MetricIntervals) {
4545
cmd.RLock()
4646
defer cmd.RUnlock()
4747
return cmd.PresetDefs[name].Metrics

internal/reaper/metric_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ var (
3535
"metric1": metrics.Metric{Description: "metric1"},
3636
}
3737
initialPresetDefs = metrics.PresetDefs{
38-
"preset1": metrics.Preset{Description: "preset1", Metrics: map[string]float64{"metric1": 1.0}},
38+
"preset1": metrics.Preset{Description: "preset1", Metrics: metrics.MetricIntervals{"metric1": 1.0}},
3939
}
4040

4141
newMetricDefs = metrics.MetricDefs{
4242
"metric2": metrics.Metric{Description: "metric2"},
4343
}
4444
newPresetDefs = metrics.PresetDefs{
45-
"preset2": metrics.Preset{Description: "preset2", Metrics: map[string]float64{"metric2": 2.0}},
45+
"preset2": metrics.Preset{Description: "preset2", Metrics: metrics.MetricIntervals{"metric2": 2.0}},
4646
}
4747
)
4848

@@ -139,7 +139,7 @@ func TestReaper_LoadMetrics(t *testing.T) {
139139
t.Run("updates metricDefs on success", func(t *testing.T) {
140140
defs := &metrics.Metrics{
141141
MetricDefs: metrics.MetricDefs{"m1": {Description: "M1"}},
142-
PresetDefs: metrics.PresetDefs{"p1": {Description: "P1", Metrics: map[string]float64{"m1": 1.0}}},
142+
PresetDefs: metrics.PresetDefs{"p1": {Description: "P1", Metrics: metrics.MetricIntervals{"m1": 1.0}}},
143143
}
144144
r := NewReaper(ctx, &cmdopts.Options{
145145
MetricsReaderWriter: &testutil.MockMetricsReaderWriter{
@@ -197,8 +197,8 @@ func TestReaper_LoadMetrics(t *testing.T) {
197197
"m2": {Description: "M2"},
198198
},
199199
PresetDefs: metrics.PresetDefs{
200-
"preset1": {Metrics: map[string]float64{"m1": 10.0}},
201-
"standby1": {Metrics: map[string]float64{"m2": 20.0}},
200+
"preset1": {Metrics: metrics.MetricIntervals{"m1": 10.0}},
201+
"standby1": {Metrics: metrics.MetricIntervals{"m2": 20.0}},
202202
},
203203
}
204204
r := NewReaper(ctx, &cmdopts.Options{
@@ -216,12 +216,12 @@ func TestReaper_LoadMetrics(t *testing.T) {
216216
assert.NoError(t, r.LoadMetrics())
217217

218218
sc := r.monitoredSources[0]
219-
assert.Equal(t, map[string]float64{"m1": 10.0}, sc.Metrics)
220-
assert.Equal(t, map[string]float64{"m2": 20.0}, sc.MetricsStandby)
219+
assert.Equal(t, metrics.MetricIntervals{"m1": 10.0}, sc.Metrics)
220+
assert.Equal(t, metrics.MetricIntervals{"m2": 20.0}, sc.MetricsStandby)
221221
})
222222

223223
t.Run("skips preset resolution for sources without presets", func(t *testing.T) {
224-
customMetrics := map[string]float64{"cpu": 5.0}
224+
customMetrics := metrics.MetricIntervals{"cpu": 5.0}
225225
defs := &metrics.Metrics{
226226
MetricDefs: metrics.MetricDefs{"cpu": {Description: "CPU"}},
227227
PresetDefs: metrics.PresetDefs{},
@@ -249,7 +249,7 @@ func TestReaper_LoadMetrics(t *testing.T) {
249249
initialDefs := &metrics.Metrics{
250250
MetricDefs: metrics.MetricDefs{"test_metric": {}},
251251
PresetDefs: metrics.PresetDefs{
252-
"test_preset": {Metrics: map[string]float64{"test_metric": 1}},
252+
"test_preset": {Metrics: metrics.MetricIntervals{"test_metric": 1}},
253253
},
254254
}
255255
src := sources.Source{
@@ -272,7 +272,7 @@ func TestReaper_LoadMetrics(t *testing.T) {
272272
})
273273
require.NoError(t, r.LoadSources(ctx))
274274
require.NoError(t, r.LoadMetrics())
275-
assert.Equal(t, map[string]float64{"test_metric": 1}, r.monitoredSources[0].Metrics)
275+
assert.Equal(t, metrics.MetricIntervals{"test_metric": 1}, r.monitoredSources[0].Metrics)
276276

277277
// Attach a mock connection so CloseResourcesForRemovedMonitoredDBs doesn't panic
278278
// when the custom_tags change triggers a full source restart.
@@ -288,14 +288,14 @@ func TestReaper_LoadMetrics(t *testing.T) {
288288
updatedDefs := &metrics.Metrics{
289289
MetricDefs: metrics.MetricDefs{"test_metric": {}},
290290
PresetDefs: metrics.PresetDefs{
291-
"test_preset": {Metrics: map[string]float64{"test_metric": 2}},
291+
"test_preset": {Metrics: metrics.MetricIntervals{"test_metric": 2}},
292292
},
293293
}
294294
getMetricsFn = func() (*metrics.Metrics, error) { return updatedDefs, nil }
295295

296296
require.NoError(t, r.LoadSources(ctx))
297297
require.NoError(t, r.LoadMetrics())
298-
assert.Equal(t, map[string]float64{"test_metric": 2}, r.monitoredSources[0].Metrics,
298+
assert.Equal(t, metrics.MetricIntervals{"test_metric": 2}, r.monitoredSources[0].Metrics,
299299
"preset interval should be updated after source config change triggered a restart")
300300
})
301301
}

internal/reaper/psutil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func goPsutilCalcCPUUtilization(probe0, probe1 cpu.TimesStat) float64 {
4040
}
4141

4242
// GetGoPsutilCPU simulates "psutil" metric output. Assumes the result from last call as input
43-
func GetGoPsutilCPU(interval float64) (metrics.Measurements, error) {
43+
func GetGoPsutilCPU(interval int) (metrics.Measurements, error) {
4444
prevCPULoadTimeStatsLock.RLock()
4545
prevTime := prevCPULoadTimestamp
4646
prevTimeStat := prevCPULoadTimeStats
@@ -50,7 +50,7 @@ func GetGoPsutilCPU(interval float64) (metrics.Measurements, error) {
5050
if err != nil {
5151
return nil, err
5252
}
53-
if time.Since(prevTime) >= time.Duration(float64(time.Second)*interval) {
53+
if time.Since(prevTime) >= time.Duration(int(time.Second)*interval) {
5454
prevCPULoadTimeStatsLock.Lock() // update the cache
5555
prevCPULoadTimeStats = curCallStats[0]
5656
prevCPULoadTimestamp = time.Now()

internal/reaper/reaper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const (
2727
var specialMetrics = map[string]bool{specialMetricChangeEvents: true, specialMetricServerLogEventCounts: true}
2828

2929
var hostLastKnownStatusInRecovery = make(map[string]bool) // isInRecovery
30-
var metricsConfig map[string]float64 // set to host.Metrics or host.MetricsStandby (in case optional config defined and in recovery state
30+
var metricsConfig metrics.MetricIntervals // set to host.Metrics or host.MetricsStandby (in case optional config defined and in recovery state
3131
var metricDefs = NewConcurrentMetricDefs()
3232

3333
// Reaper is the struct that responsible for fetching metrics measurements from the sources and storing them to the sinks
@@ -246,7 +246,7 @@ func (r *Reaper) ShutdownOldWorkers(ctx context.Context, hostsToShutDown map[str
246246
// or state change makes it uninteresting
247247
logger.Debug("checking if any workers need to be shut down...")
248248
for dbMetric, cancelFunc := range r.cancelFuncs {
249-
var currentMetricConfig map[string]float64
249+
var currentMetricConfig metrics.MetricIntervals
250250
var md *sources.SourceConn
251251
var dbRemovedFromConfig bool
252252
var metricRemovedFromPreset bool

0 commit comments

Comments
 (0)