-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiagram.go
More file actions
173 lines (150 loc) · 3.66 KB
/
Copy pathdiagram.go
File metadata and controls
173 lines (150 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"bytes"
"fmt"
"os"
"path"
"strings"
"github.com/upamune/jigsaw/drawer"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/parser"
)
func createServiceNameMap(m map[string]string, spans []*span) {
for _, s := range spans {
s := s
if s == nil {
continue
}
m[s.SpanID] = s.Service
if len(s.children) > 0 {
createServiceNameMap(m, s.children)
}
}
}
func buildDiagram(conf config, d drawer.Drawer, spans []*span) (string, error) {
var b bytes.Buffer
w := func(s string) {
if s == "" {
return
}
b.WriteString(fmt.Sprintf("%s\n", s))
}
serviceNameMap := make(map[string]string)
createServiceNameMap(serviceNameMap, spans)
var draw func(s *span)
draw = func(s *span) {
if s == nil {
return
}
caller := extractCaller(s, serviceNameMap, conf)
callee := extractCallee(s, conf)
isSkip := shouldSkipSpan(s, caller, callee, conf)
method := extractMethodName(s)
if !isSkip {
msg := createRequestMsgWithDrawing(s, method)
s := d.Draw(caller, callee, msg)
w(s)
}
for _, c := range s.children {
c := c
draw(c)
}
if conf.NoResponse {
return
}
if !isSkip {
msg := createResponseMsgWithDrawing(s, method)
s := d.Draw(callee, caller, msg)
w(s)
}
}
w(d.Comment("Generated by https://github.com/upamune/jigsaw"))
w(d.Header())
for _, s := range spans {
draw(s)
}
w(d.Footer())
return b.String(), nil
}
func convertServiceName(s string, conf config) string {
if alias, ok := conf.ServiceAlias[s]; ok {
return alias
}
return s
}
func extractCaller(s *span, serviceNameMap map[string]string, conf config) string {
if caller, ok := serviceNameMap[s.ParentID]; ok {
return convertServiceName(caller, conf)
}
return convertServiceName(s.Service, conf)
}
func extractCallee(s *span, conf config) string {
if s.Meta.GRPCFullMethodName != "" {
callee := extractGRPCServiceFromMethod(s.Meta.GRPCFullMethodName)
if alias, ok := conf.GRPCServiceAlias[callee]; ok {
callee = alias
}
return callee
}
return convertServiceName(s.Service, conf)
}
func extractMethodName(s *span) string {
if s.Meta.GRPCFullMethodName != "" {
return path.Base(s.Meta.GRPCFullMethodName)
}
if s.Type == "graphql" {
name, err := extractGraphQLOperationName(s.Resource)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to parse graphql query(%s): %v\n", s.Resource, err)
return "GraphQL"
}
return fmt.Sprintf("GraphQL: %s", name)
}
return s.Resource
}
func shouldSkipSpan(s *span, caller, callee string, conf config) bool {
if conf.IsSkipSelfCall && caller == callee {
return true
}
if len(conf.IncludeServices) > 0 {
return !contains(conf.IncludeServices, s.Service)
}
return contains(conf.ExcludeGRPCServices, extractGRPCServiceFromMethod(s.Meta.GRPCFullMethodName))
}
func extractGRPCServiceFromMethod(method string) string {
return path.Dir(method)
}
func extractGraphQLOperationName(query string) (string, error) {
doc, err := parser.ParseQuery(&ast.Source{Input: query})
if err != nil {
return "", err
}
var names []string
for _, op := range doc.Operations {
names = append(names, op.Name)
}
return strings.Join(names, ","), nil
}
func createRequestMsgWithDrawing(s *span, method string) string {
if s.Type == "" || s.Type == "sql" {
return method
}
return fmt.Sprintf("%s Request", method)
}
func createResponseMsgWithDrawing(s *span, method string) string {
if s.Type == "" {
return method
}
if s.Type == "sql" {
return ""
}
return fmt.Sprintf("%s Response", method)
}
func contains(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
for _, s := range slice {
set[s] = struct{}{}
}
_, ok := set[item]
return ok
}