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

log: Warp exceptions omnibus #1941

Merged
merged 2 commits into from
May 31, 2024
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
1 change: 1 addition & 0 deletions chainweb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ library
, hashable >= 1.4
, heaps >= 0.3
, hourglass >=0.2
, http2 >= 5.2.1
, http-client >= 0.5
, http-client-tls >=0.3
, http-media >= 0.7
Expand Down
1 change: 1 addition & 0 deletions changes/2024-05-30T145807-0400.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unify HTTP error logging across service and P2P APIs
1 change: 1 addition & 0 deletions changes/2024-05-30T150038-0400.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Stop logging ConnectionIsClosed
83 changes: 46 additions & 37 deletions src/Chainweb/Chainweb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ import qualified Data.Vector as V
import GHC.Generics

import qualified Network.HTTP.Client as HTTP
import qualified Network.HTTP2.Client as HTTP2
import Network.Socket (Socket)
import Network.Wai
import Network.Wai.Handler.Warp hiding (Port)
Expand Down Expand Up @@ -774,21 +775,30 @@ runChainweb cw nowServing = do
pactDbsToServe :: [(ChainId, PactServerData logger tbl)]
pactDbsToServe = _chainwebPactData cw

loggServerError msg (Just r) e =
"HTTP server error (" <> msg <> "): " <> sshow e <> ". Request: " <> sshow r
loggServerError msg Nothing e =
"HTTP server error (" <> msg <> "): " <> sshow e

logWarpException msg clientClosedConnectionsCounter r e
| Just InsecureConnectionDenied <- fromException e =
return ()
| Just ClientClosedConnectionPrematurely <- fromException e =
inc clientClosedConnectionsCounter
-- this isn't really an error, this is a graceful close.
-- see https://github.com/kazu-yamamoto/http2/issues/102
| Just HTTP2.ConnectionIsClosed <- fromException e =
return ()
| otherwise =
when (defaultShouldDisplayException e) $
logg Warn $ loggServerError msg r e

-- P2P Server

serverSettings :: Counter "clientClosedConnections" -> Settings
serverSettings closedConnectionsCounter =
serverSettings clientClosedConnectionsCounter =
peerServerSettings (_peerResPeer $ _chainwebPeer cw)
& setOnException
(\r e -> if
| Just InsecureConnectionDenied <- fromException e ->
return ()
| Just ClientClosedConnectionPrematurely <- fromException e ->
inc closedConnectionsCounter
| otherwise ->
when (defaultShouldDisplayException e) $
logg Warn $ loggServerError r e
)
& setOnException (logWarpException "P2P API" clientClosedConnectionsCounter)
& setBeforeMainLoop (nowServing (nowServingP2PAPI .~ True))

monitorConnectionsClosedByClient :: Counter "clientClosedConnections" -> IO ()
Expand Down Expand Up @@ -861,48 +871,47 @@ runChainweb cw nowServing = do
httpLog :: Middleware
httpLog = requestResponseLogger $ setComponent "http:p2p-api" (_chainwebLogger cw)

loggServerError (Just r) e = "HTTP server error: " <> sshow e <> ". Request: " <> sshow r
loggServerError Nothing e = "HTTP server error: " <> sshow e

-- Service API Server

serviceApiServerSettings :: Port -> HostPreference -> Settings
serviceApiServerSettings port interface = defaultSettings
serviceApiServerSettings
:: Counter "clientClosedConnections"
-> Port -> HostPreference -> Settings
serviceApiServerSettings clientClosedConnectionsCounter port interface = defaultSettings
& setPort (int port)
& setHost interface
& setOnException
(\r e -> when (defaultShouldDisplayException e) (logg Warn $ loggServiceApiServerError r e))
(logWarpException "Service API" clientClosedConnectionsCounter)
& setBeforeMainLoop (nowServing (nowServingServiceAPI .~ True))

serviceApiHost = _serviceApiConfigInterface $ _configServiceApi $ _chainwebConfig cw

backupApiEnabled = _enableConfigEnabled $ _configBackupApi $ _configBackup $ _chainwebConfig cw

serveServiceApi :: Middleware -> IO ()
serveServiceApi = serveServiceApiSocket
(serviceApiServerSettings (fst $ _chainwebServiceSocket cw) serviceApiHost)
(snd $ _chainwebServiceSocket cw)
(_chainwebVersion cw)
ChainwebServerDbs
{ _chainwebServerCutDb = Just cutDb
, _chainwebServerBlockHeaderDbs = chainDbsToServe
, _chainwebServerMempools = mempoolsToServe
, _chainwebServerPayloadDbs = payloadDbsToServe
, _chainwebServerPeerDbs = (CutNetwork, cutPeerDb) : memP2pToServe
}
pactDbsToServe
(_chainwebCoordinator cw)
(HeaderStream . _configHeaderStream $ _chainwebConfig cw)
(Rosetta . _configRosetta $ _chainwebConfig cw)
(_chainwebBackup cw <$ guard backupApiEnabled)
(_serviceApiPayloadBatchLimit . _configServiceApi $ _chainwebConfig cw)
serveServiceApi mw = do
clientClosedConnectionsCounter <- newCounter
serveServiceApiSocket
(serviceApiServerSettings clientClosedConnectionsCounter (fst $ _chainwebServiceSocket cw) serviceApiHost)
(snd $ _chainwebServiceSocket cw)
(_chainwebVersion cw)
ChainwebServerDbs
{ _chainwebServerCutDb = Just cutDb
, _chainwebServerBlockHeaderDbs = chainDbsToServe
, _chainwebServerMempools = mempoolsToServe
, _chainwebServerPayloadDbs = payloadDbsToServe
, _chainwebServerPeerDbs = (CutNetwork, cutPeerDb) : memP2pToServe
}
pactDbsToServe
(_chainwebCoordinator cw)
(HeaderStream . _configHeaderStream $ _chainwebConfig cw)
(Rosetta . _configRosetta $ _chainwebConfig cw)
(_chainwebBackup cw <$ guard backupApiEnabled)
(_serviceApiPayloadBatchLimit . _configServiceApi $ _chainwebConfig cw)
mw

serviceHttpLog :: Middleware
serviceHttpLog = requestResponseLogger $ setComponent "http:service-api" (_chainwebLogger cw)

loggServiceApiServerError (Just r) e = "HTTP service API server error: " <> sshow e <> ". Request: " <> sshow r
loggServiceApiServerError Nothing e = "HTTP service API server error: " <> sshow e

-- HTTP Request Logger

-- Cut DB and Miner
Expand Down
Loading