5
5
require 'sentry/metrics/distribution_metric'
6
6
require 'sentry/metrics/gauge_metric'
7
7
require 'sentry/metrics/set_metric'
8
+ require 'sentry/metrics/timing'
8
9
require 'sentry/metrics/aggregator'
9
10
10
11
module Sentry
11
12
module Metrics
13
+ DURATION_UNITS = %w[ nanosecond microsecond millisecond second minute hour day week ]
14
+ INFORMATION_UNITS = %w[ bit byte kilobyte kibibyte megabyte mebibyte gigabyte gibibyte terabyte tebibyte petabyte pebibyte exabyte exbibyte ]
15
+ FRACTIONAL_UNITS = %w[ ratio percent ]
16
+
12
17
class << self
13
18
def increment ( key , value = 1.0 , unit : 'none' , tags : { } , timestamp : nil )
14
19
Sentry . metrics_aggregator &.add ( :c , key , value , unit : unit , tags : tags , timestamp : timestamp )
@@ -25,6 +30,18 @@ def set(key, value, unit: 'none', tags: {}, timestamp: nil)
25
30
def gauge ( key , value , unit : 'none' , tags : { } , timestamp : nil )
26
31
Sentry . metrics_aggregator &.add ( :g , key , value , unit : unit , tags : tags , timestamp : timestamp )
27
32
end
33
+
34
+ def timing ( key , unit : 'second' , tags : { } , timestamp : nil , &block )
35
+ return unless Sentry . metrics_aggregator
36
+ return unless block_given?
37
+ return unless DURATION_UNITS . include? ( unit )
38
+
39
+ start = Timing . send ( unit . to_sym )
40
+ yield
41
+ value = Timing . send ( unit . to_sym ) - start
42
+
43
+ Sentry . metrics_aggregator . add ( :d , key , value , unit : unit , tags : tags , timestamp : timestamp )
44
+ end
28
45
end
29
46
end
30
47
end
0 commit comments