From 755268b7d488a6d5108d78cae2d3cc72f7c88729 Mon Sep 17 00:00:00 2001 From: yichya Date: Fri, 1 Apr 2022 16:38:27 +0800 Subject: [PATCH] fix: expvar error when observatory is not enabled & various typos --- app/metrics/metrics.go | 3 +++ app/metrics/outbound.go | 4 ++-- features/stats/stats.go | 8 ++++---- testing/scenarios/metrics_test.go | 4 +++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/metrics/metrics.go b/app/metrics/metrics.go index 2f1d6baa..d4c59590 100644 --- a/app/metrics/metrics.go +++ b/app/metrics/metrics.go @@ -64,6 +64,9 @@ func NewMetricsHandler(ctx context.Context, config *Config) (*MetricsHandler, er c.observatory = observatory return nil })) + if c.observatory == nil { + return nil + } } var resp = map[string]*observatory.OutboundStatus{} if o, err := c.observatory.GetObservation(context.Background()); err != nil { diff --git a/app/metrics/outbound.go b/app/metrics/outbound.go index 7505e2bf..32c0192b 100644 --- a/app/metrics/outbound.go +++ b/app/metrics/outbound.go @@ -11,7 +11,7 @@ import ( "github.com/xtls/xray-core/transport" ) -// OutboundListener is a net.Listener for listening pprof http connections. +// OutboundListener is a net.Listener for listening metrics http connections. type OutboundListener struct { buffer chan net.Conn done *done.Instance @@ -60,7 +60,7 @@ func (l *OutboundListener) Addr() net.Addr { } } -// Outbound is a outbound.Handler that handles pprof http connections. +// Outbound is an outbound.Handler that handles metrics http connections. type Outbound struct { tag string listener *OutboundListener diff --git a/features/stats/stats.go b/features/stats/stats.go index 585a6298..bf83df78 100644 --- a/features/stats/stats.go +++ b/features/stats/stats.go @@ -25,11 +25,11 @@ type Counter interface { // // xray:api:stable type Channel interface { - // Channel is a runnable unit. + // Runnable implies that Channel is a runnable unit. common.Runnable // Publish broadcasts a message through the channel with a controlling context. Publish(context.Context, interface{}) - // SubscriberCount returns the number of the subscribers. + // Subscribers returns all subscribers. Subscribers() []chan interface{} // Subscribe registers for listening to channel stream and returns a new listener channel. Subscribe() (chan interface{}, error) @@ -47,7 +47,7 @@ func SubscribeRunnableChannel(c Channel) (chan interface{}, error) { return c.Subscribe() } -// UnsubscribeClosableChannel unsubcribes the channel and close it if there is no more subscriber. +// UnsubscribeClosableChannel unsubscribes the channel and close it if there is no more subscriber. func UnsubscribeClosableChannel(c Channel, sub chan interface{}) error { if err := c.Unsubscribe(sub); err != nil { return err @@ -73,7 +73,7 @@ type Manager interface { // RegisterChannel registers a new channel to the manager. The identifier string must not be empty, and unique among other channels. RegisterChannel(string) (Channel, error) - // UnregisterCounter unregisters a channel from the manager by its identifier. + // UnregisterChannel unregisters a channel from the manager by its identifier. UnregisterChannel(string) error // GetChannel returns a channel by its identifier. GetChannel(string) Channel diff --git a/testing/scenarios/metrics_test.go b/testing/scenarios/metrics_test.go index 8c30c514..914e46c4 100644 --- a/testing/scenarios/metrics_test.go +++ b/testing/scenarios/metrics_test.go @@ -1,6 +1,7 @@ package scenarios import ( + "encoding/json" "fmt" "io/ioutil" "net/http" @@ -99,7 +100,8 @@ func TestMetrics(t *testing.T) { if err2 != nil { t.Fatal(err2) } - if string(body2)[0] != '{' { + var json2 map[string]interface{} + if json.Unmarshal(body2, &json2) != nil { t.Error("unexpected response body from expvars handler") } }