apm_exporter.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package otlptrace
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform"
  6. tracesdk "go.opentelemetry.io/otel/sdk/trace"
  7. tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
  8. "strings"
  9. )
  10. type RootData struct {
  11. AccountId int `json:"account_id"`
  12. AgentId int64 `json:"agent_id"`
  13. AgentVersion string `json:"agent_version"`
  14. AppId int64 `json:"app_id"`
  15. AppIdFrom int `json:"app_id_from"`
  16. AppName string `json:"app_name"`
  17. CalledId int `json:"called_id"`
  18. ClientIp string `json:"client_ip"`
  19. CollTime uint64 `json:"coll_time"`
  20. Cpu int `json:"cpu"`
  21. Custom string `json:"custom"`
  22. HostId int64 `json:"host_id"`
  23. HostName string `json:"host_name"`
  24. HttpCode int64 `json:"http_code"`
  25. HttpMethod string `json:"http_method"`
  26. InstanceId int64 `json:"instance_id"`
  27. InstanceIdFrom int `json:"instance_id_from"`
  28. LocalPort int64 `json:"local_port"`
  29. Maps []MapInfo `json:"maps"`
  30. MemU int `json:"mem_u"`
  31. MemUP int `json:"mem_u_p"`
  32. OperType string `json:"oper_type"`
  33. Parameters []interface{} `json:"parameters"`
  34. ParentTaskName int `json:"parent_task_name"`
  35. Period int `json:"period"`
  36. RespTime uint64 `json:"resp_time"`
  37. Sampling int `json:"sampling"`
  38. ServiceName string `json:"service_name"`
  39. ServiceType string `json:"service_type"`
  40. Sip string `json:"sip"`
  41. Sn string `json:"sn"`
  42. SpanIdFrom string `json:"span_id_from"`
  43. Sport int64 `json:"sport"`
  44. TId int `json:"t_id"`
  45. TName string `json:"t_name"`
  46. TraceId string `json:"trace_id"`
  47. TransIds []interface{} `json:"trans_ids"`
  48. TypeFrom string `json:"type_from"`
  49. Uri string `json:"uri"`
  50. UserDir int `json:"user_dir"`
  51. VipIds []interface{} `json:"vip_ids"`
  52. }
  53. type MapInfo struct {
  54. Dbn string `json:"dbn,omitempty"`
  55. Exception int `json:"exception"`
  56. ExceptionMsg string `json:"exception_msg"`
  57. ExceptionStack string `json:"exception_stack"`
  58. Ip string `json:"ip,omitempty"`
  59. Level int `json:"level"`
  60. MethodDesc string `json:"method_desc"`
  61. MethodName string `json:"method_name"`
  62. Nid int `json:"nid"`
  63. OperType string `json:"oper_type"`
  64. Pid int `json:"pid"`
  65. Port int64 `json:"port,omitempty"`
  66. Ps []string `json:"ps,omitempty"`
  67. PureTime uint64 `json:"pure_time"`
  68. ServiceName string `json:"service_name"`
  69. ServiceType string `json:"service_type"`
  70. StartTime uint64 `json:"start_time"`
  71. WallTime uint64 `json:"wall_time"`
  72. }
  73. type TraceMapT struct {
  74. RootData RootData `json:"root_data"`
  75. Index int `json:"index"`
  76. }
  77. func tracetransformData(sdl []tracesdk.ReadOnlySpan) []RootData {
  78. if len(sdl) == 0 {
  79. return nil
  80. }
  81. //traceMap := make(map[string][]MapInfo)
  82. //traceIndexMap := make(map[string]int)
  83. traceRoot := make(map[string]*TraceMapT)
  84. sendData := []RootData{}
  85. for _, sd := range sdl {
  86. if sd == nil {
  87. continue
  88. }
  89. traceId := sd.SpanContext().TraceID().String()
  90. if _, ok := traceRoot[traceId]; !ok {
  91. traceRoot[traceId] = &TraceMapT{RootData: initRootData(traceId), Index: 1}
  92. }
  93. traceRoot[traceId].Index++
  94. initMapInfo(sd, traceRoot[traceId])
  95. }
  96. for _, v := range traceRoot {
  97. sendData = append(sendData, v.RootData)
  98. }
  99. // Transform the categorized map into a slice
  100. aa, _ := json.Marshal(sendData)
  101. fmt.Println(string(aa))
  102. fmt.Println(len(sendData))
  103. return sendData
  104. }
  105. func initRootData(traceId string) RootData {
  106. data := RootData{
  107. AccountId: 110,
  108. AgentId: 1011005252979954,
  109. AgentVersion: "2.1.0",
  110. AppId: 5410049101545798,
  111. AppIdFrom: -1,
  112. AppName: "eBPF-agent",
  113. CalledId: -1,
  114. ClientIp: "",
  115. CollTime: 0,
  116. Cpu: 0,
  117. Custom: "",
  118. HostId: 10154813500555812,
  119. HostName: "localhost",
  120. HttpCode: 0,
  121. HttpMethod: "GET",
  122. InstanceId: 1005051101515357,
  123. InstanceIdFrom: -1,
  124. Maps: []MapInfo{},
  125. MemU: 0,
  126. MemUP: 0,
  127. OperType: "",
  128. Parameters: []interface{}{},
  129. ParentTaskName: 0,
  130. Period: -1,
  131. RespTime: 0,
  132. Sampling: 0,
  133. ServiceName: "GO",
  134. ServiceType: "APPLICATION",
  135. Sip: "",
  136. Sn: "",
  137. SpanIdFrom: "",
  138. Sport: 0,
  139. TId: -1,
  140. TName: "",
  141. TraceId: traceId,
  142. TransIds: []interface{}{},
  143. TypeFrom: "",
  144. Uri: "",
  145. UserDir: 0,
  146. VipIds: []interface{}{},
  147. }
  148. return data
  149. }
  150. func initMapInfo(sd tracesdk.ReadOnlySpan, traceRoot *TraceMapT) MapInfo {
  151. mNode := MapInfo{
  152. Exception: 0,
  153. ExceptionMsg: "",
  154. ExceptionStack: "",
  155. Ip: "",
  156. Level: 2,
  157. MethodDesc: "unknown",
  158. MethodName: "unknown",
  159. Nid: traceRoot.Index,
  160. OperType: "",
  161. Pid: 1,
  162. Port: 0,
  163. Ps: []string{},
  164. PureTime: (span(sd).EndTimeUnixNano - span(sd).StartTimeUnixNano) / 1e3,
  165. ServiceName: "",
  166. ServiceType: "",
  167. StartTime: span(sd).StartTimeUnixNano / 1e6,
  168. WallTime: 0,
  169. }
  170. mNode.WallTime = mNode.PureTime
  171. mapType := span(sd).Name
  172. switch mapType {
  173. case "MYSQL":
  174. mNode.Dbn = "unknown"
  175. mNode.ServiceName = mapType
  176. mNode.ServiceType = "SQL"
  177. mNode.MethodName = "(mysql)"
  178. mNode.MethodDesc = "(mysql)"
  179. for _, attr := range sd.Attributes() {
  180. fmt.Println(attr.Key, ":", attr.Value.AsInterface())
  181. switch attr.Key {
  182. case "net.peer.name":
  183. mNode.Ip = attr.Value.AsString()
  184. case "net.peer.port":
  185. mNode.Port = attr.Value.AsInt64()
  186. case "db.statement":
  187. query := attr.Value.AsString()
  188. mNode.Ps = []string{query}
  189. upperOperType := strings.ToUpper(query[:6])
  190. if upperOperType == "SELECT" ||
  191. upperOperType == "UPDATE" ||
  192. upperOperType == "INSERT" ||
  193. upperOperType == "DELETE" {
  194. mNode.OperType = upperOperType
  195. }
  196. }
  197. }
  198. traceRoot.RootData.Maps = append(traceRoot.RootData.Maps, mNode)
  199. case "APPLICATION":
  200. mNode.ServiceName = "GO"
  201. mNode.ServiceType = "APPLICATION"
  202. mNode.MethodName = "main"
  203. mNode.MethodDesc = "main"
  204. mNode.Level = 1
  205. mNode.Nid = 1
  206. mNode.Pid = 0
  207. // 构建root节点
  208. traceRoot.RootData.RespTime = mNode.PureTime
  209. traceRoot.RootData.CollTime = mNode.StartTime
  210. for _, attr := range sd.Attributes() {
  211. switch attr.Key {
  212. case "http.uri":
  213. traceRoot.RootData.Uri = attr.Value.AsString()
  214. case "http.method":
  215. traceRoot.RootData.HttpMethod = attr.Value.AsString()
  216. case "http.status_code":
  217. traceRoot.RootData.HttpCode = attr.Value.AsInt64()
  218. case "net.peer.name":
  219. traceRoot.RootData.ClientIp = attr.Value.AsString()
  220. traceRoot.RootData.Sip = attr.Value.AsString()
  221. traceRoot.RootData.Sn = attr.Value.AsString()
  222. case "net.peer.port":
  223. traceRoot.RootData.Sport = attr.Value.AsInt64()
  224. traceRoot.RootData.LocalPort = attr.Value.AsInt64()
  225. }
  226. }
  227. traceRoot.RootData.Maps = append([]MapInfo{mNode}, traceRoot.RootData.Maps...)
  228. }
  229. return mNode
  230. }
  231. func isEnter(_type string) bool {
  232. if _type == "APPLICATION" {
  233. return true
  234. }
  235. return false
  236. }
  237. func span(sd tracesdk.ReadOnlySpan) *tracepb.Span {
  238. if sd == nil {
  239. return nil
  240. }
  241. tid := sd.SpanContext().TraceID()
  242. sid := sd.SpanContext().SpanID()
  243. s := &tracepb.Span{
  244. TraceId: tid[:],
  245. SpanId: sid[:],
  246. TraceState: sd.SpanContext().TraceState().String(),
  247. //Status: status(sd.Status().Code, sd.Status().Description),
  248. StartTimeUnixNano: uint64(sd.StartTime().UnixNano()),
  249. EndTimeUnixNano: uint64(sd.EndTime().UnixNano()),
  250. //Links: links(sd.Links()),
  251. //Kind: spanKind(sd.SpanKind()),
  252. Name: sd.Name(),
  253. Attributes: tracetransform.KeyValues(sd.Attributes()),
  254. //Events: spanEvents(sd.Events()),
  255. DroppedAttributesCount: uint32(sd.DroppedAttributes()),
  256. DroppedEventsCount: uint32(sd.DroppedEvents()),
  257. DroppedLinksCount: uint32(sd.DroppedLinks()),
  258. }
  259. if psid := sd.Parent().SpanID(); psid.IsValid() {
  260. s.ParentSpanId = psid[:]
  261. }
  262. return s
  263. }