mirror of
https://gitee.com/milvus-io/milvus.git
synced 2025-12-30 23:45:28 +08:00
issue: #38399 - add metrics for broadcaster component. - add metrics for wal flusher component. - add metrics for wal interceptors. - add slow log for wal. - add more label for some wal metrics. (local or remote/catcup or tailing...) Signed-off-by: chyezh <chyezh@outlook.com>
76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package metricsutil
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/milvus-io/milvus/pkg/v2/metrics"
|
|
"github.com/milvus-io/milvus/pkg/v2/streaming/util/message"
|
|
"github.com/milvus-io/milvus/pkg/v2/util/paramtable"
|
|
"github.com/milvus-io/milvus/pkg/v2/util/syncutil"
|
|
)
|
|
|
|
const labelExpired = "expired"
|
|
|
|
func NewTxnMetrics(pchannel string) *TxnMetrics {
|
|
constLabel := prometheus.Labels{
|
|
metrics.NodeIDLabelName: paramtable.GetStringNodeID(),
|
|
metrics.WALChannelLabelName: pchannel,
|
|
}
|
|
return &TxnMetrics{
|
|
mu: syncutil.ClosableLock{},
|
|
constLabel: constLabel,
|
|
inflightTxnGauge: metrics.WALInflightTxn.With(constLabel),
|
|
duration: metrics.WALTxnDurationSeconds.MustCurryWith(constLabel),
|
|
}
|
|
}
|
|
|
|
type TxnMetrics struct {
|
|
mu syncutil.ClosableLock
|
|
constLabel prometheus.Labels
|
|
inflightTxnGauge prometheus.Gauge
|
|
duration prometheus.ObserverVec
|
|
}
|
|
|
|
func (m *TxnMetrics) BeginTxn() *TxnMetricsGuard {
|
|
if !m.mu.LockIfNotClosed() {
|
|
return nil
|
|
}
|
|
m.inflightTxnGauge.Inc()
|
|
m.mu.Unlock()
|
|
|
|
return &TxnMetricsGuard{
|
|
inner: m,
|
|
start: time.Now(),
|
|
}
|
|
}
|
|
|
|
type TxnMetricsGuard struct {
|
|
inner *TxnMetrics
|
|
start time.Time
|
|
}
|
|
|
|
func (g *TxnMetricsGuard) Done(state message.TxnState) {
|
|
if g == nil {
|
|
return
|
|
}
|
|
if !g.inner.mu.LockIfNotClosed() {
|
|
return
|
|
}
|
|
g.inner.inflightTxnGauge.Dec()
|
|
|
|
s := labelExpired
|
|
if state == message.TxnStateRollbacked || state == message.TxnStateCommitted {
|
|
s = state.String()
|
|
}
|
|
g.inner.duration.WithLabelValues(s).Observe(time.Since(g.start).Seconds())
|
|
g.inner.mu.Unlock()
|
|
}
|
|
|
|
func (m *TxnMetrics) Close() {
|
|
m.mu.Close()
|
|
metrics.WALInflightTxn.Delete(m.constLabel)
|
|
metrics.WALTxnDurationSeconds.DeletePartialMatch(m.constLabel)
|
|
}
|