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

Commit c8f1872

Browse files
committed
Polish change logging, fix change detection
1 parent 31dc39d commit c8f1872

3 files changed

Lines changed: 31 additions & 24 deletions

File tree

src/CallPolly/Context.fs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
open Serilog
44
open System
55

6+
module private SerilogHelpers =
7+
let inline kv x y = System.Collections.Generic.KeyValuePair<_,_>(x,y)
8+
let inline lo x (*lp*) = Serilog.Events.StructureValue x
9+
let inline ls x (*lp seq*)= Serilog.Events.SequenceValue x
10+
let inline ld x (*lv,kvp(lv)*) = Serilog.Events.DictionaryValue x
11+
let inline lp n v(*:lo/lv/ls/ld*) = Serilog.Events.LogEventProperty(n, v)
12+
let inline lv (x:#obj) = Serilog.Events.ScalarValue(x)
13+
let forContextExplicit k v (log : ILogger) =
14+
let enrich (e : Serilog.Events.LogEvent) = e.AddPropertyIfAbsent(Serilog.Events.LogEventProperty(k, v))
15+
log.ForContext({ new Serilog.Core.ILogEventEnricher with member __.Enrich(evt,_) = enrich evt })
16+
617
module private Impl =
718

819
type Warning = { service: string; call: string; ruleText: string }
@@ -11,9 +22,9 @@ module private Impl =
1122
let mutable current = readDefinitions ()
1223
let ingest () =
1324
let res = Parser.parse current
14-
if not (List.isEmpty res.Warnings) then
15-
let msgs = seq { for w in res.Warnings -> { service=w.serviceName; call=w.callName; ruleText = string w.unknownRule } }
16-
log.ForContext("{count} Warnings", msgs, true).Warning("Definition had {count} unrecognized rules", List.length res.Warnings)
25+
if not (Array.isEmpty res.Warnings) then
26+
let msgs = seq { for w in res.Warnings -> { service=w.serviceName; call=w.callName; ruleText=string w.unknownRule } }
27+
log.ForContext("warnings", msgs).Warning("Policy definitions had {count} unrecognized rules", res.Warnings.Length)
1728
res
1829
let tryReadUpdates () =
1930
let updated = readDefinitions()
@@ -24,20 +35,16 @@ module private Impl =
2435

2536
ingest(),tryReadUpdates
2637

27-
type ServicePolicyUpdate =
28-
{ service: string
29-
actionUpdates: (string * CallPolly.Rules.ChangeLevel) [] }
30-
31-
let logChanges (log: ILogger) res =
32-
let changes =
33-
seq { for service, actionUpdates in res -> { service = service; actionUpdates = List.toArray actionUpdates } }
34-
|> Seq.distinct
35-
|> Seq.cache
36-
log
37-
.ForContext("dump", changes, true)
38-
.Information("Updated {count} values for {@services}",
39-
Seq.length changes,
40-
seq { for x in changes -> x.service, Array.length x.actionUpdates });
38+
open SerilogHelpers
39+
let logChanges (log: ILogger) (res: (string*(string*Rules.ChangeLevel) list) list) =
40+
let xs = res |> Seq.filter (function _,c -> not (List.isEmpty c)) |> Seq.sortBy (function _,c -> -List.length c) |> Seq.cache
41+
if not (Seq.isEmpty xs) then
42+
//.ForContext("changes", seq { for s,calls in xs do for c,change in calls -> sprintf "%s:%s:%O" s c change }, true )
43+
let changesAsDictionary calls = ld (seq { for callname,change in calls -> kv (lv callname) (lv (string change) :> _) })
44+
let dump = ls <| seq { for s,calls in xs -> lo [ lp "service" (lv s); lp "changes" (changesAsDictionary calls)] }
45+
(log |> forContextExplicit "dump" dump).Information("Updated {count} values for {services}",
46+
xs |> Seq.sumBy (function _service,changes -> changes.Length),
47+
xs |> Seq.map (function service,changes -> kv service changes.Length))
4148

4249
[<NoComparison>]
4350
type CallPolicyInternalState =

src/CallPolly/Parser.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,13 @@ type ParseResult(services: ParsedService[]) =
173173
let mapped = services |> Seq.map mapService
174174

175175
member __.Warnings =
176-
[ for service in services do
176+
[| for service in services do
177177
for call in service.calls do
178178
for rule in call.rules do
179179
match rule with
180180
| ParsedRule.Unknown jo ->
181181
yield { serviceName = service.serviceName; callName = call.callName; unknownRule = jo }
182-
| _ -> () ]
182+
| _ -> () |]
183183

184184
member __.Raw : Map<string,Map<string,ParsedRule list>> = Map.ofSeq <| seq {
185185
for service in services ->

src/CallPolly/Rules.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,22 @@ type CallPolicy<'TConfig when 'TConfig: equality> (makeGoverner : CallConfig<'TC
189189

190190
/// Ingest an updated set of config values, reporting diffs, if any
191191
member __.TryUpdate(updated : CallConfig<'TConfig>) =
192-
let changes =
192+
let level =
193193
match updated.policy = cfg.policy, updated.config = cfg.config with
194-
| true, true -> Some ChangeLevel.ConfigurationAndPolicy
194+
| false, false -> Some ChangeLevel.ConfigurationAndPolicy
195195
| true, false -> Some ChangeLevel.Configuration
196196
| false, true -> Some ChangeLevel.Policy
197-
| false, false -> None
197+
| true, true -> None
198198

199-
match changes with
199+
match level with
200200
| Some ChangeLevel.ConfigurationAndPolicy | Some ChangeLevel.Policy ->
201201
governor <- makeGoverner updated
202202
cfg <- updated
203203
| Some ChangeLevel.Configuration ->
204204
cfg <- updated
205205
| _ -> ()
206206

207-
changes
207+
level
208208

209209
member __.Policy = cfg.policy
210210
member __.Config = cfg.config

0 commit comments

Comments
 (0)