container_apm.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package containers
  2. import (
  3. "fmt"
  4. "github.com/coroot/coroot-node-agent/ebpftracer/l7"
  5. "github.com/coroot/coroot-node-agent/tracing"
  6. "github.com/coroot/coroot-node-agent/utils"
  7. "inet.af/netaddr"
  8. )
  9. func (c *Container) getTrace(traceId uint64) (*tracing.Trace, bool) {
  10. trace, ok := c.traceMap[traceId]
  11. return trace, ok
  12. }
  13. func (c *Container) InitTrace(traceId uint64, r *l7.RequestData) error {
  14. method, path, hostIp, port := l7.ParseHttpHost(r.Payload)
  15. ip, err := netaddr.ParseIP(hostIp)
  16. if err != nil {
  17. return fmt.Errorf("host ip error")
  18. }
  19. addr := netaddr.IPPortFrom(ip, port)
  20. trace := tracing.NewTrace(string(c.id), addr)
  21. if trace == nil {
  22. return fmt.Errorf("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT is null")
  23. }
  24. c.traceMap[traceId] = trace
  25. trace.TraceStart(method, path, r.Status, r.Duration)
  26. return nil
  27. }
  28. func (c *Container) onL7RequestApm(pid uint32, fd uint64, timestamp uint64, r *l7.RequestData) map[netaddr.IP]string {
  29. c.lock.Lock()
  30. defer c.lock.Unlock()
  31. if r.Protocol == l7.ProtocolDNS {
  32. return c.onDNSRequest(r)
  33. }
  34. if r.Protocol == l7.ProtocolTrace {
  35. //fmt.Println("r.TraceStart:", r.TraceStart)
  36. //fmt.Println("r.TraceEnd:", r.TraceEnd)
  37. if r.TraceStart == 1 {
  38. //fmt.Println("====ProtocolTrace start1====", r.TraceId)
  39. err := c.InitTrace(r.TraceId, r)
  40. if err != nil {
  41. fmt.Println(err)
  42. }
  43. //fmt.Println("init r.TraceId:", r.TraceId)
  44. //trace, _ := c.getTrace(r.TraceId)
  45. //fmt.Println("init traceId", trace)
  46. //stats.observe(r.Status.Http(), "", r.Duration)
  47. //method, path := l7.ParseHttp(r.Payload)
  48. //fmt.Println("r.Payload:", string(r.Payload))
  49. //fmt.Println("method:", method)
  50. //fmt.Println("path:", path)
  51. //fmt.Println("====ProtocolTrace start2====")
  52. return nil
  53. }
  54. if r.TraceEnd == 1 {
  55. //fmt.Println("r:", r)
  56. //fmt.Println("r.Payload:", string(r.Payload))
  57. //fmt.Println("====ProtocolTrace end2====")
  58. trace, ok := c.getTrace(r.TraceId)
  59. if ok {
  60. trace.TraceEnd(r)
  61. delete(c.traceMap, r.TraceId)
  62. }
  63. //fmt.Println("====ProtocolTrace end1====", ok, r.TraceId)
  64. return nil
  65. }
  66. }
  67. //if r.Protocol != l7.ProtocolHTTP {
  68. //
  69. //}
  70. conn := c.connectionsByPidFd[PidFd{Pid: pid, Fd: fd}]
  71. fmt.Println("l7.connectionsByPidFd", conn, pid, fd)
  72. if conn == nil {
  73. return nil
  74. }
  75. fmt.Println("l7.timestamp", timestamp, conn.Timestamp)
  76. //if r.Protocol != l7.ProtocolHTTP {
  77. //
  78. //}
  79. if r.Protocol != l7.ProtocolHTTP && timestamp != 0 && conn.Timestamp != timestamp {
  80. return nil
  81. }
  82. stats := c.l7Stats.get(r.Protocol, conn.Dest, conn.ActualDest)
  83. trace := tracing.NewTrace(string(c.id), conn.ActualDest)
  84. switch r.Protocol {
  85. case l7.ProtocolHTTP:
  86. fmt.Println("l7.ProtocolHTTP", r.TraceId)
  87. stats.observe(r.Status.Http(), "", r.Duration)
  88. method, path, hostIp, port := l7.ParseHttpHost(r.Payload)
  89. //trace.HttpRequest(method, path, r.Status, r.Duration)
  90. apmTrace, ok := c.getTrace(r.TraceId)
  91. if ok {
  92. apmTrace.HttpTraceRequest(method, path, hostIp, port, r)
  93. }
  94. case l7.ProtocolHTTP2:
  95. if conn.http2Parser == nil {
  96. conn.http2Parser = l7.NewHttp2Parser()
  97. }
  98. requests := conn.http2Parser.Parse(r.Method, r.Payload, uint64(r.Duration))
  99. for _, req := range requests {
  100. stats.observe(req.Status.Http(), "", req.Duration)
  101. trace.Http2Request(req.Method, req.Path, req.Scheme, req.Status, req.Duration)
  102. }
  103. case l7.ProtocolPostgres:
  104. if r.Method != l7.MethodStatementClose {
  105. stats.observe(r.Status.String(), "", r.Duration)
  106. }
  107. if conn.postgresParser == nil {
  108. conn.postgresParser = l7.NewPostgresParser()
  109. }
  110. query := conn.postgresParser.Parse(r.Payload)
  111. trace.PostgresQuery(query, r.Status.Error(), r.Duration)
  112. case l7.ProtocolMysql:
  113. //fmt.Println("mysql mysql")
  114. //fmt.Println(conn)
  115. if r.Method != l7.MethodStatementClose {
  116. stats.observe(r.Status.String(), "", r.Duration)
  117. }
  118. if conn.mysqlParser == nil {
  119. conn.mysqlParser = l7.NewMysqlParser()
  120. }
  121. query := conn.mysqlParser.Parse(r.Payload, r.StatementId)
  122. //trace.MysqlQuery(query, r.Status.Error(), r.Duration)
  123. apmTrace, ok := c.getTrace(r.TraceId)
  124. //fmt.Println("mysql r.TraceId:", r.TraceId)
  125. //fmt.Println("ok:", ok)
  126. //fmt.Println("traceMap:", len(c.traceMap))
  127. if ok {
  128. apmTrace.MysqlTraceQuery(query, r.Status.Error(), r.Duration, conn.ActualDest)
  129. }
  130. case l7.ProtocolMemcached:
  131. stats.observe(r.Status.String(), "", r.Duration)
  132. cmd, items := l7.ParseMemcached(r.Payload)
  133. trace.MemcachedQuery(cmd, items, r.Status.Error(), r.Duration)
  134. case l7.ProtocolRedis:
  135. fmt.Println("redis redis")
  136. stats.observe(r.Status.String(), "", r.Duration)
  137. cmd, args := l7.ParseRedis(r.Payload)
  138. fmt.Println("cmd", cmd)
  139. fmt.Println("args", args)
  140. apmTrace, ok := c.getTrace(r.TraceId)
  141. fmt.Println("redis r.TraceId:", r.TraceId)
  142. fmt.Println("ok:", ok)
  143. fmt.Println("traceMap:", len(c.traceMap))
  144. if ok {
  145. apmTrace.RedisTraceQuery(cmd, args, r.Status.Error(), r.Duration)
  146. }
  147. //trace.RedisQuery(cmd, args, r.Status.Error(), r.Duration)
  148. case l7.ProtocolMongo:
  149. stats.observe(r.Status.String(), "", r.Duration)
  150. query := l7.ParseMongo(r.Payload)
  151. trace.MongoQuery(query, r.Status.Error(), r.Duration)
  152. case l7.ProtocolKafka, l7.ProtocolCassandra:
  153. stats.observe(r.Status.String(), "", r.Duration)
  154. case l7.ProtocolRabbitmq, l7.ProtocolNats:
  155. stats.observe(r.Status.String(), r.Method.String(), 0)
  156. }
  157. return nil
  158. }
  159. func (c *Container) buildInstanceID() {
  160. c.lock.Lock()
  161. defer c.lock.Unlock()
  162. for address, val := range c.getListens() {
  163. if val == 1 {
  164. ip := address.IP()
  165. if ip.Is4() && !ip.IsLoopback() {
  166. // 获取端口号
  167. port := address.Port()
  168. c.instanceID.IntVal, c.instanceID.HashtVal = utils.SetInsID(fmt.Sprintf("%s:%d", ip, port))
  169. break
  170. }
  171. }
  172. }
  173. }