fix: record and delete bucket metrics after inactive (#6349)

This commit is contained in:
zouyixiong 2024-12-13 12:34:02 +08:00 committed by GitHub
parent b0210df081
commit 9987a65e8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 10 deletions

View File

@ -1,12 +1,13 @@
package s3api package s3api
import ( import (
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
stats_collect "github.com/seaweedfs/seaweedfs/weed/stats"
"github.com/seaweedfs/seaweedfs/weed/util"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
stats_collect "github.com/seaweedfs/seaweedfs/weed/stats"
"github.com/seaweedfs/seaweedfs/weed/util"
) )
func track(f http.HandlerFunc, action string) http.HandlerFunc { func track(f http.HandlerFunc, action string) http.HandlerFunc {
@ -25,10 +26,12 @@ func track(f http.HandlerFunc, action string) http.HandlerFunc {
} }
stats_collect.S3RequestHistogram.WithLabelValues(action, bucket).Observe(time.Since(start).Seconds()) stats_collect.S3RequestHistogram.WithLabelValues(action, bucket).Observe(time.Since(start).Seconds())
stats_collect.S3RequestCounter.WithLabelValues(action, strconv.Itoa(recorder.Status), bucket).Inc() stats_collect.S3RequestCounter.WithLabelValues(action, strconv.Itoa(recorder.Status), bucket).Inc()
stats_collect.RecordBucketActiveTime(bucket)
} }
} }
func TimeToFirstByte(action string, start time.Time, r *http.Request) { func TimeToFirstByte(action string, start time.Time, r *http.Request) {
bucket, _ := s3_constants.GetBucketAndObject(r) bucket, _ := s3_constants.GetBucketAndObject(r)
stats_collect.S3TimeToFirstByteHistogram.WithLabelValues(action, bucket).Observe(float64(time.Since(start).Milliseconds())) stats_collect.S3TimeToFirstByteHistogram.WithLabelValues(action, bucket).Observe(float64(time.Since(start).Milliseconds()))
stats_collect.RecordBucketActiveTime(bucket)
} }

View File

@ -23,9 +23,11 @@ const (
NoWriteOrDelete = "noWriteOrDelete" NoWriteOrDelete = "noWriteOrDelete"
NoWriteCanDelete = "noWriteCanDelete" NoWriteCanDelete = "noWriteCanDelete"
IsDiskSpaceLow = "isDiskSpaceLow" IsDiskSpaceLow = "isDiskSpaceLow"
bucketAtiveTTL = 10 * time.Minute
) )
var readOnlyVolumeTypes = [4]string{IsReadOnly, NoWriteOrDelete, NoWriteCanDelete, IsDiskSpaceLow} var readOnlyVolumeTypes = [4]string{IsReadOnly, NoWriteOrDelete, NoWriteCanDelete, IsDiskSpaceLow}
var bucketLastActiveTsNs map[string]int64 = map[string]int64{}
var ( var (
Gather = prometheus.NewRegistry() Gather = prometheus.NewRegistry()
@ -281,6 +283,7 @@ var (
Name: "request_total", Name: "request_total",
Help: "Counter of s3 requests.", Help: "Counter of s3 requests.",
}, []string{"type", "code", "bucket"}) }, []string{"type", "code", "bucket"})
S3HandlerCounter = prometheus.NewCounterVec( S3HandlerCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Namespace: Namespace, Namespace: Namespace,
@ -288,6 +291,7 @@ var (
Name: "handler_total", Name: "handler_total",
Help: "Counter of s3 server handlers.", Help: "Counter of s3 server handlers.",
}, []string{"type"}) }, []string{"type"})
S3RequestHistogram = prometheus.NewHistogramVec( S3RequestHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{ prometheus.HistogramOpts{
Namespace: Namespace, Namespace: Namespace,
@ -296,6 +300,7 @@ var (
Help: "Bucketed histogram of s3 request processing time.", Help: "Bucketed histogram of s3 request processing time.",
Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24), Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
}, []string{"type", "bucket"}) }, []string{"type", "bucket"})
S3TimeToFirstByteHistogram = prometheus.NewHistogramVec( S3TimeToFirstByteHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{ prometheus.HistogramOpts{
Namespace: Namespace, Namespace: Namespace,
@ -354,6 +359,8 @@ func init() {
Gather.MustRegister(S3RequestHistogram) Gather.MustRegister(S3RequestHistogram)
Gather.MustRegister(S3InFlightRequestsGauge) Gather.MustRegister(S3InFlightRequestsGauge)
Gather.MustRegister(S3TimeToFirstByteHistogram) Gather.MustRegister(S3TimeToFirstByteHistogram)
go bucketMetricTTLControl()
} }
func LoopPushingMetric(name, instance, addr string, intervalSeconds int) { func LoopPushingMetric(name, instance, addr string, intervalSeconds int) {
@ -401,11 +408,40 @@ func SourceName(port uint32) string {
return net.JoinHostPort(hostname, strconv.Itoa(int(port))) return net.JoinHostPort(hostname, strconv.Itoa(int(port)))
} }
// todo - can be changed to DeletePartialMatch when https://github.com/prometheus/client_golang/pull/1013 gets released func RecordBucketActiveTime(bucket string) {
func DeleteCollectionMetrics(collection string) { bucketLastActiveTsNs[bucket] = time.Now().UnixNano()
VolumeServerDiskSizeGauge.DeleteLabelValues(collection, "normal") }
for _, volume_type := range readOnlyVolumeTypes {
VolumeServerReadOnlyVolumeGauge.DeleteLabelValues(collection, volume_type) func DeleteCollectionMetrics(collection string) {
} labels := prometheus.Labels{"collection": collection}
VolumeServerVolumeGauge.DeleteLabelValues(collection, "volume") c := MasterReplicaPlacementMismatch.DeletePartialMatch(labels)
c += MasterVolumeLayoutWritable.DeletePartialMatch(labels)
c += MasterVolumeLayoutCrowded.DeletePartialMatch(labels)
c += VolumeServerDiskSizeGauge.DeletePartialMatch(labels)
c += VolumeServerVolumeGauge.DeletePartialMatch(labels)
c += VolumeServerReadOnlyVolumeGauge.DeletePartialMatch(labels)
glog.V(0).Infof("delete collection metrics, %s: %d", collection, c)
}
func bucketMetricTTLControl() {
ttlNs := bucketAtiveTTL.Nanoseconds()
for {
now := time.Now().UnixNano()
for bucket, ts := range bucketLastActiveTsNs {
if (now - ts) > ttlNs {
delete(bucketLastActiveTsNs, bucket)
labels := prometheus.Labels{"bucket": bucket}
c := S3RequestCounter.DeletePartialMatch(labels)
c += S3RequestHistogram.DeletePartialMatch(labels)
c += S3TimeToFirstByteHistogram.DeletePartialMatch(labels)
glog.V(0).Infof("delete inactive bucket metrics, %s: %d", bucket, c)
}
}
time.Sleep(bucketAtiveTTL)
}
} }