Skip to content

Commit

Permalink
support more service
Browse files Browse the repository at this point in the history
  • Loading branch information
fuwx295 committed Oct 16, 2024
1 parent 922c105 commit ea957f3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 24 deletions.
7 changes: 5 additions & 2 deletions backend/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4779,8 +4779,11 @@ const docTemplate = `{
"request.GetServiceRouteRequest": {
"type": "object",
"properties": {
"serviceName": {
"type": "string"
"serviceNames": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down
7 changes: 5 additions & 2 deletions backend/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -4771,8 +4771,11 @@
"request.GetServiceRouteRequest": {
"type": "object",
"properties": {
"serviceName": {
"type": "string"
"serviceNames": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down
6 changes: 4 additions & 2 deletions backend/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -857,8 +857,10 @@ definitions:
type: object
request.GetServiceRouteRequest:
properties:
serviceName:
type: string
serviceNames:
items:
type: string
type: array
type: object
request.GetTraceFilterValueRequest:
properties:
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/model/request/log_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type AddLogParseRequest struct {
}

type GetServiceRouteRequest struct {
Service string `form:"serviceName"`
Service []string `form:"serviceNames"`
}

type LogTable struct {
Expand Down
8 changes: 7 additions & 1 deletion backend/pkg/services/log/service_add_log_parse_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ import (
func getRouteRule(routeMap map[string]string) string {
var res []string
for k, v := range routeMap {
if k == "k8s.pod.name" {
strValues := strings.Split(v, ",")
for _, vv := range strValues {
res = append(res, fmt.Sprintf(`starts_with(string!(."%s"), "%s")`, k, vv))
}
}
res = append(res, fmt.Sprintf(`starts_with(string!(."%s"), "%s")`, k, v))
}
return strings.Join(res, " && ")
return strings.Join(res, " || ")
}

func (s *service) AddLogParseRule(req *request.AddLogParseRequest) (*response.LogParseResponse, error) {
Expand Down
12 changes: 8 additions & 4 deletions backend/pkg/services/log/service_get_log_parse_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
var routeReg = regexp.MustCompile(`\"(.*?)\"`)

func getRouteRuleMap(routeRule string) map[string]string {
res := make(map[string]string)
lines := strings.Split(routeRule, "&&")
res := make(map[string][]string)
lines := strings.Split(routeRule, "||")
for _, line := range lines {
if line == "" {
continue
Expand All @@ -22,10 +22,14 @@ func getRouteRuleMap(routeRule string) map[string]string {
if len(matches) == 2 {
key := matches[0][1]
value := matches[1][1]
res[key] = value
res[key] = append(res[key], value)
}
}
return res
rc := make(map[string]string)
for k, v := range res {
rc[k] = strings.Join(v, ",")
}
return rc
}

func (s *service) GetLogParseRule(req *request.QueryLogParseRequest) (*response.LogParseResponse, error) {
Expand Down
25 changes: 13 additions & 12 deletions backend/pkg/services/log/service_get_service_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@ import (
)

func (s *service) GetServiceRoute(req *request.GetServiceRouteRequest) (*response.GetServiceRouteResponse, error) {
serviceNames := []string{}
now := time.Now()
currentTimestamp := now.UnixMicro()
sevenDaysAgo := now.AddDate(0, 0, -7)
sevenDaysAgoTimestamp := sevenDaysAgo.UnixMicro()

instances, err := s.promRepo.GetActiveInstanceList(sevenDaysAgoTimestamp, currentTimestamp, req.Service)
if err != nil {
return nil, err
}
var deployName string
for instanceName, _ := range instances.GetInstanceIdMap() {
parts := strings.Split(instanceName, "-")
if len(parts) >= 3 {
deployName = strings.Join(parts[:len(parts)-2], "-")
break
for _, service := range req.Service {
instances, err := s.promRepo.GetActiveInstanceList(sevenDaysAgoTimestamp, currentTimestamp, service)
if err != nil {
return nil, err
}
for instanceName, _ := range instances.GetInstanceIdMap() {
parts := strings.Split(instanceName, "-")
if len(parts) >= 3 {
serviceNames = append(serviceNames, strings.Join(parts[:len(parts)-2], "-"))
break
}
}
}

return &response.GetServiceRouteResponse{
RouteRule: map[string]string{"k8s.pod.name": deployName},
RouteRule: map[string]string{"k8s.pod.name": strings.Join(serviceNames, ",")},
}, nil

}

0 comments on commit ea957f3

Please sign in to comment.