metrics.go 3.1 KB

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