apm_exporter.go 26 KB

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