pprof.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package http
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "go-common/app/admin/main/apm/conf"
  6. mpprof "go-common/app/admin/main/apm/model/pprof"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. func buildSvg(c *bm.Context) {
  12. v := new(struct {
  13. URL string `form:"url" validate:"required"`
  14. URI string `form:"uri" validate:"required"`
  15. SvgName string `form:"name" validate:"required"`
  16. Time int64 `form:"time" validate:"required"`
  17. HostName string `form:"hostname" validate:"required"`
  18. })
  19. var err error
  20. if err = c.Bind(v); err != nil {
  21. return
  22. }
  23. if !(v.URI == "profile" || v.URI == "heap") {
  24. c.JSON(nil, ecode.RequestErr)
  25. return
  26. }
  27. err = apmSvc.Pprof(v.URL, v.URI, v.SvgName, v.HostName, v.Time, 2)
  28. c.JSON(nil, err)
  29. }
  30. func readSvg(c *bm.Context) {
  31. v := new(struct {
  32. SvgName string `form:"name" validate:"required"`
  33. URI string `form:"uri" validate:"required"`
  34. HostName string `form:"hostname" validate:"required"`
  35. })
  36. var err error
  37. if err = c.Bind(v); err != nil {
  38. return
  39. }
  40. url := conf.Conf.Pprof.Dir + "/" + v.SvgName + "_" + v.HostName + "_" + v.URI + ".svg"
  41. data, err := ioutil.ReadFile(url)
  42. if err != nil {
  43. log.Error("readfile error(%v)", err)
  44. c.JSON(nil, err)
  45. return
  46. }
  47. c.Bytes(http.StatusOK, "image/svg+xml; charset=utf-8", data)
  48. }
  49. func heap(c *bm.Context) {
  50. v := new(struct {
  51. URL string `form:"url" validate:"required"`
  52. URI string `form:"uri" validate:"required"`
  53. SvgName string `form:"name" validate:"required"`
  54. HostName string `form:"hostname" validate:"required"`
  55. })
  56. var err error
  57. var data []byte
  58. if err = c.Bind(v); err != nil {
  59. return
  60. }
  61. if v.URI != "heap" {
  62. c.JSON(nil, ecode.RequestErr)
  63. return
  64. }
  65. err = apmSvc.Pprof(v.URL, v.URI, v.SvgName, v.HostName, 1, 1)
  66. if err != nil {
  67. c.JSON(nil, err)
  68. return
  69. }
  70. url := conf.Conf.Pprof.Dir + "/" + v.SvgName + "_" + v.HostName + "_" + v.URI + ".svg"
  71. data, err = ioutil.ReadFile(url)
  72. if err != nil {
  73. log.Error("readfile error(%v)", err)
  74. c.JSON(nil, err)
  75. return
  76. }
  77. c.Bytes(http.StatusOK, "image/svg+xml; charset=utf-8", data)
  78. }
  79. func flame(c *bm.Context) {
  80. v := new(struct {
  81. URL string `form:"url" validate:"required"`
  82. URI string `form:"uri" validate:"required"`
  83. SvgName string `form:"name" validate:"required"`
  84. Time int64 `form:"time"`
  85. HostName string `form:"hostname" validate:"required"`
  86. })
  87. var err error
  88. var data []byte
  89. if err = c.Bind(v); err != nil {
  90. return
  91. }
  92. if !(v.URI == "profile" || v.URI == "heap") {
  93. c.JSON(nil, ecode.RequestErr)
  94. return
  95. }
  96. if v.URI == "heap" {
  97. err = apmSvc.Torch(c, v.URL, v.URI, v.SvgName, v.HostName, 1, 1)
  98. if err != nil {
  99. c.JSON(nil, err)
  100. return
  101. }
  102. url := conf.Conf.Pprof.Dir + "/" + v.SvgName + "_" + v.HostName + "_" + v.URI + "_flame.svg"
  103. data, err = ioutil.ReadFile(url)
  104. if err != nil {
  105. log.Error("readfile error(%v)", err)
  106. c.JSON(nil, err)
  107. return
  108. }
  109. c.Bytes(http.StatusOK, "image/svg+xml; charset=utf-8", data)
  110. } else {
  111. err = apmSvc.Torch(c, v.URL, v.URI, v.SvgName, v.HostName, v.Time, 2)
  112. c.JSON(nil, err)
  113. }
  114. }
  115. func activeWarning(c *bm.Context) {
  116. // var (
  117. // err error
  118. // body []byte
  119. // )
  120. // if body, err = ioutil.ReadAll(c.Request.Body); err != nil {
  121. // c.JSON(nil, fmt.Errorf("warning body empty"))
  122. // return
  123. // }
  124. // if err = apmSvc.ActiveWarning(c, string(body)); err != nil {
  125. // c.JSON(nil, err)
  126. // return
  127. // }
  128. // ioutil.ReadAll You need close body.
  129. c.JSON(nil, nil)
  130. }
  131. func pprof(c *bm.Context) {
  132. var (
  133. err error
  134. pws = make([]*mpprof.Warn, 0)
  135. req = &mpprof.Params{}
  136. )
  137. if err = c.Bind(req); err != nil {
  138. c.JSON(nil, err)
  139. return
  140. }
  141. if pws, err = apmSvc.PprofWarn(c, req); err != nil {
  142. c.JSON(nil, err)
  143. return
  144. }
  145. c.JSON(pws, nil)
  146. }