33open Serilog
44open 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+
617module 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>]
4350type CallPolicyInternalState =
0 commit comments