Commit f97a304a authored by vrg0's avatar vrg0

path_list OK

parent b5f98256
......@@ -90,7 +90,6 @@ func (d *Delayed5s) Run() {
func (d *Delayed5s) AddNode(point TraceMsg) {
d.lock.Lock()
defer d.lock.Unlock()
switch point.Kind {
case "SERVER":
d.Server[point.Id] = Node{time.Now(), point}
......
package points
import (
"fmt"
"sync"
)
//服务A 的 X 接口 被 那些服务访问? 那些节点访问
type PathNode struct {
ServiceName string
Path string
RemoteServiceMap map[string]struct{}
}
func NewPathNode(path string, serviceName string) PathNode {
return PathNode{
Path: path,
ServiceName: serviceName,
RemoteServiceMap: make(map[string]struct{}),
}
}
type ServiceNode struct {
ServiceName string
PathMap map[string]PathNode
IpMap map[string]struct{}
}
func NewServiceNode(serviceName string) ServiceNode {
return ServiceNode{
ServiceName: serviceName,
PathMap: make(map[string]PathNode),
IpMap: make(map[string]struct{}),
}
}
type Statistics struct {
ServiceMap map[string]ServiceNode
lock *sync.Mutex
}
func (s *Statistics) String() string {
s.lock.Lock()
defer s.lock.Unlock()
rtn := ""
for _, service := range s.ServiceMap {
rtn += fmt.Sprintf("%s: ", service.ServiceName)
for ip := range service.IpMap {
rtn += ip + " "
}
rtn += "\n"
for _, path := range service.PathMap {
rtn += path.Path + " "
for remoteService := range path.RemoteServiceMap {
rtn += remoteService + " "
}
rtn += "\n"
}
rtn += "\n"
}
return rtn
}
func NewStatistics() *Statistics {
return &Statistics{
ServiceMap: make(map[string]ServiceNode),
lock: new(sync.Mutex),
}
}
func (s *Statistics) addServer(local localEndpoint, remote remoteEndpoint, tag tags) {
if _, ok := s.ServiceMap[local.ServiceName]; !ok {
s.ServiceMap[local.ServiceName] = NewServiceNode(local.ServiceName)
}
if _, ok := s.ServiceMap[local.ServiceName].IpMap[local.Ipv4]; !ok {
s.ServiceMap[local.ServiceName].IpMap[local.Ipv4] = struct{}{}
}
path := tag.HttpMethod + "-" + tag.HttpPath
_, ok := s.ServiceMap[local.ServiceName].PathMap[path]
if !ok {
s.ServiceMap[local.ServiceName].PathMap[path] = NewPathNode(path, local.ServiceName)
}
}
func (s *Statistics) addClient(local localEndpoint, remote remoteEndpoint, tag tags) {
for _, service := range s.ServiceMap {
for ip := range service.IpMap {
if ip == remote.Ipv4 {
//添加服务
path := tag.HttpMethod + "-" + tag.HttpPath
if _, ok := service.PathMap[path]; !ok {
service.PathMap[path] = NewPathNode(path, service.ServiceName)
}
//service.PathMap[path].
service.PathMap[path].RemoteServiceMap[local.ServiceName] = struct{}{}
}
}
}
}
func (s *Statistics) AddNode(point TraceMsg) {
s.lock.Lock()
defer s.lock.Unlock()
switch point.Kind {
case "SERVER":
s.addServer(point.LocalEndpoint, point.RemoteEndpoint, point.Tags)
case "CLIENT":
s.addClient(point.LocalEndpoint, point.RemoteEndpoint, point.Tags)
default:
//位置类型,不处理
}
}
/*
type PathNode struct {
ServiceName string
Path string
SrcServiceList map[string]struct{}
SrcNodeIp map[string]struct{}
}
type Statistics struct {
PathMap map[string]PathNode
lock *sync.Mutex
}
func NewStatistics() *Statistics {
return &Statistics{
PathMap: make(map[string]PathNode),
lock: new(sync.Mutex),
}
}
*/
......@@ -2,8 +2,10 @@ package points
import (
"encoding/json"
api_server "git.quantgroup.cn/DevOps/enoch/pkg/api-server"
"git.quantgroup.cn/DevOps/enoch/pkg/glog"
"github.com/influxdata/influxdb/client/v2"
"github.com/valyala/fasthttp"
"github.com/vrg0/go-common/util"
"strings"
"time"
......@@ -72,15 +74,21 @@ func TraceBaseInfoToPoint(data []byte) ([]*client.Point, error) {
}
var d5sObj = NewDelayed5s()
var pathObj = NewStatistics()
func init() {
d5sObj.Run()
api_server.HandlerFunc(api_server.GET, "/path/list", func(ctx *fasthttp.RequestCtx) {
ctx.Response.SetBodyString(pathObj.String())
})
}
//失败返回nil
func buildTraceInfluxMsg(traceInfo TraceMsg) *client.Point {
//临时:统计5s延时问题
d5sObj.AddNode(traceInfo)
//临时:统计服务间访问
pathObj.AddNode(traceInfo)
if traceInfo.Kind != "SERVER" {
return nil
......
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