mirror of https://github.com/dapr/dapr.git
add profiling server (#287)
This commit is contained in:
parent
40db92bfdf
commit
b8b116ea8e
|
|
@ -24,6 +24,7 @@ func main() {
|
|||
actionHTTPPort := flag.String("actions-http-port", fmt.Sprintf("%v", actionsrt.DefaultActionsHTTPPort), "HTTP port for Actions to listen on")
|
||||
actionGRPCPort := flag.String("actions-grpc-port", fmt.Sprintf("%v", actionsrt.DefaultActionsGRPCPort), "gRPC port for Actions to listen on")
|
||||
appPort := flag.String("app-port", "", "The port the application is listening on")
|
||||
profilePort := flag.String("profile-port", fmt.Sprintf("%v", actionsrt.DefaultProfilePort), "The port for the profile server")
|
||||
appProtocol := flag.String("protocol", string(actionsrt.HTTPProtocol), "Protocol for the application: gRPC or http")
|
||||
componentsPath := flag.String("components-path", actionsrt.DefaultComponentsPath, "Path for components directory. Standalone mode only")
|
||||
config := flag.String("config", "", "Path to config file, or name of a configuration object")
|
||||
|
|
@ -52,6 +53,11 @@ func main() {
|
|||
log.Fatalf("error parsing actions-grpc-port flag: %s", err)
|
||||
}
|
||||
|
||||
profPort, err := strconv.Atoi(*profilePort)
|
||||
if err != nil {
|
||||
log.Fatalf("error parsing profile-port flag: %s", err)
|
||||
}
|
||||
|
||||
applicationPort := 0
|
||||
if *appPort != "" {
|
||||
applicationPort, err = strconv.Atoi(*appPort)
|
||||
|
|
@ -61,7 +67,7 @@ func main() {
|
|||
}
|
||||
|
||||
runtimeConfig := actionsrt.NewRuntimeConfig(*actionsID, *placementServiceAddress, *controlPlaneAddress, *allowedOrigins, *config, *componentsPath,
|
||||
*appProtocol, *mode, actionHTTP, actionGRPC, applicationPort)
|
||||
*appProtocol, *mode, actionHTTP, actionGRPC, applicationPort, profPort)
|
||||
|
||||
var globalConfig *global_config.Configuration
|
||||
|
||||
|
|
|
|||
|
|
@ -6,14 +6,16 @@ type ServerConfig struct {
|
|||
ActionID string
|
||||
HostAddress string
|
||||
Port int
|
||||
ProfilePort int
|
||||
}
|
||||
|
||||
// NewServerConfig returns a new HTTP server config
|
||||
func NewServerConfig(actionID string, hostAddress string, port int, allowedOrigins string) ServerConfig {
|
||||
func NewServerConfig(actionID string, hostAddress string, port int, profilePort int, allowedOrigins string) ServerConfig {
|
||||
return ServerConfig{
|
||||
AllowedOrigins: allowedOrigins,
|
||||
ActionID: actionID,
|
||||
HostAddress: hostAddress,
|
||||
Port: port,
|
||||
ProfilePort: profilePort,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/valyala/fasthttp/pprofhandler"
|
||||
|
||||
cors "github.com/AdhityaRamadhanus/fasthttpcors"
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/actionscore/actions/pkg/config"
|
||||
|
|
@ -48,6 +50,11 @@ func (s *server) StartNonBlocking() {
|
|||
log.Fatal(fasthttp.ListenAndServe(fmt.Sprintf(":%v", s.config.Port), corsHandler.CorsMiddleware(router.HandleRequest)))
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
log.Infof("starting profiling server on port %v", s.config.ProfilePort)
|
||||
log.Fatal(fasthttp.ListenAndServe(fmt.Sprintf(":%v", s.config.ProfilePort), pprofhandler.PprofHandler))
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *server) getCorsHandler(allowedOrigins []string) *cors.CorsHandler {
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ const (
|
|||
DefaultActionsHTTPPort = 3500
|
||||
// DefaultActionsGRPCPort is the default gRPC port for Actions
|
||||
DefaultActionsGRPCPort = 50001
|
||||
// DefaultProfilePort is the default port for profiling endpoints
|
||||
DefaultProfilePort = 7777
|
||||
// DefaultComponentsPath is the default dir for Actions components (standalone mode)
|
||||
DefaultComponentsPath = "./components"
|
||||
// DefaultAllowedOrigins is the default origins allowed for the Actions HTTP servers
|
||||
|
|
@ -27,6 +29,7 @@ const (
|
|||
type Config struct {
|
||||
ID string
|
||||
HTTPPort int
|
||||
ProfilePort int
|
||||
GRPCPort int
|
||||
ApplicationPort int
|
||||
ApplicationProtocol Protocol
|
||||
|
|
@ -39,12 +42,13 @@ type Config struct {
|
|||
}
|
||||
|
||||
// NewRuntimeConfig returns a new runtime config
|
||||
func NewRuntimeConfig(id, placementServiceAddress, controlPlaneAddress, allowedOrigins, globalConfig, componentsPath, appProtocol, mode string, httpPort, grpcPort, appPort int) *Config {
|
||||
func NewRuntimeConfig(id, placementServiceAddress, controlPlaneAddress, allowedOrigins, globalConfig, componentsPath, appProtocol, mode string, httpPort, grpcPort, appPort, profilePort int) *Config {
|
||||
return &Config{
|
||||
ID: id,
|
||||
HTTPPort: httpPort,
|
||||
GRPCPort: grpcPort,
|
||||
ApplicationPort: appPort,
|
||||
ProfilePort: profilePort,
|
||||
ApplicationProtocol: Protocol(appProtocol),
|
||||
Mode: modes.ActionsMode(mode),
|
||||
PlacementServiceAddress: placementServiceAddress,
|
||||
|
|
|
|||
|
|
@ -141,7 +141,7 @@ func (a *ActionsRuntime) initRuntime() error {
|
|||
log.Warnf("failed to init actors: %s", err)
|
||||
}
|
||||
|
||||
a.startHTTPServer(a.runtimeConfig.HTTPPort, a.runtimeConfig.AllowedOrigins)
|
||||
a.startHTTPServer(a.runtimeConfig.HTTPPort, a.runtimeConfig.ProfilePort, a.runtimeConfig.AllowedOrigins)
|
||||
log.Infof("http server is running on port %v", a.runtimeConfig.HTTPPort)
|
||||
|
||||
err = a.startGRPCServer(a.runtimeConfig.GRPCPort)
|
||||
|
|
@ -334,9 +334,9 @@ func (a *ActionsRuntime) readFromBinding(name string, binding bindings.InputBind
|
|||
return err
|
||||
}
|
||||
|
||||
func (a *ActionsRuntime) startHTTPServer(port int, allowedOrigins string) {
|
||||
func (a *ActionsRuntime) startHTTPServer(port, profilePort int, allowedOrigins string) {
|
||||
api := http.NewAPI(a.runtimeConfig.ID, a.appChannel, a.directMessaging, a.stateStore, a.pubSub, a.actor, a.sendToOutputBinding)
|
||||
serverConf := http.NewServerConfig(a.runtimeConfig.ID, a.hostAddress, port, allowedOrigins)
|
||||
serverConf := http.NewServerConfig(a.runtimeConfig.ID, a.hostAddress, port, profilePort, allowedOrigins)
|
||||
server := http.NewServer(api, serverConf, a.globalConfig.Spec.TracingSpec)
|
||||
server.StartNonBlocking()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -186,7 +186,8 @@ func NewTestActionsRuntime() *ActionsRuntime {
|
|||
string(modes.StandaloneMode),
|
||||
DefaultActionsHTTPPort,
|
||||
DefaultActionsGRPCPort,
|
||||
1024)
|
||||
1024,
|
||||
DefaultProfilePort)
|
||||
|
||||
rt := NewActionsRuntime(testRuntimeConfig, &config.Configuration{})
|
||||
rt.components = []components.Component{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,142 @@
|
|||
// Package fasthttpadaptor provides helper functions for converting net/http
|
||||
// request handlers to fasthttp request handlers.
|
||||
package fasthttpadaptor
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// NewFastHTTPHandlerFunc wraps net/http handler func to fasthttp
|
||||
// request handler, so it can be passed to fasthttp server.
|
||||
//
|
||||
// While this function may be used for easy switching from net/http to fasthttp,
|
||||
// it has the following drawbacks comparing to using manually written fasthttp
|
||||
// request handler:
|
||||
//
|
||||
// * A lot of useful functionality provided by fasthttp is missing
|
||||
// from net/http handler.
|
||||
// * net/http -> fasthttp handler conversion has some overhead,
|
||||
// so the returned handler will be always slower than manually written
|
||||
// fasthttp handler.
|
||||
//
|
||||
// So it is advisable using this function only for quick net/http -> fasthttp
|
||||
// switching. Then manually convert net/http handlers to fasthttp handlers
|
||||
// according to https://github.com/valyala/fasthttp#switching-from-nethttp-to-fasthttp .
|
||||
func NewFastHTTPHandlerFunc(h http.HandlerFunc) fasthttp.RequestHandler {
|
||||
return NewFastHTTPHandler(h)
|
||||
}
|
||||
|
||||
// NewFastHTTPHandler wraps net/http handler to fasthttp request handler,
|
||||
// so it can be passed to fasthttp server.
|
||||
//
|
||||
// While this function may be used for easy switching from net/http to fasthttp,
|
||||
// it has the following drawbacks comparing to using manually written fasthttp
|
||||
// request handler:
|
||||
//
|
||||
// * A lot of useful functionality provided by fasthttp is missing
|
||||
// from net/http handler.
|
||||
// * net/http -> fasthttp handler conversion has some overhead,
|
||||
// so the returned handler will be always slower than manually written
|
||||
// fasthttp handler.
|
||||
//
|
||||
// So it is advisable using this function only for quick net/http -> fasthttp
|
||||
// switching. Then manually convert net/http handlers to fasthttp handlers
|
||||
// according to https://github.com/valyala/fasthttp#switching-from-nethttp-to-fasthttp .
|
||||
func NewFastHTTPHandler(h http.Handler) fasthttp.RequestHandler {
|
||||
return func(ctx *fasthttp.RequestCtx) {
|
||||
var r http.Request
|
||||
|
||||
body := ctx.PostBody()
|
||||
r.Method = string(ctx.Method())
|
||||
r.Proto = "HTTP/1.1"
|
||||
r.ProtoMajor = 1
|
||||
r.ProtoMinor = 1
|
||||
r.RequestURI = string(ctx.RequestURI())
|
||||
r.ContentLength = int64(len(body))
|
||||
r.Host = string(ctx.Host())
|
||||
r.RemoteAddr = ctx.RemoteAddr().String()
|
||||
|
||||
hdr := make(http.Header)
|
||||
ctx.Request.Header.VisitAll(func(k, v []byte) {
|
||||
sk := string(k)
|
||||
sv := string(v)
|
||||
switch sk {
|
||||
case "Transfer-Encoding":
|
||||
r.TransferEncoding = append(r.TransferEncoding, sv)
|
||||
default:
|
||||
hdr.Set(sk, sv)
|
||||
}
|
||||
})
|
||||
r.Header = hdr
|
||||
r.Body = &netHTTPBody{body}
|
||||
rURL, err := url.ParseRequestURI(r.RequestURI)
|
||||
if err != nil {
|
||||
ctx.Logger().Printf("cannot parse requestURI %q: %s", r.RequestURI, err)
|
||||
ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
r.URL = rURL
|
||||
|
||||
var w netHTTPResponseWriter
|
||||
h.ServeHTTP(&w, &r)
|
||||
|
||||
ctx.SetStatusCode(w.StatusCode())
|
||||
for k, vv := range w.Header() {
|
||||
for _, v := range vv {
|
||||
ctx.Response.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
ctx.Write(w.body)
|
||||
}
|
||||
}
|
||||
|
||||
type netHTTPBody struct {
|
||||
b []byte
|
||||
}
|
||||
|
||||
func (r *netHTTPBody) Read(p []byte) (int, error) {
|
||||
if len(r.b) == 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
n := copy(p, r.b)
|
||||
r.b = r.b[n:]
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r *netHTTPBody) Close() error {
|
||||
r.b = r.b[:0]
|
||||
return nil
|
||||
}
|
||||
|
||||
type netHTTPResponseWriter struct {
|
||||
statusCode int
|
||||
h http.Header
|
||||
body []byte
|
||||
}
|
||||
|
||||
func (w *netHTTPResponseWriter) StatusCode() int {
|
||||
if w.statusCode == 0 {
|
||||
return http.StatusOK
|
||||
}
|
||||
return w.statusCode
|
||||
}
|
||||
|
||||
func (w *netHTTPResponseWriter) Header() http.Header {
|
||||
if w.h == nil {
|
||||
w.h = make(http.Header)
|
||||
}
|
||||
return w.h
|
||||
}
|
||||
|
||||
func (w *netHTTPResponseWriter) WriteHeader(statusCode int) {
|
||||
w.statusCode = statusCode
|
||||
}
|
||||
|
||||
func (w *netHTTPResponseWriter) Write(p []byte) (int, error) {
|
||||
w.body = append(w.body, p...)
|
||||
return len(p), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
package pprofhandler
|
||||
|
||||
import (
|
||||
"net/http/pprof"
|
||||
rtp "runtime/pprof"
|
||||
"strings"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
"github.com/valyala/fasthttp/fasthttpadaptor"
|
||||
)
|
||||
|
||||
var (
|
||||
cmdline = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Cmdline)
|
||||
profile = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Profile)
|
||||
symbol = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Symbol)
|
||||
trace = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Trace)
|
||||
index = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Index)
|
||||
)
|
||||
|
||||
// PprofHandler serves server runtime profiling data in the format expected by the pprof visualization tool.
|
||||
//
|
||||
// See https://golang.org/pkg/net/http/pprof/ for details.
|
||||
func PprofHandler(ctx *fasthttp.RequestCtx) {
|
||||
ctx.Response.Header.Set("Content-Type", "text/html")
|
||||
if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/cmdline") {
|
||||
cmdline(ctx)
|
||||
} else if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/profile") {
|
||||
profile(ctx)
|
||||
} else if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/symbol") {
|
||||
symbol(ctx)
|
||||
} else if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/trace") {
|
||||
trace(ctx)
|
||||
} else {
|
||||
for _, v := range rtp.Profiles() {
|
||||
ppName := v.Name()
|
||||
if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/"+ppName) {
|
||||
namedHandler := fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler(ppName).ServeHTTP)
|
||||
namedHandler(ctx)
|
||||
return
|
||||
}
|
||||
}
|
||||
index(ctx)
|
||||
}
|
||||
}
|
||||
|
|
@ -222,8 +222,10 @@ github.com/stretchr/testify/assert
|
|||
github.com/valyala/bytebufferpool
|
||||
# github.com/valyala/fasthttp v1.4.0
|
||||
github.com/valyala/fasthttp
|
||||
github.com/valyala/fasthttp/pprofhandler
|
||||
github.com/valyala/fasthttp/fasthttputil
|
||||
github.com/valyala/fasthttp/stackless
|
||||
github.com/valyala/fasthttp/fasthttpadaptor
|
||||
# go.opencensus.io v0.21.0
|
||||
go.opencensus.io/trace
|
||||
go.opencensus.io/trace/propagation
|
||||
|
|
|
|||
Loading…
Reference in New Issue