Class: StatsD::Instrument::Aggregator
- Inherits:
-
Object
- Object
- StatsD::Instrument::Aggregator
- Defined in:
- lib/statsd/instrument/aggregator.rb
Constant Summary collapse
- DEFAULT_MAX_CONTEXT_SIZE =
250
Class Method Summary collapse
- .finalize(aggregation_state, sink, datagram_builders, datagram_builder_class, default_tags) ⇒ Object
Instance Method Summary collapse
- #aggregate_timing(name, value, tags: [], no_prefix: false, type: DISTRIBUTION) ⇒ Object
- #flush ⇒ Object
- #gauge(name, value, tags: [], no_prefix: false) ⇒ Object
-
#increment(name, value = 1, tags: [], no_prefix: false) ⇒ void
Increment a counter by a given value and save it for later flushing.
-
#initialize(sink, datagram_builder_class, prefix, default_tags, flush_interval: 5.0, max_values: DEFAULT_MAX_CONTEXT_SIZE) ⇒ Aggregator
constructor
A new instance of Aggregator.
Constructor Details
#initialize(sink, datagram_builder_class, prefix, default_tags, flush_interval: 5.0, max_values: DEFAULT_MAX_CONTEXT_SIZE) ⇒ Aggregator
Returns a new instance of Aggregator.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/statsd/instrument/aggregator.rb', line 84 def initialize( sink, datagram_builder_class, prefix, , flush_interval: 5.0, max_values: DEFAULT_MAX_CONTEXT_SIZE ) @sink = sink @datagram_builder_class = datagram_builder_class @metric_prefix = prefix @default_tags = @datagram_builders = { true: nil, false: nil, } @max_values = max_values # Mutex protects the aggregation_state and flush_thread from concurrent access @mutex = Mutex.new @aggregation_state = {} @pid = Process.pid @flush_interval = flush_interval @flush_thread = Thread.new do Thread.current.abort_on_exception = true loop do sleep(@flush_interval) thread_healthcheck flush rescue => e StatsD.logger.error { "[#{self.class.name}] Error in flush thread: #{e}" } raise e end end ObjectSpace.define_finalizer( self, self.class.finalize(@aggregation_state, @sink, @datagram_builders, @datagram_builder_class, @default_tags), ) end |
Class Method Details
.finalize(aggregation_state, sink, datagram_builders, datagram_builder_class, default_tags) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/statsd/instrument/aggregator.rb', line 38 def finalize(aggregation_state, sink, datagram_builders, datagram_builder_class, ) proc do aggregation_state.each do |key, agg_value| no_prefix = key.no_prefix datagram_builders[no_prefix] ||= datagram_builder_class.new( prefix: no_prefix ? nil : @metric_prefix, default_tags: , ) case key.type when COUNT sink << datagram_builders[no_prefix].c( key.name, agg_value, CONST_SAMPLE_RATE, key., ) when DISTRIBUTION, MEASURE, HISTOGRAM sink << datagram_builders[no_prefix].timing_value_packed( key.name, key.type.to_s, agg_value, CONST_SAMPLE_RATE, key., ) when GAUGE sink << datagram_builders[no_prefix].g( key.name, agg_value, CONST_SAMPLE_RATE, key., ) else StatsD.logger.error { "[#{self.class.name}] Unknown aggregation type: #{key.type}" } end end aggregation_state.clear end end |
Instance Method Details
#aggregate_timing(name, value, tags: [], no_prefix: false, type: DISTRIBUTION) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/statsd/instrument/aggregator.rb', line 147 def aggregate_timing(name, value, tags: [], no_prefix: false, type: DISTRIBUTION) unless thread_healthcheck sink << datagram_builder(no_prefix: no_prefix).timing_value_packed( name, type, [value], CONST_SAMPLE_RATE, ) return end = () key = packet_key(name, , no_prefix, type) @mutex.synchronize do values = @aggregation_state[key] ||= [] if values.size + 1 >= @max_values do_flush end values << value end end |
#flush ⇒ Object
181 182 183 |
# File 'lib/statsd/instrument/aggregator.rb', line 181 def flush @mutex.synchronize { do_flush } end |
#gauge(name, value, tags: [], no_prefix: false) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/statsd/instrument/aggregator.rb', line 167 def gauge(name, value, tags: [], no_prefix: false) unless thread_healthcheck sink << datagram_builder(no_prefix: no_prefix).g(name, value, CONST_SAMPLE_RATE, ) return end = () key = packet_key(name, , no_prefix, GAUGE) @mutex.synchronize do @aggregation_state[key] = value end end |
#increment(name, value = 1, tags: [], no_prefix: false) ⇒ void
This method returns an undefined value.
Increment a counter by a given value and save it for later flushing.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/statsd/instrument/aggregator.rb', line 132 def increment(name, value = 1, tags: [], no_prefix: false) unless thread_healthcheck sink << datagram_builder(no_prefix: no_prefix).c(name, value, CONST_SAMPLE_RATE, ) return end = () key = packet_key(name, , no_prefix, COUNT) @mutex.synchronize do @aggregation_state[key] ||= 0 @aggregation_state[key] += value end end |