Class: Metrics::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid/metrics/reporter.rb

Direct Known Subclasses

LoggerReporter, TrackerReporter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Reporter

Returns a new instance of Reporter.



14
15
16
17
18
19
20
21
# File 'lib/liquid/metrics/reporter.rb', line 14

def initialize(*args)
  @registry = ::Metrics.registry
  @filter = MetricFilter::ALL
  @executor = Executors.newSingleThreadScheduledExecutor
  self.rate_unit = TimeUnit::SECONDS
  self.duration_unit = TimeUnit::MILLISECONDS
  ::Metrics.register_reporter(self)
end

Instance Attribute Details

#duration_unitObject

Returns the value of attribute duration_unit.



12
13
14
# File 'lib/liquid/metrics/reporter.rb', line 12

def duration_unit
  @duration_unit
end

#filterObject

Returns the value of attribute filter.



10
11
12
# File 'lib/liquid/metrics/reporter.rb', line 10

def filter
  @filter
end

#rate_unitObject

Returns the value of attribute rate_unit.



11
12
13
# File 'lib/liquid/metrics/reporter.rb', line 11

def rate_unit
  @rate_unit
end

Instance Method Details

#convert_duration(duration) ⇒ Object



156
157
158
# File 'lib/liquid/metrics/reporter.rb', line 156

def convert_duration(duration)
  (duration * @duration_factor).round(3)
end

#convert_rate(rate) ⇒ Object



160
161
162
# File 'lib/liquid/metrics/reporter.rb', line 160

def convert_rate(rate)
  (rate * @rate_factor).round(3)
end

#report_counter(name, counter) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/liquid/metrics/reporter.rb', line 91

def report_counter(name, counter)
  {
    timestamp: Time.now.to_i,
    type: :counter,
    name: name,
    count: counter.count,
  }
end

#report_countersObject



58
59
60
61
62
# File 'lib/liquid/metrics/reporter.rb', line 58

def report_counters
  @registry.counters.each do |name, counter|
    report_counter(name, counter)
  end
end

#report_gauge(name, gauge) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/liquid/metrics/reporter.rb', line 82

def report_gauge(name, gauge)
  {
    timestamp: Time.now.to_i,
    type: :gauge,
    name: name,
    value: gauge.getValue,
  }
end

#report_gaugesObject



52
53
54
55
56
# File 'lib/liquid/metrics/reporter.rb', line 52

def report_gauges
  @registry.gauges.each do |name, gauge|
    report_gauge(name, gauge)
  end
end

#report_histogram(name, histogram) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/liquid/metrics/reporter.rb', line 100

def report_histogram(name, histogram)
  snapshot = histogram.snapshot
  {
    timestamp: Time.now.to_i,
    type: :histogram,
    name: name,
    count: histogram.count,
    min: snapshot.getMin,
    max: snapshot.getMax,
    mean: snapshot.getMean,
    stdev: snapshot.getStdDev,
    median: snapshot.getMedian,
    p75: snapshot.get75thPercentile,
    p95: snapshot.get95thPercentile,
    p98: snapshot.get98thPercentile,
    p99: snapshot.get99thPercentile,
    p999: snapshot.get999thPercentile,
  }
end

#report_histogramsObject



64
65
66
67
68
# File 'lib/liquid/metrics/reporter.rb', line 64

def report_histograms
  @registry.histograms.each do |name, histogram|
    report_histogram(name, histogram)
  end
end

#report_meter(name, meter) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/liquid/metrics/reporter.rb', line 120

def report_meter(name, meter)
  {
    timestamp: Time.now.to_i,
    type: :meter,
    name: name,
    count: meter.count,
    mean_rate: convert_rate(meter.getMeanRate),
    m1: convert_rate(meter.getOneMinuteRate),
    m5: convert_rate(meter.getFiveMinuteRate),
    m15: convert_rate(meter.getFifteenMinuteRate),
  }
end

#report_metersObject



70
71
72
73
74
# File 'lib/liquid/metrics/reporter.rb', line 70

def report_meters
  @registry.meters.each do |name, meter|
    report_meter(name, meter)
  end
end

#report_timer(name, timer) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/liquid/metrics/reporter.rb', line 133

def report_timer(name, timer)
  snapshot = timer.snapshot
  {
    timestamp: Time.now.to_i,
    type: :timer,
    name: name,
    min: convert_duration(snapshot.getMin),
    max: convert_duration(snapshot.getMax),
    mean: convert_duration(snapshot.getMean),
    stdev: convert_duration(snapshot.getStdDev),
    median: convert_duration(snapshot.getMedian),
    p75: convert_duration(snapshot.get75thPercentile),
    p95: convert_duration(snapshot.get95thPercentile),
    p98: convert_duration(snapshot.get98thPercentile),
    p99: convert_duration(snapshot.get99thPercentile),
    p999: convert_duration(snapshot.get999thPercentile),
    mean_rate: convert_rate(timer.getMeanRate),
    m1: convert_rate(timer.getOneMinuteRate),
    m5: convert_rate(timer.getFiveMinuteRate),
    m15: convert_rate(timer.getFifteenMinuteRate),
  }
end

#report_timersObject



76
77
78
79
80
# File 'lib/liquid/metrics/reporter.rb', line 76

def report_timers
  @registry.timers.each do |name, timer|
    report_timer(name, timer)
  end
end

#runObject



33
34
35
36
37
38
39
40
41
# File 'lib/liquid/metrics/reporter.rb', line 33

def run
  report_gauges
  report_counters
  report_histograms
  report_meters
  report_timers
rescue => e
  $log.exception(e)
end

#start(period = 60, unit = TimeUnit::SECONDS) ⇒ Object



43
44
45
# File 'lib/liquid/metrics/reporter.rb', line 43

def start(period = 60, unit = TimeUnit::SECONDS)
  @executor.scheduleAtFixedRate(self, period, period, unit)
end

#stopObject



47
48
49
50
# File 'lib/liquid/metrics/reporter.rb', line 47

def stop
  @executor.shutdown
  @executor.awaitTermination(1, TimeUnit::SECONDS) rescue nil
end