chewbranca commented on code in PR #5602:
URL: https://github.com/apache/couchdb/pull/5602#discussion_r2221208922


##########
src/couch_stats/src/csrt_util.erl:
##########
@@ -0,0 +1,386 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(csrt_util).
+
+-export([
+    is_enabled/0,
+    is_enabled_init_p/0,
+    is_enabled_reporting/0,
+    is_enabled_rpc_reporting/0,
+    get_pid_ref/0,
+    get_pid_ref/1,
+    set_pid_ref/1,
+    should_track_init_p/2,
+    tnow/0,
+    tutc/0,
+    tutc/1
+]).
+
+%% Delta API
+-export([
+    add_delta/2,
+    extract_delta/1,
+    get_delta/1,
+    get_delta_a/0,
+    get_updated_at/0,
+    maybe_add_delta/1,
+    maybe_add_delta/2,
+    make_delta/1,
+    make_dt/2,
+    make_dt/3,
+    rctx_delta/2,
+    put_delta_a/1,
+    put_updated_at/1
+]).
+
+%% Extra niceties and testing facilities
+-export([
+    set_fabric_init_p/2,
+    set_fabric_init_p/3
+]).
+
+-include_lib("csrt.hrl").
+
+-ifdef(TEST).
+-spec is_enabled() -> boolean().
+is_enabled() ->
+    %% randomly enable CSRT during testing to handle unexpected failures
+    case config:get_boolean(?CSRT, "randomize_testing", true) of
+        true ->
+            rand:uniform(100) > 80;
+        false ->
+            config:get_boolean(?CSRT, "enable", true)
+    end.
+-else.
+-spec is_enabled() -> boolean().
+is_enabled() ->
+    %% TODO: toggle back to false before merging
+    config:get_boolean(?CSRT, "enable", true).
+-endif.
+
+-spec is_enabled_init_p() -> boolean().
+is_enabled_init_p() ->
+    %% TODO: toggle back to false before merging
+    config:get_boolean(?CSRT, "enable_init_p", true).
+
+-spec should_track_init_p(Mod :: atom(), Func :: atom()) -> boolean().
+should_track_init_p(fabric_rpc, Func) ->
+    is_enabled_init_p() andalso config:get_boolean(?CSRT_INIT_P, 
fabric_conf_key(Func), false);
+should_track_init_p(_Mod, _Func) ->
+    false.
+
+%% Toggle to disable all reporting
+-spec is_enabled_reporting() -> boolean().
+is_enabled_reporting() ->
+    %% TODO: toggle back to false before merging
+    config:get_boolean(?CSRT, "enable_reporting", true).
+
+%% Toggle to disable all reporting from #rpc_worker{} types, eg only log
+%% #coordinator{} types. This is a bit of a kludge that would be better served
+%% by a dynamic match spec generator, but this provides a know for disabling
+%% any rpc worker logs, even if they hit the normal logging Threshold's.
+-spec is_enabled_rpc_reporting() -> boolean().
+is_enabled_rpc_reporting() ->
+    config:get_boolean(?CSRT, "enable_rpc_reporting", false).
+
+%% Monotnonic time now in native format using time forward only event tracking
+-spec tnow() -> integer().
+tnow() ->
+    erlang:monotonic_time().
+
+%% Get current system time in UTC RFC 3339 format
+-spec tutc() -> calendar:rfc3339_string().
+tutc() ->
+    tutc(tnow()).
+
+%% Convert a integer system time in milliseconds into UTC RFC 3339 format
+-spec tutc(Time :: integer()) -> calendar:rfc3339_string().
+tutc(Time0) when is_integer(Time0) ->
+    Unit = millisecond,
+    Time1 = Time0 + erlang:time_offset(),
+    Time = erlang:convert_time_unit(Time1, native, Unit),
+    calendar:system_time_to_rfc3339(Time, [{unit, Unit}, {offset, "z"}]).
+
+%% Returns dt (delta time) in microseconds
+%% @equiv make_dt(A, B, microsecond)
+-spec make_dt(A, B) -> pos_integer() when
+    A :: integer(),
+    B :: integer().
+make_dt(A, B) ->
+    make_dt(A, B, microsecond).
+
+%% Returns monotonic dt (delta time) in specified time_unit()
+-spec make_dt(A, B, Unit) -> pos_integer() when
+    A :: integer(),
+    B :: integer(),
+    Unit :: erlang:time_unit().
+make_dt(A, A, _Unit) when is_integer(A) ->
+    %% Handle edge case when monotonic_time()'s are equal
+    %% Always return a non zero value so we don't divide by zero
+    %% This always returns 1, independent of unit, as that's the smallest
+    %% possible positive integer value delta.
+    1;
+make_dt(A, B, Unit) when is_integer(A) andalso is_integer(B) andalso B > A ->
+    case erlang:convert_time_unit(abs(B - A), native, Unit) of

Review Comment:
   Yes, `abs` is necessary, see my other comments.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to