Skip to content

Commit

Permalink
restore pprof service (#3848)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Oct 8, 2024
1 parent a1e09f1 commit 6de1b26
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/bluenviron/mediacommon v1.13.0
github.com/datarhei/gosrt v0.7.0
github.com/fsnotify/fsnotify v1.7.0
github.com/gin-contrib/pprof v1.5.0
github.com/gin-gonic/gin v1.10.0
github.com/go-git/go-git/v5 v5.12.0
github.com/golang-jwt/jwt/v5 v5.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/pprof v1.5.0 h1:E/Oy7g+kNw94KfdCy3bZxQFtyDnAX2V7axRS7sNYVrU=
github.com/gin-contrib/pprof v1.5.0/go.mod h1:GqFL6LerKoCQ/RSWnkYczkTJ+tOAUVN/8sbnEtaqOKs=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
Expand Down
2 changes: 1 addition & 1 deletion internal/core/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ func TestAPIProtocolKick(t *testing.T) {
} `json:"items"`
}
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/"+pa+"/list", nil, &out2)
require.Equal(t, 0, len(out2.Items))
require.Empty(t, out2.Items)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/path_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestPathAutoDeletion(t *testing.T) {
data, err := p.pathManager.APIPathsList()
require.NoError(t, err)

require.Equal(t, 0, len(data.Items))
require.Empty(t, data.Items)
})
}
}
13 changes: 4 additions & 9 deletions internal/pprof/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"net/http"
"time"

// start pprof
_ "net/http/pprof"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"

"github.com/bluenviron/mediamtx/internal/auth"
"github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/protocols/httpp"
"github.com/bluenviron/mediamtx/internal/restrictnetwork"
"github.com/gin-gonic/gin"
)

type pprofAuthManager interface {
Expand Down Expand Up @@ -48,7 +47,7 @@ func (pp *PPROF) Initialize() error {
router.Use(pp.middlewareOrigin)
router.Use(pp.middlewareAuth)

router.Use(pp.onRequest)
pprof.Register(router)

network, address := restrictnetwork.Restrict("tcp", pp.Address)

Expand Down Expand Up @@ -100,7 +99,7 @@ func (pp *PPROF) middlewareOrigin(ctx *gin.Context) {
func (pp *PPROF) middlewareAuth(ctx *gin.Context) {
err := pp.AuthManager.Authenticate(&auth.Request{
IP: net.ParseIP(ctx.ClientIP()),
Action: conf.AuthActionMetrics,
Action: conf.AuthActionPprof,
HTTPRequest: ctx.Request,
})
if err != nil {
Expand All @@ -117,7 +116,3 @@ func (pp *PPROF) middlewareAuth(ctx *gin.Context) {
return
}
}

func (pp *PPROF) onRequest(ctx *gin.Context) {
http.DefaultServeMux.ServeHTTP(ctx.Writer, ctx.Request)
}
30 changes: 30 additions & 0 deletions internal/pprof/pprof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,33 @@ func TestPreflightRequest(t *testing.T) {
require.Equal(t, "Authorization", res.Header.Get("Access-Control-Allow-Headers"))
require.Equal(t, byts, []byte{})
}

func TestPprof(t *testing.T) {
s := &PPROF{
Address: "127.0.0.1:9999",
AllowOrigin: "*",
ReadTimeout: conf.StringDuration(10 * time.Second),
AuthManager: test.NilAuthManager,
Parent: test.NilLogger,
}
err := s.Initialize()
require.NoError(t, err)
defer s.Close()

tr := &http.Transport{}
defer tr.CloseIdleConnections()
hc := &http.Client{Transport: tr}

req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:9999/debug/pprof/heap", nil)
require.NoError(t, err)

res, err := hc.Do(req)
require.NoError(t, err)
defer res.Body.Close()

require.Equal(t, http.StatusOK, res.StatusCode)

byts, err := io.ReadAll(res.Body)
require.NoError(t, err)
require.NotEmpty(t, byts)
}
2 changes: 1 addition & 1 deletion internal/servers/webrtc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,5 +763,5 @@ func TestICEServerClientOnly(t *testing.T) {
require.Equal(t, len(s.ICEServers), len(clientICEServers))
serverICEServers, err := s.generateICEServers(false)
require.NoError(t, err)
require.Equal(t, 0, len(serverICEServers))
require.Empty(t, serverICEServers)
}

0 comments on commit 6de1b26

Please sign in to comment.