metrics.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package metrics
  2. import (
  3. "context"
  4. "expvar"
  5. "net/http"
  6. _ "net/http/pprof"
  7. "strings"
  8. "github.com/xtls/xray-core/app/observatory"
  9. "github.com/xtls/xray-core/app/stats"
  10. "github.com/xtls/xray-core/common"
  11. "github.com/xtls/xray-core/common/errors"
  12. "github.com/xtls/xray-core/common/net"
  13. "github.com/xtls/xray-core/common/signal/done"
  14. "github.com/xtls/xray-core/core"
  15. "github.com/xtls/xray-core/features/extension"
  16. "github.com/xtls/xray-core/features/outbound"
  17. feature_stats "github.com/xtls/xray-core/features/stats"
  18. )
  19. type MetricsHandler struct {
  20. ohm outbound.Manager
  21. statsManager feature_stats.Manager
  22. observatory extension.Observatory
  23. tag string
  24. }
  25. // NewMetricsHandler creates a new MetricsHandler based on the given config.
  26. func NewMetricsHandler(ctx context.Context, config *Config) (*MetricsHandler, error) {
  27. c := &MetricsHandler{
  28. tag: config.Tag,
  29. }
  30. common.Must(core.RequireFeatures(ctx, func(om outbound.Manager, sm feature_stats.Manager) {
  31. c.statsManager = sm
  32. c.ohm = om
  33. }))
  34. expvar.Publish("stats", expvar.Func(func() interface{} {
  35. manager, ok := c.statsManager.(*stats.Manager)
  36. if !ok {
  37. return nil
  38. }
  39. resp := map[string]map[string]map[string]int64{
  40. "inbound": {},
  41. "outbound": {},
  42. "user": {},
  43. }
  44. manager.VisitCounters(func(name string, counter feature_stats.Counter) bool {
  45. nameSplit := strings.Split(name, ">>>")
  46. typeName, tagOrUser, direction := nameSplit[0], nameSplit[1], nameSplit[3]
  47. if item, found := resp[typeName][tagOrUser]; found {
  48. item[direction] = counter.Value()
  49. } else {
  50. resp[typeName][tagOrUser] = map[string]int64{
  51. direction: counter.Value(),
  52. }
  53. }
  54. return true
  55. })
  56. return resp
  57. }))
  58. expvar.Publish("observatory", expvar.Func(func() interface{} {
  59. if c.observatory == nil {
  60. common.Must(core.RequireFeatures(ctx, func(observatory extension.Observatory) error {
  61. c.observatory = observatory
  62. return nil
  63. }))
  64. if c.observatory == nil {
  65. return nil
  66. }
  67. }
  68. resp := map[string]*observatory.OutboundStatus{}
  69. if o, err := c.observatory.GetObservation(context.Background()); err != nil {
  70. return err
  71. } else {
  72. for _, x := range o.(*observatory.ObservationResult).GetStatus() {
  73. resp[x.OutboundTag] = x
  74. }
  75. }
  76. return resp
  77. }))
  78. return c, nil
  79. }
  80. func (p *MetricsHandler) Type() interface{} {
  81. return (*MetricsHandler)(nil)
  82. }
  83. func (p *MetricsHandler) Start() error {
  84. listener := &OutboundListener{
  85. buffer: make(chan net.Conn, 4),
  86. done: done.New(),
  87. }
  88. go func() {
  89. if err := http.Serve(listener, http.DefaultServeMux); err != nil {
  90. errors.LogErrorInner(context.Background(), err, "failed to start metrics server")
  91. }
  92. }()
  93. if err := p.ohm.RemoveHandler(context.Background(), p.tag); err != nil {
  94. errors.LogInfo(context.Background(), "failed to remove existing handler")
  95. }
  96. return p.ohm.AddHandler(context.Background(), &Outbound{
  97. tag: p.tag,
  98. listener: listener,
  99. })
  100. }
  101. func (p *MetricsHandler) Close() error {
  102. return nil
  103. }
  104. func init() {
  105. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
  106. return NewMetricsHandler(ctx, cfg.(*Config))
  107. }))
  108. }