Class: TsdMetrics::TsdMetric

Inherits:
Object
  • Object
show all
Defined in:
lib/tsd_metrics/tsd_metric.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(startTime, metricSink, mutexStrategy) ⇒ TsdMetric

Returns a new instance of TsdMetric.



26
27
28
29
30
31
32
33
34
35
# File 'lib/tsd_metrics/tsd_metric.rb', line 26

def initialize(startTime, metricSink, mutexStrategy)
  @metricSink = metricSink
  @annotations = {initTimestamp: startTime}
  @mutexStrategy = mutexStrategy
  @gauges = {}
  @metrics = {timers: {}, counters: {}}
  @metricClasses = {timers: Timer, counters: Counter}
  @staticSamples = {timers: {}, counters: {}}
  @closed = false
end

Instance Attribute Details

#annotationsObject (readonly)

Implements TsdMetricEvent interface



24
25
26
# File 'lib/tsd_metrics/tsd_metric.rb', line 24

def annotations
  @annotations
end

#gaugesObject (readonly)

Implements TsdMetricEvent interface



24
25
26
# File 'lib/tsd_metrics/tsd_metric.rb', line 24

def gauges
  @gauges
end

Instance Method Details

#annotate(name, value) ⇒ Object



119
120
121
122
123
124
# File 'lib/tsd_metrics/tsd_metric.rb', line 119

def annotate(name, value)
  @mutexStrategy.synchronize do
    assertNotClosed
    @annotations[name] = value
  end
end

#closeObject



126
127
128
129
130
131
132
133
# File 'lib/tsd_metrics/tsd_metric.rb', line 126

def close
  @mutexStrategy.synchronize do
    assertNotClosed
    @closed = true
    @annotations[:finalTimestamp] = Time.now()
    @metricSink.record(self)
  end
end

#countersObject



143
144
145
146
147
148
149
# File 'lib/tsd_metrics/tsd_metric.rb', line 143

def counters
  countersHash = {}
  getMetricsOfType(:counters).each do |k,v|
    countersHash[k] = v.values
  end
  countersHash
end

#createCounter(name) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/tsd_metrics/tsd_metric.rb', line 111

def createCounter(name)
  @mutexStrategy.synchronize do
    assertNotClosed
    ensureMetricExists(:counters, name)
    getMetric(:counters, name).createNewSample
  end
end

#createTimer(name) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/tsd_metrics/tsd_metric.rb', line 79

def createTimer(name)
  @mutexStrategy.synchronize do
    assertNotClosed
    ensureMetricExists(:timers, name)
    sample = getMetric(:timers, name).createNewSample
    sample
  end
end

#decrementCounter(name, magnitude = 1) ⇒ Object



104
105
106
107
108
109
# File 'lib/tsd_metrics/tsd_metric.rb', line 104

def decrementCounter(name, magnitude=1)
  @mutexStrategy.synchronize do
    assertNotClosed
    incrementCounter(name, -1*magnitude)
  end
end

#incrementCounter(name, magnitude = 1) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/tsd_metrics/tsd_metric.rb', line 96

def incrementCounter(name, magnitude=1)
  @mutexStrategy.synchronize do
    assertNotClosed
    ensureStaticCounterSampleExists(name)
    getStaticSample(:counters, name).increment(magnitude)
  end
end

#metricIsClosedObject

“Implements” metricStatusSupplier



152
153
154
# File 'lib/tsd_metrics/tsd_metric.rb', line 152

def metricIsClosed
  return @closed
end

#open?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/tsd_metrics/tsd_metric.rb', line 37

def open?
  @mutexStrategy.synchronize do
    not @closed
  end
end

#resetCounter(name) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/tsd_metrics/tsd_metric.rb', line 88

def resetCounter(name)
  @mutexStrategy.synchronize do
    assertNotClosed
    ensureCounterExists(name)
    @staticSamples[:counters][name] = getMetric(:counters, name).createNewSample
  end
end

#setGauge(name, value, unit = :noUnit) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/tsd_metrics/tsd_metric.rb', line 43

def setGauge(name, value, unit = :noUnit)
  @mutexStrategy.synchronize do
    assertNotClosed
    assertValidUnit(unit)
    @gauges[name] ||= []
    @gauges[name].push({value: value, unit: unit}.select{|k, v| v != :noUnit})
  end
end

#setTimer(name, duration, unit = :noUnit) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/tsd_metrics/tsd_metric.rb', line 68

def setTimer(name, duration, unit = :noUnit)
  @mutexStrategy.synchronize do
    assertNotClosed
    assertValidUnit(unit)
    pushNewStaticSample(:timers, name)
    sample = getStaticSample(:timers, name)
    sample.stop()
    sample.set(duration, unit)
  end
end

#startTimer(name) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/tsd_metrics/tsd_metric.rb', line 52

def startTimer(name)
  @mutexStrategy.synchronize do
    assertNotClosed
    # Timer sample is started on creation
    pushNewStaticSample(:timers, name)
  end
end

#stopTimer(name) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/tsd_metrics/tsd_metric.rb', line 60

def stopTimer(name)
  @mutexStrategy.synchronize do
    assertNotClosed
    sample = getStaticSample(:timers, name)
    sample.stop
  end
end

#timersObject



135
136
137
138
139
140
141
# File 'lib/tsd_metrics/tsd_metric.rb', line 135

def timers
  samplesHash = {}
  getMetricsOfType(:timers).each do |timerName,timer|
    samplesHash[timerName] = timer.samples
  end
  samplesHash
end