From 3bd4cf51174022974181db567d558b4c9f24e251 Mon Sep 17 00:00:00 2001 From: Anthony Molinaro Date: Sat, 15 Dec 2018 00:39:50 +0000 Subject: [PATCH] Fix a serialization bug, switch to rebar3 --- .gitignore | 1 - ChangeLog | 7 +++- Makefile | 38 ++++++++++++--------- rebar.config | 10 ++---- src/mondemand_statsmsg.erl | 69 +++++++++++++++++++++++++++++++++++++- 5 files changed, 99 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 54723de..f00b245 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ ebin .eunit .rebar _build -rebar.lock diff --git a/ChangeLog b/ChangeLog index 9ebb788..6a54181 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,9 @@ -* Fri Jun 8 2018 Yoshihiro Tanaka (yoshi) 6.10.0 +* Fri Dec 14 2018 Anthony Molinaro (djnym) 6.10.1 +- use rebar3 in Makefile +- fix for when statsmsg contexts are mismatched, the library will no longer +crash + +* Fri Jun 08 2018 Yoshihiro Tanaka (yoshi) 6.10.0 - Add literal memory usage stat * Tue May 15 2018 Anthony Molinaro (djnym) 6.9.4 diff --git a/Makefile b/Makefile index 71c59c8..97b719c 100644 --- a/Makefile +++ b/Makefile @@ -1,22 +1,30 @@ +NAME=mondemand + +REBAR3=rebar3 all: - @rebar get-deps compile + $(REBAR3) compile + +dialyzer: + $(REBAR3) as test do dialyzer -perf: - @(cd tests ; erlc -I../src -I../include *.erl) +test: + $(REBAR3) as test do dialyzer,eunit,cover -edoc: - @rebar skip_deps=true doc +# Compile and run unit test for individual modules: 'make test-oxgw_util' +# or 'make test-oxgw_util test-oxgw_request' +test-%: src/%.erl + $(REBAR3) as test do eunit -m $* -check: - @rm -rf .eunit - @mkdir -p .eunit - @dialyzer --src src -I deps -pa deps/parse_trans/ebin - @rebar skip_deps=true eunit +name: + @echo $(NAME) + +version: + @echo $(shell awk 'match($$0, /[0-9]+\.[0-9]+(\.[0-9]+)+/){print substr($$0, RSTART,RLENGTH); exit}' ChangeLog) clean: - @rebar clean + if test -d _build; then $(REBAR3) clean; fi + +maintainer-clean: clean + rm -rf _build -maintainer-clean: - @rebar clean - @rebar delete-deps - @rm -rf deps +.PHONY: all test name version clean maintainer-clean diff --git a/rebar.config b/rebar.config index 5d8b641..71e1da7 100644 --- a/rebar.config +++ b/rebar.config @@ -17,13 +17,7 @@ {deps, [ - { lwes, - "4.6.1", - {git, "git://github.com/lwes/lwes-erlang.git", {tag, "4.6.1"}} - }, - { parse_trans, - "3.2.0", - {git, "git://github.com/uwiger/parse_trans.git", {tag, "3.2.0"} } - } + { lwes, {git, "git://github.com/lwes/lwes-erlang.git", {tag, "4.7.1"}} }, + { parse_trans, {git, "git://github.com/uwiger/parse_trans.git", {tag, "3.2.0"} } } ] }. diff --git a/src/mondemand_statsmsg.erl b/src/mondemand_statsmsg.erl index 6c3602e..7ccc27a 100644 --- a/src/mondemand_statsmsg.erl +++ b/src/mondemand_statsmsg.erl @@ -440,7 +440,19 @@ zip_and_find_host ([], [], Host, Context) -> zip_and_find_host ([{K, <<"host">>} | Keys], [{K, Host} | Vals], _, Context) -> zip_and_find_host (Keys, Vals, Host, Context); zip_and_find_host ([{K, Key} | Keys ], [{K, Val} | Vals ], Host, Context) -> - zip_and_find_host (Keys, Vals, Host, [{Key, Val} | Context]). + zip_and_find_host (Keys, Vals, Host, [{Key, Val} | Context]); +zip_and_find_host (L1 = [{K1, Key} | Keys ], + L2 = [{K2, Val} | Vals], + Host, Context) -> + % error case, there's something missing so we will log and drop it + case K1 < K2 of + true -> + % we have a key but no value, so just add an empty context for the key + zip_and_find_host(Keys, L2, Host, [{Key, missing} | Context]); + false -> + % we have a context but no key! So make something up + zip_and_find_host(L1, Vals, Host, [{missing, Val} | Context]) + end. process ([], ReceiptTime, @@ -1787,6 +1799,48 @@ index (<<"1024">>) -> 1025. -ifdef (TEST). -include_lib ("eunit/include/eunit.hrl"). +% we had the situation where some events were being constructed improperly +% and deserialization was crashing so here's a bad stats msg to test +% against. +bad_event1() -> + #lwes_event { name = <<"MonDemand::StatsMsg">>, + attrs = + [ {string,<<"prog_id">>,<<"foo">>}, + {int16,<<"enc">>,1}, % ignored as this is not used + + {uint16,<<"num">>,1}, + {string,<<"t0">>,<<"counter">>}, + {string,<<"k0">>,<<"blank">>}, + {int64,<<"v0">>,0}, + {uint16,<<"ctxt_num">>,3}, + {string,<<"ctxt_k0">>,<<"host">>}, + {string,<<"ctxt_v0">>,<<"localhost">>}, + {string,<<"ctxt_k1">>,<<"p">>}, + % notice the missing ctxt_v1 here + {string,<<"ctxt_k2">>,<<"s">>}, + {string,<<"ctxt_v2">>, <<"bar">>} + ] + }. +bad_event2() -> + #lwes_event { name = <<"MonDemand::StatsMsg">>, + attrs = + [ {string,<<"prog_id">>,<<"foo">>}, + {int16,<<"enc">>,1}, % ignored as this is not used + + {uint16,<<"num">>,1}, + {string,<<"t0">>,<<"counter">>}, + {string,<<"k0">>,<<"blank">>}, + {int64,<<"v0">>,0}, + {uint16,<<"ctxt_num">>,3}, + {string,<<"ctxt_k0">>,<<"host">>}, + {string,<<"ctxt_v0">>,<<"localhost">>}, + % notice the missing ctxt_k1 here + {string,<<"ctxt_v1">>,<<"p">>}, + {string,<<"ctxt_k2">>,<<"s">>}, + {string,<<"ctxt_v2">>, <<"bar">>} + ] + }. + statsmsg_test_ () -> [ { "basic constructor test", @@ -2082,6 +2136,19 @@ statsmsg_test_ () -> ?assertEqual (prog_id (S1), prog_id (S2)), ?assertEqual (lists:sort(context(S1)), lists:sort(context (S2))) end + }, + { "malformed events", + fun() -> + % missing value for key + {0, B1} = + from_lwes ( + lwes_event:from_binary(lwes_event:to_binary(bad_event1()),list)), + ?assertEqual( context_value(B1, <<"p">>), missing), + {0, B2} = + from_lwes ( + lwes_event:from_binary(lwes_event:to_binary(bad_event2()),list)), + ?assertEqual( context_value(B2, missing), <<"p">>) + end } ].