Commit 6f222ec4 authored by jingbo.wang's avatar jingbo.wang

service-map ok

parent 2d93ae6e
...@@ -51,10 +51,9 @@ func (p *Path) initCount() { ...@@ -51,10 +51,9 @@ func (p *Path) initCount() {
const sqlGetCount = `SELECT count("traceId") AS count FROM trace_info ` + const sqlGetCount = `SELECT count("traceId") AS count FROM trace_info ` +
`WHERE sys_name = '%s' AND path = '%s' AND time >= %d AND time < %d;` `WHERE sys_name = '%s' AND path = '%s' AND time >= %d AND time < %d;`
sql := fmt.Sprintf(sqlGetCount, p.serviceName, p.path, p.startTime.UnixNano(), p.endTime.UnixNano()) sql := fmt.Sprintf(sqlGetCount, p.serviceName, p.path, p.startTime.UnixNano(), p.endTime.UnixNano())
// glog.Debug(sql)
count, ok := queryOneAndToInt(sql) count, ok := queryOneAndToInt(sql)
if !ok { if !ok {
glog.Error("init count query one") glog.Warn("init count query one:", sql)
return return
} }
p.count = count p.count = count
...@@ -65,10 +64,9 @@ func (p *Path) initSumDuration() { ...@@ -65,10 +64,9 @@ func (p *Path) initSumDuration() {
const sqlGetAverageDuration = `SELECT sum("duration") AS average FROM trace_info ` + const sqlGetAverageDuration = `SELECT sum("duration") AS average FROM trace_info ` +
`WHERE sys_name = '%s' AND path = '%s' AND time >= %d AND time < %d;` `WHERE sys_name = '%s' AND path = '%s' AND time >= %d AND time < %d;`
sql := fmt.Sprintf(sqlGetAverageDuration, p.serviceName, p.path, p.startTime.UnixNano(), p.endTime.UnixNano()) sql := fmt.Sprintf(sqlGetAverageDuration, p.serviceName, p.path, p.startTime.UnixNano(), p.endTime.UnixNano())
// glog.Debug(sql)
sum, ok := queryOneAndToInt(sql) sum, ok := queryOneAndToInt(sql)
if !ok { if !ok {
glog.Error("init average query one") glog.Warn("init average query one:", sql)
return return
} }
...@@ -90,10 +88,9 @@ func (p *Path) initMedianDuration() { ...@@ -90,10 +88,9 @@ func (p *Path) initMedianDuration() {
const sqlGetMedianDuration = `SELECT median("duration") AS media FROM trace_info ` + const sqlGetMedianDuration = `SELECT median("duration") AS media FROM trace_info ` +
`WHERE sys_name = '%s' AND path = '%s' AND time >= %d AND time < %d;` `WHERE sys_name = '%s' AND path = '%s' AND time >= %d AND time < %d;`
sql := fmt.Sprintf(sqlGetMedianDuration, p.serviceName, p.path, p.startTime.UnixNano(), p.endTime.UnixNano()) sql := fmt.Sprintf(sqlGetMedianDuration, p.serviceName, p.path, p.startTime.UnixNano(), p.endTime.UnixNano())
// glog.Debug(sql)
median, ok := queryOneAndToInt(sql) median, ok := queryOneAndToInt(sql)
if !ok { if !ok {
glog.Error("init median query one") glog.Warn("init median query one:", sql)
return return
} }
p.medianDuration = time.Duration(median * 1e6) p.medianDuration = time.Duration(median * 1e6)
...@@ -111,14 +108,17 @@ func (p *Path) initMaxDurationTracePoint() { ...@@ -111,14 +108,17 @@ func (p *Path) initMaxDurationTracePoint() {
return return
} }
if len(req.Results) != 1 { if len(req.Results) != 1 {
glog.Warn("init max duration req:", sql)
return return
} }
resp := req.Results[0] resp := req.Results[0]
if len(resp.Series) != 1 { if len(resp.Series) != 1 {
glog.Warn("init max duration resp:", sql)
return return
} }
row := resp.Series[0] row := resp.Series[0]
if len(row.Values) != 1 { if len(row.Values) != 1 {
glog.Warn("init max duration row:", sql)
return return
} }
idxTime, ok1 := getModRowKeyIdx("time", row) idxTime, ok1 := getModRowKeyIdx("time", row)
......
...@@ -72,11 +72,11 @@ func jsonNumberToInt(i interface{}) (int, bool) { ...@@ -72,11 +72,11 @@ func jsonNumberToInt(i interface{}) (int, bool) {
if !ok { if !ok {
return -1, false return -1, false
} }
rtn64, err := j.Int64() rtnFloat64, err := j.Float64()
if err != nil { if err != nil {
return -1, false return -1, false
} }
return int(rtn64), true return int(rtnFloat64), true
} }
//查询并且转换成int //查询并且转换成int
......
...@@ -3,19 +3,21 @@ package report_form ...@@ -3,19 +3,21 @@ package report_form
import ( import (
"fmt" "fmt"
"git.quantgroup.cn/DevOps/enoch/pkg/glog" "git.quantgroup.cn/DevOps/enoch/pkg/glog"
"strings"
"time" "time"
) )
type Service struct { type Service struct {
name string //服务名称 name string //服务名称
startTime time.Time //开始时间 startTime time.Time //开始时间
endTime time.Time //结束时间 endTime time.Time //结束时间
pathMap map[string]*Path //path列表 pathMap map[string]*Path //path列表
sumDuration time.Duration //总响应时间 sumDuration time.Duration //总响应时间
count int //总访问量 count int //总访问量
averageDuration time.Duration //平均响应时间 averageDuration time.Duration //平均响应时间
maxMedianPath *Path //最大中位响应时间path maxMedianPath *Path //最大中位响应时间path
maxAveragePath *Path //最大平均响应时间path maxAveragePath *Path //最大平均响应时间path
maxDurationTracePoint *TracePoint //最大响应时间的tracePoint
} }
func NewService(name string, startTime time.Time, endTime time.Time) *Service { func NewService(name string, startTime time.Time, endTime time.Time) *Service {
...@@ -29,6 +31,11 @@ func NewService(name string, startTime time.Time, endTime time.Time) *Service { ...@@ -29,6 +31,11 @@ func NewService(name string, startTime time.Time, endTime time.Time) *Service {
//初始化path //初始化path
pathList := rtn.getPathList() pathList := rtn.getPathList()
for _, path := range pathList { for _, path := range pathList {
//屏蔽掉健康检查url,屏蔽掉命名不合法的url
pathSplit := strings.Split(path, " ")
if len(pathSplit) != 2 || pathSplit[1] == "/tech/health/check" {
continue
}
pathObj, ok := NewPath(rtn.name, path, rtn.startTime, rtn.endTime) pathObj, ok := NewPath(rtn.name, path, rtn.startTime, rtn.endTime)
if !ok { if !ok {
continue continue
...@@ -97,15 +104,23 @@ func (s *Service) initDuration() { ...@@ -97,15 +104,23 @@ func (s *Service) initDuration() {
if len(s.pathMap) != 0 { if len(s.pathMap) != 0 {
medianMax := time.Duration(-99999) medianMax := time.Duration(-99999)
averageMax := time.Duration(-99999) averageMax := time.Duration(-99999)
maxDuration := time.Duration(-99999)
for _, pathObj := range s.pathMap { for _, pathObj := range s.pathMap {
if pathObj.GetMedianDuration() > medianMax { if pathObj.GetMedianDuration() > medianMax {
s.maxMedianPath = pathObj s.maxMedianPath = pathObj
medianMax = pathObj.GetMedianDuration()
} }
if pathObj.GetAverageDuration() > averageMax { if pathObj.GetAverageDuration() > averageMax {
s.maxAveragePath = pathObj s.maxAveragePath = pathObj
averageMax = pathObj.GetAverageDuration()
} }
}
point := pathObj.GetMaxDurationTracePoint()
if point.Duration > maxDuration {
s.maxDurationTracePoint = &point
maxDuration = point.Duration
}
}
} }
} }
...@@ -133,3 +148,8 @@ func (s *Service) GetMaxMedianDurationPath() (*Path, bool) { ...@@ -133,3 +148,8 @@ func (s *Service) GetMaxMedianDurationPath() (*Path, bool) {
func (s *Service) GetMaxAverageDurationPath() (*Path, bool) { func (s *Service) GetMaxAverageDurationPath() (*Path, bool) {
return s.maxAveragePath, s.maxAveragePath != nil return s.maxAveragePath, s.maxAveragePath != nil
} }
//获取最大访问时间最大的Point
func (s *Service) GetMaxDurationTracePoint() (*TracePoint, bool) {
return s.maxDurationTracePoint, s.maxDurationTracePoint != nil
}
package report_form package report_form
import (
"sort"
"time"
)
type ServiceMap struct {
startTime time.Time
endTime time.Time
serviceMap map[string]*Service
}
func NewServiceMap(startTime time.Time, endTime time.Time) *ServiceMap {
rtn := &ServiceMap{
startTime: startTime,
endTime: endTime,
serviceMap: make(map[string]*Service),
}
//初始化service
serviceList := rtn.getServiceList()
for _, serviceName := range serviceList {
rtn.serviceMap[serviceName] = NewService(serviceName, startTime, endTime)
}
return rtn
}
func (sm *ServiceMap) getServiceList() []string {
sql := `show tag values from trace_info with key = "sys_name";`
resp, err := query(sql)
if err != nil {
return []string{}
}
if len(resp.Results) != 1 {
return []string{}
}
res := resp.Results[0]
if len(res.Series) != 1 {
return []string{}
}
row := res.Series[0]
idx, ok := getModRowKeyIdx("value", row)
if !ok {
return []string{}
}
rtn := make([]string, 0)
for _, v := range row.Values {
serviceName, ok := v[idx].(string)
if !ok {
continue
}
rtn = append(rtn, serviceName)
}
return rtn
}
//获取最大中位响应时间列表
func (sm *ServiceMap) GetMedianDurationPathList() []*Path {
rtn := make([]*Path, 0)
for _, s := range sm.serviceMap {
pathObj, ok := s.GetMaxMedianDurationPath()
if ok {
rtn = append(rtn, pathObj)
}
}
sort.Slice(rtn, func(i, j int) bool {
if rtn[i].GetMedianDuration() > rtn[j].GetMedianDuration() {
return true
}
return false
})
return rtn
}
//获取最大平均响应时间列表
func (sm *ServiceMap) GetAverageDurationPathList() []*Path {
rtn := make([]*Path, 0)
for _, s := range sm.serviceMap {
pathObj, ok := s.GetMaxAverageDurationPath()
if ok {
rtn = append(rtn, pathObj)
}
}
sort.Slice(rtn, func(i, j int) bool {
if rtn[i].GetAverageDuration() > rtn[j].GetAverageDuration() {
return true
}
return false
})
return rtn
}
//获取最大响应时间列表
func (sm *ServiceMap) GetMaxDurationTracePointList() []*TracePoint {
rtn := make([]*TracePoint, 0)
for _, s := range sm.serviceMap {
point, ok := s.GetMaxDurationTracePoint()
if ok {
rtn = append(rtn, point)
}
}
sort.Slice(rtn, func(i, j int) bool {
if rtn[i].Duration > rtn[j].Duration {
return true
}
return false
})
return rtn
}
//获取访问量
func (sm *ServiceMap) GetCount() int {
sum := 0
for _, s := range sm.serviceMap {
sum += s.GetCount()
}
return sum
}
//获取总响应时间
func (sm *ServiceMap) GetSumDuration() time.Duration {
sum := time.Duration(0)
for _, s := range sm.serviceMap {
sum += s.GetSumDuration()
}
return sum
}
//获取总平均响应时间
func (sm *ServiceMap) GetAverageDuration() time.Duration {
count := sm.GetCount()
if count == 0 {
return 0
}
return sm.GetSumDuration() / time.Duration(count)
}
package report_form package report_form
import (
"testing"
"time"
)
func TestNewServiceMap(t *testing.T) {
sm := NewServiceMap(time.Now().Add(time.Hour*24*-1), time.Now())
t.Log("访问量:", sm.GetCount())
t.Log("总访问时间:", sm.GetSumDuration())
t.Log("总平均访问时间:", sm.GetAverageDuration())
t.Log("------------------median-------------------")
medianList := sm.GetMedianDurationPathList()
t.Log("服务数量:", len(medianList))
for _, m := range medianList {
t.Log(m.GetMedianDuration(), m.GetServiceName(), m.GetPath())
}
t.Log("------------------average------------------")
averageList := sm.GetAverageDurationPathList()
t.Log("服务数量:", len(averageList))
for _, m := range averageList {
t.Log(m.GetAverageDuration(), m.GetServiceName(), m.GetPath())
}
t.Log("------------------max----------------------")
maxList := sm.GetMaxDurationTracePointList()
t.Log("服务数量:", len(maxList))
for _, m := range maxList {
t.Log(m.Duration, m.ServiceName, m.Path, m.Timestamp, m.TraceId)
}
}
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
func TestNewService(t *testing.T) { func TestNewService(t *testing.T) {
const ( const (
serviceName = "cuishou" serviceName = "gu-bei"
) )
s := NewService(serviceName, time.Now().Add(time.Hour*-100), time.Now()) s := NewService(serviceName, time.Now().Add(time.Hour*-100), time.Now())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment