Skip to content

Commit

Permalink
Create a stat to track updates from ics (#266)
Browse files Browse the repository at this point in the history
* Create a stat to track updates from ics

* Fix
  • Loading branch information
macpie authored Nov 7, 2023
1 parent 336b922 commit ae01061
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
4 changes: 3 additions & 1 deletion include/hpr_metrics.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
-define(METRICS_FIND_ROUTES_HISTOGRAM, "hpr_find_routes_histogram").
-define(METRICS_VM_ETS_MEMORY, "hpr_vm_ets_memory").
-define(METRICS_VM_PROC_Q, "hpr_vm_process_queue").
-define(METRICS_ICS_UPDATES_COUNTER, "hpr_iot_config_service_updates_counter").

-define(METRICS, [
{?METRICS_GRPC_CONNECTION_GAUGE, prometheus_gauge, [], "Number of active GRPC Connections"},
Expand All @@ -34,5 +35,6 @@
{?METRICS_MULTI_BUY_GET_HISTOGRAM, prometheus_histogram, [status], "Multi Buy Service Get"},
{?METRICS_FIND_ROUTES_HISTOGRAM, prometheus_histogram, [], "Find Routes"},
{?METRICS_VM_ETS_MEMORY, prometheus_gauge, [name], "HPR ets memory"},
{?METRICS_VM_PROC_Q, prometheus_gauge, [name], "Process queue"}
{?METRICS_VM_PROC_Q, prometheus_gauge, [name], "Process queue"},
{?METRICS_ICS_UPDATES_COUNTER, prometheus_counter, [type, action], "ICS updates counter"}
]).
8 changes: 6 additions & 2 deletions src/grpc/iot_config/hpr_route_stream_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
devaddr_added := non_neg_integer()
}.


-record(state, {
stream :: grpcbox_client:stream() | undefined,
conn_backoff :: backoff:backoff()
Expand Down Expand Up @@ -205,7 +204,12 @@ handle_info({data, _StreamID, RouteStreamRes}, #state{} = State) ->
Data = hpr_route_stream_res:data(RouteStreamRes),
{Type, _} = Data,
lager:debug([{action, Action}, {type, Type}], "got route stream update"),
_ = erlang:spawn(fun() -> ok = process_route_stream_res(Action, Data) end),
_ = erlang:spawn(
fun() ->
ok = process_route_stream_res(Action, Data),
ok = hpr_metrics:ics_update(Type, Action)
end
),
{noreply, State};
handle_info({headers, _StreamID, _Headers}, State) ->
%% noop on headers
Expand Down
18 changes: 7 additions & 11 deletions src/hpr_routing_cache.erl
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,11 @@ terminate(_Reason, _State = #state{}) ->
do_crawl_routing(Window) ->
Now = erlang:system_time(millisecond) - Window,
%% MS = ets:fun2ms(fun(#routing_entry{time = Time}) when Time < 1234 -> true end).
MS = [{
{routing_entry,
'_',
'$1',
'_',
'_',
'_'
},
[{'<', '$1', Now}],
[true]
}],
MS = [
{
{routing_entry, '_', '$1', '_', '_', '_'},
[{'<', '$1', Now}],
[true]
}
],
ets:select_delete(?ROUTING_ETS, MS).
11 changes: 10 additions & 1 deletion src/metrics/hpr_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
observe_packet_report/2,
observe_multi_buy/2,
observe_find_routes/1,
observe_grpc_connection/2
observe_grpc_connection/2,
ics_update/2
]).

%% ------------------------------------------------------------------
Expand Down Expand Up @@ -117,6 +118,14 @@ observe_grpc_connection(Type, Start) ->
erlang:system_time(millisecond) - Start
).

-spec ics_update(
Type :: route | eui_pair | devaddr_range | skf,
Action :: add | remove
) -> ok.
ics_update(Type, Action) ->
_ = prometheus_counter:inc(?METRICS_ICS_UPDATES_COUNTER, [Type, Action]),
ok.

%% ------------------------------------------------------------------
%% gen_server Function Definitions
%% ------------------------------------------------------------------
Expand Down
24 changes: 24 additions & 0 deletions test/hpr_route_stream_worker_SUITE.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-module(hpr_route_stream_worker_SUITE).

-include_lib("eunit/include/eunit.hrl").

-include("hpr_metrics.hrl").
-include("../src/grpc/autogen/iot_config_pb.hrl").

-export([
Expand Down Expand Up @@ -107,6 +109,23 @@ main_test(_Config) ->
end
),

?assertEqual(
1,
prometheus_counter:value(?METRICS_ICS_UPDATES_COUNTER, [route, add])
),
?assertEqual(
1,
prometheus_counter:value(?METRICS_ICS_UPDATES_COUNTER, [eui_pair, add])
),
?assertEqual(
1,
prometheus_counter:value(?METRICS_ICS_UPDATES_COUNTER, [devaddr_range, add])
),
?assertEqual(
1,
prometheus_counter:value(?METRICS_ICS_UPDATES_COUNTER, [skf, add])
),

[RouteETS1] = hpr_route_ets:lookup_route(Route1ID),
SKFETS1 = hpr_route_ets:skf_ets(RouteETS1),

Expand All @@ -132,6 +151,11 @@ main_test(_Config) ->
end
),

?assertEqual(
1,
prometheus_counter:value(?METRICS_ICS_UPDATES_COUNTER, [eui_pair, remove])
),

?assertEqual([], hpr_route_ets:lookup_eui_pair(1, 12)),
?assertEqual([], hpr_route_ets:lookup_eui_pair(1, 100)),
?assertEqual([], hpr_route_ets:lookup_eui_pair(3, 3)),
Expand Down

0 comments on commit ae01061

Please sign in to comment.