-
Notifications
You must be signed in to change notification settings - Fork 14
/
Monadic.lhs
254 lines (215 loc) · 9.06 KB
/
Monadic.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
\subsection{Cardano.BM.Observer.Monadic}
\label{code:Cardano.BM.Observer.Monadic}
%if style == newcode
\begin{code}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-@ LIQUID "--prune-unsorted" @-}
module Cardano.BM.Observer.Monadic
(
bracketObserveIO
, bracketObserveM
, bracketObserveX
-- * observing functions
, observeOpen
, observeClose
) where
import Control.Exception.Safe (MonadCatch, SomeException, catch, throwM)
import Control.Monad (forM_)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Maybe (fromMaybe)
import Data.Text
import qualified Data.Text.IO as TIO
import System.IO (stderr)
import Cardano.BM.Data.Counter (CounterState (..), diffCounters)
import Cardano.BM.Data.LogItem (LOContent (..), LOMeta,
PrivacyAnnotation(Confidential), mkLOMeta)
import Cardano.BM.Data.Severity (Severity)
import qualified Cardano.BM.Configuration as Config
import Cardano.BM.Counters (readCounters)
import Cardano.BM.Data.SubTrace (SubTrace (Neutral, NoTrace))
import Cardano.BM.Trace (Trace, traceNamedObject)
\end{code}
%endif
\subsubsection{Monadic.bracketObserverIO}
Observes an |IO| action. The subtrace type is found in the configuration with
the passed-in name.
\newline
\par\noindent
Microbenchmarking steps:
\newline
\par
1. Create a |trace| which will have been configured
to observe things besides logging.
\begin{spec}
import qualified Cardano.BM.Configuration.Model as CM
. . .
c <- config
trace <- setupTrace (Right c) "demo-playground"
where
config :: IO CM.Configuration
config = do
c <- CM.empty
CM.setMinSeverity c Debug
CM.setSetupBackends c [KatipBK, AggregationBK]
CM.setDefaultBackends c [KatipBK, AggregationBK]
CM.setSetupScribes c [ ScribeDefinition {
scName = "stdout"
, scKind = StdoutSK
, scRotation = Nothing
}
]
CM.setDefaultScribes c ["StdoutSK::stdout"]
return c
\end{spec}
2. |c| is the |Configuration| of |trace|. In order to
enable the collection and processing of measurements
(min, max, mean, std-dev) |AggregationBK| is needed.
\begin{spec}
CM.setDefaultBackends c [KatipBK, AggregationBK]
\end{spec}
in a configuration file (YAML) means
\begin{spec}
defaultBackends:
- KatipBK
- AggregationBK
\end{spec}
3. Set the measurements that you want to take by changing
the configuration of the |trace| using |setSubTrace|,
in order to declare the namespace where we want to
enable the particular measurements and the list with
the kind of measurements.
\begin{spec}
CM.setSubTrace
config
"submit-tx"
(Just $ ObservableTraceSelf observablesSet)
where
observablesSet = [MonotonicClock, MemoryStats]
\end{spec}
4. Find an action to measure. e.g.:
\begin{spec}
runProtocolWithPipe x hdl proto `catch` (\ProtocolStopped -> return ())
\end{spec}
and use |bracketObserveIO|. e.g.:
\begin{spec}
bracketObserveIO trace "submit-tx" $
runProtocolWithPipe x hdl proto `catch` (\ProtocolStopped -> return ())
\end{spec}
\begin{code}
bracketObserveIO :: Config.Configuration -> Trace IO a -> Severity -> Text -> IO t -> IO t
bracketObserveIO config trace severity name action = do
subTrace <- fromMaybe Neutral <$> Config.findSubTrace config name
bracketObserveIO' subTrace severity trace action
where
bracketObserveIO' :: SubTrace -> Severity -> Trace IO a -> IO t -> IO t
bracketObserveIO' NoTrace _ _ act = act
bracketObserveIO' subtrace sev logTrace act = do
mCountersid <- observeOpen subtrace sev logTrace
-- run action; if an exception is caught it will be logged and rethrown.
t <- act `catch` (\(e :: SomeException) -> (TIO.hPutStrLn stderr (pack (show e)) >> throwM e))
case mCountersid of
Left openException ->
-- since observeOpen faced an exception there is no reason to call observeClose
-- however the result of the action is returned
TIO.hPutStrLn stderr ("ObserveOpen: " <> pack (show openException))
Right countersid -> do
res <- observeClose subtrace sev logTrace countersid []
case res of
Left ex -> TIO.hPutStrLn stderr ("ObserveClose: " <> pack (show ex))
_ -> pure ()
pure t
\end{code}
\subsubsection{Monadic.bracketObserverM}
Observes a |MonadIO m => m| action.
\begin{code}
bracketObserveM :: (MonadCatch m, MonadIO m) => Config.Configuration -> Trace m a -> Severity -> Text -> m t -> m t
bracketObserveM config trace severity name action = do
subTrace <- liftIO $ fromMaybe Neutral <$> Config.findSubTrace config name
bracketObserveM' subTrace severity trace action
where
bracketObserveM' :: (MonadCatch m, MonadIO m) => SubTrace -> Severity -> Trace m a -> m t -> m t
bracketObserveM' NoTrace _ _ act = act
bracketObserveM' subtrace sev logTrace act = do
mCountersid <- observeOpen subtrace sev logTrace
-- run action; if an exception is caught it will be logged and rethrown.
t <- act `catch` (\(e :: SomeException) -> liftIO (TIO.hPutStrLn stderr (pack (show e)) >> throwM e))
case mCountersid of
Left openException ->
-- since observeOpen faced an exception there is no reason to call observeClose
-- however the result of the action is returned
liftIO $ TIO.hPutStrLn stderr ("ObserveOpen: " <> pack (show openException))
Right countersid -> do
res <- observeClose subtrace sev logTrace countersid []
case res of
Left ex -> liftIO (TIO.hPutStrLn stderr ("ObserveClose: " <> pack (show ex)))
_ -> pure ()
pure t
\end{code}
\subsubsection{Monadic.bracketObserver}
Observes a |MonadIO m => m| action. This observer bracket does not interfere
on exceptions.
\begin{code}
bracketObserveX :: (MonadIO m) => Config.Configuration -> Trace m a -> Severity -> Text -> m t -> m t
bracketObserveX config trace severity name action = do
subTrace <- liftIO $ fromMaybe Neutral <$> Config.findSubTrace config name
bracketObserveX' subTrace severity trace action
where
bracketObserveX' :: (MonadIO m) => SubTrace -> Severity -> Trace m a -> m t -> m t
bracketObserveX' NoTrace _ _ act = act
bracketObserveX' subtrace sev logTrace act = do
countersid <- observeOpen0 subtrace sev logTrace
-- run action
t <- act
observeClose0 subtrace sev logTrace countersid []
pure t
\end{code}
\subsubsection{observerOpen}\label{observeOpen}
\begin{code}
observeOpen :: (MonadCatch m, MonadIO m) => SubTrace -> Severity -> Trace m a -> m (Either SomeException CounterState)
observeOpen subtrace severity logTrace = (do
state <- observeOpen0 subtrace severity logTrace
return (Right state)) `catch` (return . Left)
observeOpen0 :: (MonadIO m) => SubTrace -> Severity -> Trace m a -> m CounterState
observeOpen0 subtrace severity logTrace = do
-- take measurement
counters <- liftIO $ readCounters subtrace
let state = CounterState counters
if counters == []
then return ()
else do
-- send opening message to Trace
meta <- mkLOMeta severity Confidential
traceNamedObject logTrace (meta, ObserveOpen state)
return state
\end{code}
\subsubsection{observeClose}\label{observeClose}
\begin{code}
observeClose
:: (MonadCatch m, MonadIO m) => SubTrace -> Severity -> Trace m a
-> CounterState -> [(LOMeta, LOContent a)]
-> m (Either SomeException ())
observeClose subtrace sev logTrace initState logObjects = (do
observeClose0 subtrace sev logTrace initState logObjects
return (Right ())) `catch` (return . Left)
observeClose0 :: (MonadIO m) => SubTrace -> Severity -> Trace m a
-> CounterState -> [(LOMeta, LOContent a)]
-> m ()
observeClose0 subtrace sev logTrace initState logObjects = do
let initialCounters = csCounters initState
-- take measurement
counters <- liftIO $ readCounters subtrace
if counters == []
then return ()
else do
mle <- mkLOMeta sev Confidential
-- send closing message to Trace
traceNamedObject logTrace $
(mle, ObserveClose (CounterState counters))
-- send diff message to Trace
traceNamedObject logTrace $
(mle, ObserveDiff (CounterState (diffCounters initialCounters counters)))
-- trace the messages gathered from inside the action
forM_ logObjects $ traceNamedObject logTrace
return ()
\end{code}