2019-06-07 18:16:05 +00:00
|
|
|
package tenantmetrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2024-10-01 14:37:18 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
|
2019-06-07 18:16:05 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
2021-02-16 21:25:27 +00:00
|
|
|
// TenantID defines metric tenant.
|
|
|
|
type TenantID struct {
|
|
|
|
AccountID uint32
|
|
|
|
ProjectID uint32
|
2019-06-07 18:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CounterMap is a map of counters keyed by tenant.
|
|
|
|
type CounterMap struct {
|
|
|
|
metric string
|
2023-07-21 03:55:27 +00:00
|
|
|
|
|
|
|
// do not use atomic.Pointer, since the stored map there is already a pointer type.
|
|
|
|
m atomic.Value
|
2024-10-01 14:37:18 +00:00
|
|
|
// mt holds value for multi-tenant metrics.
|
|
|
|
mt atomic.Value
|
2019-06-07 18:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 21:25:27 +00:00
|
|
|
// NewCounterMap creates new CounterMap for the given metric.
|
2019-06-07 18:16:05 +00:00
|
|
|
func NewCounterMap(metric string) *CounterMap {
|
|
|
|
cm := &CounterMap{
|
|
|
|
metric: metric,
|
|
|
|
}
|
2021-02-16 21:25:27 +00:00
|
|
|
cm.m.Store(make(map[TenantID]*metrics.Counter))
|
2019-06-07 18:16:05 +00:00
|
|
|
return cm
|
|
|
|
}
|
|
|
|
|
2021-02-16 21:25:27 +00:00
|
|
|
// Get returns counter for the given at
|
2019-06-07 18:16:05 +00:00
|
|
|
func (cm *CounterMap) Get(at *auth.Token) *metrics.Counter {
|
2024-10-01 14:37:18 +00:00
|
|
|
if at == nil {
|
|
|
|
return cm.GetByTenant(nil)
|
|
|
|
}
|
|
|
|
|
2021-02-16 21:25:27 +00:00
|
|
|
key := TenantID{
|
|
|
|
AccountID: at.AccountID,
|
|
|
|
ProjectID: at.ProjectID,
|
2019-06-07 18:16:05 +00:00
|
|
|
}
|
2024-10-01 14:37:18 +00:00
|
|
|
return cm.GetByTenant(&key)
|
2021-02-16 21:25:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 14:28:35 +00:00
|
|
|
// MultiAdd adds multiple values grouped by auth.Token
|
|
|
|
func (cm *CounterMap) MultiAdd(perTenantValues map[auth.Token]int) {
|
|
|
|
for token, value := range perTenantValues {
|
|
|
|
cm.Get(&token).Add(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 21:25:27 +00:00
|
|
|
// GetByTenant returns counter for the given key.
|
2024-10-01 14:37:18 +00:00
|
|
|
func (cm *CounterMap) GetByTenant(key *TenantID) *metrics.Counter {
|
|
|
|
if key == nil {
|
|
|
|
mtm := cm.mt.Load()
|
|
|
|
if mtm == nil {
|
|
|
|
mtc := metrics.GetOrCreateCounter(createMetricNameMultitenant(cm.metric))
|
|
|
|
cm.mt.Store(mtc)
|
|
|
|
return mtc
|
|
|
|
}
|
|
|
|
return mtm.(*metrics.Counter)
|
|
|
|
}
|
|
|
|
|
2021-02-16 21:25:27 +00:00
|
|
|
m := cm.m.Load().(map[TenantID]*metrics.Counter)
|
2024-10-01 14:37:18 +00:00
|
|
|
if c := m[*key]; c != nil {
|
2019-06-07 18:16:05 +00:00
|
|
|
// Fast path - the counter for k already exists.
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slow path - create missing counter for k and re-create m.
|
2021-02-16 21:25:27 +00:00
|
|
|
newM := make(map[TenantID]*metrics.Counter, len(m)+1)
|
2019-06-07 18:16:05 +00:00
|
|
|
for k, c := range m {
|
|
|
|
newM[k] = c
|
|
|
|
}
|
2024-10-01 14:37:18 +00:00
|
|
|
metricName := createMetricName(cm.metric, *key)
|
2019-06-07 18:16:05 +00:00
|
|
|
c := metrics.GetOrCreateCounter(metricName)
|
2024-10-01 14:37:18 +00:00
|
|
|
newM[*key] = c
|
2019-06-07 18:16:05 +00:00
|
|
|
cm.m.Store(newM)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-02-16 21:25:27 +00:00
|
|
|
func createMetricName(metric string, key TenantID) string {
|
2019-06-07 18:16:05 +00:00
|
|
|
if len(metric) == 0 {
|
|
|
|
logger.Panicf("BUG: metric cannot be empty")
|
|
|
|
}
|
|
|
|
if metric[len(metric)-1] != '}' {
|
|
|
|
// Metric without labels.
|
2021-02-16 21:25:27 +00:00
|
|
|
return fmt.Sprintf(`%s{accountID="%d",projectID="%d"}`, metric, key.AccountID, key.ProjectID)
|
2019-06-07 18:16:05 +00:00
|
|
|
}
|
|
|
|
// Metric with labels.
|
2021-02-16 21:25:27 +00:00
|
|
|
return fmt.Sprintf(`%s,accountID="%d",projectID="%d"}`, metric[:len(metric)-1], key.AccountID, key.ProjectID)
|
2019-06-07 18:16:05 +00:00
|
|
|
}
|
2024-10-01 14:37:18 +00:00
|
|
|
|
|
|
|
func createMetricNameMultitenant(metric string) string {
|
|
|
|
if len(metric) == 0 {
|
|
|
|
logger.Panicf("BUG: metric cannot be empty")
|
|
|
|
}
|
|
|
|
if metric[len(metric)-1] != '}' {
|
|
|
|
// Metric without labels.
|
|
|
|
return fmt.Sprintf(`%s{accountID="multitenant",projectID="multitenant"}`, metric)
|
|
|
|
}
|
|
|
|
// Metric with labels.
|
|
|
|
return fmt.Sprintf(`%s,accountID="multitenant",projectID="multitenant"}`, metric[:len(metric)-1])
|
|
|
|
}
|