2023-02-09 08:59:31 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const billingSubsystem = "billing"
|
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
const (
|
|
|
|
userRequestsMetric = "user_requests"
|
|
|
|
userTrafficMetric = "user_traffic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RequestType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
UNKNOWNRequest RequestType = iota
|
|
|
|
HEADRequest RequestType = iota
|
|
|
|
PUTRequest RequestType = iota
|
|
|
|
LISTRequest RequestType = iota
|
|
|
|
GETRequest RequestType = iota
|
|
|
|
DELETERequest RequestType = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t RequestType) String() string {
|
|
|
|
switch t {
|
|
|
|
case 1:
|
|
|
|
return "HEAD"
|
|
|
|
case 2:
|
|
|
|
return "PUT"
|
|
|
|
case 3:
|
|
|
|
return "LIST"
|
|
|
|
case 4:
|
|
|
|
return "GET"
|
|
|
|
case 5:
|
|
|
|
return "DELETE"
|
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 08:59:31 +00:00
|
|
|
type TrafficType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
UnknownTraffic TrafficType = iota
|
|
|
|
INTraffic TrafficType = iota
|
|
|
|
OUTTraffic TrafficType = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t TrafficType) String() string {
|
|
|
|
switch t {
|
|
|
|
case 1:
|
|
|
|
return "IN"
|
|
|
|
case 2:
|
|
|
|
return "OUT"
|
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
OperationList [6]int
|
|
|
|
|
|
|
|
UsersAPIStats struct {
|
|
|
|
users map[string]*userAPIStats
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketKey struct {
|
2023-12-01 11:16:19 +00:00
|
|
|
name string
|
|
|
|
cid string
|
|
|
|
namespace string
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bucketStat struct {
|
|
|
|
Operations OperationList
|
|
|
|
InTraffic uint64
|
|
|
|
OutTraffic uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
userAPIStats struct {
|
|
|
|
buckets map[bucketKey]bucketStat
|
|
|
|
user string
|
|
|
|
}
|
|
|
|
|
|
|
|
UserBucketInfo struct {
|
|
|
|
User string
|
|
|
|
Bucket string
|
|
|
|
ContainerID string
|
2023-12-01 11:16:19 +00:00
|
|
|
Namespace string
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UserMetricsInfo struct {
|
|
|
|
UserBucketInfo
|
2023-04-07 14:28:21 +00:00
|
|
|
Operation RequestType
|
2023-02-09 08:59:31 +00:00
|
|
|
Requests int
|
|
|
|
}
|
|
|
|
|
|
|
|
UserTrafficMetricsInfo struct {
|
|
|
|
UserBucketInfo
|
|
|
|
Type TrafficType
|
|
|
|
Value uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
UserMetrics struct {
|
|
|
|
Requests []UserMetricsInfo
|
|
|
|
Traffic []UserTrafficMetricsInfo
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-12-01 11:16:19 +00:00
|
|
|
func (u *UsersAPIStats) Update(user, bucket, cnrID, ns string, reqType RequestType, in, out uint64) {
|
2024-02-26 13:18:34 +00:00
|
|
|
if u == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-09 08:59:31 +00:00
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
|
|
|
|
usersStat := u.users[user]
|
|
|
|
if usersStat == nil {
|
|
|
|
if u.users == nil {
|
|
|
|
u.users = make(map[string]*userAPIStats)
|
|
|
|
}
|
|
|
|
usersStat = &userAPIStats{
|
|
|
|
buckets: make(map[bucketKey]bucketStat, 1),
|
|
|
|
user: user,
|
|
|
|
}
|
|
|
|
u.users[user] = usersStat
|
|
|
|
}
|
|
|
|
|
|
|
|
key := bucketKey{
|
2023-12-01 11:16:19 +00:00
|
|
|
name: bucket,
|
|
|
|
cid: cnrID,
|
|
|
|
namespace: ns,
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bktStat := usersStat.buckets[key]
|
|
|
|
bktStat.Operations[reqType]++
|
|
|
|
bktStat.InTraffic += in
|
|
|
|
bktStat.OutTraffic += out
|
|
|
|
usersStat.buckets[key] = bktStat
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UsersAPIStats) DumpMetrics() UserMetrics {
|
2024-02-26 13:18:34 +00:00
|
|
|
if u == nil {
|
|
|
|
return UserMetrics{}
|
|
|
|
}
|
|
|
|
|
2023-02-09 08:59:31 +00:00
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
|
|
|
|
result := UserMetrics{
|
|
|
|
Requests: make([]UserMetricsInfo, 0, len(u.users)),
|
|
|
|
Traffic: make([]UserTrafficMetricsInfo, 0, len(u.users)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for user, userStat := range u.users {
|
|
|
|
for key, bktStat := range userStat.buckets {
|
|
|
|
userBktInfo := UserBucketInfo{
|
|
|
|
User: user,
|
|
|
|
Bucket: key.name,
|
|
|
|
ContainerID: key.cid,
|
2023-12-01 11:16:19 +00:00
|
|
|
Namespace: key.namespace,
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if bktStat.InTraffic != 0 {
|
|
|
|
result.Traffic = append(result.Traffic, UserTrafficMetricsInfo{
|
|
|
|
UserBucketInfo: userBktInfo,
|
|
|
|
Type: INTraffic,
|
|
|
|
Value: bktStat.InTraffic,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if bktStat.OutTraffic != 0 {
|
|
|
|
result.Traffic = append(result.Traffic, UserTrafficMetricsInfo{
|
|
|
|
UserBucketInfo: userBktInfo,
|
|
|
|
Type: OUTTraffic,
|
|
|
|
Value: bktStat.OutTraffic,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for op, val := range bktStat.Operations {
|
|
|
|
if val != 0 {
|
|
|
|
result.Requests = append(result.Requests, UserMetricsInfo{
|
|
|
|
UserBucketInfo: userBktInfo,
|
2023-04-07 14:28:21 +00:00
|
|
|
Operation: RequestType(op),
|
2023-02-09 08:59:31 +00:00
|
|
|
Requests: val,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u.users = make(map[string]*userAPIStats)
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
type billingMetrics struct {
|
|
|
|
registry *prometheus.Registry
|
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
userRequestsDesc *prometheus.Desc
|
|
|
|
userTrafficDesc *prometheus.Desc
|
2024-02-26 13:18:34 +00:00
|
|
|
apiStat *UsersAPIStats
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newBillingMetrics() *billingMetrics {
|
|
|
|
return &billingMetrics{
|
2023-04-07 14:28:21 +00:00
|
|
|
registry: prometheus.NewRegistry(),
|
2023-04-07 14:58:36 +00:00
|
|
|
userRequestsDesc: newDesc(appMetricsDesc[billingSubsystem][userRequestsMetric]),
|
|
|
|
userTrafficDesc: newDesc(appMetricsDesc[billingSubsystem][userTrafficMetric]),
|
2024-02-26 13:18:34 +00:00
|
|
|
apiStat: &UsersAPIStats{},
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 08:40:58 +00:00
|
|
|
func (b *billingMetrics) Gatherer() prometheus.Gatherer {
|
|
|
|
return b.registry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *billingMetrics) Register() {
|
2023-02-09 08:59:31 +00:00
|
|
|
b.registry.MustRegister(b)
|
|
|
|
}
|
|
|
|
|
2023-04-10 08:40:58 +00:00
|
|
|
func (b *billingMetrics) Unregister() {
|
2023-02-09 08:59:31 +00:00
|
|
|
b.registry.Unregister(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *billingMetrics) Describe(ch chan<- *prometheus.Desc) {
|
2023-04-07 14:28:21 +00:00
|
|
|
ch <- b.userRequestsDesc
|
|
|
|
ch <- b.userTrafficDesc
|
2023-02-09 08:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *billingMetrics) Collect(ch chan<- prometheus.Metric) {
|
|
|
|
userMetrics := b.apiStat.DumpMetrics()
|
|
|
|
|
|
|
|
for _, value := range userMetrics.Requests {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2023-04-07 14:28:21 +00:00
|
|
|
b.userRequestsDesc,
|
2023-02-09 08:59:31 +00:00
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(value.Requests),
|
|
|
|
value.User,
|
|
|
|
value.Bucket,
|
|
|
|
value.ContainerID,
|
|
|
|
value.Operation.String(),
|
2023-12-01 11:16:19 +00:00
|
|
|
value.Namespace,
|
2023-02-09 08:59:31 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range userMetrics.Traffic {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2023-04-07 14:28:21 +00:00
|
|
|
b.userTrafficDesc,
|
2023-02-09 08:59:31 +00:00
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(value.Value),
|
|
|
|
value.User,
|
|
|
|
value.Bucket,
|
|
|
|
value.ContainerID,
|
|
|
|
value.Type.String(),
|
2023-12-01 11:16:19 +00:00
|
|
|
value.Namespace,
|
2023-02-09 08:59:31 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|