apm_exporter.go 34 KB

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