apm_exporter.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. // todo AccountId
  353. AccountId: 110,
  354. AgentId: 0, // 基于 ip:port + process_name + exe路径生成
  355. AgentVersion: "2.1.0",
  356. AppId: 0, // 基于appname生成
  357. AppIdFrom: -1,
  358. AppName: "eBPF-agent", // server配置
  359. CalledId: -1,
  360. ClientIp: "",
  361. CollTime: 0,
  362. Cpu: 0,
  363. Custom: "",
  364. HostId: hostID,
  365. HostName: "localhost",
  366. HttpCode: 0,
  367. HttpMethod: "",
  368. InstanceId: 0, // 基于ip:port
  369. InstanceIdFrom: -1,
  370. Maps: []MapInfoT{},
  371. MemU: 0,
  372. MemUP: 0,
  373. OperType: "",
  374. Parameters: []interface{}{},
  375. ParentTaskName: 0,
  376. Period: -1,
  377. RespTime: 0,
  378. Sampling: 0,
  379. ServiceName: "",
  380. ServiceType: APP_SERVICE_TYPE,
  381. Sip: "",
  382. Sn: "",
  383. SpanIdFrom: "",
  384. Sport: 0,
  385. TId: -1,
  386. TName: "",
  387. TraceId: "",
  388. TransIds: []interface{}{},
  389. TypeFrom: "",
  390. Uri: "",
  391. UserDir: 0,
  392. VipIds: []interface{}{},
  393. }
  394. return data
  395. }
  396. func initRootDataJava() RootDataT {
  397. data := RootDataT{
  398. AccountId: 110,
  399. AgentId: 3934815089541000, // TODO 更新 基于 ip:port + process_name + exe路径生成
  400. AgentVersion: "2.21.0",
  401. AppId: 3365853273187618, // TODO 更新 基于appname生成
  402. AppIdFrom: -1,
  403. AppName: "eBPF-javaApplication", // TODO 更新 ip:port || process_name
  404. CalledId: -1,
  405. ClientIp: "",
  406. CollTime: 0,
  407. Cpu: 0,
  408. Custom: "",
  409. HostId: 2315065183171055,
  410. HostName: "localhost",
  411. HttpCode: 0,
  412. HttpMethod: "",
  413. InstanceId: 1128864082033413, // TODO 更新 基于ip:port
  414. InstanceIdFrom: -1,
  415. Maps: []MapInfoT{},
  416. MemU: 0,
  417. MemUP: 0,
  418. OperType: "",
  419. Parameters: []interface{}{},
  420. ParentTaskName: 0,
  421. Period: -1,
  422. RespTime: 0,
  423. Sampling: 0,
  424. ServiceName: "TOMCAT",
  425. ServiceType: APP_SERVICE_TYPE,
  426. Sip: "",
  427. Sn: "",
  428. SpanIdFrom: "",
  429. Sport: 0,
  430. TId: -1,
  431. TName: "",
  432. TraceId: "",
  433. TransIds: []interface{}{},
  434. TypeFrom: "",
  435. Uri: "",
  436. UserDir: 0,
  437. VipIds: []interface{}{},
  438. }
  439. return data
  440. }
  441. func initMapNode(spanSd *tracepb.Span) (MapInfoT, string) {
  442. mNode := MapInfoT{
  443. Exception: 0,
  444. ExceptionMsg: "",
  445. ExceptionStack: "",
  446. Ip: "",
  447. Level: 2,
  448. Pid: 1,
  449. Port: 0,
  450. Ps: []string{},
  451. ServiceName: "",
  452. ServiceType: "",
  453. WallTime: 0,
  454. }
  455. mNode.MethodName = spanSd.Name
  456. mNode.PureTime = (spanSd.EndTimeUnixNano - spanSd.StartTimeUnixNano) / 1e3
  457. mNode.WallTime = mNode.PureTime
  458. mNode.StartTime = spanSd.StartTimeUnixNano
  459. mNode.EndTime = spanSd.EndTimeUnixNano
  460. for _, attr := range spanSd.GetAttributes() {
  461. fmt.Println(attr.Key, ":", attr.Value.GetValue())
  462. switch attr.Key {
  463. case "nid":
  464. mNode.Nid = int(attr.Value.GetIntValue())
  465. case "pid":
  466. mNode.Pid = int(attr.Value.GetIntValue())
  467. case "level":
  468. mNode.Level = int(attr.Value.GetIntValue())
  469. }
  470. }
  471. return mNode, spanSd.Name
  472. }
  473. func buildMapNodeFromEvent(event tracesdk.Event) MapInfoT {
  474. mNode := MapInfoT{
  475. Exception: 0,
  476. ExceptionMsg: "",
  477. ExceptionStack: "",
  478. Ip: "",
  479. Level: 2,
  480. Pid: 1,
  481. Port: 0,
  482. Ps: []string{},
  483. ServiceName: "",
  484. ServiceType: "",
  485. WallTime: 0,
  486. }
  487. mNode.MethodName = event.Name
  488. //mNode.PureTime = (event.EndTimeUnixNano - event.StartTimeUnixNano) / 1e3
  489. //mNode.WallTime = mNode.PureTime
  490. //mNode.StartTime = spanSd.StartTimeUnixNano
  491. //mNode.EndTime = spanSd.EndTimeUnixNano
  492. for _, attr := range event.Attributes {
  493. fmt.Println(event.Name, "--->buildMapNodeFromEvent--->", attr.Key, ":", attr.Value.AsInterface())
  494. switch attr.Key {
  495. case "nid":
  496. mNode.Nid = int(attr.Value.AsInt64())
  497. case "pid":
  498. mNode.Pid = int(attr.Value.AsInt64())
  499. case "level":
  500. mNode.Level = int(attr.Value.AsInt64())
  501. case "time.start_at":
  502. mNode.StartTime = uint64(attr.Value.AsInt64())
  503. case "time.end_at":
  504. mNode.EndTime = uint64(attr.Value.AsInt64())
  505. case "time.duration":
  506. //mNode.PureTime = uint64(attr.Value.AsInt64()) / 1e3
  507. mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
  508. }
  509. }
  510. return mNode
  511. }
  512. // 构建拼装
  513. //func buildAndAssemblyMap(sd apmTraceSpan, traceRoot *TraceMapT) MapInfoT {
  514. // mNode, mapType := initMapNode(span(sd))
  515. // switch mapType {
  516. // case "APPLICATION":
  517. // buildAppMap(&mNode, traceRoot, sd)
  518. // traceRoot.TheEnd = true
  519. // case "HTTP":
  520. // buildHttpMap(&mNode, sd)
  521. // case "Mysql":
  522. // buildMysqlMap(&mNode, sd)
  523. // case "Redis":
  524. // buildRedisMap(&mNode, sd)
  525. // }
  526. // if mapType != "" {
  527. // mNode.Nid = traceRoot.Index
  528. // traceRoot.RootData.Maps = append(traceRoot.RootData.Maps, mNode)
  529. // }
  530. // return mNode
  531. //}
  532. //func buildAndAssemblyMapFromEvent(event tracesdk.Event, traceRoot *RootDataT) MapInfoT {
  533. // mNode := buildMapNodeFromEvent(event)
  534. // switch mapType {
  535. // case "HTTP":
  536. // buildHttpMapFromEvent(mNode, event)
  537. // //case "Mysql":
  538. // // buildMysqlMap(mNode, sd)
  539. // //case "Redis":
  540. // // buildRedisMap(mNode, sd)
  541. // }
  542. // if mapType != "" {
  543. // //mNode.Nid = traceRoot.Index
  544. // traceRoot.Maps = append(traceRoot.Maps, mNode)
  545. // }
  546. // return mNode
  547. //}
  548. //func buildAppMap(mNode *MapInfoT, traceRoot *TraceMapT, sd apmTraceSpan) {
  549. // mNode.ServiceName = GO_SERVICE_NAME
  550. // mNode.ServiceType = APP_SERVICE_TYPE
  551. // mNode.MethodName = "net/http.(*Transport).roundTrip()"
  552. // mNode.Level = 1
  553. // mNode.Pid = 0
  554. // mNode.Nid = 1
  555. // // 构建root节点
  556. // traceRoot.RootData.RespTime = mNode.PureTime
  557. // traceRoot.RootData.CollTime = mNode.StartTime
  558. // traceRoot.Index = 1
  559. // for _, attr := range sd.Attributes() {
  560. // fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  561. // switch attr.Key {
  562. // case "http.uri":
  563. // traceRoot.RootData.Uri = attr.Value.AsString()
  564. // case "http.method":
  565. // traceRoot.RootData.HttpMethod = attr.Value.AsString()
  566. // case "http.status_code":
  567. // traceRoot.RootData.HttpCode = attr.Value.AsInt64()
  568. // case "net.peer.name":
  569. // traceRoot.RootData.ClientIp = attr.Value.AsString()
  570. // traceRoot.RootData.Sip = attr.Value.AsString()
  571. // traceRoot.RootData.Sn = attr.Value.AsString()
  572. // case "net.peer.port":
  573. // traceRoot.RootData.Sport = attr.Value.AsInt64()
  574. // traceRoot.RootData.LocalPort = attr.Value.AsInt64()
  575. // case "server.trace_id_from":
  576. // traceRoot.RootData.TraceId = attr.Value.AsString()
  577. // case "server.called_id":
  578. // traceRoot.RootData.CalledId = attr.Value.AsInt64()
  579. // case "server.instance_id_from":
  580. // traceRoot.RootData.InstanceIdFrom = attr.Value.AsInt64()
  581. // case "server.app_id_from":
  582. // traceRoot.RootData.AppIdFrom = attr.Value.AsInt64()
  583. // case "server.span_id_from":
  584. // traceRoot.RootData.SpanIdFrom = attr.Value.AsString()
  585. // case "server.type_from":
  586. // traceRoot.RootData.TypeFrom = attr.Value.AsString()
  587. // }
  588. // }
  589. //
  590. //}
  591. func buildAppMapFromEvent(traceRoot *RootDataT, sd apmTraceSpan) int {
  592. mNode := MapInfoT{
  593. Exception: 0,
  594. ExceptionMsg: "",
  595. ExceptionStack: "",
  596. Ip: "",
  597. Level: 1,
  598. Pid: 1,
  599. Port: 0,
  600. Ps: []string{},
  601. ServiceName: "",
  602. ServiceType: "",
  603. WallTime: 0,
  604. }
  605. mNode.ServiceName = GO_SERVICE_NAME
  606. mNode.ServiceType = APP_SERVICE_TYPE
  607. mNode.MethodName = "Kernel Endpoint()"
  608. mNode.Level = 1
  609. mNode.Pid = 0
  610. mNode.Nid = 1
  611. var code_type int64
  612. // 构建root节点
  613. //traceRoot.RespTime = mNode.PureTimex
  614. //traceRoot.CollTime = mNode.StartTime
  615. for _, attr := range sd.Attributes() {
  616. fmt.Println("Appmap:", attr.Key, ":", attr.Value.AsInterface())
  617. switch attr.Key {
  618. case "http.uri":
  619. traceRoot.Uri = attr.Value.AsString()
  620. case "http.method":
  621. traceRoot.HttpMethod = attr.Value.AsString()
  622. case "http.status_code":
  623. traceRoot.HttpCode = attr.Value.AsInt64()
  624. case "net.peer.name":
  625. traceRoot.ClientIp = attr.Value.AsString()
  626. traceRoot.Sip = attr.Value.AsString()
  627. traceRoot.Sn = attr.Value.AsString()
  628. case "net.peer.port":
  629. traceRoot.Sport = attr.Value.AsInt64()
  630. traceRoot.LocalPort = attr.Value.AsInt64()
  631. case "server.trace_id_from":
  632. traceRoot.TraceId = attr.Value.AsString()
  633. case "server.called_id":
  634. traceRoot.CalledId = attr.Value.AsInt64()
  635. case "server.instance_id_from":
  636. traceRoot.InstanceIdFrom = attr.Value.AsInt64()
  637. case "server.app_id_from":
  638. traceRoot.AppIdFrom = attr.Value.AsInt64()
  639. case "server.span_id_from":
  640. traceRoot.SpanIdFrom = attr.Value.AsString()
  641. case "server.type_from":
  642. traceRoot.TypeFrom = attr.Value.AsString()
  643. case "time.start_at":
  644. traceRoot.CollTime = uint64(attr.Value.AsInt64())
  645. mNode.StartTime = traceRoot.CollTime
  646. case "time.end_at":
  647. mNode.EndTime = uint64(attr.Value.AsInt64())
  648. case "time.duration":
  649. traceRoot.RespTime = uint64(attr.Value.AsInt64()) / 1e3
  650. //mNode.PureTime = traceRoot.RespTime
  651. mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
  652. case "server.code_type":
  653. code_type = attr.Value.AsInt64()
  654. case "server.app_name":
  655. traceRoot.AppName = attr.Value.AsString()
  656. case "server.service_name":
  657. traceRoot.ServiceName = attr.Value.AsString()
  658. mNode.ServiceName = attr.Value.AsString()
  659. case "server.app_id":
  660. traceRoot.AppId = attr.Value.AsInt64()
  661. case "server.agent_id":
  662. traceRoot.AgentId = attr.Value.AsInt64()
  663. case "server.instance_id":
  664. traceRoot.InstanceId = attr.Value.AsInt64()
  665. }
  666. }
  667. traceRoot.Maps = append(traceRoot.Maps, mNode)
  668. return int(code_type)
  669. }
  670. //func buildHttpMap(mNode *MapInfoT, sd apmTraceSpan) {
  671. // mNode.ServiceName = HTTP_SERVICE_NAME
  672. // mNode.ServiceType = HTTP_SERVICE_TYPE
  673. // mNode.Schema = "http"
  674. // mNode.MethodName = "net/http.serverHandler.ServeHTTP()"
  675. // var descAddr string
  676. // for _, attr := range sd.Attributes() {
  677. // //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  678. // switch attr.Key {
  679. // case "http.ip":
  680. // mNode.Ip = attr.Value.AsString()
  681. // descAddr += mNode.Ip
  682. // case "http.port":
  683. // mNode.Port = attr.Value.AsInt64()
  684. // descAddr += ":" + attr.Value.AsString()
  685. // case "http.uri":
  686. // mNode.Uri = attr.Value.AsString()
  687. // case "http.assumed_app_id":
  688. // mNode.AssumedAppId = attr.Value.AsInt64()
  689. // case "http.span_id":
  690. // mNode.SpanId = attr.Value.AsString()
  691. // }
  692. // }
  693. // //mNode.AssumedAppId = Md5ToInt64(descAddr, 16)
  694. //}
  695. func buildHttpMapFromEvent(mNode *MapInfoT, event tracesdk.Event) {
  696. mNode.ServiceName = HTTP_SERVICE_NAME
  697. mNode.ServiceType = HTTP_SERVICE_TYPE
  698. mNode.Schema = "http"
  699. mNode.MethodName = "net/http.serverHandler.ServeHTTP()"
  700. //var descAddr string
  701. for _, attr := range event.Attributes {
  702. fmt.Println("HTTP--->", attr.Key, ":", attr.Value.AsInterface())
  703. switch attr.Key {
  704. case "http.ip":
  705. mNode.Ip = attr.Value.AsString()
  706. //descAddr += mNode.Ip
  707. case "http.port":
  708. mNode.Port = attr.Value.AsInt64()
  709. //descAddr += ":" + attr.Value.AsString()
  710. case "http.uri":
  711. mNode.Uri = attr.Value.AsString()
  712. case "http.assumed_app_id":
  713. mNode.AssumedAppId = attr.Value.AsInt64()
  714. case "http.span_id":
  715. mNode.SpanId = attr.Value.AsString()
  716. case "time.start_at":
  717. mNode.StartTime = uint64(attr.Value.AsInt64())
  718. case "time.end_at":
  719. mNode.EndTime = uint64(attr.Value.AsInt64())
  720. case "time.duration":
  721. //mNode.PureTime = uint64(attr.Value.AsInt64()) / 1e3
  722. mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
  723. }
  724. }
  725. //mNode.AssumedAppId = Md5ToInt64(descAddr, 16)
  726. }
  727. //func buildMysqlMap(mNode *MapInfoT, sd apmTraceSpan) {
  728. // mNode.Dbn = "unknown"
  729. // mNode.ServiceName = MYSQL_SERVICE_NAME
  730. // mNode.ServiceType = SQL_SERVICE_TYPE
  731. // mNode.MethodName = "database/sql.Query()"
  732. // for _, attr := range sd.Attributes() {
  733. // //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  734. // switch attr.Key {
  735. // case "net.peer.name":
  736. // mNode.Ip = attr.Value.AsString()
  737. // case "net.peer.port":
  738. // mNode.Port = attr.Value.AsInt64()
  739. // case "db.statement":
  740. // query := attr.Value.AsString()
  741. // mNode.Ps = []string{query}
  742. // words := strings.Fields(query)
  743. // if len(words) > 0 {
  744. // mNode.OperType = strings.ToUpper(words[0])
  745. // }
  746. // }
  747. // }
  748. //}
  749. func buildMysqlMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  750. mNode.Dbn = "unknown"
  751. mNode.ServiceName = MYSQL_SERVICE_NAME
  752. mNode.ServiceType = SQL_SERVICE_TYPE
  753. mNode.MethodName = "database/sql.Query()"
  754. for _, attr := range event.Attributes {
  755. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  756. switch attr.Key {
  757. case "net.peer.name":
  758. mNode.Ip = attr.Value.AsString()
  759. case "net.peer.port":
  760. mNode.Port = attr.Value.AsInt64()
  761. case "db.statement":
  762. query := attr.Value.AsString()
  763. mNode.Ps = []string{query}
  764. words := strings.Fields(query)
  765. if len(words) > 0 {
  766. mNode.OperType = strings.ToUpper(words[0])
  767. }
  768. }
  769. }
  770. }
  771. func buildRedisMap(mNode *MapInfoT, sd apmTraceSpan) {
  772. mNode.ServiceName = REDIS_SERVICE_NAME
  773. mNode.ServiceType = NOSQL_SERVICE_TYPE
  774. //mNode.MethodName = span(sd).Name + " query"
  775. mNode.MethodName = "redis.Do()"
  776. for _, attr := range sd.Attributes() {
  777. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  778. switch attr.Key {
  779. case "net.peer.name":
  780. mNode.Ip = attr.Value.AsString()
  781. case "net.peer.port":
  782. mNode.Port = attr.Value.AsInt64()
  783. case "db.statement":
  784. query := attr.Value.AsString()
  785. mNode.Ps = []string{query}
  786. words := strings.Fields(query)
  787. if len(words) > 0 {
  788. mNode.OperType = strings.ToUpper(words[0])
  789. }
  790. }
  791. }
  792. }
  793. func buildRedisMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  794. mNode.ServiceName = REDIS_SERVICE_NAME
  795. mNode.ServiceType = NOSQL_SERVICE_TYPE
  796. //mNode.MethodName = span(sd).Name + " query"
  797. mNode.MethodName = "redis.Do()"
  798. for _, attr := range event.Attributes {
  799. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  800. switch attr.Key {
  801. case "net.peer.name":
  802. mNode.Ip = attr.Value.AsString()
  803. case "net.peer.port":
  804. mNode.Port = attr.Value.AsInt64()
  805. case "db.statement":
  806. query := attr.Value.AsString()
  807. mNode.Ps = []string{query}
  808. words := strings.Fields(query)
  809. if len(words) > 0 {
  810. mNode.OperType = strings.ToUpper(words[0])
  811. }
  812. }
  813. }
  814. }
  815. func isEnter(_type string) bool {
  816. if _type == "APPLICATION" {
  817. return true
  818. }
  819. return false
  820. }
  821. func span(sd apmTraceSpan) *tracepb.Span {
  822. if sd == nil {
  823. return nil
  824. }
  825. tid := sd.SpanContext().TraceID()
  826. sid := sd.SpanContext().SpanID()
  827. s := &tracepb.Span{
  828. TraceId: tid[:],
  829. SpanId: sid[:],
  830. TraceState: sd.SpanContext().TraceState().String(),
  831. //Status: status(sd.Status().Code, sd.Status().Description),
  832. StartTimeUnixNano: uint64(sd.StartTime().UnixNano()),
  833. EndTimeUnixNano: uint64(sd.EndTime().UnixNano()),
  834. //Links: links(sd.Links()),
  835. //Kind: spanKind(sd.SpanKind()),
  836. Name: sd.Name(),
  837. Attributes: tracetransform.KeyValues(sd.Attributes()),
  838. //Events: spanEvents(sd.Events()),
  839. DroppedAttributesCount: uint32(sd.DroppedAttributes()),
  840. DroppedEventsCount: uint32(sd.DroppedEvents()),
  841. DroppedLinksCount: uint32(sd.DroppedLinks()),
  842. }
  843. if psid := sd.Parent().SpanID(); psid.IsValid() {
  844. s.ParentSpanId = psid[:]
  845. }
  846. return s
  847. }
  848. func Md5ToInt64(strParam string, Len int) int64 {
  849. sign := md5.Sum([]byte(strParam))
  850. signStr := fmt.Sprintf("%x", sign)
  851. charArr := []rune(signStr)
  852. var intStr string
  853. for _, value := range charArr {
  854. intStr += strconv.Itoa(int(value))
  855. }
  856. intStr = intStr[:Len]
  857. int64Data, err := strconv.ParseInt(intStr, 10, 64)
  858. if err != nil {
  859. return 0
  860. }
  861. return int64Data
  862. }