You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by ch...@apache.org on 2020/12/24 06:35:24 UTC

[apisix-ingress-controller] branch master updated: chore: go profile (#126)

This is an automated email from the ASF dual-hosted git repository.

chenjunxu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-ingress-controller.git


The following commit(s) were added to refs/heads/master by this push:
     new a3591e7  chore: go profile (#126)
a3591e7 is described below

commit a3591e7f618db044029f438777cb0b0e87333e4d
Author: Alex Zhang <zc...@gmail.com>
AuthorDate: Thu Dec 24 14:35:15 2020 +0800

    chore: go profile (#126)
---
 pkg/api/server.go      | 19 +++++++++++++++++--
 pkg/api/server_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/pkg/api/server.go b/pkg/api/server.go
index 484e055..59c747b 100644
--- a/pkg/api/server.go
+++ b/pkg/api/server.go
@@ -16,6 +16,8 @@ package api
 
 import (
 	"net"
+	"net/http"
+	"net/http/pprof"
 
 	"github.com/gin-gonic/gin"
 
@@ -29,6 +31,7 @@ import (
 type Server struct {
 	router       *gin.Engine
 	httpListener net.Listener
+	pprofMu      *http.ServeMux
 }
 
 // NewServer initializes the API Server.
@@ -42,10 +45,22 @@ func NewServer(cfg *config.Config) (*Server, error) {
 	router.Use(gin.Recovery(), gin.Logger())
 	apirouter.Mount(router)
 
-	return &Server{
+	srv := &Server{
 		router:       router,
 		httpListener: httpListener,
-	}, nil
+	}
+
+	if cfg.EnableProfiling {
+		srv.pprofMu = new(http.ServeMux)
+		srv.pprofMu.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
+		srv.pprofMu.HandleFunc("/debug/pprof/profile", pprof.Profile)
+		srv.pprofMu.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
+		srv.pprofMu.HandleFunc("/debug/pprof/trace", pprof.Trace)
+		srv.pprofMu.HandleFunc("/debug/pprof/", pprof.Index)
+		router.GET("/debug/pprof/*profile", gin.WrapF(srv.pprofMu.ServeHTTP))
+	}
+
+	return srv, nil
 }
 
 // Run launches the API Server.
diff --git a/pkg/api/server_test.go b/pkg/api/server_test.go
index 80f56cd..09ecf91 100644
--- a/pkg/api/server_test.go
+++ b/pkg/api/server_test.go
@@ -15,6 +15,8 @@
 package api
 
 import (
+	"net/http"
+	"net/url"
 	"testing"
 	"time"
 
@@ -46,3 +48,47 @@ func TestServerRun(t *testing.T) {
 	err = srv.Run(stopCh)
 	assert.Nil(t, err, "see non-nil error: ", err)
 }
+
+func TestProfileNotMount(t *testing.T) {
+	cfg := &config.Config{HTTPListen: "127.0.0.1:0"}
+	srv, err := NewServer(cfg)
+	assert.Nil(t, err, "see non-nil error: ", err)
+	stopCh := make(chan struct{})
+	go func() {
+		err := srv.Run(stopCh)
+		assert.Nil(t, err, "see non-nil error: ", err)
+	}()
+
+	u := (&url.URL{
+		Scheme: "http",
+		Host:   srv.httpListener.Addr().String(),
+		Path:   "/debug/pprof/cmdline",
+	}).String()
+
+	resp, err := http.Get(u)
+	assert.Nil(t, err, nil)
+	assert.Equal(t, resp.StatusCode, http.StatusNotFound)
+	close(stopCh)
+}
+
+func TestProfile(t *testing.T) {
+	cfg := &config.Config{HTTPListen: "127.0.0.1:0", EnableProfiling: true}
+	srv, err := NewServer(cfg)
+	assert.Nil(t, err, "see non-nil error: ", err)
+	stopCh := make(chan struct{})
+	go func() {
+		err := srv.Run(stopCh)
+		assert.Nil(t, err, "see non-nil error: ", err)
+	}()
+
+	u := (&url.URL{
+		Scheme: "http",
+		Host:   srv.httpListener.Addr().String(),
+		Path:   "/debug/pprof/cmdline",
+	}).String()
+
+	resp, err := http.Get(u)
+	assert.Nil(t, err, nil)
+	assert.Equal(t, resp.StatusCode, http.StatusOK)
+	close(stopCh)
+}