nickva commented on code in PR #4672: URL: https://github.com/apache/couchdb/pull/4672#discussion_r1261496212
########## src/couch_stats/src/couch_stats.erl: ########## @@ -29,102 +24,174 @@ update_gauge/2 ]). --include("couch_stats.hrl"). - --type response() :: ok | {error, unknown_metric}. +-type response() :: ok | {error, unknown_metric} | {error, invalid_metric}. -type stat() :: {any(), [{atom(), any()}]}. -start() -> - application:start(couch_stats). - -stop() -> - application:stop(couch_stats). - fetch() -> - couch_stats_aggregator:fetch(). + Seconds = couch_stats_util:histogram_interval_sec(), + StartSec = now_sec() - (Seconds - 1), + % Last -1 is because the interval ends are inclusive + couch_stats_util:fetch(stats(), StartSec, Seconds). reload() -> - couch_stats_aggregator:reload(). + couch_stats_server:reload(). -spec sample(any()) -> stat(). sample(Name) -> - [{Name, Info}] = folsom_metrics:get_metric_info(Name), - sample_type(Name, proplists:get_value(type, Info)). - --spec new(atom(), any()) -> ok | {error, metric_exists | unsupported_type}. -new(counter, Name) -> - case folsom_metrics:new_counter(Name) of - ok -> ok; - {error, Name, metric_already_exists} -> {error, metric_exists} - end; -new(histogram, Name) -> - Time = config:get_integer("stats", "interval", ?DEFAULT_INTERVAL), - case folsom_metrics:new_histogram(Name, slide_uniform, {Time, 1024}) of - ok -> ok; - {error, Name, metric_already_exists} -> {error, metric_exists} - end; -new(gauge, Name) -> - case folsom_metrics:new_gauge(Name) of - ok -> ok; - {error, Name, metric_already_exists} -> {error, metric_exists} - end; -new(_, _) -> - {error, unsupported_type}. - -delete(Name) -> - folsom_metrics:delete_metric(Name). - -list() -> - folsom_metrics:get_metrics_info(). + Seconds = couch_stats_util:histogram_interval_sec(), + StartSec = now_sec() - (Seconds - 1), + % Last -1 is because the interval ends are inclusive + couch_stats_util:sample(Name, stats(), StartSec, Seconds). -spec increment_counter(any()) -> response(). increment_counter(Name) -> - notify_existing_metric(Name, {inc, 1}, counter). + increment_counter(Name, 1). -spec increment_counter(any(), pos_integer()) -> response(). increment_counter(Name, Value) -> - notify_existing_metric(Name, {inc, Value}, counter). + case couch_stats_util:get_counter(Name, stats()) of + {ok, Ctx} -> couch_stats_counter:increment(Ctx, Value); + {error, Error} -> {error, Error} + end. -spec decrement_counter(any()) -> response(). decrement_counter(Name) -> - notify_existing_metric(Name, {dec, 1}, counter). + decrement_counter(Name, 1). -spec decrement_counter(any(), pos_integer()) -> response(). decrement_counter(Name, Value) -> - notify_existing_metric(Name, {dec, Value}, counter). + case couch_stats_util:get_counter(Name, stats()) of + {ok, Ctx} -> couch_stats_counter:decrement(Ctx, Value); + {error, Error} -> {error, Error} + end. + +-spec update_gauge(any(), number()) -> response(). +update_gauge(Name, Value) -> + case couch_stats_util:get_gauge(Name, stats()) of + {ok, Ctx} -> couch_stats_gauge:update(Ctx, Value); + {error, Error} -> {error, Error} + end. -spec update_histogram (any(), number()) -> response(); (any(), function()) -> any(). update_histogram(Name, Fun) when is_function(Fun, 0) -> - Begin = os:timestamp(), + Begin = erlang:monotonic_time(), Result = Fun(), - Duration = timer:now_diff(os:timestamp(), Begin) div 1000, - case notify_existing_metric(Name, Duration, histogram) of + Dt = erlang:monotonic_time() - Begin, + Duration = erlang:convert_time_unit(Dt, native, millisecond), + case update_histogram(Name, Duration) of ok -> Result; {error, unknown_metric} -> - throw({unknown_metric, Name}) + throw({unknown_metric, Name}); + {error, invalid_metric} -> + throw({invalid_metric, Name}) end; update_histogram(Name, Value) when is_number(Value) -> - notify_existing_metric(Name, Value, histogram). - --spec update_gauge(any(), number()) -> response(). -update_gauge(Name, Value) -> - notify_existing_metric(Name, Value, gauge). - --spec notify_existing_metric(any(), any(), any()) -> response(). -notify_existing_metric(Name, Op, Type) -> - try - ok = folsom_metrics:notify_existing_metric(Name, Op, Type) - catch - _:_ -> - error_logger:error_msg("unknown metric: ~p", [Name]), - {error, unknown_metric} + case couch_stats_util:get_histogram(Name, stats()) of + {ok, Ctx} -> couch_stats_histogram:update(Ctx, now_sec(), Value); + {error, Error} -> {error, Error} end. --spec sample_type(any(), atom()) -> stat(). -sample_type(Name, histogram) -> - folsom_metrics:get_histogram_statistics(Name); -sample_type(Name, _) -> - folsom_metrics:get_metric_value(Name). +stats() -> + couch_stats_util:stats(). + +now_sec() -> + erlang:monotonic_time(second). + +-ifdef(TEST). + +-include_lib("couch/include/couch_eunit.hrl"). + +couch_stats_test_() -> + { + foreach, + fun setup/0, + fun teardown/1, + [ + ?TDEF_FE(t_can_fetch_metrics), + ?TDEF_FE(t_can_sample_metrics), + ?TDEF_FE(t_can_reload), + ?TDEF_FE(t_increment_counter), + ?TDEF_FE(t_decrement_counter), + ?TDEF_FE(t_update_gauge), + ?TDEF_FE(t_update_histogram), + ?TDEF_FE(t_update_histogram_fun), + ?TDEF_FE(t_accessing_invalid_metrics) Review Comment: Agree, that's a better name. -- 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