Skip to content

Commit 393b952

Browse files
authored
[*] SourceConn.GetMetricInterval() now returns time.Duration (#1315)
1 parent 4a9aa4d commit 393b952

File tree

7 files changed

+18
-18
lines changed

7 files changed

+18
-18
lines changed

internal/reaper/logparser.go

Lines changed: 2 additions & 2 deletions
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 int
46+
Interval time.Duration
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
@@ -97,7 +97,7 @@ func NewLogParser(ctx context.Context, mdb *sources.SourceConn, storeCh chan<- m
9797
}
9898

9999
func (lp *LogParser) HasSendIntervalElapsed() bool {
100-
return lp.lastSendTime.IsZero() || lp.lastSendTime.Before(time.Now().Add(-time.Second*time.Duration(lp.Interval)))
100+
return lp.lastSendTime.IsZero() || lp.lastSendTime.Before(time.Now().Add(-lp.Interval))
101101
}
102102

103103
func (lp *LogParser) ParseLogs() error {

internal/reaper/logparser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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, lp.Interval)
51+
assert.Equal(t, 60*time.Second, lp.Interval)
5252
assert.NotNil(t, lp.LogsMatchRegex)
5353
assert.NoError(t, mock.ExpectationsWereMet())
5454
})

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 int) (metrics.Measurements, error) {
43+
func GetGoPsutilCPU(interval time.Duration) (metrics.Measurements, error) {
4444
prevCPULoadTimeStatsLock.RLock()
4545
prevTime := prevCPULoadTimestamp
4646
prevTimeStat := prevCPULoadTimeStats
@@ -50,7 +50,7 @@ func GetGoPsutilCPU(interval int) (metrics.Measurements, error) {
5050
if err != nil {
5151
return nil, err
5252
}
53-
if time.Since(prevTime) >= time.Duration(int(time.Second)*interval) {
53+
if time.Since(prevTime) >= interval {
5454
prevCPULoadTimeStatsLock.Lock() // update the cache
5555
prevCPULoadTimeStats = curCallStats[0]
5656
prevCPULoadTimestamp = time.Now()

internal/reaper/psutil_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestGetGoPsutilCPU(t *testing.T) {
1818
time.Sleep(100 * time.Millisecond)
1919

2020
// Call the GetGoPsutilCPU function with a 1-second interval
21-
result, err := GetGoPsutilCPU(1.0)
21+
result, err := GetGoPsutilCPU(time.Second)
2222
a.NoError(err)
2323
a.NotEmpty(result)
2424

internal/reaper/reaper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ func (r *Reaper) reapMetricMeasurements(ctx context.Context, md *sources.SourceC
344344
metricStoreMessages, err = r.FetchMetric(ctx, md, metricName)
345345
}
346346

347-
if time.Since(t1) > (time.Second * time.Duration(interval)) {
348-
l.Warningf("Total fetching time of %v bigger than %vs interval", time.Since(t1), interval)
347+
if time.Since(t1) > interval {
348+
l.Warningf("Total fetching time of %v bigger than %v interval", time.Since(t1), interval)
349349
}
350350

351351
if err != nil {
@@ -385,7 +385,7 @@ func (r *Reaper) reapMetricMeasurements(ctx context.Context, md *sources.SourceC
385385
select {
386386
case <-ctx.Done():
387387
return
388-
case <-time.After(time.Second * time.Duration(interval)):
388+
case <-time.After(interval):
389389
// continue
390390
}
391391
}
@@ -482,7 +482,7 @@ func (r *Reaper) FetchMetric(ctx context.Context, md *sources.SourceConn, metric
482482
}
483483
l := log.GetLogger(ctx)
484484

485-
if metric.IsInstanceLevel && r.Metrics.InstanceLevelCacheMaxSeconds > 0 && time.Second*time.Duration(md.GetMetricInterval(metricName)) < r.Metrics.CacheAge() {
485+
if metric.IsInstanceLevel && r.Metrics.InstanceLevelCacheMaxSeconds > 0 && md.GetMetricInterval(metricName) < r.Metrics.CacheAge() {
486486
cacheKey = fmt.Sprintf("%s:%s", md.GetClusterIdentifier(), metricName)
487487
}
488488
data = r.measurementCache.Get(cacheKey, r.Metrics.CacheAge())

internal/sources/conn.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ func (md *SourceConn) GetDatabaseName() string {
128128
}
129129

130130
// GetMetricInterval returns the metric interval for the connection
131-
func (md *SourceConn) GetMetricInterval(name string) int {
131+
func (md *SourceConn) GetMetricInterval(name string) time.Duration {
132132
md.RLock()
133133
defer md.RUnlock()
134134
if md.IsInRecovery && len(md.MetricsStandby) > 0 {
135-
return md.MetricsStandby[name]
135+
return time.Duration(md.MetricsStandby[name]) * time.Second
136136
}
137-
return md.Metrics[name]
137+
return time.Duration(md.Metrics[name]) * time.Second
138138
}
139139

140140
// IsClientOnSameHost checks if the pgwatch client is running on the same host as the PostgreSQL server

internal/sources/conn_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,20 @@ func TestSourceConn_GetMetricInterval(t *testing.T) {
181181

182182
t.Run("primary uses Metrics", func(t *testing.T) {
183183
md.IsInRecovery = false
184-
assert.Equal(t, 15, md.GetMetricInterval("foo"))
185-
assert.Equal(t, 25, md.GetMetricInterval("bar"))
184+
assert.Equal(t, 15*time.Second, md.GetMetricInterval("foo"))
185+
assert.Equal(t, 25*time.Second, md.GetMetricInterval("bar"))
186186
})
187187

188188
t.Run("standby uses MetricsStandby if present", func(t *testing.T) {
189189
md.IsInRecovery = true
190-
assert.Equal(t, 35, md.GetMetricInterval("foo"))
191-
assert.Equal(t, 0, md.GetMetricInterval("bar"))
190+
assert.Equal(t, 35*time.Second, md.GetMetricInterval("foo"))
191+
assert.Equal(t, time.Duration(0), md.GetMetricInterval("bar"))
192192
})
193193

194194
t.Run("standby with empty MetricsStandby falls back to Metrics", func(t *testing.T) {
195195
md.IsInRecovery = true
196196
md.MetricsStandby = metrics.MetricIntervals{}
197-
assert.Equal(t, 15, md.GetMetricInterval("foo"))
197+
assert.Equal(t, 15*time.Second, md.GetMetricInterval("foo"))
198198
})
199199
}
200200

0 commit comments

Comments
 (0)