Skip to content
This repository was archived by the owner on Nov 20, 2020. It is now read-only.

Commit 84cdd70

Browse files
committed
Add Debug logging
1 parent 8172923 commit 84cdd70

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/CallPolly/Rules.fs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ type Governor(log: Serilog.ILogger, buildFailurePolicy: unit -> Polly.PolicyBuil
3636
// In the general case, it's always possible to write an async expression that honors cancellation
3737
// If anyone tries to insist on using Pessimistic mode, this should instead by accomplished by explicitly having
3838
// the inner computation cut the orphan work adrift in the place where the code requires such questionable semantics
39-
| Some cutoff when not cutoff.dryRun -> Some <| Policy.TimeoutAsync(cutoff.timeout)
39+
| Some cutoff when not cutoff.dryRun ->
40+
log.Debug("Establishing TimeoutAsync for {service:l}-{call:l}: {timeout}", serviceName, callName, cutoff.timeout)
41+
Some <| Policy.TimeoutAsync(cutoff.timeout)
4042
| _ -> None
4143
let logQueuing() = log |> Events.Log.queuing (serviceName, callName)
4244
let logDeferral interval concurrencyLimit = log |> Events.Log.deferral (serviceName, callName, policyName) interval concurrencyLimit
@@ -49,6 +51,7 @@ type Governor(log: Serilog.ILogger, buildFailurePolicy: unit -> Polly.PolicyBuil
4951
| Some limit ->
5052
let logRejection (_: Context) : Task = logShedding { dop = limit.dop; queue = limit.queue } ; Task.CompletedTask
5153
let effectiveLimit = if limit.dryRun then Int32.MaxValue else limit.dop // https://github.com/App-vNext/Polly/issues/496#issuecomment-420183946
54+
log.Debug("Establishing BulkheadAsync for {service:l}-{call:l} {effectiveLimit}+{queue}", serviceName, callName, effectiveLimit, limit.queue)
5255
Some <| Policy.BulkheadAsync(maxParallelization = effectiveLimit, maxQueuingActions = limit.queue, onBulkheadRejectedAsync = logRejection)
5356
let logBreaking (exn : exn) (timespan: TimeSpan) =
5457
match config with
@@ -71,6 +74,7 @@ type Governor(log: Serilog.ILogger, buildFailurePolicy: unit -> Polly.PolicyBuil
7174
| None ->
7275
None
7376
| Some bc ->
77+
log.Debug("Establishing AdvancedCircuitBreakerAsync for {service:l}-{call:l}", serviceName, callName)
7478
buildFailurePolicy()
7579
.AdvancedCircuitBreakerAsync(
7680
failureThreshold = bc.errorRateThreshold,
@@ -121,7 +125,9 @@ type Governor(log: Serilog.ILogger, buildFailurePolicy: unit -> Polly.PolicyBuil
121125
/// Execute and/or log failures regarding invocation of a function with the relevant policy applied
122126
member __.Execute(inner : Async<'a>) : Async<'a> =
123127
match asyncPolicy with
124-
| None -> inner
128+
| None ->
129+
log.Debug("Policy Execute Raw {service:l}-{call:l}", serviceName, callName)
130+
inner
125131
| Some polly -> async {
126132
let mutable wasFull = false
127133
// NB This logging is on a best-effort basis - obviously the guard here has an implied race condition
@@ -161,6 +167,7 @@ type Governor(log: Serilog.ILogger, buildFailurePolicy: unit -> Polly.PolicyBuil
161167
Async.StartAsTask(inner, cancellationToken=pollyCt)
162168
let execute = async {
163169
let! ct = Async.CancellationToken // Grab async cancellation token of this `Execute` call, so cancellation gets propagated into the Polly [wrap]
170+
log.Debug("Policy Execute Inner {service:l}-{call:l}", serviceName, callName)
164171
try return! polly.ExecuteAsync(startInnerTask, ct) |> Async.AwaitTaskCorrect
165172
// TODO find/add a cleaner way to use the Polly API to log when the event fires due to the the circuit being Isolated/Broken
166173
with LogWhenRejectedFilter jitProcessingInterval -> return! invalidOp "not possible; Filter always returns None" }
@@ -172,9 +179,11 @@ type Governor(log: Serilog.ILogger, buildFailurePolicy: unit -> Polly.PolicyBuil
172179
finally
173180
if not jitProcessingInterval.IsValueCreated then
174181
let processingInterval = jitProcessingInterval.Force()
175-
match sla, processingInterval.Elapsed with
176-
| _, elapsed when elapsed > timeout && dryRun -> logTimeout config processingInterval
177-
| Some sla, elapsed when elapsed > sla -> logBreach sla processingInterval
182+
let elapsed = processingInterval.Elapsed
183+
log.Debug("Policy Executed in {elapsedMs} {service:l}-{call:l}", elapsed.TotalMilliseconds, serviceName, callName)
184+
match sla with
185+
| _ when elapsed > timeout && dryRun -> logTimeout config processingInterval
186+
| Some sla when elapsed > sla -> logBreach sla processingInterval
178187
| _ -> () }
179188

180189
/// Diagnostic state

0 commit comments

Comments
 (0)