Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: log HTTP requests when logLevel is debug #1687

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions internal/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ type api struct {
webRTCServer apiWebRTCServer
parent apiParent

ln net.Listener
mutex sync.Mutex
s *http.Server
ln net.Listener
httpServer *http.Server
mutex sync.Mutex
}

func newAPI(
Expand Down Expand Up @@ -153,7 +153,6 @@ func newAPI(
}

router := gin.New()

router.SetTrustedProxies(nil)

mwLog := httpLoggerMiddleware(a)
Expand Down Expand Up @@ -199,12 +198,12 @@ func newAPI(
group.POST("/v1/webrtcconns/kick/:id", a.onWebRTCConnsKick)
}

a.s = &http.Server{
a.httpServer = &http.Server{
Handler: router,
ErrorLog: log.New(&nilWriter{}, "", 0),
}

go a.s.Serve(ln)
go a.httpServer.Serve(ln)

a.log(logger.Info, "listener opened on "+address)

Expand All @@ -213,7 +212,7 @@ func newAPI(

func (a *api) close() {
a.log(logger.Info, "listener is closing")
a.s.Shutdown(context.Background())
a.httpServer.Shutdown(context.Background())
a.ln.Close() // in case Shutdown() is called before Serve()
}

Expand Down
50 changes: 21 additions & 29 deletions internal/core/hls_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,18 @@ type hlsServer struct {
partDuration conf.StringDuration
segmentMaxSize conf.StringSize
allowOrigin string
trustedProxies conf.IPsOrCIDRs
directory string
readBufferCount int
pathManager *pathManager
metrics *metrics
parent hlsServerParent

ctx context.Context
ctxCancel func()
wg sync.WaitGroup
ln net.Listener
tlsConfig *tls.Config
muxers map[string]*hlsMuxer
ctx context.Context
ctxCancel func()
wg sync.WaitGroup
ln net.Listener
httpServer *http.Server
muxers map[string]*hlsMuxer

// in
chPathSourceReady chan *path
Expand Down Expand Up @@ -135,7 +134,6 @@ func newHLSServer(
partDuration: partDuration,
segmentMaxSize: segmentMaxSize,
allowOrigin: allowOrigin,
trustedProxies: trustedProxies,
directory: directory,
readBufferCount: readBufferCount,
pathManager: pathManager,
Expand All @@ -144,7 +142,6 @@ func newHLSServer(
ctx: ctx,
ctxCancel: ctxCancel,
ln: ln,
tlsConfig: tlsConfig,
muxers: make(map[string]*hlsMuxer),
chPathSourceReady: make(chan *path),
chPathSourceNotReady: make(chan *path),
Expand All @@ -153,6 +150,17 @@ func newHLSServer(
chAPIMuxerList: make(chan hlsServerAPIMuxersListReq),
}

router := gin.New()
httpSetTrustedProxies(router, trustedProxies)

router.NoRoute(httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest)

s.httpServer = &http.Server{
Handler: router,
TLSConfig: tlsConfig,
ErrorLog: log.New(&nilWriter{}, "", 0),
}

s.log(logger.Info, "listener opened on "+address)

s.pathManager.hlsServerSet(s)
Expand Down Expand Up @@ -181,26 +189,10 @@ func (s *hlsServer) close() {
func (s *hlsServer) run() {
defer s.wg.Done()

router := gin.New()

tmp := make([]string, len(s.trustedProxies))
for i, entry := range s.trustedProxies {
tmp[i] = entry.String()
}
router.SetTrustedProxies(tmp)

router.NoRoute(httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest)

hs := &http.Server{
Handler: router,
TLSConfig: s.tlsConfig,
ErrorLog: log.New(&nilWriter{}, "", 0),
}

if s.tlsConfig != nil {
go hs.ServeTLS(s.ln, "", "")
if s.httpServer.TLSConfig != nil {
go s.httpServer.ServeTLS(s.ln, "", "")
} else {
go hs.Serve(s.ln)
go s.httpServer.Serve(s.ln)
}

outer:
Expand Down Expand Up @@ -258,7 +250,7 @@ outer:

s.ctxCancel()

hs.Shutdown(context.Background())
s.httpServer.Shutdown(context.Background())
s.ln.Close() // in case Shutdown() is called before Serve()

s.pathManager.hlsServerSet(nil)
Expand Down
15 changes: 15 additions & 0 deletions internal/core/http_set_trusted_proxies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core

import (
"github.com/gin-gonic/gin"

"github.com/aler9/mediamtx/internal/conf"
)

func httpSetTrustedProxies(router *gin.Engine, trustedProxies conf.IPsOrCIDRs) {
tmp := make([]string, len(trustedProxies))
for i, entry := range trustedProxies {
tmp[i] = entry.String()
}
router.SetTrustedProxies(tmp)
}
13 changes: 8 additions & 5 deletions internal/core/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type metrics struct {
parent metricsParent

ln net.Listener
server *http.Server
httpServer *http.Server
mutex sync.Mutex
pathManager apiPathManager
rtspServer apiRTSPServer
Expand All @@ -52,23 +52,26 @@ func newMetrics(

router := gin.New()
router.SetTrustedProxies(nil)
router.GET("/metrics", m.onMetrics)

m.server = &http.Server{
mwLog := httpLoggerMiddleware(m)
router.NoRoute(mwLog)
router.GET("/metrics", mwLog, m.onMetrics)

m.httpServer = &http.Server{
Handler: router,
ErrorLog: log.New(&nilWriter{}, "", 0),
}

m.log(logger.Info, "listener opened on "+address)

go m.server.Serve(m.ln)
go m.httpServer.Serve(m.ln)

return m, nil
}

func (m *metrics) close() {
m.log(logger.Info, "listener is closing")
m.server.Shutdown(context.Background())
m.httpServer.Shutdown(context.Background())
m.ln.Close() // in case Shutdown() is called before Serve()
}

Expand Down
10 changes: 5 additions & 5 deletions internal/core/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type pprofParent interface {
type pprof struct {
parent pprofParent

ln net.Listener
server *http.Server
ln net.Listener
httpServer *http.Server
}

func newPPROF(
Expand All @@ -37,21 +37,21 @@ func newPPROF(
ln: ln,
}

pp.server = &http.Server{
pp.httpServer = &http.Server{
Handler: http.DefaultServeMux,
ErrorLog: log.New(&nilWriter{}, "", 0),
}

pp.log(logger.Info, "listener opened on "+address)

go pp.server.Serve(pp.ln)
go pp.httpServer.Serve(pp.ln)

return pp, nil
}

func (pp *pprof) close() {
pp.log(logger.Info, "listener is closing")
pp.server.Shutdown(context.Background())
pp.httpServer.Shutdown(context.Background())
pp.ln.Close() // in case Shutdown() is called before Serve()
}

Expand Down
45 changes: 20 additions & 25 deletions internal/core/webrtc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ type webRTCServer struct {
ctx context.Context
ctxCancel func()
ln net.Listener
requestPool *httpRequestPool
httpServer *http.Server
udpMuxLn net.PacketConn
tcpMuxLn net.Listener
tlsConfig *tls.Config
conns map[*webRTCConn]struct{}
iceHostNAT1To1IPs []string
iceUDPMux ice.UDPMux
Expand Down Expand Up @@ -170,7 +171,6 @@ func newWebRTCServer(
ln: ln,
udpMuxLn: udpMuxLn,
tcpMuxLn: tcpMuxLn,
tlsConfig: tlsConfig,
iceUDPMux: iceUDPMux,
iceTCPMux: iceTCPMux,
iceHostNAT1To1IPs: iceHostNAT1To1IPs,
Expand All @@ -182,6 +182,19 @@ func newWebRTCServer(
done: make(chan struct{}),
}

s.requestPool = newHTTPRequestPool()

router := gin.New()
httpSetTrustedProxies(router, trustedProxies)

router.NoRoute(s.requestPool.mw, httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest)

s.httpServer = &http.Server{
Handler: router,
TLSConfig: tlsConfig,
ErrorLog: log.New(&nilWriter{}, "", 0),
}

str := "listener opened on " + address + " (HTTP)"
if udpMuxLn != nil {
str += ", " + iceUDPMuxAddress + " (ICE/UDP)"
Expand Down Expand Up @@ -214,29 +227,10 @@ func (s *webRTCServer) close() {
func (s *webRTCServer) run() {
defer close(s.done)

rp := newHTTPRequestPool()
defer rp.close()

router := gin.New()

tmp := make([]string, len(s.trustedProxies))
for i, entry := range s.trustedProxies {
tmp[i] = entry.String()
}
router.SetTrustedProxies(tmp)

router.NoRoute(rp.mw, httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest)

hs := &http.Server{
Handler: router,
TLSConfig: s.tlsConfig,
ErrorLog: log.New(&nilWriter{}, "", 0),
}

if s.tlsConfig != nil {
go hs.ServeTLS(s.ln, "", "")
if s.httpServer.TLSConfig != nil {
go s.httpServer.ServeTLS(s.ln, "", "")
} else {
go hs.Serve(s.ln)
go s.httpServer.Serve(s.ln)
}

var wg sync.WaitGroup
Expand Down Expand Up @@ -307,8 +301,9 @@ outer:

s.ctxCancel()

hs.Shutdown(context.Background())
s.httpServer.Shutdown(context.Background())
s.ln.Close() // in case Shutdown() is called before Serve()
s.requestPool.close()

wg.Wait()

Expand Down