Browse Source

metrics: log HTTP requests when logLevel is debug (#1687)

pull/1689/head
Alessandro Ros 2 years ago committed by GitHub
parent
commit
2a5e6e2651
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      internal/core/api.go
  2. 50
      internal/core/hls_server.go
  3. 15
      internal/core/http_set_trusted_proxies.go
  4. 13
      internal/core/metrics.go
  5. 10
      internal/core/pprof.go
  6. 45
      internal/core/webrtc_server.go

13
internal/core/api.go

@ -117,9 +117,9 @@ type api struct { @@ -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(
@ -153,7 +153,6 @@ func newAPI( @@ -153,7 +153,6 @@ func newAPI(
}
router := gin.New()
router.SetTrustedProxies(nil)
mwLog := httpLoggerMiddleware(a)
@ -199,12 +198,12 @@ func newAPI( @@ -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)
@ -213,7 +212,7 @@ func newAPI( @@ -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()
}

50
internal/core/hls_server.go

@ -63,19 +63,18 @@ type hlsServer struct { @@ -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
@ -135,7 +134,6 @@ func newHLSServer( @@ -135,7 +134,6 @@ func newHLSServer(
partDuration: partDuration,
segmentMaxSize: segmentMaxSize,
allowOrigin: allowOrigin,
trustedProxies: trustedProxies,
directory: directory,
readBufferCount: readBufferCount,
pathManager: pathManager,
@ -144,7 +142,6 @@ func newHLSServer( @@ -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),
@ -153,6 +150,17 @@ func newHLSServer( @@ -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)
@ -181,26 +189,10 @@ func (s *hlsServer) close() { @@ -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:
@ -258,7 +250,7 @@ outer: @@ -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)

15
internal/core/http_set_trusted_proxies.go

@ -0,0 +1,15 @@ @@ -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
internal/core/metrics.go

@ -26,7 +26,7 @@ type metrics struct { @@ -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
@ -52,23 +52,26 @@ func newMetrics( @@ -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()
}

10
internal/core/pprof.go

@ -19,8 +19,8 @@ type pprofParent interface { @@ -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(
@ -37,21 +37,21 @@ func newPPROF( @@ -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()
}

45
internal/core/webrtc_server.go

@ -80,9 +80,10 @@ type webRTCServer struct { @@ -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
@ -170,7 +171,6 @@ func newWebRTCServer( @@ -170,7 +171,6 @@ func newWebRTCServer(
ln: ln,
udpMuxLn: udpMuxLn,
tcpMuxLn: tcpMuxLn,
tlsConfig: tlsConfig,
iceUDPMux: iceUDPMux,
iceTCPMux: iceTCPMux,
iceHostNAT1To1IPs: iceHostNAT1To1IPs,
@ -182,6 +182,19 @@ func newWebRTCServer( @@ -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)"
@ -214,29 +227,10 @@ func (s *webRTCServer) close() { @@ -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
@ -307,8 +301,9 @@ outer: @@ -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()

Loading…
Cancel
Save