forked from sourcegraph/appdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_mysql.go
311 lines (287 loc) · 9.33 KB
/
store_mysql.go
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package appdash
import (
"log"
"time"
"github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"fmt"
"github.com/go-xorm/core"
"database/sql"
"encoding/json"
)
func MsTimestampNow() int64 {
return time.Now().UnixNano() / 1000000
}
type MySQLConfig struct {
DBName string `toml:"dbname"`
Host string `toml:"host"`
Port int `toml:"port"`
User string `toml:"user"`
Password string `toml:"password"`
Sslmode string `toml:"sslmode"`
ShowLog bool
ShowExecTime bool
LogLevel core.LogLevel
DataSaveDir string
DataFileSaveLoopSize int
MaxIdleConns int `toml:"max_idle_conns"`
MaxOpenConns int `toml:"max_open_conns"`
MaxLifeTime int `toml:"max_life_time"`
Timeout int `toml:"timeout"`
RTimeout int `toml:"rtimeout"`
WTimeout int `toml:"wtimeout"`
}
func (c MySQLConfig) MySQLSource() string {
params := make(map[string]string, 0)
params["charset"] = "utf8mb4"
cfg := mysql.Config{}
cfg.Net = "tcp"
cfg.Addr = c.Host
cfg.User = c.User
cfg.Passwd = c.Password
cfg.DBName = c.DBName
cfg.ParseTime = true
cfg.Collation = "utf8mb4_unicode_ci"
cfg.Params = params
cfg.Loc, _ = time.LoadLocation("Asia/Chongqing")
cfg.Timeout = time.Duration(c.Timeout) * time.Second
cfg.MultiStatements = true
cfg.ReadTimeout = time.Duration(c.RTimeout) * time.Second
cfg.WriteTimeout = time.Duration(c.WTimeout) * time.Second
return cfg.FormatDSN()
}
type TraceTable struct {
Id int64 `xorm:"id BIGINT(20) notnull autoincr pk" json:"id"`
TraceKey string `xorm:"trace_key varchar(32) not null index" json:"trace_id"`
TraceName string `xorm:"trace_name varchar(30) not null" json:"trace_name"`
CostTime int64 `xorm:"cost_time int(11) not null default 0" json:"cost_time"`
CreatedAt int64 `xorm:"created_at BIGINT(20) notnull default 0" json:"created_at"`
UpdatedAt int64 `xorm:"updated_at BIGINT(20) notnull default 0" json:"updated_at"`
}
func (t *TraceTable) TableName() string {
return "appdash_traces"
}
func (t *TraceTable) BeforeInsert() {
t.CreatedAt = MsTimestampNow()
t.UpdatedAt = t.CreatedAt
}
func (t *TraceTable) BeforeUpdate() {
t.UpdatedAt = MsTimestampNow()
}
type EventType int
const (
EventType_Http EventType = 1
EventType_Span EventType = 2
EventType_Log EventType = 3
EventType_Msg EventType = 4
EventType_Time EventType = 5
)
type SpanTable struct {
Id int64 `xorm:"id BIGINT(20) notnull autoincr pk" json:"id"`
SpanKey string `xorm:"span_key varchar(32) not null index" json:"span_id"`
SpanName string `xorm:"span_name varchar(30) not null" json:"span_name"`
Content string `xorm:"content text" json:"content"`
CostTime int64 `xorm:"cost_time int(11) not null default 0" json:"cost_time"`
ParentSpanKey string `xorm:"parent_span_key varchar(32) not null index" json:"parent_span_key"`
ParentSpanID int64 `xorm:"parent_span_id BIGINT(20) not null default 0 index" json:"parent_span_id"`
TraceKey string `xorm:"trace_key varchar(32) not null index" json:"trace_key"`
Recv time.Time `xorm:"-" json:"client_recv"`
Send time.Time `xorm:"-" json:"client_send"`
ClientRecv int64 `xorm:"client_recv BIGINT(20) notnull default 0" json:"client_recv"`
ClientSend int64 `xorm:"client_send BIGINT(20) notnull default 0" json:"client_send"`
TraceId int64 `xorm:"trace_id BIGINT(20) not null default 0 index" json:"trace_id"`
CreatedAt int64 `xorm:"created_at BIGINT(20) notnull default 0" json:"created_at"`
UpdatedAt int64 `xorm:"updated_at BIGINT(20) notnull default 0" json:"updated_at"`
}
func (s *SpanTable) TableName() string {
return "appdash_spans"
}
func (s *SpanTable) BeforeInsert() {
s.CreatedAt = MsTimestampNow()
s.UpdatedAt = s.CreatedAt
}
func (s *SpanTable) BeforeUpdate() {
s.UpdatedAt = MsTimestampNow()
}
type MySQLStore struct {
DBXorm *xorm.Engine
Log bool
}
var (
DBXorm *xorm.Engine
err error
)
func NewMySQLStore(cfg MySQLConfig) (*MySQLStore) {
log.Print("start NewMySQLStore")
DBXorm, err = xorm.NewEngine("mysql", cfg.MySQLSource())
if err != nil {
panic(fmt.Errorf("sql.Open failed: %v", err))
}
DBXorm.ShowSQL(cfg.ShowLog)
DBXorm.SetLogLevel(cfg.LogLevel)
DBXorm.ShowExecTime(cfg.ShowExecTime)
DBXorm.SetTableMapper(core.GonicMapper{})
DBXorm.SetMaxIdleConns(cfg.MaxIdleConns)
DBXorm.SetMaxOpenConns(cfg.MaxOpenConns)
DBXorm.SetConnMaxLifetime(time.Duration(cfg.MaxLifeTime) * time.Second)
err = DBXorm.Ping()
if err != nil {
panic(fmt.Sprintf("DBXorm.Ping Err:%v", err))
}
err = DBXorm.Sync2(new(TraceTable), new(SpanTable))
return &MySQLStore{DBXorm, true}
}
func CloseMySQLStore() {
err = DBXorm.Ping()
if err == nil {
err = DBXorm.Close()
}
}
func (s *MySQLStore) Collect(id SpanID, as ...Annotation) error {
content := make(map[string]string, 0)
//log.Printf("Collect id Trace:%v", id.Trace)
//log.Printf("Collect id Span:%v", id.Span)
//log.Printf("Collect id Parent:%v", id.Parent)
t := SpanTable{}
t.TraceKey = id.Trace.String()
t.SpanKey = id.Span.String()
t.ParentSpanKey = id.Parent.String()
if len(as) > 0 {
for _, v := range as {
if v.Key == "Name" {
t.SpanName = string(v.Value)
}
if v.Key == "ClientSend" {
t.Send, _ = time.Parse("2006-01-02T15:04:05.999999999-07:00", string(v.Value))
}
if v.Key == "ClientRecv" {
t.Recv, _ = time.Parse("2006-01-02T15:04:05.999999999-07:00", string(v.Value))
}
if v.Key == "Server.Send" {
t.Send, _ = time.Parse("2006-01-02T15:04:05.999999999-07:00", string(v.Value))
}
if v.Key == "Server.Recv" {
t.Recv, _ = time.Parse("2006-01-02T15:04:05.999999999-07:00", string(v.Value))
}
content[v.Key] = string(v.Value)
//log.Printf("Collect as key:%v", v.Key)
//log.Printf("Collect as value:%v", string(v.Value))
}
}
//log.Print("------------------------------")
//byteArr, _ := json.Marshal(content)
byteArr, _ := json.Marshal(as)
t.ClientSend = t.Send.UnixNano()
t.ClientRecv = t.Recv.UnixNano()
t.Content = string(byteArr)
//log.Printf("span----->:%v\n", t)
_, err = insertSpan(t)
return err
}
func insertSpan(t SpanTable) (r SpanTable, err error) {
var (
parent SpanTable
trace TraceTable
)
RETRY:
err = DBXorm.Ping()
if err != nil && err.Error() == "invalid connection" {
goto RETRY
}
_, err = DBXorm.Where("span_key=?", t.SpanKey).Get(&r)
if r.Id > 0 {
return
}
if t.ParentSpanKey == "0000000000000000" {
_, err = DBXorm.Where("span_key=?", t.SpanKey).Get(&parent)
} else {
_, err = DBXorm.Where("span_key=?", t.ParentSpanKey).Get(&parent)
}
_, err = DBXorm.Where("trace_key=?", t.TraceKey).Get(&trace)
if trace.Id == 0 {
trace.TraceName = t.SpanName
trace.TraceKey = t.TraceKey
trace.CostTime = t.Recv.UnixNano() - t.Send.UnixNano()
_, err = DBXorm.Insert(&trace)
} else {
if t.ParentSpanKey == "0000000000000000" {
trace.TraceName = t.SpanName
}
trace.CostTime = trace.CostTime + (t.Recv.UnixNano() - t.Send.UnixNano())
_, err = DBXorm.Where("trace_key=?", t.TraceKey).Update(&trace)
}
t.TraceId = trace.Id
if parent.Id == 0 {
t.CostTime = t.Recv.UnixNano() - t.Send.UnixNano()
_, err = DBXorm.Insert(&t)
r = t
} else if parent.Id > 0 {
t.ParentSpanID = parent.Id
_, err = DBXorm.Insert(&t)
r = t
}
sqlStr := `UPDATE %s AS t,(SELECT id FROM %s AS t2 WHERE t2.span_key=? AND t2.parent_span_key='0000000000000000') AS t2 SET t.parent_span_id=t2.id WHERE t.parent_span_key=?`
_, err = DBXorm.Exec(fmt.Sprintf(sqlStr, t.TableName(), t.TableName()), t.SpanKey, t.SpanKey)
return
}
func (s *MySQLStore) Traces(opts TracesOpts) ([]*Trace, error) {
return recursiveHandleSpan("0000000000000000")
}
func recursiveHandleSpan(rootKey string) (parent []*Trace, err error) {
RETRY:
err = DBXorm.Ping()
if err != nil && err.Error() == "invalid connection" {
goto RETRY
}
var spans = make([]*SpanTable, 0, 0)
err = DBXorm.Where("parent_span_key=?", rootKey).OrderBy("client_send DESC").Limit(20, 0).Find(&spans)
if err != nil && err != sql.ErrNoRows {
return
} else if err == sql.ErrNoRows {
err = nil
return
}
if len(spans) > 0 {
for _, v := range spans {
trace := new(Trace)
trace.Sub = make([]*Trace, 0, 0)
spanId := SpanID{}
spanId.Trace, _ = ParseID(v.TraceKey)
spanId.Span, _ = ParseID(v.SpanKey)
spanId.Parent, _ = ParseID(v.ParentSpanKey)
span := Span{ID: spanId}
err = json.Unmarshal([]byte(v.Content), &span.Annotations)
trace.Span = span
child, _ := recursiveHandleSpan(v.SpanKey)
if len(child) > 0 {
trace.Sub = child
}
parent = append(parent, trace)
}
}
return
}
func (s *MySQLStore) Trace(id ID) (parent *Trace, err error) {
//log.Printf("Trace id:%v\n", id)
var span SpanTable
var subs = make([]*Trace, 0, 0)
parent = new(Trace)
_, err = DBXorm.Where("trace_key=? AND parent_span_key=?", id.String(), "0000000000000000").Get(&span)
if err != nil && err != sql.ErrNoRows {
return
} else if err == sql.ErrNoRows {
err = nil
return
}
spanId := SpanID{}
spanId.Trace, _ = ParseID(span.TraceKey)
spanId.Span, _ = ParseID(span.SpanKey)
spanId.Parent, _ = ParseID(span.ParentSpanKey)
parent.Span = Span{ID: spanId}
err = json.Unmarshal([]byte(span.Content), &parent.Span.Annotations)
subs, _ = recursiveHandleSpan(span.SpanKey)
if len(subs) > 0 {
parent.Sub = subs
}
return
}