Skip to content

Commit 5e5e9a0

Browse files
committed
Var: detect duplicate keys in cc
1 parent bc9231c commit 5e5e9a0

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

logstash.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ let common_fields () =
8282
let get () =
8383
let open Var in
8484
let l = ref [] in
85-
Var.iter begin fun attr v ->
85+
Var.iter begin fun attr' v ->
8686
let (previous,attr) =
87-
try Hashtbl.find state attr with
87+
try Hashtbl.find state attr' with
8888
| Not_found ->
89-
let a = List.map (fun (k, s) -> escape k, `String s) attr in
89+
let a = List.map (fun (k, s) -> escape k, `String s) attr' in
9090
let x = ref (zero v), a in
91-
Hashtbl.add state attr x; x
91+
Hashtbl.add state attr' x;
92+
x
9293
in
9394
let this = (common_fields () @ attr : (string * json) list :> (string * [> json ]) list) in
9495
match v, !previous with
@@ -103,7 +104,7 @@ let get () =
103104
if delta > epsilon_float then begin previous := v; tuck l @@ `Assoc (("seconds", `Float delta) :: this) end
104105
| Count _, Bytes _ | Count _, Time _
105106
| Bytes _, Count _ | Bytes _, Time _
106-
| Time _, Count _ | Time _, Bytes _ -> () (* cannot happen *)
107+
| Time _, Count _ | Time _, Bytes _ -> log #warn "the impossible happened : mismatched type for %S" (show_a attr')
107108
end;
108109
dynamic |> Hashtbl.iter begin fun attr v ->
109110
let attr = List.map (fun (k, s) -> escape k, s) attr in

var.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,13 @@ object(self)
9595
end
9696

9797
let iter f =
98-
h_families |> Hashtbl.iter begin fun _name g -> (* iterate over counter families *)
98+
h_families |> Hashtbl.iter begin fun name g -> (* iterate over counter families *)
9999
match g.get with
100100
| [get] -> (* no duplicates in this family *)
101-
get () |> List.iter begin fun (k,v) ->
101+
let l = get () in
102+
let l' = List.sort_uniq (fun (a,_) (b,_) -> String.compare a b) l in
103+
if List.length l <> List.length l' then log#warn "var %s : duplicate keys found and will be ignored" (show_a @@ Attr.get name);
104+
l' |> List.iter begin fun (k,v) ->
102105
let attr = (g.k, k) :: Attr.get g.attr in (* this was checked to be valid in [register] *)
103106
match v with Some v -> f attr v | _ -> ()
104107
end

0 commit comments

Comments
 (0)