update_histogram with timing of a function histogram-fun 3/head
authorRobert Newson <rnewson@apache.org>
Sat, 1 Nov 2014 16:32:16 +0000 (16:32 +0000)
committerRobert Newson <rnewson@apache.org>
Sun, 2 Nov 2014 18:49:45 +0000 (18:49 +0000)
src/couch_stats.erl

index b4b3b22a0cfa41261d52d2b246155aaa77278362..59854e22754eb94140c2d80eb99d794f6b91c78a 100644 (file)
@@ -87,8 +87,19 @@ decrement_counter(Name) ->
 decrement_counter(Name, Value) ->
     notify(Name, {dec, Value}).
 
--spec update_histogram(any(), number()) -> response().
-update_histogram(Name, Value) ->
+-spec update_histogram(any(), number()) -> response();
+                      (any(), function()) -> any().
+update_histogram(Name, Fun) when is_function(Fun, 0) ->
+    Begin = os:timestamp(),
+    Result = Fun(),
+    Duration = timer:now_diff(os:timestamp(), Begin) div 1000,
+    case notify(Name, Duration) of
+        ok ->
+            Result;
+        {error, unknown_metric} ->
+            throw({unknown_metric, Name})
+    end;
+update_histogram(Name, Value) when is_number(Value) ->
     notify(Name, Value).
 
 -spec update_gauge(any(), number()) -> response().