apm_exporter.go 26 KB

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