VictoriaMetrics / metrics

Lightweight alternative to github.com/prometheus/client_golang
MIT License
526 stars 59 forks source link

What's the best way to purge a metrics set? #34

Open dmitry-ee opened 2 years ago

dmitry-ee commented 2 years ago

Hi,

I'm trying to implement some clean-up logic on metrics.Set

func generateMetricSet() *metrics.Set {
    ms := metrics.NewSet()
    total_metrics := 1000
    for total_metrics > 0 {
        ms.GetOrCreateCounter(RandStringBytesRmndr(50)).Set(uint64(rand.Int63()))
        total_metrics--
    }
    return ms
}

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func RandStringBytesRmndr(n int) string {
    b := make([]byte, n)
    for i := range b {
        b[i] = letterBytes[rand.Int63() % int64(len(letterBytes))]
    }
    return string(b)
}

func BenchmarkMetricParser_Purge(b *testing.B) {
    for i := 0; i < b.N; i++ {
        ms := generateMetricSet()
        for _,m := range ms.ListMetricNames() {
            ms.UnregisterMetric(m)
        }
    }
}
func BenchmarkMetricParser_Purge2(b *testing.B) {
    for i := 0; i < b.N; i++ {
        ms := generateMetricSet()
        _ = ms
        ms = nil
    }
}
BenchmarkMetricParser_Purge-4                458           5385638 ns/op          366105 B/op       4066 allocs/op
BenchmarkMetricParser_Purge2-4               618           3673018 ns/op          315736 B/op       4054 allocs/op

What's is a best approach to do the periodic cleanup?