mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-04-13 20:35:17 +08:00
实现基础的统计指标
This commit is contained in:
@@ -3,29 +3,8 @@
|
||||
package serverconfigs
|
||||
|
||||
import (
|
||||
"github.com/cespare/xxhash/v2"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// MetricItemCategory 指标分类
|
||||
type MetricItemCategory = string
|
||||
|
||||
const (
|
||||
MetricItemCategoryHTTP MetricItemCategory = "http"
|
||||
MetricItemCategoryTCP MetricItemCategory = "tcp"
|
||||
MetricItemCategoryUDP MetricItemCategory = "udp"
|
||||
)
|
||||
|
||||
// MetricItemPeriodUnit 指标周期单位
|
||||
type MetricItemPeriodUnit = string
|
||||
|
||||
const (
|
||||
MetricItemPeriodUnitMinute MetricItemPeriodUnit = "minute"
|
||||
MetricItemPeriodUnitHour MetricItemPeriodUnit = "hour"
|
||||
MetricItemPeriodUnitDay MetricItemPeriodUnit = "day"
|
||||
MetricItemPeriodUnitWeek MetricItemPeriodUnit = "week"
|
||||
MetricItemPeriodUnitMonth MetricItemPeriodUnit = "month"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"time"
|
||||
)
|
||||
|
||||
// MetricItemConfig 指标配置
|
||||
@@ -33,32 +12,147 @@ type MetricItemConfig struct {
|
||||
Id int64 `yaml:"id" json:"id"`
|
||||
IsOn bool `yaml:"isOn" json:"isOn"`
|
||||
Category MetricItemCategory `yaml:"category" json:"category"`
|
||||
Period int `yaml:"period" json:"period"`
|
||||
Period int `yaml:"period" json:"period"` // 单个周期
|
||||
PeriodUnit MetricItemPeriodUnit `yaml:"periodUnit" json:"periodUnit"`
|
||||
Keys []string `yaml:"keys" json:"keys"`
|
||||
Value string `yaml:"value" json:"value"`
|
||||
Version int `yaml:"version" json:"version"`
|
||||
|
||||
sumType string // 统计类型
|
||||
sumType string // 统计类型
|
||||
baseTime time.Time // 基准时间
|
||||
hasHTTPConnectionValue bool // 是否有统计HTTP连接数的数值
|
||||
}
|
||||
|
||||
// Init 初始化
|
||||
func (this *MetricItemConfig) Init() error {
|
||||
// 所有时间以 2020-01-01日 为基准
|
||||
this.baseTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local)
|
||||
|
||||
if this.Period <= 0 {
|
||||
this.Period = 1
|
||||
}
|
||||
|
||||
if len(this.PeriodUnit) == 0 {
|
||||
this.PeriodUnit = MetricItemPeriodUnitDay
|
||||
}
|
||||
|
||||
this.hasHTTPConnectionValue = this.Category == MetricItemCategoryHTTP && this.Value == "${countConnection}"
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseRequest 处理请求
|
||||
func (this *MetricItemConfig) ParseRequest(format func(string) string) (key string, hash string, value float64) {
|
||||
for _, k := range this.Keys {
|
||||
key += "@" + format(k)
|
||||
}
|
||||
hash = strconv.FormatUint(xxhash.Sum64String(key), 10)
|
||||
// CurrentTime 根据周期计算时间
|
||||
func (this *MetricItemConfig) CurrentTime() string {
|
||||
var t string
|
||||
|
||||
// TODO value将来支持复杂运算,比如 ${request.traffic.bytes} * 8
|
||||
if len(this.Value) == 0 {
|
||||
value = 1
|
||||
} else {
|
||||
value = types.Float64(format(this.Value))
|
||||
switch this.PeriodUnit {
|
||||
case MetricItemPeriodUnitMonth:
|
||||
if this.Period > 1 {
|
||||
var now = time.Now()
|
||||
var months = (now.Year()-this.baseTime.Year())*12 + int(now.Month())
|
||||
var delta = months % this.Period
|
||||
if delta == 0 {
|
||||
t = timeutil.Format("Ym")
|
||||
} else {
|
||||
t = timeutil.Format("Ym", now.AddDate(0, -delta, 0))
|
||||
}
|
||||
} else {
|
||||
t = timeutil.Format("Ym")
|
||||
}
|
||||
case MetricItemPeriodUnitWeek:
|
||||
if this.Period > 1 {
|
||||
var now = time.Now()
|
||||
var weeks = int((now.Unix() - this.baseTime.Unix()) / (86400 * 7))
|
||||
var delta = weeks % this.Period
|
||||
if delta == 0 {
|
||||
t = timeutil.Format("YW")
|
||||
} else {
|
||||
t = timeutil.FormatTime("YW", now.Unix()-int64(delta*7*86400))
|
||||
}
|
||||
} else {
|
||||
t = timeutil.Format("YW")
|
||||
}
|
||||
case MetricItemPeriodUnitDay:
|
||||
if this.Period > 1 {
|
||||
var now = time.Now()
|
||||
var days = int((now.Unix() - this.baseTime.Unix()) / 86400)
|
||||
var delta = days % this.Period
|
||||
if delta == 0 {
|
||||
t = timeutil.Format("Ymd")
|
||||
} else {
|
||||
t = timeutil.FormatTime("Ymd", now.Unix()-int64(delta*86400))
|
||||
}
|
||||
} else {
|
||||
t = timeutil.Format("Ymd")
|
||||
}
|
||||
case MetricItemPeriodUnitHour:
|
||||
if this.Period > 1 {
|
||||
var now = time.Now()
|
||||
var hours = int((now.Unix() - this.baseTime.Unix()) / 3600)
|
||||
var delta = hours % this.Period
|
||||
if delta == 0 {
|
||||
t = timeutil.Format("YmdH")
|
||||
} else {
|
||||
t = timeutil.FormatTime("YmdH", now.Unix()-int64(delta*3600))
|
||||
}
|
||||
} else {
|
||||
t = timeutil.Format("YmdH")
|
||||
}
|
||||
case MetricItemPeriodUnitMinute:
|
||||
if this.Period > 1 {
|
||||
var now = time.Now()
|
||||
var minutes = int((now.Unix() - this.baseTime.Unix()) / 60)
|
||||
var delta = minutes % this.Period
|
||||
if delta == 0 {
|
||||
t = timeutil.Format("YmdHi")
|
||||
} else {
|
||||
t = timeutil.FormatTime("YmdHi", now.Unix()-int64(delta*60))
|
||||
}
|
||||
} else {
|
||||
t = timeutil.Format("YmdHi")
|
||||
}
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
|
||||
return
|
||||
return t
|
||||
}
|
||||
|
||||
// ExpiresTime 根据周期计算过期时间
|
||||
func (this *MetricItemConfig) ExpiresTime() string {
|
||||
switch this.PeriodUnit {
|
||||
case MetricItemPeriodUnitMonth:
|
||||
return timeutil.Format("Ym", time.Now().AddDate(0, -(this.Period*4), 0))
|
||||
case MetricItemPeriodUnitWeek:
|
||||
return timeutil.FormatTime("YW", time.Now().Unix()-86400*7*int64(this.Period*5))
|
||||
case MetricItemPeriodUnitDay:
|
||||
return timeutil.FormatTime("Ymd", time.Now().Unix()-86400*int64(this.Period*32))
|
||||
case MetricItemPeriodUnitHour:
|
||||
return timeutil.FormatTime("YmdH", time.Now().Unix()-3600*int64(this.Period*25))
|
||||
case MetricItemPeriodUnitMinute:
|
||||
return timeutil.FormatTime("YmdHi", time.Now().Unix()-60*int64(this.Period*60))
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// UploadDuration 上传数据的周期
|
||||
func (this *MetricItemConfig) UploadDuration() time.Duration {
|
||||
switch this.PeriodUnit {
|
||||
case MetricItemPeriodUnitMonth:
|
||||
return 10 * time.Minute
|
||||
case MetricItemPeriodUnitWeek:
|
||||
return 10 * time.Minute
|
||||
case MetricItemPeriodUnitDay:
|
||||
return 10 * time.Minute
|
||||
case MetricItemPeriodUnitHour:
|
||||
return 5 * time.Minute
|
||||
case MetricItemPeriodUnitMinute:
|
||||
return time.Duration(this.Period) * time.Minute
|
||||
default:
|
||||
return 10 * time.Minute
|
||||
}
|
||||
}
|
||||
|
||||
func (this *MetricItemConfig) HasHTTPConnectionValue() bool {
|
||||
return this.hasHTTPConnectionValue
|
||||
}
|
||||
|
||||
@@ -2,38 +2,59 @@
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"testing"
|
||||
)
|
||||
import "testing"
|
||||
|
||||
func TestMetricItemConfig_ProcessRequest(t *testing.T) {
|
||||
var metric = &MetricItemConfig{
|
||||
Keys: []string{"${remoteAddr}", "${status}", "${requestPath}"},
|
||||
Value: "${trafficIn}",
|
||||
func TestMetricItemConfig_CurrentTime_Month(t *testing.T) {
|
||||
for _, period := range []int{1, 2, 3, 4, 5, 100} {
|
||||
var item = &MetricItemConfig{
|
||||
Period: period,
|
||||
PeriodUnit: MetricItemPeriodUnitMonth,
|
||||
}
|
||||
_ = item.Init()
|
||||
t.Logf(item.CurrentTime())
|
||||
}
|
||||
key, hash, value := metric.ParseRequest(func(s string) string {
|
||||
return configutils.ParseVariables(s, func(varName string) (value string) {
|
||||
switch varName {
|
||||
case "trafficIn":
|
||||
return "1000"
|
||||
}
|
||||
return "[" + varName + "]"
|
||||
})
|
||||
})
|
||||
t.Log("key:", key, "hash:", hash)
|
||||
t.Logf("value: %f", value)
|
||||
}
|
||||
|
||||
func BenchmarkMetricItemConfig_ProcessRequest(b *testing.B) {
|
||||
var metric = &MetricItemConfig{
|
||||
Keys: []string{"${remoteAddr}", "${status}"},
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
metric.ParseRequest(func(s string) string {
|
||||
return configutils.ParseVariables(s, func(varName string) (value string) {
|
||||
return "[" + varName + "]"
|
||||
})
|
||||
})
|
||||
func TestMetricItemConfig_CurrentTime_Week(t *testing.T) {
|
||||
for _, period := range []int{1, 2, 3, 4, 5} {
|
||||
var item = &MetricItemConfig{
|
||||
Period: period,
|
||||
PeriodUnit: MetricItemPeriodUnitWeek,
|
||||
}
|
||||
_ = item.Init()
|
||||
t.Log(period, ":", item.CurrentTime())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetricItemConfig_CurrentTime_Day(t *testing.T) {
|
||||
for _, period := range []int{1, 2, 3, 4, 5, 13} {
|
||||
var item = &MetricItemConfig{
|
||||
Period: period,
|
||||
PeriodUnit: MetricItemPeriodUnitDay,
|
||||
}
|
||||
_ = item.Init()
|
||||
t.Log(period, ":", item.CurrentTime())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetricItemConfig_CurrentTime_Hour(t *testing.T) {
|
||||
for _, period := range []int{1, 2, 3, 4, 5, 13} {
|
||||
var item = &MetricItemConfig{
|
||||
Period: period,
|
||||
PeriodUnit: MetricItemPeriodUnitHour,
|
||||
}
|
||||
_ = item.Init()
|
||||
t.Log(period, ":", item.CurrentTime())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetricItemConfig_CurrentTime_Minute(t *testing.T) {
|
||||
for _, period := range []int{1, 2, 3, 4, 5, 13} {
|
||||
var item = &MetricItemConfig{
|
||||
Period: period,
|
||||
PeriodUnit: MetricItemPeriodUnitMinute,
|
||||
}
|
||||
_ = item.Init()
|
||||
t.Log(period, ":", item.CurrentTime())
|
||||
}
|
||||
}
|
||||
|
||||
143
pkg/serverconfigs/metric_item_utils.go
Normal file
143
pkg/serverconfigs/metric_item_utils.go
Normal file
@@ -0,0 +1,143 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package serverconfigs
|
||||
|
||||
// MetricItemCategory 指标分类
|
||||
type MetricItemCategory = string
|
||||
|
||||
const (
|
||||
MetricItemCategoryHTTP MetricItemCategory = "http"
|
||||
MetricItemCategoryTCP MetricItemCategory = "tcp"
|
||||
MetricItemCategoryUDP MetricItemCategory = "udp"
|
||||
)
|
||||
|
||||
// MetricItemPeriodUnit 指标周期单位
|
||||
type MetricItemPeriodUnit = string
|
||||
|
||||
const (
|
||||
MetricItemPeriodUnitMinute MetricItemPeriodUnit = "minute"
|
||||
MetricItemPeriodUnitHour MetricItemPeriodUnit = "hour"
|
||||
MetricItemPeriodUnitDay MetricItemPeriodUnit = "day"
|
||||
MetricItemPeriodUnitWeek MetricItemPeriodUnit = "week"
|
||||
MetricItemPeriodUnitMonth MetricItemPeriodUnit = "month"
|
||||
)
|
||||
|
||||
// HTTP相关指标对象
|
||||
type metricKeyDefinition struct {
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type metricValueDefinition struct {
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
func FindAllHTTPMetricKeyDefinitions() []*metricKeyDefinition {
|
||||
// TODO
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func FindAllMetricValueDefinitions(category MetricItemCategory) []*metricValueDefinition {
|
||||
switch category {
|
||||
case MetricItemCategoryHTTP:
|
||||
return []*metricValueDefinition{
|
||||
{
|
||||
Name: "请求数",
|
||||
Code: "${countRequest}",
|
||||
},
|
||||
{
|
||||
Name: "连接数",
|
||||
Code: "${countConnection}",
|
||||
},
|
||||
{
|
||||
Name: "下行流量",
|
||||
Code: "${countTrafficOut}",
|
||||
},
|
||||
{
|
||||
Name: "上行流量",
|
||||
Code: "${countTrafficIn}",
|
||||
},
|
||||
}
|
||||
case MetricItemCategoryTCP:
|
||||
return []*metricValueDefinition{
|
||||
{
|
||||
Name: "连接数",
|
||||
Code: "${countConnection}",
|
||||
},
|
||||
{
|
||||
Name: "下行流量",
|
||||
Code: "${countTrafficOut}",
|
||||
},
|
||||
{
|
||||
Name: "上行流量",
|
||||
Code: "${countTrafficIn}",
|
||||
},
|
||||
}
|
||||
case MetricItemCategoryUDP:
|
||||
return []*metricValueDefinition{
|
||||
{
|
||||
Name: "连接数",
|
||||
Code: "${countConnection}",
|
||||
},
|
||||
{
|
||||
Name: "下行流量",
|
||||
Code: "${countTrafficOut}",
|
||||
},
|
||||
{
|
||||
Name: "上行流量",
|
||||
Code: "${countTrafficIn}",
|
||||
},
|
||||
}
|
||||
}
|
||||
return []*metricValueDefinition{}
|
||||
}
|
||||
|
||||
// FindAllTCPMetricKeyDefinitions TCP相关指标对象
|
||||
func FindAllTCPMetricKeyDefinitions() []*metricKeyDefinition {
|
||||
// TODO
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindAllUDPMetricKeyDefinitions UDP相关指标对象
|
||||
func FindAllUDPMetricKeyDefinitions() []*metricKeyDefinition {
|
||||
// TODO
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// HumanMetricTime 格式化时间,让时间更易读
|
||||
func HumanMetricTime(periodUnit MetricItemPeriodUnit, time string) string {
|
||||
switch periodUnit {
|
||||
case MetricItemPeriodUnitMonth:
|
||||
if len(time) != 6 {
|
||||
return time
|
||||
}
|
||||
return time[:4] + "-" + time[4:]
|
||||
case MetricItemPeriodUnitWeek:
|
||||
if len(time) != 6 {
|
||||
return time
|
||||
}
|
||||
return time[:4] + "-" + time[4:]
|
||||
case MetricItemPeriodUnitDay:
|
||||
if len(time) != 8 {
|
||||
return time
|
||||
}
|
||||
return time[:4] + "-" + time[4:6] + "-" + time[6:]
|
||||
case MetricItemPeriodUnitHour:
|
||||
if len(time) != 10 {
|
||||
return time
|
||||
}
|
||||
return time[:4] + "-" + time[4:6] + "-" + time[6:8] + " " + time[8:]
|
||||
case MetricItemPeriodUnitMinute:
|
||||
if len(time) != 12 {
|
||||
return time
|
||||
}
|
||||
return time[:4] + "-" + time[4:6] + "-" + time[6:8] + " " + time[8:10] + ":" + time[10:]
|
||||
}
|
||||
return time
|
||||
}
|
||||
Reference in New Issue
Block a user