@@ -10,6 +10,7 @@ module Network.Mux.Trace
1010 ( Error (.. )
1111 , handleIOException
1212 , Trace (.. )
13+ , Tracers (.. )
1314 , BearerState (.. )
1415 , WithBearer (.. )
1516 , TraceLabelPeer (.. )
@@ -22,6 +23,7 @@ import Text.Printf
2223import Control.Exception hiding (throwIO )
2324import Control.Monad.Class.MonadThrow
2425import Control.Monad.Class.MonadTime.SI
26+ import Control.Tracer (Tracer )
2527import Data.Bifunctor (Bifunctor (.. ))
2628import Data.Word
2729import GHC.Generics (Generic (.. ))
@@ -118,9 +120,19 @@ data BearerState = Mature
118120 -- closed.
119121 deriving (Eq , Show )
120122
123+ -- todo The Trace type mixes tags which are output by
124+ -- separate components but share the type. It would make more sense
125+ -- to break this up into separate types. Care must be
126+ -- excercised to ensure that a particular tracer goes
127+ -- into the component that outputs the desired tags. For instance,
128+ -- the low level bearer tags are not output by the tracer which
129+ -- is passed to Mux via 'Tracers'.
130+
121131-- | Enumeration of Mux events that can be traced.
122132--
123133data Trace =
134+ -- low level bearer trace tags (these are not traced by the tracer
135+ -- which is passed to Mux)
124136 TraceRecvHeaderStart
125137 | TraceRecvHeaderEnd SDUHeader
126138 | TraceRecvDeltaQObservation SDUHeader Time
@@ -131,27 +143,37 @@ data Trace =
131143 | TraceSendStart SDUHeader
132144 | TraceSendEnd
133145 | TraceState BearerState
134- | TraceCleanExit MiniProtocolNum MiniProtocolDir
135- | TraceExceptionExit MiniProtocolNum MiniProtocolDir SomeException
136- | TraceChannelRecvStart MiniProtocolNum
137- | TraceChannelRecvEnd MiniProtocolNum Int
138- | TraceChannelSendStart MiniProtocolNum Int
139- | TraceChannelSendEnd MiniProtocolNum
146+ | TraceSDUReadTimeoutException
147+ | TraceSDUWriteTimeoutException
148+ | TraceTCPInfo StructTCPInfo Word16
149+ -- low level handshake bearer tags (not traced by tracer in Mux)
140150 | TraceHandshakeStart
141151 | TraceHandshakeClientEnd DiffTime
142152 | TraceHandshakeServerEnd
143153 | forall e . Exception e => TraceHandshakeClientError e DiffTime
144154 | forall e . Exception e => TraceHandshakeServerError e
145- | TraceSDUReadTimeoutException
146- | TraceSDUWriteTimeoutException
155+ -- mid level channel tags traced independently by each mini protocol
156+ -- job in Mux, for each complete message, by the 'channelTracer'
157+ -- within 'Tracers'
158+ | TraceChannelRecvStart MiniProtocolNum
159+ | TraceChannelRecvEnd MiniProtocolNum Int
160+ | TraceChannelSendStart MiniProtocolNum Int
161+ | TraceChannelSendEnd MiniProtocolNum
162+ -- high level Mux tags traced by the main Mux/Connection handler
163+ -- thread forked by CM. These may be monitored by the inbound
164+ -- governor information channel tracer. These should be traced
165+ -- by muxTracer of 'Tracers' and their ordering
166+ -- is significant at call sites or bad things will happen.
167+ -- You have been warned.
168+ | TraceCleanExit MiniProtocolNum MiniProtocolDir
169+ | TraceExceptionExit MiniProtocolNum MiniProtocolDir SomeException
147170 | TraceStartEagerly MiniProtocolNum MiniProtocolDir
148171 | TraceStartOnDemand MiniProtocolNum MiniProtocolDir
149172 | TraceStartOnDemandAny MiniProtocolNum MiniProtocolDir
150173 | TraceStartedOnDemand MiniProtocolNum MiniProtocolDir
151174 | TraceTerminating MiniProtocolNum MiniProtocolDir
152175 | TraceStopping
153176 | TraceStopped
154- | TraceTCPInfo StructTCPInfo Word16
155177
156178instance Show Trace where
157179 show TraceRecvHeaderStart = printf " Bearer Receive Header Start"
@@ -208,3 +230,18 @@ instance Show Trace where
208230 show (TraceTCPInfo _ len) = printf " TCPInfo len %d" len
209231#endif
210232
233+ -- | Bundle of tracers passed to mux
234+ -- Consult the 'Trace' type to determine which
235+ -- tags are required/expected to be served by these tracers.
236+ -- In principle, the channelTracer can be == muxTracer
237+ -- but performance likely degrades in typical conditions
238+ -- unnecessarily.
239+ --
240+ data Tracers m = Tracers {
241+ channelTracer :: Tracer m Trace ,
242+ -- ^ a low level tracer for events emitted by a bearer. It emits events as frequently
243+ -- as receiving individual `SDU`s from the network.
244+ muxTracer :: Tracer m Trace
245+ -- ^ mux events which are emitted less frequently. It emits events which allow one
246+ -- to observe the current state of a mini-protocol.
247+ }
0 commit comments