apm_exporter.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. package otlptrace
  2. import (
  3. "crypto/md5"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. "sync"
  11. "time"
  12. "go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform"
  13. tracesdk "go.opentelemetry.io/otel/sdk/trace"
  14. tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
  15. )
  16. const (
  17. ProtocolTrace int = 200
  18. ProtocolHTTP int = 1
  19. ProtocolPostgres int = 2
  20. ProtocolRedis int = 3
  21. ProtocolMemcached int = 4
  22. ProtocolMysql int = 5
  23. ProtocolMongo int = 6
  24. ProtocolKafka int = 7
  25. ProtocolCassandra int = 8
  26. ProtocolRabbitmq int = 9
  27. ProtocolNats int = 10
  28. ProtocolHTTP2 int = 11
  29. ProtocolDubbo2 int = 12
  30. ProtocolDNS int = 13
  31. ProtocolDM int = 14
  32. )
  33. const (
  34. APP_SERVICE_TYPE = "APPLICATION"
  35. SQL_SERVICE_TYPE = "SQL"
  36. NOSQL_SERVICE_TYPE = "NOSQL"
  37. HTTP_SERVICE_TYPE = "HTTP"
  38. )
  39. const (
  40. GO_SERVICE_NAME = "GO"
  41. MYSQL_SERVICE_NAME = "MYSQL"
  42. DM_SERVICE_NAME = "DM"
  43. REDIS_SERVICE_NAME = "REDIS"
  44. HTTP_SERVICE_NAME = "HTTPCLIENT"
  45. )
  46. type apmTraceSpan tracesdk.ReadOnlySpan
  47. // GO:0:10154813500555812:5450531005555981:5610250100539899:ee022542c3940f1b:1001025098564810:888ceb3df1bdbe2c:110
  48. type RootDataT struct {
  49. AccountId int `json:"account_id"`
  50. AgentId int64 `json:"agent_id"`
  51. AgentVersion string `json:"agent_version"`
  52. AppId int64 `json:"app_id"`
  53. AppIdFrom int64 `json:"app_id_from"` // from header app_id
  54. AppName string `json:"app_name"`
  55. CalledId int64 `json:"called_id"` // from header assumed_app_id
  56. ClientIp string `json:"client_ip"`
  57. CollTime uint64 `json:"coll_time"`
  58. Cpu int `json:"cpu"`
  59. Custom string `json:"custom"`
  60. HostId int64 `json:"host_id"`
  61. HostName string `json:"host_name"`
  62. HttpCode int64 `json:"http_code"`
  63. HttpMethod string `json:"http_method"`
  64. InstanceId int64 `json:"instance_id"`
  65. InstanceIdFrom int64 `json:"instance_id_from"` // from header instance_id
  66. LocalPort int64 `json:"local_port"`
  67. Maps []MapInfoT `json:"maps"`
  68. MemU int `json:"mem_u"`
  69. MemUP int `json:"mem_u_p"`
  70. OperType string `json:"oper_type"`
  71. Parameters []interface{} `json:"parameters"`
  72. ParentTaskName int `json:"parent_task_name"`
  73. Period int `json:"period"`
  74. RespTime uint64 `json:"resp_time"`
  75. Sampling int `json:"sampling"`
  76. ServiceName string `json:"service_name"`
  77. ServiceType string `json:"service_type"`
  78. Sip string `json:"sip"`
  79. Sn string `json:"sn"`
  80. SpanIdFrom string `json:"span_id_from"` // from header span_id
  81. Sport int64 `json:"sport"`
  82. TId int `json:"t_id"`
  83. TName string `json:"t_name"`
  84. TraceId string `json:"trace_id"` // from header trace_id
  85. TransIds []interface{} `json:"trans_ids"`
  86. TypeFrom string `json:"type_from"`
  87. Uri string `json:"uri"`
  88. UserDir int `json:"user_dir"`
  89. VipIds []interface{} `json:"vip_ids"`
  90. }
  91. type MapInfoT struct {
  92. Dbn string `json:"dbn,omitempty"`
  93. Exception int `json:"exception,omitempty"`
  94. ExceptionMsg string `json:"exception_msg,omitempty"`
  95. ExceptionStack string `json:"exception_stack,omitempty"`
  96. Ip string `json:"ip,omitempty"`
  97. Level int `json:"level"`
  98. MethodDesc string `json:"method_desc,omitempty"`
  99. MethodName string `json:"method_name"`
  100. Nid int `json:"nid"`
  101. OperType string `json:"oper_type,omitempty"`
  102. Pid int `json:"pid"`
  103. Port int64 `json:"port,omitempty"`
  104. Ps []string `json:"ps,omitempty"`
  105. PureTime uint64 `json:"pure_time"`
  106. ServiceName string `json:"service_name"`
  107. ServiceType string `json:"service_type"`
  108. StartTime uint64 `json:"start_time"`
  109. EndTime uint64 `json:"end_time"`
  110. WallTime uint64 `json:"wall_time"`
  111. Schema string `json:"schema,omitempty"`
  112. AssumedAppId int64 `json:"assumed_app_id,omitempty"`
  113. Uri string `json:"uri,omitempty"`
  114. SpanId string `json:"span_id,omitempty"`
  115. }
  116. type TraceMapT struct {
  117. RootData RootDataT
  118. Index int
  119. lock *sync.RWMutex
  120. TheEnd bool
  121. }
  122. var TraceRootMap map[string]*TraceMapT
  123. func init() {
  124. TraceRootMap = make(map[string]*TraceMapT)
  125. go func() {
  126. for {
  127. //fmt.Println(G_sdl)
  128. time.Sleep(5 * time.Second)
  129. }
  130. }()
  131. }
  132. var G_sdl int
  133. func tracetransformData(sdl []tracesdk.ReadOnlySpan) map[int][]RootDataT {
  134. G_sdl += len(sdl)
  135. if len(sdl) == 0 {
  136. return nil
  137. }
  138. // 多次请求 sdl
  139. sendDataMap := make(map[int][]RootDataT)
  140. //sendData := []RootDataT{}
  141. for _, sd := range sdl {
  142. if sd == nil {
  143. continue
  144. }
  145. //traceId := sd.SpanContext().TraceID().String()
  146. fmt.Println("------event_num---- "+sd.Name(), "--->", len(sd.Events())) // 一次请求完整数据
  147. // 构建map *RootDataT
  148. var rootData RootDataT
  149. // todo 应用注册逻辑
  150. if os.Getenv("JAVA") == "1" {
  151. rootData = initRootDataJava()
  152. } else {
  153. rootData = initRootDataFromEvent()
  154. }
  155. // build http入口 MapInfoT
  156. code_type := buildAppMapFromEvent(&rootData, sd)
  157. // 构建maps
  158. for _, event := range sd.Events() {
  159. aaa, _ := json.Marshal(event)
  160. fmt.Println("event.info", string(aaa))
  161. mNode := buildMapNodeFromEvent(event)
  162. switch event.EventType {
  163. // stack
  164. case 11:
  165. // l7 event
  166. case 10:
  167. switch event.ProtocolType {
  168. // http
  169. case ProtocolHTTP:
  170. buildHttpMapFromEvent(&mNode, event)
  171. // mysql
  172. case ProtocolMysql:
  173. buildMysqlMapEvent(&mNode, event)
  174. // redis
  175. case ProtocolRedis:
  176. buildRedisMapEvent(&mNode, event)
  177. // dm
  178. case ProtocolDM:
  179. buildDMMapEvent(&mNode, event)
  180. }
  181. }
  182. rootData.Maps = append(rootData.Maps, mNode)
  183. fmt.Println(event.Name)
  184. //buildAndAssemblyMapFromEvent(event, rootData)
  185. }
  186. buildLevelFromEvent(&rootData)
  187. sendDataMap[code_type] = append(sendDataMap[code_type], rootData)
  188. //a, _ := json.Marshal(rootData)
  189. //fmt.Println(string(a))
  190. //sendData = append(sendData, rootData)
  191. //if _, ok := TraceRootMap[traceId]; !ok {
  192. //TraceRootMap[traceId] = &TraceMapT{RootData: initRootData(traceId), Index: 1}
  193. //}
  194. //TraceRootMap[traceId].Index++
  195. //buildAndAssemblyMap(sd, TraceRootMap[traceId])
  196. }
  197. // 发送完整数据 | 大量长耗时请求会增加内存占用
  198. //sendData := []RootDataT{}
  199. //for traceId, v := range TraceRootMap {
  200. // if v.TheEnd {
  201. // buildLevel(v)
  202. // sendData = append(sendData, v.RootData)
  203. // delete(TraceRootMap, traceId)
  204. // //fmt.Println("the end!")
  205. // } else {
  206. // //fmt.Println("not end!")
  207. // }
  208. //}
  209. //Transform the categorized map into a slice
  210. aa, _ := json.Marshal(sendDataMap)
  211. fmt.Println(string(aa))
  212. //fmt.Println(len(sendData))
  213. //fmt.Println("sdl len:", len(sdl))
  214. return sendDataMap
  215. }
  216. type TimeMap struct {
  217. Time uint64
  218. Type int
  219. Map *MapInfoT
  220. }
  221. //type TraceMapT struct {
  222. // RootData RootDataT
  223. // Index int
  224. // lock *sync.RWMutex
  225. // TheEnd bool
  226. //}
  227. func buildLevel(sdl *TraceMapT) {
  228. nidMap := make(map[int]*MapInfoT)
  229. mapSlice := []TimeMap{}
  230. for i, v := range sdl.RootData.Maps {
  231. if v.ServiceType == "APPLICATION" {
  232. continue
  233. }
  234. nidMap[v.Nid] = &sdl.RootData.Maps[i]
  235. timeStartMap := TimeMap{
  236. Time: v.StartTime,
  237. Type: 0,
  238. Map: &sdl.RootData.Maps[i],
  239. }
  240. mapSlice = append(mapSlice, timeStartMap)
  241. timeEndMap := TimeMap{
  242. Time: v.EndTime,
  243. Type: 1,
  244. Map: &sdl.RootData.Maps[i],
  245. }
  246. mapSlice = append(mapSlice, timeEndMap)
  247. }
  248. sort.Slice(mapSlice, func(i, j int) bool {
  249. return mapSlice[i].Time < mapSlice[j].Time
  250. })
  251. funStack := []TimeMap{}
  252. currentNid := 1
  253. Nid := 2
  254. level := 2
  255. for _, v := range mapSlice {
  256. // fmt.Println("SliceSliceindex", k, "value", v.Time, v.Type, v.Map.MethodName, v.Map.Nid)
  257. if v.Type == 0 {
  258. // 函数入口
  259. funStack = append(funStack, v)
  260. v.Map.Pid = currentNid
  261. v.Map.Level = level
  262. v.Map.Nid = Nid
  263. currentNid = Nid
  264. level += 1
  265. Nid += 1
  266. } else if v.Type == 1 {
  267. // 函数出口
  268. len := len(funStack)
  269. funStack = funStack[:len-1]
  270. if (len - 2) < 0 {
  271. currentNid = 1
  272. } else {
  273. currentNid = funStack[len-2].Map.Nid
  274. }
  275. level -= 1
  276. }
  277. }
  278. }
  279. func buildLevelFromEvent(sdl *RootDataT) {
  280. nidMap := make(map[int]*MapInfoT)
  281. mapSlice := []TimeMap{}
  282. for i, v := range sdl.Maps {
  283. if v.ServiceType == "APPLICATION" {
  284. continue
  285. }
  286. nidMap[v.Nid] = &sdl.Maps[i]
  287. timeStartMap := TimeMap{
  288. Time: v.StartTime,
  289. Type: 0,
  290. Map: &sdl.Maps[i],
  291. }
  292. mapSlice = append(mapSlice, timeStartMap)
  293. timeEndMap := TimeMap{
  294. Time: v.EndTime,
  295. Type: 1,
  296. Map: &sdl.Maps[i],
  297. }
  298. mapSlice = append(mapSlice, timeEndMap)
  299. }
  300. sort.Slice(mapSlice, func(i, j int) bool {
  301. return mapSlice[i].Time < mapSlice[j].Time
  302. })
  303. funStack := []TimeMap{}
  304. currentNid := 1
  305. Nid := 2
  306. level := 2
  307. for k, v := range mapSlice {
  308. fmt.Println("SliceSliceindex", k, "value", v.Time, v.Type, v.Map.MethodName, v.Map.Nid)
  309. if v.Type == 0 {
  310. // 函数入口
  311. funStack = append(funStack, v)
  312. v.Map.Pid = currentNid
  313. v.Map.Level = level
  314. v.Map.Nid = Nid
  315. currentNid = Nid
  316. level += 1
  317. Nid += 1
  318. } else if v.Type == 1 {
  319. // 函数出口
  320. len := len(funStack)
  321. funStack = funStack[:len-1]
  322. if (len - 2) < 0 {
  323. currentNid = 1
  324. } else {
  325. currentNid = funStack[len-2].Map.Nid
  326. }
  327. level -= 1
  328. }
  329. }
  330. }
  331. func initRootData(traceId string) RootDataT {
  332. data := RootDataT{
  333. AccountId: 110,
  334. AgentId: 1011005252979954, // TODO 更新 基于 ip:port + process_name + exe路径生成
  335. AgentVersion: "2.1.0",
  336. AppId: 5410049101545798, // TODO 更新 基于appname生成
  337. AppIdFrom: -1,
  338. AppName: "eBPF-agent", // TODO 更新 ip:port || process_name
  339. CalledId: -1,
  340. ClientIp: "",
  341. CollTime: 0,
  342. Cpu: 0,
  343. Custom: "",
  344. HostId: 10154813500555812,
  345. HostName: "localhost",
  346. HttpCode: 0,
  347. HttpMethod: "",
  348. InstanceId: 1005051101515357, // TODO 更新 基于ip:port
  349. InstanceIdFrom: -1,
  350. Maps: []MapInfoT{},
  351. MemU: 0,
  352. MemUP: 0,
  353. OperType: "",
  354. Parameters: []interface{}{},
  355. ParentTaskName: 0,
  356. Period: -1,
  357. RespTime: 0,
  358. Sampling: 0,
  359. ServiceName: "GO",
  360. ServiceType: APP_SERVICE_TYPE,
  361. Sip: "",
  362. Sn: "",
  363. SpanIdFrom: "",
  364. Sport: 0,
  365. TId: -1,
  366. TName: "",
  367. TraceId: traceId,
  368. TransIds: []interface{}{},
  369. TypeFrom: "",
  370. Uri: "",
  371. UserDir: 0,
  372. VipIds: []interface{}{},
  373. }
  374. return data
  375. }
  376. func initRootDataFromEvent() RootDataT {
  377. data := RootDataT{
  378. AccountId: 110,
  379. AgentId: 1011005252979954, // TODO 更新 基于 ip:port + process_name + exe路径生成
  380. AgentVersion: "2.1.0",
  381. AppId: 5410049101545798, // TODO 更新 基于appname生成
  382. AppIdFrom: -1,
  383. AppName: "eBPF-agent", // TODO 更新 ip:port || process_name
  384. CalledId: -1,
  385. ClientIp: "",
  386. CollTime: 0,
  387. Cpu: 0,
  388. Custom: "",
  389. HostId: 10154813500555812,
  390. HostName: "localhost",
  391. HttpCode: 0,
  392. HttpMethod: "",
  393. InstanceId: 1005051101515357, // TODO 更新 基于ip:port
  394. InstanceIdFrom: -1,
  395. Maps: []MapInfoT{},
  396. MemU: 0,
  397. MemUP: 0,
  398. OperType: "",
  399. Parameters: []interface{}{},
  400. ParentTaskName: 0,
  401. Period: -1,
  402. RespTime: 0,
  403. Sampling: 0,
  404. ServiceName: "GO",
  405. ServiceType: APP_SERVICE_TYPE,
  406. Sip: "",
  407. Sn: "",
  408. SpanIdFrom: "",
  409. Sport: 0,
  410. TId: -1,
  411. TName: "",
  412. TraceId: "",
  413. TransIds: []interface{}{},
  414. TypeFrom: "",
  415. Uri: "",
  416. UserDir: 0,
  417. VipIds: []interface{}{},
  418. }
  419. return data
  420. }
  421. func initRootDataJava() RootDataT {
  422. data := RootDataT{
  423. AccountId: 110,
  424. AgentId: 3934815089541000, // TODO 更新 基于 ip:port + process_name + exe路径生成
  425. AgentVersion: "2.21.0",
  426. AppId: 3365853273187618, // TODO 更新 基于appname生成
  427. AppIdFrom: -1,
  428. AppName: "eBPF-javaApplication", // TODO 更新 ip:port || process_name
  429. CalledId: -1,
  430. ClientIp: "",
  431. CollTime: 0,
  432. Cpu: 0,
  433. Custom: "",
  434. HostId: 2315065183171055,
  435. HostName: "localhost",
  436. HttpCode: 0,
  437. HttpMethod: "",
  438. InstanceId: 1128864082033413, // TODO 更新 基于ip:port
  439. InstanceIdFrom: -1,
  440. Maps: []MapInfoT{},
  441. MemU: 0,
  442. MemUP: 0,
  443. OperType: "",
  444. Parameters: []interface{}{},
  445. ParentTaskName: 0,
  446. Period: -1,
  447. RespTime: 0,
  448. Sampling: 0,
  449. ServiceName: "TOMCAT",
  450. ServiceType: APP_SERVICE_TYPE,
  451. Sip: "",
  452. Sn: "",
  453. SpanIdFrom: "",
  454. Sport: 0,
  455. TId: -1,
  456. TName: "",
  457. TraceId: "",
  458. TransIds: []interface{}{},
  459. TypeFrom: "",
  460. Uri: "",
  461. UserDir: 0,
  462. VipIds: []interface{}{},
  463. }
  464. return data
  465. }
  466. func initMapNode(spanSd *tracepb.Span) (MapInfoT, string) {
  467. mNode := MapInfoT{
  468. Exception: 0,
  469. ExceptionMsg: "",
  470. ExceptionStack: "",
  471. Ip: "",
  472. Level: 2,
  473. Pid: 1,
  474. Port: 0,
  475. Ps: []string{},
  476. ServiceName: "",
  477. ServiceType: "",
  478. WallTime: 0,
  479. }
  480. mNode.MethodName = spanSd.Name
  481. mNode.PureTime = (spanSd.EndTimeUnixNano - spanSd.StartTimeUnixNano) / 1e3
  482. mNode.WallTime = mNode.PureTime
  483. mNode.StartTime = spanSd.StartTimeUnixNano
  484. mNode.EndTime = spanSd.EndTimeUnixNano
  485. for _, attr := range spanSd.GetAttributes() {
  486. fmt.Println(attr.Key, ":", attr.Value.GetValue())
  487. switch attr.Key {
  488. case "nid":
  489. mNode.Nid = int(attr.Value.GetIntValue())
  490. case "pid":
  491. mNode.Pid = int(attr.Value.GetIntValue())
  492. case "level":
  493. mNode.Level = int(attr.Value.GetIntValue())
  494. }
  495. }
  496. return mNode, spanSd.Name
  497. }
  498. func buildMapNodeFromEvent(event tracesdk.Event) MapInfoT {
  499. mNode := MapInfoT{
  500. Exception: 0,
  501. ExceptionMsg: "",
  502. ExceptionStack: "",
  503. Ip: "",
  504. Level: 2,
  505. Pid: 1,
  506. Port: 0,
  507. Ps: []string{},
  508. ServiceName: "",
  509. ServiceType: "",
  510. WallTime: 0,
  511. }
  512. mNode.MethodName = event.Name
  513. //mNode.PureTime = (event.EndTimeUnixNano - event.StartTimeUnixNano) / 1e3
  514. //mNode.WallTime = mNode.PureTime
  515. //mNode.StartTime = spanSd.StartTimeUnixNano
  516. //mNode.EndTime = spanSd.EndTimeUnixNano
  517. for _, attr := range event.Attributes {
  518. fmt.Println(event.Name, "--->buildMapNodeFromEvent--->", attr.Key, ":", attr.Value.AsInterface())
  519. switch attr.Key {
  520. case "nid":
  521. mNode.Nid = int(attr.Value.AsInt64())
  522. case "pid":
  523. mNode.Pid = int(attr.Value.AsInt64())
  524. case "level":
  525. mNode.Level = int(attr.Value.AsInt64())
  526. case "time.start_at":
  527. mNode.StartTime = uint64(attr.Value.AsInt64())
  528. case "time.end_at":
  529. mNode.EndTime = uint64(attr.Value.AsInt64())
  530. case "time.duration":
  531. //mNode.PureTime = uint64(attr.Value.AsInt64()) / 1e3
  532. mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
  533. }
  534. }
  535. return mNode
  536. }
  537. // 构建拼装
  538. func buildAndAssemblyMap(sd apmTraceSpan, traceRoot *TraceMapT) MapInfoT {
  539. mNode, mapType := initMapNode(span(sd))
  540. switch mapType {
  541. case "APPLICATION":
  542. buildAppMap(&mNode, traceRoot, sd)
  543. traceRoot.TheEnd = true
  544. case "HTTP":
  545. buildHttpMap(&mNode, sd)
  546. case "Mysql":
  547. buildMysqlMap(&mNode, sd)
  548. case "Redis":
  549. buildRedisMap(&mNode, sd)
  550. }
  551. if mapType != "" {
  552. mNode.Nid = traceRoot.Index
  553. traceRoot.RootData.Maps = append(traceRoot.RootData.Maps, mNode)
  554. }
  555. return mNode
  556. }
  557. //func buildAndAssemblyMapFromEvent(event tracesdk.Event, traceRoot *RootDataT) MapInfoT {
  558. // mNode := buildMapNodeFromEvent(event)
  559. // switch mapType {
  560. // case "HTTP":
  561. // buildHttpMapFromEvent(mNode, event)
  562. // //case "Mysql":
  563. // // buildMysqlMap(mNode, sd)
  564. // //case "Redis":
  565. // // buildRedisMap(mNode, sd)
  566. // }
  567. // if mapType != "" {
  568. // //mNode.Nid = traceRoot.Index
  569. // traceRoot.Maps = append(traceRoot.Maps, mNode)
  570. // }
  571. // return mNode
  572. //}
  573. func buildAppMap(mNode *MapInfoT, traceRoot *TraceMapT, sd apmTraceSpan) {
  574. mNode.ServiceName = GO_SERVICE_NAME
  575. mNode.ServiceType = APP_SERVICE_TYPE
  576. mNode.MethodName = "net/http.(*Transport).roundTrip()"
  577. mNode.Level = 1
  578. mNode.Pid = 0
  579. mNode.Nid = 1
  580. // 构建root节点
  581. traceRoot.RootData.RespTime = mNode.PureTime
  582. traceRoot.RootData.CollTime = mNode.StartTime
  583. traceRoot.Index = 1
  584. for _, attr := range sd.Attributes() {
  585. fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  586. switch attr.Key {
  587. case "http.uri":
  588. traceRoot.RootData.Uri = attr.Value.AsString()
  589. case "http.method":
  590. traceRoot.RootData.HttpMethod = attr.Value.AsString()
  591. case "http.status_code":
  592. traceRoot.RootData.HttpCode = attr.Value.AsInt64()
  593. case "net.peer.name":
  594. traceRoot.RootData.ClientIp = attr.Value.AsString()
  595. traceRoot.RootData.Sip = attr.Value.AsString()
  596. traceRoot.RootData.Sn = attr.Value.AsString()
  597. case "net.peer.port":
  598. traceRoot.RootData.Sport = attr.Value.AsInt64()
  599. traceRoot.RootData.LocalPort = attr.Value.AsInt64()
  600. case "server.trace_id_from":
  601. traceRoot.RootData.TraceId = attr.Value.AsString()
  602. case "server.called_id":
  603. traceRoot.RootData.CalledId = attr.Value.AsInt64()
  604. case "server.instance_id_from":
  605. traceRoot.RootData.InstanceIdFrom = attr.Value.AsInt64()
  606. case "server.app_id_from":
  607. traceRoot.RootData.AppIdFrom = attr.Value.AsInt64()
  608. case "server.span_id_from":
  609. traceRoot.RootData.SpanIdFrom = attr.Value.AsString()
  610. case "server.type_from":
  611. traceRoot.RootData.TypeFrom = attr.Value.AsString()
  612. }
  613. }
  614. }
  615. func buildAppMapFromEvent(traceRoot *RootDataT, sd apmTraceSpan) int {
  616. mNode := MapInfoT{
  617. Exception: 0,
  618. ExceptionMsg: "",
  619. ExceptionStack: "",
  620. Ip: "",
  621. Level: 1,
  622. Pid: 1,
  623. Port: 0,
  624. Ps: []string{},
  625. ServiceName: "",
  626. ServiceType: "",
  627. WallTime: 0,
  628. }
  629. mNode.ServiceName = GO_SERVICE_NAME
  630. mNode.ServiceType = APP_SERVICE_TYPE
  631. mNode.MethodName = "Kernel Endpoint()"
  632. mNode.Level = 1
  633. mNode.Pid = 0
  634. mNode.Nid = 1
  635. var code_type int64
  636. // 构建root节点
  637. //traceRoot.RespTime = mNode.PureTimex
  638. //traceRoot.CollTime = mNode.StartTime
  639. for _, attr := range sd.Attributes() {
  640. fmt.Println("Appmap:", attr.Key, ":", attr.Value.AsInterface())
  641. switch attr.Key {
  642. case "http.uri":
  643. traceRoot.Uri = attr.Value.AsString()
  644. case "http.method":
  645. traceRoot.HttpMethod = attr.Value.AsString()
  646. case "http.status_code":
  647. traceRoot.HttpCode = attr.Value.AsInt64()
  648. case "net.peer.name":
  649. traceRoot.ClientIp = attr.Value.AsString()
  650. traceRoot.Sip = attr.Value.AsString()
  651. traceRoot.Sn = attr.Value.AsString()
  652. case "net.peer.port":
  653. traceRoot.Sport = attr.Value.AsInt64()
  654. traceRoot.LocalPort = attr.Value.AsInt64()
  655. case "server.trace_id_from":
  656. traceRoot.TraceId = attr.Value.AsString()
  657. case "server.called_id":
  658. traceRoot.CalledId = attr.Value.AsInt64()
  659. case "server.instance_id_from":
  660. traceRoot.InstanceIdFrom = attr.Value.AsInt64()
  661. case "server.app_id_from":
  662. traceRoot.AppIdFrom = attr.Value.AsInt64()
  663. case "server.span_id_from":
  664. traceRoot.SpanIdFrom = attr.Value.AsString()
  665. case "server.type_from":
  666. traceRoot.TypeFrom = attr.Value.AsString()
  667. case "time.start_at":
  668. traceRoot.CollTime = uint64(attr.Value.AsInt64())
  669. mNode.StartTime = traceRoot.CollTime
  670. case "time.end_at":
  671. mNode.EndTime = uint64(attr.Value.AsInt64())
  672. case "time.duration":
  673. traceRoot.RespTime = uint64(attr.Value.AsInt64()) / 1e3
  674. mNode.PureTime = traceRoot.RespTime
  675. mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
  676. case "server.code_type":
  677. code_type = attr.Value.AsInt64()
  678. }
  679. }
  680. traceRoot.Maps = append(traceRoot.Maps, mNode)
  681. return int(code_type)
  682. }
  683. func buildHttpMap(mNode *MapInfoT, sd apmTraceSpan) {
  684. mNode.ServiceName = HTTP_SERVICE_NAME
  685. mNode.ServiceType = HTTP_SERVICE_TYPE
  686. mNode.Schema = "http"
  687. mNode.MethodName = "net/http.serverHandler.ServeHTTP()"
  688. var descAddr string
  689. for _, attr := range sd.Attributes() {
  690. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  691. switch attr.Key {
  692. case "http.ip":
  693. mNode.Ip = attr.Value.AsString()
  694. descAddr += mNode.Ip
  695. case "http.port":
  696. mNode.Port = attr.Value.AsInt64()
  697. descAddr += ":" + attr.Value.AsString()
  698. case "http.uri":
  699. mNode.Uri = attr.Value.AsString()
  700. case "http.assumed_app_id":
  701. mNode.AssumedAppId = attr.Value.AsInt64()
  702. case "http.span_id":
  703. mNode.SpanId = attr.Value.AsString()
  704. }
  705. }
  706. //mNode.AssumedAppId = Md5ToInt64(descAddr, 16)
  707. }
  708. func buildHttpMapFromEvent(mNode *MapInfoT, event tracesdk.Event) {
  709. mNode.ServiceName = HTTP_SERVICE_NAME
  710. mNode.ServiceType = HTTP_SERVICE_TYPE
  711. mNode.Schema = "http"
  712. mNode.MethodName = "HTTP"
  713. //var descAddr string
  714. for _, attr := range event.Attributes {
  715. fmt.Println("HTTP--->", attr.Key, ":", attr.Value.AsInterface())
  716. switch attr.Key {
  717. case "http.ip":
  718. mNode.Ip = attr.Value.AsString()
  719. //descAddr += mNode.Ip
  720. case "http.port":
  721. mNode.Port = attr.Value.AsInt64()
  722. case "http.method":
  723. mNode.MethodName += " " + attr.Value.AsString()
  724. //descAddr += ":" + attr.Value.AsString()
  725. case "http.uri":
  726. mNode.Uri = attr.Value.AsString()
  727. mNode.MethodName += " " + attr.Value.AsString()
  728. case "http.assumed_app_id":
  729. mNode.AssumedAppId = attr.Value.AsInt64()
  730. case "http.span_id":
  731. mNode.SpanId = attr.Value.AsString()
  732. case "time.start_at":
  733. mNode.StartTime = uint64(attr.Value.AsInt64())
  734. case "time.end_at":
  735. mNode.EndTime = uint64(attr.Value.AsInt64())
  736. case "time.duration":
  737. //mNode.PureTime = uint64(attr.Value.AsInt64()) / 1e3
  738. mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
  739. }
  740. }
  741. //mNode.AssumedAppId = Md5ToInt64(descAddr, 16)
  742. }
  743. func buildMysqlMap(mNode *MapInfoT, sd apmTraceSpan) {
  744. mNode.Dbn = "unknown"
  745. mNode.ServiceName = MYSQL_SERVICE_NAME
  746. mNode.ServiceType = SQL_SERVICE_TYPE
  747. mNode.MethodName = "database/sql.Query()"
  748. for _, attr := range sd.Attributes() {
  749. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  750. switch attr.Key {
  751. case "net.peer.name":
  752. mNode.Ip = attr.Value.AsString()
  753. case "net.peer.port":
  754. mNode.Port = attr.Value.AsInt64()
  755. case "db.statement":
  756. query := attr.Value.AsString()
  757. mNode.Ps = []string{query}
  758. words := strings.Fields(query)
  759. if len(words) > 0 {
  760. mNode.OperType = strings.ToUpper(words[0])
  761. }
  762. }
  763. }
  764. }
  765. func buildMysqlMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  766. mNode.Dbn = "unknown"
  767. mNode.ServiceName = MYSQL_SERVICE_NAME
  768. mNode.ServiceType = SQL_SERVICE_TYPE
  769. mNode.MethodName = "database/sql.Query()"
  770. for _, attr := range event.Attributes {
  771. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  772. switch attr.Key {
  773. case "net.peer.name":
  774. mNode.Ip = attr.Value.AsString()
  775. case "net.peer.port":
  776. mNode.Port = attr.Value.AsInt64()
  777. case "db.statement":
  778. query := attr.Value.AsString()
  779. mNode.Ps = []string{query}
  780. words := strings.Fields(query)
  781. if len(words) > 0 {
  782. mNode.OperType = strings.ToUpper(words[0])
  783. }
  784. }
  785. }
  786. }
  787. func buildDMMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  788. mNode.Dbn = "TEST"
  789. mNode.ServiceName = DM_SERVICE_NAME
  790. mNode.ServiceType = SQL_SERVICE_TYPE
  791. mNode.MethodName = "database/sql.Query()"
  792. for _, attr := range event.Attributes {
  793. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  794. switch attr.Key {
  795. case "net.peer.name":
  796. mNode.Ip = attr.Value.AsString()
  797. case "net.peer.port":
  798. mNode.Port = attr.Value.AsInt64()
  799. case "db.statement":
  800. query := attr.Value.AsString()
  801. mNode.Ps = []string{query}
  802. words := strings.Fields(query)
  803. if len(words) > 0 {
  804. mNode.OperType = strings.ToUpper(words[0])
  805. }
  806. case "sql.exception":
  807. if attr.Value.AsBool() {
  808. mNode.Exception = 1
  809. } else {
  810. mNode.Exception = 0
  811. }
  812. }
  813. }
  814. }
  815. func buildRedisMap(mNode *MapInfoT, sd apmTraceSpan) {
  816. mNode.ServiceName = REDIS_SERVICE_NAME
  817. mNode.ServiceType = NOSQL_SERVICE_TYPE
  818. //mNode.MethodName = span(sd).Name + " query"
  819. mNode.MethodName = "redis.Do()"
  820. for _, attr := range sd.Attributes() {
  821. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  822. switch attr.Key {
  823. case "net.peer.name":
  824. mNode.Ip = attr.Value.AsString()
  825. case "net.peer.port":
  826. mNode.Port = attr.Value.AsInt64()
  827. case "db.statement":
  828. query := attr.Value.AsString()
  829. mNode.Ps = []string{query}
  830. words := strings.Fields(query)
  831. if len(words) > 0 {
  832. mNode.OperType = strings.ToUpper(words[0])
  833. }
  834. }
  835. }
  836. }
  837. func buildRedisMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  838. mNode.ServiceName = REDIS_SERVICE_NAME
  839. mNode.ServiceType = NOSQL_SERVICE_TYPE
  840. //mNode.MethodName = span(sd).Name + " query"
  841. mNode.MethodName = "redis.Do()"
  842. for _, attr := range event.Attributes {
  843. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  844. switch attr.Key {
  845. case "net.peer.name":
  846. mNode.Ip = attr.Value.AsString()
  847. case "net.peer.port":
  848. mNode.Port = attr.Value.AsInt64()
  849. case "db.statement":
  850. query := attr.Value.AsString()
  851. mNode.Ps = []string{query}
  852. words := strings.Fields(query)
  853. if len(words) > 0 {
  854. mNode.OperType = strings.ToUpper(words[0])
  855. }
  856. }
  857. }
  858. }
  859. func isEnter(_type string) bool {
  860. if _type == "APPLICATION" {
  861. return true
  862. }
  863. return false
  864. }
  865. func span(sd apmTraceSpan) *tracepb.Span {
  866. if sd == nil {
  867. return nil
  868. }
  869. tid := sd.SpanContext().TraceID()
  870. sid := sd.SpanContext().SpanID()
  871. s := &tracepb.Span{
  872. TraceId: tid[:],
  873. SpanId: sid[:],
  874. TraceState: sd.SpanContext().TraceState().String(),
  875. //Status: status(sd.Status().Code, sd.Status().Description),
  876. StartTimeUnixNano: uint64(sd.StartTime().UnixNano()),
  877. EndTimeUnixNano: uint64(sd.EndTime().UnixNano()),
  878. //Links: links(sd.Links()),
  879. //Kind: spanKind(sd.SpanKind()),
  880. Name: sd.Name(),
  881. Attributes: tracetransform.KeyValues(sd.Attributes()),
  882. //Events: spanEvents(sd.Events()),
  883. DroppedAttributesCount: uint32(sd.DroppedAttributes()),
  884. DroppedEventsCount: uint32(sd.DroppedEvents()),
  885. DroppedLinksCount: uint32(sd.DroppedLinks()),
  886. }
  887. if psid := sd.Parent().SpanID(); psid.IsValid() {
  888. s.ParentSpanId = psid[:]
  889. }
  890. return s
  891. }
  892. func Md5ToInt64(strParam string, Len int) int64 {
  893. sign := md5.Sum([]byte(strParam))
  894. signStr := fmt.Sprintf("%x", sign)
  895. charArr := []rune(signStr)
  896. var intStr string
  897. for _, value := range charArr {
  898. intStr += strconv.Itoa(int(value))
  899. }
  900. intStr = intStr[:Len]
  901. int64Data, err := strconv.ParseInt(intStr, 10, 64)
  902. if err != nil {
  903. return 0
  904. }
  905. return int64Data
  906. }