mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 09:48:49 +02:00
refactor(metrics): replace hardcoded time with contants, merge three tickers into one
This commit is contained in:
@@ -18,16 +18,22 @@ const (
|
|||||||
MetricsPeriod1h Filter = "1h" // @name MetricsPeriod1h
|
MetricsPeriod1h Filter = "1h" // @name MetricsPeriod1h
|
||||||
MetricsPeriod1d Filter = "1d" // @name MetricsPeriod1d
|
MetricsPeriod1d Filter = "1d" // @name MetricsPeriod1d
|
||||||
MetricsPeriod1mo Filter = "1mo" // @name MetricsPeriod1mo
|
MetricsPeriod1mo Filter = "1mo" // @name MetricsPeriod1mo
|
||||||
|
|
||||||
|
Duration5m = 5 * time.Minute
|
||||||
|
Duration15m = 15 * time.Minute
|
||||||
|
Duration1h = 1 * time.Hour
|
||||||
|
Duration1d = 24 * time.Hour
|
||||||
|
Duration30d = 30 * 24 * time.Hour
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPeriod[T any]() *Period[T] {
|
func NewPeriod[T any]() *Period[T] {
|
||||||
return &Period[T]{
|
return &Period[T]{
|
||||||
Entries: map[Filter]*Entries[T]{
|
Entries: map[Filter]*Entries[T]{
|
||||||
MetricsPeriod5m: newEntries[T](5 * time.Minute),
|
MetricsPeriod5m: newEntries[T](Duration5m),
|
||||||
MetricsPeriod15m: newEntries[T](15 * time.Minute),
|
MetricsPeriod15m: newEntries[T](Duration15m),
|
||||||
MetricsPeriod1h: newEntries[T](1 * time.Hour),
|
MetricsPeriod1h: newEntries[T](Duration1h),
|
||||||
MetricsPeriod1d: newEntries[T](24 * time.Hour),
|
MetricsPeriod1d: newEntries[T](Duration1d),
|
||||||
MetricsPeriod1mo: newEntries[T](30 * 24 * time.Hour),
|
MetricsPeriod1mo: newEntries[T](Duration30d),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,11 +74,11 @@ func (p *Period[T]) ValidateAndFixIntervals() {
|
|||||||
defer p.mu.Unlock()
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
durations := map[Filter]time.Duration{
|
durations := map[Filter]time.Duration{
|
||||||
MetricsPeriod5m: 5 * time.Minute,
|
MetricsPeriod5m: Duration5m,
|
||||||
MetricsPeriod15m: 15 * time.Minute,
|
MetricsPeriod15m: Duration15m,
|
||||||
MetricsPeriod1h: 1 * time.Hour,
|
MetricsPeriod1h: Duration1h,
|
||||||
MetricsPeriod1d: 24 * time.Hour,
|
MetricsPeriod1d: Duration1d,
|
||||||
MetricsPeriod1mo: 30 * 24 * time.Hour,
|
MetricsPeriod1mo: Duration30d,
|
||||||
}
|
}
|
||||||
|
|
||||||
for filter, entries := range p.Entries {
|
for filter, entries := range p.Entries {
|
||||||
|
|||||||
@@ -41,6 +41,9 @@ const (
|
|||||||
gatherErrsInterval = 30 * time.Second
|
gatherErrsInterval = 30 * time.Second
|
||||||
saveInterval = 5 * time.Minute
|
saveInterval = 5 * time.Minute
|
||||||
|
|
||||||
|
gatherErrsTicks = int(gatherErrsInterval / PollInterval) // 30
|
||||||
|
saveTicks = int(saveInterval / PollInterval) // 300
|
||||||
|
|
||||||
saveBaseDir = "data/metrics"
|
saveBaseDir = "data/metrics"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -152,15 +155,12 @@ func (p *Poller[T, AggregateT]) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
pollTicker := time.NewTicker(PollInterval)
|
ticker := time.NewTicker(PollInterval)
|
||||||
gatherErrsTicker := time.NewTicker(gatherErrsInterval)
|
defer ticker.Stop()
|
||||||
saveTicker := time.NewTicker(saveInterval)
|
|
||||||
|
var tickCount int
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
pollTicker.Stop()
|
|
||||||
gatherErrsTicker.Stop()
|
|
||||||
saveTicker.Stop()
|
|
||||||
|
|
||||||
err := p.save()
|
err := p.save()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Err(err).Msg("failed to save metrics data")
|
l.Err(err).Msg("failed to save metrics data")
|
||||||
@@ -176,19 +176,25 @@ func (p *Poller[T, AggregateT]) Start() {
|
|||||||
select {
|
select {
|
||||||
case <-t.Context().Done():
|
case <-t.Context().Done():
|
||||||
return
|
return
|
||||||
case <-pollTicker.C:
|
case <-ticker.C:
|
||||||
p.pollWithTimeout(t.Context())
|
p.pollWithTimeout(t.Context())
|
||||||
case <-saveTicker.C:
|
|
||||||
err := p.save()
|
tickCount++
|
||||||
if err != nil {
|
|
||||||
p.appendErr(err)
|
if tickCount%gatherErrsTicks == 0 {
|
||||||
|
errs, ok := p.gatherErrs()
|
||||||
|
if ok {
|
||||||
|
log.Error().Msg(errs)
|
||||||
|
}
|
||||||
|
p.clearErrs()
|
||||||
}
|
}
|
||||||
case <-gatherErrsTicker.C:
|
|
||||||
errs, ok := p.gatherErrs()
|
if tickCount%saveTicks == 0 {
|
||||||
if ok {
|
err := p.save()
|
||||||
log.Error().Msg(errs)
|
if err != nil {
|
||||||
|
p.appendErr(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
p.clearErrs()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
Reference in New Issue
Block a user