monitor.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package monitor
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "github.com/labring/aiproxy/core/common"
  10. "github.com/labring/aiproxy/core/common/conv"
  11. "github.com/labring/aiproxy/core/common/notify"
  12. "github.com/labring/aiproxy/core/common/reqlimit"
  13. "github.com/labring/aiproxy/core/common/trylock"
  14. "github.com/labring/aiproxy/core/model"
  15. "github.com/labring/aiproxy/core/monitor"
  16. "github.com/labring/aiproxy/core/relay/adaptor"
  17. "github.com/labring/aiproxy/core/relay/meta"
  18. "github.com/labring/aiproxy/core/relay/plugin"
  19. "github.com/labring/aiproxy/core/relay/plugin/noop"
  20. )
  21. var _ plugin.Plugin = (*ChannelMonitor)(nil)
  22. type ChannelMonitor struct {
  23. noop.Noop
  24. }
  25. func NewChannelMonitorPlugin() plugin.Plugin {
  26. return &ChannelMonitor{}
  27. }
  28. var channelNoRetryStatusCodesMap = map[int]struct{}{
  29. http.StatusBadRequest: {},
  30. http.StatusRequestEntityTooLarge: {},
  31. http.StatusUnprocessableEntity: {},
  32. http.StatusUnavailableForLegalReasons: {},
  33. }
  34. func ShouldRetry(relayErr adaptor.Error) bool {
  35. _, ok := channelNoRetryStatusCodesMap[relayErr.StatusCode()]
  36. return !ok
  37. }
  38. var channelNoPermissionStatusCodesMap = map[int]struct{}{
  39. http.StatusUnauthorized: {},
  40. http.StatusPaymentRequired: {},
  41. http.StatusForbidden: {},
  42. http.StatusNotFound: {},
  43. }
  44. func ChannelHasPermission(relayErr adaptor.Error) bool {
  45. _, ok := channelNoPermissionStatusCodesMap[relayErr.StatusCode()]
  46. return !ok
  47. }
  48. func (m *ChannelMonitor) DoRequest(
  49. meta *meta.Meta,
  50. store adaptor.Store,
  51. c *gin.Context,
  52. req *http.Request,
  53. do adaptor.DoRequest,
  54. ) (*http.Response, error) {
  55. count, overLimitCount, secondCount := reqlimit.PushChannelModelRequest(
  56. context.Background(),
  57. strconv.Itoa(meta.Channel.ID),
  58. meta.OriginModel,
  59. )
  60. updateChannelModelRequestRate(c, meta, count+overLimitCount, secondCount)
  61. return do.DoRequest(meta, store, c, req)
  62. }
  63. func (m *ChannelMonitor) DoResponse(
  64. meta *meta.Meta,
  65. store adaptor.Store,
  66. c *gin.Context,
  67. resp *http.Response,
  68. do adaptor.DoResponse,
  69. ) (model.Usage, adaptor.Error) {
  70. log := common.GetLogger(c)
  71. usage, relayErr := do.DoResponse(meta, store, c, resp)
  72. if usage.TotalTokens > 0 {
  73. count, overLimitCount, secondCount := reqlimit.PushChannelModelTokensRequest(
  74. context.Background(),
  75. strconv.Itoa(meta.Channel.ID),
  76. meta.OriginModel,
  77. int64(usage.TotalTokens),
  78. )
  79. updateChannelModelTokensRequestRate(c, meta, count+overLimitCount, secondCount)
  80. }
  81. if relayErr == nil {
  82. if _, _, err := monitor.AddRequest(
  83. context.Background(),
  84. meta.OriginModel,
  85. int64(meta.Channel.ID),
  86. false,
  87. false,
  88. meta.ModelConfig.MaxErrorRate,
  89. ); err != nil {
  90. log.Errorf("add request failed: %+v", err)
  91. }
  92. return usage, nil
  93. }
  94. if !ShouldRetry(relayErr) {
  95. return usage, relayErr
  96. }
  97. hasPermission := ChannelHasPermission(relayErr)
  98. beyondThreshold, banExecution, err := monitor.AddRequest(
  99. context.Background(),
  100. meta.OriginModel,
  101. int64(meta.Channel.ID),
  102. true,
  103. !hasPermission,
  104. meta.ModelConfig.MaxErrorRate,
  105. )
  106. if err != nil {
  107. log.Errorf("add request failed: %+v", err)
  108. }
  109. switch {
  110. case banExecution:
  111. notifyChannelIssue(c, meta, "autoBanned", "Auto Banned", relayErr)
  112. case beyondThreshold:
  113. notifyChannelIssue(
  114. c,
  115. meta,
  116. "beyondThreshold",
  117. "Error Rate Beyond Threshold",
  118. relayErr,
  119. )
  120. case !hasPermission:
  121. notifyChannelIssue(c, meta, "channelHasPermission", "No Permission", relayErr)
  122. }
  123. return usage, relayErr
  124. }
  125. func notifyChannelIssue(
  126. c *gin.Context,
  127. meta *meta.Meta,
  128. issueType, titleSuffix string,
  129. err adaptor.Error,
  130. ) {
  131. var notifyFunc func(title, message string)
  132. lockKey := fmt.Sprintf("%s:%d:%s", issueType, meta.Channel.ID, meta.OriginModel)
  133. switch issueType {
  134. case "beyondThreshold":
  135. notifyFunc = func(title, message string) {
  136. notify.WarnThrottle(lockKey, time.Minute, title, message)
  137. }
  138. default:
  139. notifyFunc = func(title, message string) {
  140. notify.ErrorThrottle(lockKey, time.Minute, title, message)
  141. }
  142. }
  143. respBody, _ := err.MarshalJSON()
  144. message := fmt.Sprintf(
  145. "channel: %s (type: %d, type name: %s, id: %d)\nmodel: %s\nmode: %s\nstatus code: %d\ndetail: %s\nrequest id: %s",
  146. meta.Channel.Name,
  147. meta.Channel.Type,
  148. meta.Channel.Type.String(),
  149. meta.Channel.ID,
  150. meta.OriginModel,
  151. meta.Mode,
  152. err.StatusCode(),
  153. conv.BytesToString(respBody),
  154. meta.RequestID,
  155. )
  156. if err.StatusCode() == http.StatusTooManyRequests {
  157. if !trylock.Lock(lockKey, time.Minute) {
  158. return
  159. }
  160. switch issueType {
  161. case "beyondThreshold":
  162. notifyFunc = notify.Warn
  163. default:
  164. notifyFunc = notify.Error
  165. }
  166. rate := GetChannelModelRequestRate(c, meta)
  167. message += fmt.Sprintf(
  168. "\nrpm: %d\nrps: %d\ntpm: %d\ntps: %d",
  169. rate.RPM,
  170. rate.RPS,
  171. rate.TPM,
  172. rate.TPS,
  173. )
  174. }
  175. notifyFunc(
  176. fmt.Sprintf("%s `%s` %s", meta.Channel.Name, meta.OriginModel, titleSuffix),
  177. message,
  178. )
  179. }
  180. const (
  181. MetaChannelModelKeyRPM = "channel_model_rpm"
  182. MetaChannelModelKeyRPS = "channel_model_rps"
  183. MetaChannelModelKeyTPM = "channel_model_tpm"
  184. MetaChannelModelKeyTPS = "channel_model_tps"
  185. )
  186. func GetChannelModelRequestRate(c *gin.Context, meta *meta.Meta) model.RequestRate {
  187. rate := model.RequestRate{}
  188. if rpm, ok := meta.Get(MetaChannelModelKeyRPM); ok {
  189. rate.RPM, _ = rpm.(int64)
  190. rate.RPS = meta.GetInt64(MetaChannelModelKeyRPS)
  191. } else {
  192. rpm, rps := reqlimit.GetChannelModelRequest(context.Background(), strconv.Itoa(meta.Channel.ID), meta.OriginModel)
  193. rate.RPM = rpm
  194. rate.RPS = rps
  195. updateChannelModelRequestRate(c, meta, rpm, rps)
  196. }
  197. if tpm, ok := meta.Get(MetaChannelModelKeyTPM); ok {
  198. rate.TPM, _ = tpm.(int64)
  199. rate.TPS = meta.GetInt64(MetaChannelModelKeyTPS)
  200. } else {
  201. tpm, tps := reqlimit.GetChannelModelTokensRequest(context.Background(), strconv.Itoa(meta.Channel.ID), meta.OriginModel)
  202. rate.TPM = tpm
  203. rate.TPS = tps
  204. updateChannelModelTokensRequestRate(c, meta, tpm, tps)
  205. }
  206. return rate
  207. }
  208. func updateChannelModelRequestRate(c *gin.Context, meta *meta.Meta, rpm, rps int64) {
  209. meta.Set(MetaChannelModelKeyRPM, rpm)
  210. meta.Set(MetaChannelModelKeyRPS, rps)
  211. log := common.GetLogger(c)
  212. log.Data["ch_rpm"] = rpm
  213. log.Data["ch_rps"] = rps
  214. }
  215. func updateChannelModelTokensRequestRate(c *gin.Context, meta *meta.Meta, tpm, tps int64) {
  216. meta.Set(MetaChannelModelKeyTPM, tpm)
  217. meta.Set(MetaChannelModelKeyTPS, tps)
  218. log := common.GetLogger(c)
  219. log.Data["ch_tpm"] = tpm
  220. log.Data["ch_tps"] = tps
  221. }