Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

65 строки
1.3 KiB

  1. package metrics
  2. import (
  3. "fmt"
  4. "github.com/prometheus/client_golang/prometheus"
  5. "github.com/QuantumNous/new-api/common"
  6. "github.com/QuantumNous/new-api/model"
  7. )
  8. // ChannelCollector 从数据库查询渠道状态并暴露为 Prometheus Gauge
  9. type ChannelCollector struct {
  10. channelStatus *prometheus.Desc
  11. }
  12. // NewChannelCollector 创建渠道状态 Collector
  13. func NewChannelCollector() *ChannelCollector {
  14. return &ChannelCollector{
  15. channelStatus: prometheus.NewDesc(
  16. "newapi_channel_status",
  17. "Channel enabled status (1=enabled, 0=disabled)",
  18. []string{"channel", "channel_type"},
  19. nil,
  20. ),
  21. }
  22. }
  23. func (c *ChannelCollector) Describe(ch chan<- *prometheus.Desc) {
  24. ch <- c.channelStatus
  25. }
  26. func (c *ChannelCollector) Collect(ch chan<- prometheus.Metric) {
  27. if model.DB == nil {
  28. return
  29. }
  30. var channels []struct {
  31. Id int
  32. Type int
  33. Status int
  34. }
  35. err := model.DB.Table("channels").Select("id, type, status").Find(&channels).Error
  36. if err != nil {
  37. return
  38. }
  39. for _, ch2 := range channels {
  40. value := float64(0)
  41. if ch2.Status == common.ChannelStatusEnabled {
  42. value = 1
  43. }
  44. m, err := prometheus.NewConstMetric(
  45. c.channelStatus,
  46. prometheus.GaugeValue,
  47. value,
  48. fmt.Sprintf("%d", ch2.Id),
  49. fmt.Sprintf("%d", ch2.Type),
  50. )
  51. if err == nil {
  52. ch <- m
  53. }
  54. }
  55. }