apm_exporter.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. package otlptrace
  2. import (
  3. "crypto/md5"
  4. "encoding/json"
  5. "fmt"
  6. "math"
  7. "net/url"
  8. "sort"
  9. "strconv"
  10. "sync"
  11. . "github.com/coroot/coroot-node-agent/ebpftracer"
  12. "github.com/coroot/coroot-node-agent/ebpftracer/l7"
  13. "github.com/coroot/coroot-node-agent/utils"
  14. klog "github.com/sirupsen/logrus"
  15. "go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform"
  16. tracesdk "go.opentelemetry.io/otel/sdk/trace"
  17. tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
  18. )
  19. const (
  20. APP_SERVICE_TYPE = "APPLICATION"
  21. SQL_SERVICE_TYPE = "SQL"
  22. NOSQL_SERVICE_TYPE = "NOSQL"
  23. HTTP_SERVICE_TYPE = "HTTP"
  24. NET_SERVICE_TYPE = "L7_NET"
  25. RPC_SERVICE_TYPE = "RPC"
  26. MQ_SERVICE_TYPE = "MQ"
  27. )
  28. const (
  29. GO_SERVICE_NAME = "GO"
  30. MYSQL_SERVICE_NAME = "MYSQL"
  31. DM_SERVICE_NAME = "DM"
  32. REDIS_SERVICE_NAME = "REDIS"
  33. MONGO_SERVICE_NAME = "MONGODB"
  34. HTTP_SERVICE_NAME = "HTTPCLIENT"
  35. POSTGRESQL_SERVICE_NAME = "POSTGRESQL"
  36. GRPC_SERVICE_NAME = "GRPC"
  37. )
  38. type apmTraceSpan tracesdk.ReadOnlySpan
  39. // GO:0:10154813500555812:5450531005555981:5610250100539899:ee022542c3940f1b:1001025098564810:888ceb3df1bdbe2c:110
  40. type RootDataT struct {
  41. AccountId int `json:"account_id"`
  42. AgentId int64 `json:"agent_id"`
  43. AgentVersion string `json:"agent_version"`
  44. AppId int64 `json:"app_id"`
  45. AppIdFrom int64 `json:"app_id_from"` // from header app_id
  46. AppName string `json:"app_name"`
  47. CalledId int64 `json:"called_id"` // from header assumed_app_id
  48. ClientIp string `json:"client_ip"`
  49. CollTime uint64 `json:"coll_time"`
  50. Cpu int `json:"cpu"`
  51. Custom string `json:"custom"`
  52. HostId int64 `json:"host_id"`
  53. HostName string `json:"host_name"`
  54. HttpCode int64 `json:"http_code"`
  55. HttpMethod string `json:"http_method"`
  56. InstanceId int64 `json:"instance_id"`
  57. InstanceIdFrom int64 `json:"instance_id_from"` // from header instance_id
  58. LocalPort int64 `json:"local_port"`
  59. Maps []MapInfoT `json:"maps"`
  60. MemU int `json:"mem_u"`
  61. MemUP int `json:"mem_u_p"`
  62. OperType string `json:"oper_type"`
  63. Parameters []ParamStruct `json:"parameters,omitempty"`
  64. ParentTaskName string `json:"parent_task_name"`
  65. Period int `json:"period"`
  66. RespTime uint64 `json:"resp_time"`
  67. Sampling int `json:"sampling"`
  68. ServiceName string `json:"service_name"`
  69. ServiceType string `json:"service_type"`
  70. Sip string `json:"sip"`
  71. Sn string `json:"sn"`
  72. SpanIdFrom string `json:"span_id_from"` // from header span_id
  73. Sport int64 `json:"sport"`
  74. TId int `json:"t_id"`
  75. TName string `json:"t_name"`
  76. TraceId string `json:"trace_id"` // from header trace_id
  77. TransIds []interface{} `json:"trans_ids"`
  78. TypeFrom string `json:"type_from"`
  79. Uri string `json:"uri"`
  80. UserDir string `json:"user_dir"`
  81. VipIds []interface{} `json:"vip_ids"`
  82. SrcAddr string `json:"src_addr"`
  83. DestinationAddr string `json:"destination_addr"`
  84. UserAgent string `json:"user_agent"`
  85. // op 新增字段
  86. Pid uint32 `json:"pid"`
  87. ContainerID string `json:"container_id"`
  88. Sys string `json:"sys"`
  89. SystemUUID string `json:"system_uuid"`
  90. }
  91. // ParamStruct 定义目标结构
  92. type ParamStruct struct {
  93. Name string `json:"name"`
  94. Values []string `json:"values"`
  95. }
  96. type MapInfoT struct {
  97. Dbn string `json:"dbn,omitempty"`
  98. Exception int `json:"exception,omitempty"`
  99. ExceptionMsg string `json:"exception_msg,omitempty"`
  100. ExceptionStack string `json:"exception_stack,omitempty"`
  101. Ip string `json:"ip,omitempty"`
  102. Level int `json:"level"`
  103. MethodDesc string `json:"method_desc,omitempty"`
  104. MethodName string `json:"method_name"`
  105. Nid int `json:"nid"`
  106. OperType string `json:"oper_type,omitempty"`
  107. Pid int `json:"pid"`
  108. Port int64 `json:"port,omitempty"`
  109. Ps []string `json:"ps,omitempty"`
  110. PureTime uint64 `json:"pure_time"`
  111. ServiceName string `json:"service_name"`
  112. ServiceType string `json:"service_type"`
  113. StartTime uint64 `json:"-"`
  114. EndTime uint64 `json:"-"`
  115. StartTimeMs uint64 `json:"start_time"`
  116. EndTimeMs uint64 `json:"end_time"`
  117. WallTime uint64 `json:"wall_time"`
  118. Schema string `json:"schema,omitempty"`
  119. AssumedAppId int64 `json:"assumed_app_id,omitempty"`
  120. Uri string `json:"uri,omitempty"`
  121. SpanId string `json:"span_id,omitempty"`
  122. SrcAddr string `json:"src_addr,omitempty"`
  123. DestinationAddr string `json:"destination_addr,omitempty"`
  124. }
  125. type TraceMapT struct {
  126. RootData RootDataT
  127. Index int
  128. lock *sync.RWMutex
  129. TheEnd bool
  130. }
  131. var TraceRootMap map[string]*TraceMapT
  132. func init() {
  133. TraceRootMap = make(map[string]*TraceMapT)
  134. //go func() {
  135. // for {
  136. // //fmt.Println(G_sdl)
  137. // time.Sleep(5 * time.Second)
  138. // }
  139. //}()
  140. }
  141. var G_sdl int
  142. func tracetransformData(sdl []tracesdk.ReadOnlySpan) map[int][]RootDataT {
  143. //G_sdl += len(sdl)
  144. if len(sdl) == 0 {
  145. return nil
  146. }
  147. // 多次请求 sdl
  148. sendDataMap := make(map[int][]RootDataT)
  149. //sendData := []RootDataT{}
  150. for _, sd := range sdl {
  151. if sd == nil {
  152. continue
  153. }
  154. //traceId := sd.SpanContext().TraceID().String()
  155. //fmt.Println("------event_num---- "+sd.Name(), "--->", len(sd.Events())) // 一次请求完整数据
  156. // 构建map *RootDataT
  157. var rootData RootDataT
  158. rootData = initRootDataFromEvent()
  159. // build http入口 MapInfoT
  160. code_type := buildAppMapFromEvent(&rootData, sd)
  161. // 构建maps
  162. for _, event := range sd.Events() {
  163. //aaa, _ := json.Marshal(event)
  164. //fmt.Println("event.info", string(aaa))
  165. mNode := buildMapNodeFromEvent(event)
  166. switch EventType(event.EventType) {
  167. // stack
  168. case EventTypeFunEnt:
  169. // l7 event
  170. case EventTypeL7Request:
  171. switch l7.Protocol(event.ProtocolType) {
  172. case l7.ProtocolHTTP:
  173. buildHttpMapFromEvent(&mNode, event)
  174. case l7.ProtocolDNS:
  175. buildDNSMapEvent(&mNode, event)
  176. case l7.ProtocolMysql, l7.ProtocolMariaDB, l7.ProtocolTiDB, l7.ProtocolPostgres, l7.ProtocolDM:
  177. buildSQLMapEvent(&mNode, event)
  178. case l7.ProtocolRedis, l7.ProtocolMemcached, l7.ProtocolCassandra, l7.ProtocolES:
  179. buildNoSqlMapEvent(&mNode, event)
  180. case l7.ProtocolMongo:
  181. buildMongoMapEvent(&mNode, event)
  182. case l7.ProtocolGrpc:
  183. buildGrpcMapEvent(&mNode, event)
  184. case l7.ProtocolKafka:
  185. buildMQMapEvent(&mNode, event)
  186. }
  187. }
  188. rootData.Maps = append(rootData.Maps, mNode)
  189. //fmt.Println(event.Name)
  190. //buildAndAssemblyMapFromEvent(event, rootData)
  191. }
  192. buildLevelFromEvent(&rootData)
  193. sendDataMap[code_type] = append(sendDataMap[code_type], rootData)
  194. //a, _ := json.Marshal(rootData)
  195. //fmt.Println(string(a))
  196. //sendData = append(sendData, rootData)
  197. //if _, ok := TraceRootMap[traceId]; !ok {
  198. //TraceRootMap[traceId] = &TraceMapT{RootData: initRootData(traceId), Index: 1}
  199. //}
  200. //TraceRootMap[traceId].Index++
  201. //buildAndAssemblyMap(sd, TraceRootMap[traceId])
  202. }
  203. // 发送完整数据 | 大量长耗时请求会增加内存占用
  204. //sendData := []RootDataT{}
  205. //for traceId, v := range TraceRootMap {
  206. // if v.TheEnd {
  207. // buildLevel(v)
  208. // sendData = append(sendData, v.RootData)
  209. // delete(TraceRootMap, traceId)
  210. // //fmt.Println("the end!")
  211. // } else {
  212. // //fmt.Println("not end!")
  213. // }
  214. //}
  215. //Transform the categorized map into a slice
  216. data, _ := json.Marshal(sendDataMap)
  217. klog.Debug(string(data))
  218. //fmt.Println(len(sendData))
  219. //fmt.Println("sdl len:", len(sdl))
  220. return sendDataMap
  221. }
  222. type TimeMap struct {
  223. Time uint64
  224. Type int
  225. Map *MapInfoT
  226. }
  227. //type TraceMapT struct {
  228. // RootData RootDataT
  229. // Index int
  230. // lock *sync.RWMutex
  231. // TheEnd bool
  232. //}
  233. //func buildLevel(sdl *TraceMapT) {
  234. // nidMap := make(map[int]*MapInfoT)
  235. //
  236. // mapSlice := []TimeMap{}
  237. //
  238. // for i, v := range sdl.RootData.Maps {
  239. // if v.ServiceType == "APPLICATION" {
  240. // continue
  241. // }
  242. // nidMap[v.Nid] = &sdl.RootData.Maps[i]
  243. // timeStartMap := TimeMap{
  244. // Time: v.StartTime,
  245. // Type: 0,
  246. // Map: &sdl.RootData.Maps[i],
  247. // }
  248. // mapSlice = append(mapSlice, timeStartMap)
  249. // timeEndMap := TimeMap{
  250. // Time: v.EndTime,
  251. // Type: 1,
  252. // Map: &sdl.RootData.Maps[i],
  253. // }
  254. // mapSlice = append(mapSlice, timeEndMap)
  255. // }
  256. // sort.Slice(mapSlice, func(i, j int) bool {
  257. // return mapSlice[i].Time < mapSlice[j].Time
  258. // })
  259. //
  260. // funStack := []TimeMap{}
  261. //
  262. // currentNid := 1
  263. // Nid := 2
  264. // level := 2
  265. //
  266. // for _, v := range mapSlice {
  267. // // fmt.Println("SliceSliceindex", k, "value", v.Time, v.Type, v.Map.MethodName, v.Map.Nid)
  268. // if v.Type == 0 {
  269. // // 函数入口
  270. // funStack = append(funStack, v)
  271. // v.Map.Pid = currentNid
  272. // v.Map.Level = level
  273. // v.Map.Nid = Nid
  274. // currentNid = Nid
  275. // level += 1
  276. // Nid += 1
  277. // } else if v.Type == 1 {
  278. // // 函数出口
  279. // len := len(funStack)
  280. // funStack = funStack[:len-1]
  281. // if (len - 2) < 0 {
  282. // currentNid = 1
  283. // } else {
  284. // currentNid = funStack[len-2].Map.Nid
  285. // }
  286. //
  287. // level -= 1
  288. // }
  289. // }
  290. //}
  291. func buildLevelFromEvent(sdl *RootDataT) {
  292. nidMap := make(map[int]*MapInfoT)
  293. mapSlice := []TimeMap{}
  294. for i, v := range sdl.Maps {
  295. if v.ServiceType == "APPLICATION" || v.Level == 1 {
  296. continue
  297. }
  298. nidMap[v.Nid] = &sdl.Maps[i]
  299. timeStartMap := TimeMap{
  300. Time: v.StartTime,
  301. Type: 0,
  302. Map: &sdl.Maps[i],
  303. }
  304. mapSlice = append(mapSlice, timeStartMap)
  305. timeEndMap := TimeMap{
  306. Time: v.EndTime,
  307. Type: 1,
  308. Map: &sdl.Maps[i],
  309. }
  310. mapSlice = append(mapSlice, timeEndMap)
  311. }
  312. sort.Slice(mapSlice, func(i, j int) bool {
  313. return mapSlice[i].Time < mapSlice[j].Time
  314. })
  315. funStack := []TimeMap{}
  316. currentNid := 1
  317. Nid := 2
  318. level := 2
  319. for _, v := range mapSlice {
  320. //klog.Debugln("SliceSliceindex", k, "value", v.Time, v.Type, v.Map.MethodName, v.Map.Nid)
  321. if v.Type == 0 {
  322. // 函数入口
  323. funStack = append(funStack, v)
  324. v.Map.Pid = currentNid
  325. v.Map.Level = level
  326. v.Map.Nid = Nid
  327. currentNid = Nid
  328. level += 1
  329. Nid += 1
  330. } else if v.Type == 1 {
  331. // 函数出口
  332. len := len(funStack)
  333. funStack = funStack[:len-1]
  334. if (len - 2) < 0 {
  335. currentNid = 1
  336. } else {
  337. currentNid = funStack[len-2].Map.Nid
  338. }
  339. level -= 1
  340. }
  341. }
  342. }
  343. //func initRootData(traceId string) RootDataT {
  344. // data := RootDataT{
  345. // AccountId: 110,
  346. // AgentId: 1011005252979954, // TODO 更新 基于 ip:port + process_name + exe路径生成
  347. // AgentVersion: "2.1.0",
  348. // AppId: 5410049101545798, // TODO 更新 基于appname生成
  349. // AppIdFrom: -1,
  350. // AppName: "eBPF-agent", // TODO 更新 ip:port || process_name
  351. // CalledId: -1,
  352. // ClientIp: "",
  353. // CollTime: 0,
  354. // Cpu: 0,
  355. // Custom: "",
  356. // HostId: 10154813500555812,
  357. // HostName: "localhost",
  358. // HttpCode: 0,
  359. // HttpMethod: "",
  360. // InstanceId: 1005051101515357, // TODO 更新 基于ip:port
  361. // InstanceIdFrom: -1,
  362. // Maps: []MapInfoT{},
  363. // MemU: 0,
  364. // MemUP: 0,
  365. // OperType: "",
  366. // Parameters: []interface{}{},
  367. // ParentTaskName: 0,
  368. // Period: -1,
  369. // RespTime: 0,
  370. // Sampling: 0,
  371. // ServiceName: "GO",
  372. // ServiceType: APP_SERVICE_TYPE,
  373. // Sip: "",
  374. // Sn: "",
  375. // SpanIdFrom: "",
  376. // Sport: 0,
  377. // TId: -1,
  378. // TName: "",
  379. // TraceId: traceId,
  380. // TransIds: []interface{}{},
  381. // TypeFrom: "",
  382. // Uri: "",
  383. // UserDir: 0,
  384. // VipIds: []interface{}{},
  385. // }
  386. // return data
  387. //}
  388. func initRootDataFromEvent() RootDataT {
  389. hostID := utils.GetHostID()
  390. accountID := utils.GetAccountID()
  391. sip := utils.GetHostIP()
  392. sysTag := utils.GetSysTag()
  393. systemUUID := utils.GetSystemUUID()
  394. data := RootDataT{
  395. // todo AccountId
  396. AccountId: accountID,
  397. AgentId: 0, // 基于 ip:port + process_name + exe路径生成
  398. AgentVersion: "2.1.0",
  399. AppId: 0, // 基于appname生成
  400. AppIdFrom: -1,
  401. AppName: "eBPF-agent", // server配置
  402. CalledId: -1,
  403. ClientIp: "",
  404. CollTime: 0,
  405. Cpu: 0,
  406. Custom: "",
  407. HostId: hostID,
  408. HostName: "localhost",
  409. HttpCode: 0,
  410. HttpMethod: "",
  411. InstanceId: 0, // 基于ip:port
  412. InstanceIdFrom: -1,
  413. Maps: []MapInfoT{},
  414. MemU: 0,
  415. MemUP: 0,
  416. OperType: "",
  417. Parameters: []ParamStruct{},
  418. ParentTaskName: "",
  419. Period: -1,
  420. RespTime: 0,
  421. Sampling: 0,
  422. ServiceName: "",
  423. ServiceType: APP_SERVICE_TYPE,
  424. Sip: sip,
  425. Sn: "",
  426. SpanIdFrom: "",
  427. Sport: 0,
  428. TId: -1,
  429. TName: "",
  430. TraceId: "",
  431. TransIds: []interface{}{},
  432. TypeFrom: "",
  433. Uri: "",
  434. UserDir: "",
  435. VipIds: []interface{}{},
  436. SrcAddr: "",
  437. DestinationAddr: "",
  438. Sys: sysTag,
  439. SystemUUID: systemUUID,
  440. UserAgent: "",
  441. }
  442. return data
  443. }
  444. func initRootDataJava() RootDataT {
  445. data := RootDataT{
  446. AccountId: 110,
  447. AgentId: 3934815089541000, // TODO 更新 基于 ip:port + process_name + exe路径生成
  448. AgentVersion: "2.21.0",
  449. AppId: 3365853273187618, // TODO 更新 基于appname生成
  450. AppIdFrom: -1,
  451. AppName: "eBPF-javaApplication", // TODO 更新 ip:port || process_name
  452. CalledId: -1,
  453. ClientIp: "",
  454. CollTime: 0,
  455. Cpu: 0,
  456. Custom: "",
  457. HostId: 2315065183171055,
  458. HostName: "localhost",
  459. HttpCode: 0,
  460. HttpMethod: "",
  461. InstanceId: 1128864082033413, // TODO 更新 基于ip:port
  462. InstanceIdFrom: -1,
  463. Maps: []MapInfoT{},
  464. MemU: 0,
  465. MemUP: 0,
  466. OperType: "",
  467. Parameters: []ParamStruct{},
  468. ParentTaskName: "",
  469. Period: -1,
  470. RespTime: 0,
  471. Sampling: 0,
  472. ServiceName: "TOMCAT",
  473. ServiceType: APP_SERVICE_TYPE,
  474. Sip: "",
  475. Sn: "",
  476. SpanIdFrom: "",
  477. Sport: 0,
  478. TId: -1,
  479. TName: "",
  480. TraceId: "",
  481. TransIds: []interface{}{},
  482. TypeFrom: "",
  483. Uri: "",
  484. UserDir: "",
  485. VipIds: []interface{}{},
  486. }
  487. return data
  488. }
  489. //func initMapNode(spanSd *tracepb.Span) (MapInfoT, string) {
  490. // mNode := MapInfoT{
  491. // Exception: 0,
  492. // ExceptionMsg: "",
  493. // ExceptionStack: "",
  494. // Ip: "",
  495. // Level: 2,
  496. // Pid: 1,
  497. // Port: 0,
  498. // Ps: []string{},
  499. // ServiceName: "",
  500. // ServiceType: "",
  501. // WallTime: 0,
  502. // }
  503. // mNode.MethodName = spanSd.Name
  504. // mNode.PureTime = (spanSd.EndTimeUnixNano - spanSd.StartTimeUnixNano) / 1e3
  505. // mNode.WallTime = mNode.PureTime
  506. // mNode.StartTime = spanSd.StartTimeUnixNano
  507. // mNode.EndTime = spanSd.EndTimeUnixNano
  508. //
  509. // for _, attr := range spanSd.GetAttributes() {
  510. // fmt.Println(attr.Key, ":", attr.Value.GetValue())
  511. //
  512. // switch attr.Key {
  513. // case "nid":
  514. // mNode.Nid = int(attr.Value.GetIntValue())
  515. // case "pid":
  516. // mNode.Pid = int(attr.Value.GetIntValue())
  517. // case "level":
  518. // mNode.Level = int(attr.Value.GetIntValue())
  519. // }
  520. // }
  521. //
  522. // return mNode, spanSd.Name
  523. //}
  524. func buildMapNodeFromEvent(event tracesdk.Event) MapInfoT {
  525. mNode := MapInfoT{
  526. Exception: 0,
  527. ExceptionMsg: "",
  528. ExceptionStack: "",
  529. Ip: "",
  530. Level: 2,
  531. Pid: 1,
  532. Port: 0,
  533. Ps: []string{},
  534. ServiceName: "",
  535. ServiceType: "",
  536. WallTime: 0,
  537. }
  538. mNode.MethodName = event.Name
  539. //mNode.PureTime = (event.EndTimeUnixNano - event.StartTimeUnixNano) / 1e3
  540. //mNode.WallTime = mNode.PureTime
  541. //mNode.StartTime = spanSd.StartTimeUnixNano
  542. //mNode.EndTime = spanSd.EndTimeUnixNano
  543. for _, attr := range event.Attributes {
  544. //fmt.Println(event.Name, "--->buildMapNodeFromEvent--->", attr.Key, ":", attr.Value.AsInterface())
  545. switch attr.Key {
  546. case "nid":
  547. mNode.Nid = int(attr.Value.AsInt64())
  548. case "pid":
  549. mNode.Pid = int(attr.Value.AsInt64())
  550. case "level":
  551. mNode.Level = int(attr.Value.AsInt64())
  552. case "time.start_at":
  553. mNode.StartTime, mNode.StartTimeMs = cleanNsTime(attr.Value.AsInt64())
  554. case "time.end_at":
  555. mNode.EndTime, mNode.EndTimeMs = cleanNsTime(attr.Value.AsInt64())
  556. case "time.duration":
  557. //mNode.PureTime = uint64(attr.Value.AsInt64()) / 1e3
  558. mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
  559. }
  560. }
  561. return mNode
  562. }
  563. func parseURIToParams(input string) (string, []ParamStruct, error) {
  564. // 解析输入 URI
  565. parsedURL, err := url.Parse(input)
  566. if err != nil {
  567. return "", nil, fmt.Errorf("failed to parse URI: %w", err)
  568. }
  569. // 提取查询参数
  570. queryParams := parsedURL.Query()
  571. // 转换为目标结构
  572. var params []ParamStruct
  573. for key, values := range queryParams {
  574. params = append(params, ParamStruct{
  575. Name: key,
  576. Values: values,
  577. })
  578. }
  579. return parsedURL.Path, params, nil
  580. }
  581. // 构建拼装
  582. //func buildAndAssemblyMap(sd apmTraceSpan, traceRoot *TraceMapT) MapInfoT {
  583. // mNode, mapType := initMapNode(span(sd))
  584. // switch mapType {
  585. // case "APPLICATION":
  586. // buildAppMap(&mNode, traceRoot, sd)
  587. // traceRoot.TheEnd = true
  588. // case "HTTP":
  589. // buildHttpMap(&mNode, sd)
  590. // case "Mysql":
  591. // buildMysqlMap(&mNode, sd)
  592. // case "Redis":
  593. // buildRedisMap(&mNode, sd)
  594. // }
  595. // if mapType != "" {
  596. // mNode.Nid = traceRoot.Index
  597. // traceRoot.RootData.Maps = append(traceRoot.RootData.Maps, mNode)
  598. // }
  599. // return mNode
  600. //}
  601. //func buildAndAssemblyMapFromEvent(event tracesdk.Event, traceRoot *RootDataT) MapInfoT {
  602. // mNode := buildMapNodeFromEvent(event)
  603. // switch mapType {
  604. // case "HTTP":
  605. // buildHttpMapFromEvent(mNode, event)
  606. // //case "Mysql":
  607. // // buildMysqlMap(mNode, sd)
  608. // //case "Redis":
  609. // // buildRedisMap(mNode, sd)
  610. // }
  611. // if mapType != "" {
  612. // //mNode.Nid = traceRoot.Index
  613. // traceRoot.Maps = append(traceRoot.Maps, mNode)
  614. // }
  615. // return mNode
  616. //}
  617. //func buildAppMap(mNode *MapInfoT, traceRoot *TraceMapT, sd apmTraceSpan) {
  618. // mNode.ServiceName = GO_SERVICE_NAME
  619. // mNode.ServiceType = APP_SERVICE_TYPE
  620. // mNode.MethodName = "net/http.(*Transport).roundTrip()"
  621. // mNode.Level = 1
  622. // mNode.Pid = 0
  623. // mNode.Nid = 1
  624. // // 构建root节点
  625. // traceRoot.RootData.RespTime = mNode.PureTime
  626. // traceRoot.RootData.CollTime = mNode.StartTime
  627. // traceRoot.Index = 1
  628. // for _, attr := range sd.Attributes() {
  629. // fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  630. // switch attr.Key {
  631. // case "http.uri":
  632. // traceRoot.RootData.Uri = attr.Value.AsString()
  633. // case "http.method":
  634. // traceRoot.RootData.HttpMethod = attr.Value.AsString()
  635. // case "http.status_code":
  636. // traceRoot.RootData.HttpCode = attr.Value.AsInt64()
  637. // case "net.peer.name":
  638. // traceRoot.RootData.ClientIp = attr.Value.AsString()
  639. // traceRoot.RootData.Sip = attr.Value.AsString()
  640. // traceRoot.RootData.Sn = attr.Value.AsString()
  641. // case "net.peer.port":
  642. // traceRoot.RootData.Sport = attr.Value.AsInt64()
  643. // traceRoot.RootData.LocalPort = attr.Value.AsInt64()
  644. // case "server.trace_id_from":
  645. // traceRoot.RootData.TraceId = attr.Value.AsString()
  646. // case "server.called_id":
  647. // traceRoot.RootData.CalledId = attr.Value.AsInt64()
  648. // case "server.instance_id_from":
  649. // traceRoot.RootData.InstanceIdFrom = attr.Value.AsInt64()
  650. // case "server.app_id_from":
  651. // traceRoot.RootData.AppIdFrom = attr.Value.AsInt64()
  652. // case "server.span_id_from":
  653. // traceRoot.RootData.SpanIdFrom = attr.Value.AsString()
  654. // case "server.type_from":
  655. // traceRoot.RootData.TypeFrom = attr.Value.AsString()
  656. // }
  657. // }
  658. //
  659. //}
  660. func buildAppMapFromEvent(traceRoot *RootDataT, sd apmTraceSpan) int {
  661. mNode := MapInfoT{
  662. Exception: 0,
  663. ExceptionMsg: "",
  664. ExceptionStack: "",
  665. Ip: "",
  666. Level: 1,
  667. Pid: 1,
  668. Port: 0,
  669. Ps: []string{},
  670. ServiceName: "",
  671. ServiceType: "",
  672. WallTime: 0,
  673. }
  674. mNode.ServiceName = GO_SERVICE_NAME
  675. mNode.ServiceType = APP_SERVICE_TYPE
  676. mNode.MethodName = "Kernel Endpoint()"
  677. mNode.Level = 1
  678. mNode.Pid = 0
  679. mNode.Nid = 1
  680. var code_type int64
  681. // 构建root节点
  682. //traceRoot.RespTime = mNode.PureTimex
  683. //traceRoot.CollTime = mNode.StartTime
  684. for _, attr := range sd.Attributes() {
  685. klog.Debugln("Appmap:", attr.Key, ":", attr.Value.AsInterface())
  686. switch attr.Key {
  687. case "http.uri":
  688. traceRoot.Uri, traceRoot.Parameters, _ = parseURIToParams(attr.Value.AsString())
  689. case "rpc.uri":
  690. traceRoot.Uri = attr.Value.AsString()
  691. mNode.Uri = attr.Value.AsString()
  692. case "http.method":
  693. traceRoot.HttpMethod = attr.Value.AsString()
  694. case "http.status_code":
  695. traceRoot.HttpCode = attr.Value.AsInt64()
  696. case "net.peer.name":
  697. // TODO 修改 ClientIp sip获取方式
  698. traceRoot.ClientIp = attr.Value.AsString()
  699. //traceRoot.Sip = attr.Value.AsString()
  700. traceRoot.Sn = attr.Value.AsString()
  701. case "net.peer.port":
  702. traceRoot.Sport = attr.Value.AsInt64()
  703. traceRoot.LocalPort = attr.Value.AsInt64()
  704. case "server.trace_id_from":
  705. traceRoot.TraceId = attr.Value.AsString()
  706. case "server.called_id":
  707. traceRoot.CalledId = attr.Value.AsInt64()
  708. case "server.instance_id_from":
  709. traceRoot.InstanceIdFrom = attr.Value.AsInt64()
  710. case "server.app_id_from":
  711. traceRoot.AppIdFrom = attr.Value.AsInt64()
  712. case "server.span_id_from":
  713. traceRoot.SpanIdFrom = attr.Value.AsString()
  714. case "server.type_from":
  715. traceRoot.TypeFrom = attr.Value.AsString()
  716. case "time.start_at":
  717. mNode.StartTime, mNode.StartTimeMs = cleanNsTime(attr.Value.AsInt64())
  718. traceRoot.CollTime = mNode.StartTimeMs
  719. case "time.end_at":
  720. mNode.EndTime, mNode.EndTimeMs = cleanNsTime(attr.Value.AsInt64())
  721. case "time.duration":
  722. traceRoot.RespTime = uint64(attr.Value.AsInt64()) / 1e3
  723. mNode.PureTime = traceRoot.RespTime
  724. mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
  725. case "server.code_type":
  726. code_type = attr.Value.AsInt64()
  727. case "server.app_name":
  728. traceRoot.AppName = attr.Value.AsString()
  729. case "server.service_name":
  730. traceRoot.ServiceName = attr.Value.AsString()
  731. mNode.ServiceName = attr.Value.AsString()
  732. case "rpc.service_type":
  733. traceRoot.ServiceType = RPC_SERVICE_TYPE
  734. mNode.ServiceType = RPC_SERVICE_TYPE
  735. case "rpc.oper_type":
  736. traceRoot.OperType = "PROVIDER"
  737. mNode.OperType = "PROVIDER"
  738. // map tag
  739. case "service_type":
  740. mNode.ServiceType = attr.Value.AsString()
  741. // map tag
  742. case "oper_type":
  743. mNode.OperType = attr.Value.AsString()
  744. case "server.app_id":
  745. traceRoot.AppId = attr.Value.AsInt64()
  746. case "server.agent_id":
  747. traceRoot.AgentId = attr.Value.AsInt64()
  748. case "server.instance_id":
  749. traceRoot.InstanceId = attr.Value.AsInt64()
  750. case "server.src_addr":
  751. traceRoot.SrcAddr = attr.Value.AsString()
  752. case "server.dst_addr":
  753. traceRoot.DestinationAddr = attr.Value.AsString()
  754. case "server.pid":
  755. traceRoot.Pid = uint32(attr.Value.AsInt64())
  756. case "server.container_id":
  757. traceRoot.ContainerID = attr.Value.AsString()
  758. case "server.user_agent":
  759. traceRoot.UserAgent = attr.Value.AsString()
  760. case "method_name":
  761. mNode.MethodName = attr.Value.AsString()
  762. case "mq.ip":
  763. mNode.Ip = attr.Value.AsString()
  764. case "mq.port":
  765. mNode.Port = attr.Value.AsInt64()
  766. // root tag
  767. case "server.service_type":
  768. traceRoot.ServiceType = attr.Value.AsString()
  769. // root tag
  770. case "server.oper_type":
  771. traceRoot.OperType = attr.Value.AsString()
  772. }
  773. }
  774. traceRoot.Maps = append(traceRoot.Maps, mNode)
  775. return int(code_type)
  776. }
  777. //func buildHttpMap(mNode *MapInfoT, sd apmTraceSpan) {
  778. // mNode.ServiceName = HTTP_SERVICE_NAME
  779. // mNode.ServiceType = HTTP_SERVICE_TYPE
  780. // mNode.Schema = "http"
  781. // mNode.MethodName = "net/http.serverHandler.ServeHTTP()"
  782. // var descAddr string
  783. // for _, attr := range sd.Attributes() {
  784. // //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  785. // switch attr.Key {
  786. // case "http.ip":
  787. // mNode.Ip = attr.Value.AsString()
  788. // descAddr += mNode.Ip
  789. // case "http.port":
  790. // mNode.Port = attr.Value.AsInt64()
  791. // descAddr += ":" + attr.Value.AsString()
  792. // case "http.uri":
  793. // mNode.Uri = attr.Value.AsString()
  794. // case "http.assumed_app_id":
  795. // mNode.AssumedAppId = attr.Value.AsInt64()
  796. // case "http.span_id":
  797. // mNode.SpanId = attr.Value.AsString()
  798. // }
  799. // }
  800. // //mNode.AssumedAppId = Md5ToInt64(descAddr, 16)
  801. //}
  802. func buildGrpcMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  803. mNode.ServiceName = GRPC_SERVICE_NAME
  804. mNode.ServiceType = RPC_SERVICE_TYPE
  805. mNode.Schema = "grpc"
  806. //mNode.MethodName = "HTTP"
  807. //var descAddr string
  808. // var method string
  809. for _, attr := range event.Attributes {
  810. switch attr.Key {
  811. case "rpc.ip":
  812. mNode.Ip = attr.Value.AsString()
  813. //descAddr += mNode.Ip
  814. case "rpc.port":
  815. mNode.Port = attr.Value.AsInt64()
  816. case "rpc.method":
  817. //mNode.MethodName += " " + attr.Value.AsString()
  818. // method = attr.Value.AsString()
  819. mNode.MethodName = attr.Value.AsString()
  820. //descAddr += ":" + attr.Value.AsString()
  821. case "rpc.uri":
  822. mNode.Uri = attr.Value.AsString()
  823. //mNode.MethodName += " " + attr.Value.AsString()
  824. case "rpc.oper_type":
  825. mNode.OperType = attr.Value.AsString()
  826. case "rpc.assumed_app_id":
  827. mNode.AssumedAppId = attr.Value.AsInt64()
  828. case "rpc.span_id":
  829. mNode.SpanId = attr.Value.AsString()
  830. case "time.start_at":
  831. mNode.StartTime = uint64(attr.Value.AsInt64())
  832. case "time.end_at":
  833. mNode.EndTime = uint64(attr.Value.AsInt64())
  834. case "time.duration":
  835. //mNode.PureTime = uint64(attr.Value.AsInt64()) / 1e3
  836. mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
  837. }
  838. }
  839. // mNode.MethodName = fmt.Sprintf("%s %s %s:%d%s", "HTTP", method, mNode.Ip, mNode.Port, mNode.Uri)
  840. //mNode.AssumedAppId = Md5ToInt64(descAddr, 16)
  841. }
  842. func buildHttpMapFromEvent(mNode *MapInfoT, event tracesdk.Event) {
  843. mNode.ServiceName = HTTP_SERVICE_NAME
  844. mNode.ServiceType = HTTP_SERVICE_TYPE
  845. mNode.Schema = "http"
  846. //mNode.MethodName = "HTTP"
  847. //var descAddr string
  848. var method string
  849. for _, attr := range event.Attributes {
  850. klog.Debugln("HTTP--->", attr.Key, ":", attr.Value.AsInterface())
  851. switch attr.Key {
  852. case "http.ip":
  853. mNode.Ip = attr.Value.AsString()
  854. //descAddr += mNode.Ip
  855. case "http.port":
  856. mNode.Port = attr.Value.AsInt64()
  857. case "http.method":
  858. //mNode.MethodName += " " + attr.Value.AsString()
  859. method = attr.Value.AsString()
  860. //descAddr += ":" + attr.Value.AsString()
  861. case "http.uri":
  862. mNode.Uri = attr.Value.AsString()
  863. //mNode.MethodName += " " + attr.Value.AsString()
  864. case "http.assumed_app_id":
  865. mNode.AssumedAppId = attr.Value.AsInt64()
  866. case "http.span_id":
  867. mNode.SpanId = attr.Value.AsString()
  868. case "time.start_at":
  869. mNode.StartTime = uint64(attr.Value.AsInt64())
  870. case "time.end_at":
  871. mNode.EndTime = uint64(attr.Value.AsInt64())
  872. case "time.duration":
  873. //mNode.PureTime = uint64(attr.Value.AsInt64()) / 1e3
  874. mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
  875. case "http.src_addr":
  876. mNode.SrcAddr = attr.Value.AsString()
  877. case "http.destination_addr":
  878. mNode.DestinationAddr = attr.Value.AsString()
  879. case "http.is_tls":
  880. if attr.Value.AsBool() {
  881. mNode.Schema = "https"
  882. }
  883. }
  884. }
  885. mNode.MethodName = fmt.Sprintf("%s %s %s:%d%s", "HTTP", method, mNode.Ip, mNode.Port, mNode.Uri)
  886. //mNode.AssumedAppId = Md5ToInt64(descAddr, 16)
  887. }
  888. //func buildMysqlMap(mNode *MapInfoT, sd apmTraceSpan) {
  889. // mNode.Dbn = "unknown"
  890. // mNode.ServiceName = MYSQL_SERVICE_NAME
  891. // mNode.ServiceType = SQL_SERVICE_TYPE
  892. // mNode.MethodName = "database/sql.Query()"
  893. // for _, attr := range sd.Attributes() {
  894. // //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  895. // switch attr.Key {
  896. // case "net.peer.name":
  897. // mNode.Ip = attr.Value.AsString()
  898. // case "net.peer.port":
  899. // mNode.Port = attr.Value.AsInt64()
  900. // case "db.statement":
  901. // query := attr.Value.AsString()
  902. // mNode.Ps = []string{query}
  903. // words := strings.Fields(query)
  904. // if len(words) > 0 {
  905. // mNode.OperType = strings.ToUpper(words[0])
  906. // }
  907. // }
  908. // }
  909. //}
  910. func buildSQLMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  911. mNode.Dbn = "-"
  912. mNode.ServiceName = l7.Protocol(event.ProtocolType).ServiceNameString()
  913. mNode.ServiceType = SQL_SERVICE_TYPE
  914. //mNode.MethodName = "database/sql.Query()"
  915. for _, attr := range event.Attributes {
  916. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  917. switch attr.Key {
  918. case "net.peer.name":
  919. mNode.Ip = attr.Value.AsString()
  920. case "net.peer.port":
  921. mNode.Port = attr.Value.AsInt64()
  922. case "db.statement":
  923. query := attr.Value.AsString()
  924. mNode.MethodName = query
  925. mNode.Ps = []string{query}
  926. //words := strings.Fields(query)
  927. //if len(words) > 0 {
  928. // mNode.OperType = strings.ToUpper(words[0])
  929. //}
  930. case "sql.exception":
  931. if attr.Value.AsBool() {
  932. mNode.Exception = 1
  933. } else {
  934. mNode.Exception = 0
  935. }
  936. case "sql.exception_msg":
  937. mNode.ExceptionMsg = attr.Value.AsString()
  938. case "sql.src_addr":
  939. mNode.SrcAddr = attr.Value.AsString()
  940. case "sql.destination_addr":
  941. mNode.DestinationAddr = attr.Value.AsString()
  942. case "sql.dbn":
  943. mNode.Dbn = attr.Value.AsString()
  944. }
  945. }
  946. }
  947. func buildDNSMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  948. mNode.ServiceName = l7.Protocol(event.ProtocolType).ServiceNameString()
  949. mNode.ServiceType = NET_SERVICE_TYPE
  950. var _type string
  951. var fqdn string
  952. var ips string
  953. var ttl int64
  954. for _, attr := range event.Attributes {
  955. switch attr.Key {
  956. case "dns.type":
  957. _type = attr.Value.AsString()
  958. case "dns.fqdn":
  959. fqdn = attr.Value.AsString()
  960. case "dns.ttl":
  961. ttl = attr.Value.AsInt64()
  962. case "dns.ips":
  963. if attr.Value.AsString() != "" {
  964. ips = "Addr: " + attr.Value.AsString()
  965. }
  966. }
  967. }
  968. mNode.MethodName = fmt.Sprintf("DNS Name: %s Type: %s TTL: %d %s", fqdn, _type, ttl, ips)
  969. }
  970. func buildPostGreSqlMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  971. mNode.Dbn = "-"
  972. mNode.ServiceName = POSTGRESQL_SERVICE_NAME
  973. mNode.ServiceType = SQL_SERVICE_TYPE
  974. //mNode.MethodName = "database/sql.Query()"
  975. for _, attr := range event.Attributes {
  976. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  977. switch attr.Key {
  978. case "net.peer.name":
  979. mNode.Ip = attr.Value.AsString()
  980. case "net.peer.port":
  981. mNode.Port = attr.Value.AsInt64()
  982. case "db.statement":
  983. query := attr.Value.AsString()
  984. mNode.Ps = []string{query}
  985. //words := strings.Fields(query)
  986. //if len(words) > 0 {
  987. // mNode.OperType = strings.ToUpper(words[0])
  988. //}
  989. case "sql.exception":
  990. if attr.Value.AsBool() {
  991. mNode.Exception = 1
  992. } else {
  993. mNode.Exception = 0
  994. }
  995. case "sql.src_addr":
  996. mNode.SrcAddr = attr.Value.AsString()
  997. case "sql.destination_addr":
  998. mNode.DestinationAddr = attr.Value.AsString()
  999. }
  1000. }
  1001. }
  1002. func buildMysqlMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  1003. mNode.Dbn = "-"
  1004. mNode.ServiceName = MYSQL_SERVICE_NAME
  1005. mNode.ServiceType = SQL_SERVICE_TYPE
  1006. //mNode.MethodName = "database/sql.Query()"
  1007. for _, attr := range event.Attributes {
  1008. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  1009. switch attr.Key {
  1010. case "net.peer.name":
  1011. mNode.Ip = attr.Value.AsString()
  1012. case "net.peer.port":
  1013. mNode.Port = attr.Value.AsInt64()
  1014. case "db.statement":
  1015. query := attr.Value.AsString()
  1016. mNode.MethodName = query
  1017. mNode.Ps = []string{query}
  1018. //words := strings.Fields(query)
  1019. //if len(words) > 0 {
  1020. // mNode.OperType = strings.ToUpper(words[0])
  1021. //}
  1022. case "sql.exception":
  1023. if attr.Value.AsBool() {
  1024. mNode.Exception = 1
  1025. } else {
  1026. mNode.Exception = 0
  1027. }
  1028. case "sql.src_addr":
  1029. mNode.SrcAddr = attr.Value.AsString()
  1030. case "sql.destination_addr":
  1031. mNode.DestinationAddr = attr.Value.AsString()
  1032. }
  1033. }
  1034. }
  1035. func buildDMMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  1036. mNode.Dbn = "TEST"
  1037. mNode.ServiceName = DM_SERVICE_NAME
  1038. mNode.ServiceType = SQL_SERVICE_TYPE
  1039. mNode.MethodName = "database/sql.Query()"
  1040. for _, attr := range event.Attributes {
  1041. //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  1042. switch attr.Key {
  1043. case "net.peer.name":
  1044. mNode.Ip = attr.Value.AsString()
  1045. case "net.peer.port":
  1046. mNode.Port = attr.Value.AsInt64()
  1047. case "db.statement":
  1048. query := attr.Value.AsString()
  1049. mNode.Ps = []string{query}
  1050. //words := strings.Fields(query)
  1051. //if len(words) > 0 {
  1052. // mNode.OperType = strings.ToUpper(words[0])
  1053. //}
  1054. case "sql.exception":
  1055. if attr.Value.AsBool() {
  1056. mNode.Exception = 1
  1057. } else {
  1058. mNode.Exception = 0
  1059. }
  1060. case "sql.src_addr":
  1061. mNode.SrcAddr = attr.Value.AsString()
  1062. case "sql.destination_addr":
  1063. mNode.DestinationAddr = attr.Value.AsString()
  1064. }
  1065. }
  1066. }
  1067. func buildNoSqlMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  1068. mNode.ServiceName = l7.Protocol(event.ProtocolType).ServiceNameString()
  1069. mNode.ServiceType = NOSQL_SERVICE_TYPE
  1070. for _, attr := range event.Attributes {
  1071. switch attr.Key {
  1072. case "net.peer.name":
  1073. mNode.Ip = attr.Value.AsString()
  1074. case "net.peer.port":
  1075. mNode.Port = attr.Value.AsInt64()
  1076. case "db.statement":
  1077. query := attr.Value.AsString()
  1078. mNode.MethodName = query
  1079. mNode.Ps = []string{query}
  1080. case "nosql.src_addr":
  1081. mNode.SrcAddr = attr.Value.AsString()
  1082. case "nosql.destination_addr":
  1083. mNode.DestinationAddr = attr.Value.AsString()
  1084. case "nosql.exception":
  1085. if attr.Value.AsBool() {
  1086. mNode.Exception = 1
  1087. }
  1088. case "nosql.exception_msg":
  1089. mNode.ExceptionMsg = attr.Value.AsString()
  1090. }
  1091. }
  1092. }
  1093. func buildMQMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  1094. mNode.ServiceName = l7.Protocol(event.ProtocolType).ServiceNameString()
  1095. mNode.ServiceType = MQ_SERVICE_TYPE
  1096. for _, attr := range event.Attributes {
  1097. switch attr.Key {
  1098. case "net.peer.name":
  1099. mNode.Ip = attr.Value.AsString()
  1100. case "net.peer.port":
  1101. mNode.Port = attr.Value.AsInt64()
  1102. case "mq.info":
  1103. query := attr.Value.AsString()
  1104. mNode.MethodName = query
  1105. case "mq.topic":
  1106. mNode.Uri = "/" + attr.Value.AsString()
  1107. case "mq.destination_addr":
  1108. mNode.DestinationAddr = attr.Value.AsString()
  1109. case "mq.exception":
  1110. if attr.Value.AsBool() {
  1111. mNode.Exception = 1
  1112. }
  1113. case "mq.exception_msg":
  1114. mNode.ExceptionMsg = attr.Value.AsString()
  1115. case "mq.oper_type":
  1116. mNode.OperType = attr.Value.AsString()
  1117. case "mq.assumed_app_id":
  1118. mNode.AssumedAppId = attr.Value.AsInt64()
  1119. case "mq.span_id":
  1120. mNode.SpanId = attr.Value.AsString()
  1121. }
  1122. }
  1123. }
  1124. func buildMongoMapEvent(mNode *MapInfoT, event tracesdk.Event) {
  1125. mNode.ServiceName = MONGO_SERVICE_NAME
  1126. mNode.ServiceType = NOSQL_SERVICE_TYPE
  1127. for _, attr := range event.Attributes {
  1128. switch attr.Key {
  1129. case "net.peer.name":
  1130. mNode.Ip = attr.Value.AsString()
  1131. case "net.peer.port":
  1132. mNode.Port = attr.Value.AsInt64()
  1133. case "db.statement":
  1134. query := attr.Value.AsString()
  1135. mNode.MethodName = query
  1136. mNode.Ps = []string{query}
  1137. case "nosql.src_addr":
  1138. mNode.SrcAddr = attr.Value.AsString()
  1139. case "nosql.destination_addr":
  1140. mNode.DestinationAddr = attr.Value.AsString()
  1141. }
  1142. }
  1143. }
  1144. func isEnter(_type string) bool {
  1145. if _type == "APPLICATION" {
  1146. return true
  1147. }
  1148. return false
  1149. }
  1150. func span(sd apmTraceSpan) *tracepb.Span {
  1151. if sd == nil {
  1152. return nil
  1153. }
  1154. tid := sd.SpanContext().TraceID()
  1155. sid := sd.SpanContext().SpanID()
  1156. s := &tracepb.Span{
  1157. TraceId: tid[:],
  1158. SpanId: sid[:],
  1159. TraceState: sd.SpanContext().TraceState().String(),
  1160. //Status: status(sd.Status().Code, sd.Status().Description),
  1161. StartTimeUnixNano: uint64(sd.StartTime().UnixNano()),
  1162. EndTimeUnixNano: uint64(sd.EndTime().UnixNano()),
  1163. //Links: links(sd.Links()),
  1164. //Kind: spanKind(sd.SpanKind()),
  1165. Name: sd.Name(),
  1166. Attributes: tracetransform.KeyValues(sd.Attributes()),
  1167. //Events: spanEvents(sd.Events()),
  1168. DroppedAttributesCount: uint32(sd.DroppedAttributes()),
  1169. DroppedEventsCount: uint32(sd.DroppedEvents()),
  1170. DroppedLinksCount: uint32(sd.DroppedLinks()),
  1171. }
  1172. if psid := sd.Parent().SpanID(); psid.IsValid() {
  1173. s.ParentSpanId = psid[:]
  1174. }
  1175. return s
  1176. }
  1177. func Md5ToInt64(strParam string, Len int) int64 {
  1178. sign := md5.Sum([]byte(strParam))
  1179. signStr := fmt.Sprintf("%x", sign)
  1180. charArr := []rune(signStr)
  1181. var intStr string
  1182. for _, value := range charArr {
  1183. intStr += strconv.Itoa(int(value))
  1184. }
  1185. intStr = intStr[:Len]
  1186. int64Data, err := strconv.ParseInt(intStr, 10, 64)
  1187. if err != nil {
  1188. return 0
  1189. }
  1190. return int64Data
  1191. }
  1192. // ns,ms
  1193. func cleanNsTime(time int64) (uint64, uint64) {
  1194. return uint64(time), uint64(math.Round(float64(time) / 1e6))
  1195. }