mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-17 03:24:40 -07:00
Add /stacksz to monitoring
Allows to get the server's stacks from the monitoring interface.
This commit is contained in:
@@ -352,6 +352,26 @@ func (s *Server) HandleSubsz(w http.ResponseWriter, r *http.Request) {
|
||||
ResponseHandler(w, r, b)
|
||||
}
|
||||
|
||||
// HandleStacksz processes HTTP requests for getting stacks
|
||||
func (s *Server) HandleStacksz(w http.ResponseWriter, r *http.Request) {
|
||||
// Do not get any lock here that would prevent gettign the stacks
|
||||
// if we were to have a deadlock somewhere.
|
||||
var buf []byte
|
||||
size := 10000
|
||||
n := 0
|
||||
for {
|
||||
buf = make([]byte, size)
|
||||
n = runtime.Stack(buf, true)
|
||||
if n < size {
|
||||
break
|
||||
}
|
||||
|
||||
size = 2 * size
|
||||
}
|
||||
// Handle response
|
||||
ResponseHandler(w, r, buf[:n])
|
||||
}
|
||||
|
||||
// Varz will output server information on the monitoring port at /varz.
|
||||
type Varz struct {
|
||||
*Info
|
||||
|
||||
@@ -1206,3 +1206,41 @@ func createClientConnWithName(t *testing.T, name string) *nats.Conn {
|
||||
|
||||
return nc
|
||||
}
|
||||
|
||||
func TestStacksz(t *testing.T) {
|
||||
s := runMonitorServer()
|
||||
defer s.Shutdown()
|
||||
|
||||
url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT)
|
||||
resp, err := http.Get(url + "stacksz")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error: Got %v\n", err)
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("Expected a 200 response, got %d\n", resp.StatusCode)
|
||||
}
|
||||
ct := resp.Header.Get("Content-Type")
|
||||
if ct != "application/json" {
|
||||
t.Fatalf("Expected application/json content-type, got %s\n", ct)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Got an error reading the body: %v\n", err)
|
||||
}
|
||||
// Check content
|
||||
str := string(body)
|
||||
if !strings.Contains(str, "HandleStacksz") {
|
||||
t.Fatalf("Result does not seem to contain server's stacks:\n%v", str)
|
||||
}
|
||||
// Test JSONP
|
||||
respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) + "subscriptionsz?callback=callback")
|
||||
ct = respj.Header.Get("Content-Type")
|
||||
if errj != nil {
|
||||
t.Fatalf("Expected no error: Got %v\n", err)
|
||||
}
|
||||
if ct != "application/javascript" {
|
||||
t.Fatalf("Expected application/javascript content-type, got %s\n", ct)
|
||||
}
|
||||
defer respj.Body.Close()
|
||||
}
|
||||
|
||||
@@ -422,11 +422,12 @@ func (s *Server) StartHTTPSMonitoring() {
|
||||
|
||||
// HTTP endpoints
|
||||
const (
|
||||
RootPath = "/"
|
||||
VarzPath = "/varz"
|
||||
ConnzPath = "/connz"
|
||||
RoutezPath = "/routez"
|
||||
SubszPath = "/subsz"
|
||||
RootPath = "/"
|
||||
VarzPath = "/varz"
|
||||
ConnzPath = "/connz"
|
||||
RoutezPath = "/routez"
|
||||
SubszPath = "/subsz"
|
||||
StackszPath = "/stacksz"
|
||||
)
|
||||
|
||||
// Start the monitoring server
|
||||
@@ -476,6 +477,8 @@ func (s *Server) startMonitoring(secure bool) {
|
||||
mux.HandleFunc(SubszPath, s.HandleSubsz)
|
||||
// Subz alias for backwards compatibility
|
||||
mux.HandleFunc("/subscriptionsz", s.HandleSubsz)
|
||||
// Stacksz
|
||||
mux.HandleFunc(StackszPath, s.HandleStacksz)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: hp,
|
||||
|
||||
Reference in New Issue
Block a user