Class: Atatus::Collector::Base

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/atatus/collector/base.rb

Constant Summary

Constants included from Logging

Logging::LEVELS, Logging::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #error, #fatal, #info, #warn

Constructor Details

#initialize(config) ⇒ Base

Returns a new instance of Base.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/atatus/collector/base.rb', line 27

def initialize(config)
  # info 'Initializing Collector'
  @config = config
  @spans = Hash.new {|h,k| h[k]=[]}

  @txns_lock = Mutex.new
  @txns_agg = {}
  @txn_hist_agg = {}
  @traces_agg = []
  @error_metrics_agg = {}
  @error_requests_agg = []

  @errors_lock = Mutex.new
  @errors_aggs = []

  @metrics_lock = Mutex.new
  @metrics_agg = []

  @hostinfo_response = {}
  @transport = Atatus::BaseTransport.new(config)
  @collect_counter = 0
  @running = false
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



51
52
53
# File 'lib/atatus/collector/base.rb', line 51

def config
  @config
end

Instance Method Details

#add_error(error) ⇒ Object



81
82
83
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
# File 'lib/atatus/collector/base.rb', line 81

def add_error(error)
  ensure_worker_running
  ignore_error = false
  if
    !error.exception.nil?  &&
    !error.exception.type.nil? &&
    !error.exception.message.nil?
  then
    if @hostinfo_response.key?("ignoreExceptionPatterns")
      @hostinfo_response['ignoreExceptionPatterns'].each do |k, v|
        if error.exception.type.match(k)
          exception_values = v
          if exception_values.length == 0
            ignore_error = true
          else
            exception_values.each do |value|
              if error.exception.message.include?(value)
                ignore_error = true
              end
            end
          end
          break
        end
      end
    end
  end
  if ignore_error == false
    @errors_lock.synchronize do
      if @errors_aggs.length < 20
        @errors_aggs.push(error)
      else
        i = rand(20)
        @errors_aggs[i] = error
      end
    end
  end
end

#add_metrics(metricset) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/atatus/collector/base.rb', line 119

def add_metrics(metricset)
  ensure_worker_running

  metric_added = false
  metric = {}

  if %i[system.cpu.total.norm.pct system.memory.actual.free system.memory.total system.process.cpu.total.norm.pct system.process.memory.size system.process.memory.rss.bytes].all? {|s| metricset.samples.key? s}
  then
    metric[:'system.cpu.total.norm.pct'] = metricset.samples[:'system.cpu.total.norm.pct']
    metric[:'system.memory.actual.free'] = metricset.samples[:'system.memory.actual.free']
    metric[:'system.memory.total'] = metricset.samples[:'system.memory.total']
    metric[:'system.process.cpu.total.norm.pct'] = metricset.samples[:'system.process.cpu.total.norm.pct']
    metric[:'system.process.memory.size'] = metricset.samples[:'system.process.memory.size']
    metric[:'system.process.memory.rss.bytes'] = metricset.samples[:'system.process.memory.rss.bytes']
    metric_added = true
  end

  if %i[ruby.gc.count ruby.threads ruby.heap.slots.live ruby.heap.slots.free ruby.heap.allocations.total].all? {|s| metricset.samples.key? s}
  then
    metric[:'ruby.gc.count'] = metricset.samples[:'ruby.gc.count']
    metric[:'ruby.threads'] = metricset.samples[:'ruby.threads']
    metric[:'ruby.heap.slots.live'] = metricset.samples[:'ruby.heap.slots.live']
    metric[:'ruby.heap.slots.free'] = metricset.samples[:'ruby.heap.slots.free']
    metric[:'ruby.heap.allocations.total'] = metricset.samples[:'ruby.heap.allocations.total']
    if %i[ruby.gc.time].all? {|s| metricset.samples.key? s}
    then
      metric[:'ruby.gc.time'] = metricset.samples[:'ruby.gc.time']
    end
    metric_added = true
  end

  if metric_added
    @metrics_lock.synchronize do
      @metrics_agg << metric
    end
  end
end

#add_span(span) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/atatus/collector/base.rb', line 157

def add_span(span)
  ensure_worker_running

  if
    span.transaction_id.nil? ||
    span.name.nil? ||
    span.type.nil? ||
    span.subtype.nil? ||
    span.duration.nil?
  then
    return
  end

  @spans[span.transaction_id] << span if span.transaction_id
end

#add_txn(txn) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/atatus/collector/base.rb', line 173

def add_txn(txn)
  ensure_worker_running

  if
    txn.name.nil? ||
    txn.id.nil? ||
    txn.timestamp.nil? ||
    txn.duration.nil?
  then
    return
  end

  ignore_txn = false
  if @hostinfo_response.key?("ignoreTxnNamePatterns")
    @hostinfo_response['ignoreTxnNamePatterns'].each do |k|
      if txn.name.match(k)
        ignore_txn = true
        break
      end
    end
  end
  if ignore_txn == true
    return
  end

  return if txn.name.empty?
  return if txn.duration <= 0

  @txns_lock.synchronize do
    txn_type = @config.framework_name || "Ruby"
    background = false
    if !txn.type.nil?
      if txn.type == "Sidekiq"
        background = true
      end
    end

    if !@txns_agg.key?(txn.name)
      @txns_agg[txn.name] = Txn.new(txn_type, "Ruby", txn.duration, background: background)
      @txns_agg[txn.name].id = txn.id
      @txns_agg[txn.name].pid = txn.id
    else
      @txns_agg[txn.name].aggregate! txn.duration
    end

    if background == false and txn.duration <= 150*1000*1000.0
      if !@txn_hist_agg.key?(txn.name)
        @txn_hist_agg[txn.name] = TxnHist.new(txn_type, "Ruby", Util.ms(txn.duration))
      else
        @txn_hist_agg[txn.name].aggregate! Util.ms(txn.duration)
      end
    end

    spans_present = false
    ruby_time = 0
    spans_tuple = []
    if @spans.key?(txn.id)
      spans_present = true
      @spans[txn.id].each do |span|
        if
          span.name.nil? ||
          span.type.nil? ||
          span.subtype.nil? ||
          span.timestamp.nil? ||
          span.duration.nil?
        then
          next
        end

        next if span.name.empty?

        if span.timestamp >= txn.timestamp
          start = Util.ms(span.timestamp - txn.timestamp)
          spans_tuple.push(SpanTiming.new(span.name, span.type, span.subtype, start, start + Util.ms(span.duration), span.duration, span.id, span.transaction_id))
        end
      end
    end

    if spans_tuple.length == 0
      ruby_time = Util.ms(txn.duration)
    else
      spans_tuple.sort! {| a, b | a[:start] <=> b[:start] }
      j = 0
      while j < spans_tuple.length
        if spans_tuple[j].subtype == 'controller' || spans_tuple[j].subtype == 'view' || spans_tuple[j].subtype == 'tilt'
          k = j+1
          while k < spans_tuple.length
            if spans_tuple[k].start >= spans_tuple[j].end
              break
            else
              if spans_tuple[k].end <= spans_tuple[j].end
                spans_tuple[j].duration -= Util.us(spans_tuple[k].end - spans_tuple[k].start)
              else
                spans_tuple[j].duration -= Util.us(spans_tuple[j].end - spans_tuple[k].start)
              end
            end
            k += 1
          end
          if spans_tuple[j].duration <= 0
            spans_tuple[j].duration = 1
          end
        end

        if !@txns_agg[txn.name].spans.key?(spans_tuple[j].name)
          kind = Layer.span_kind(spans_tuple[j].type)
          type = Layer.span_type(spans_tuple[j].subtype)
          @txns_agg[txn.name].spans[spans_tuple[j].name] = Layer.new(type, kind, spans_tuple[j].duration)
          @txns_agg[txn.name].spans[spans_tuple[j].name].id = spans_tuple[j].id
          @txns_agg[txn.name].spans[spans_tuple[j].name].pid = spans_tuple[j].transaction_id
        else
          @txns_agg[txn.name].spans[spans_tuple[j].name].aggregate! spans_tuple[j].duration
        end

        j += 1
      end

      ruby_time = spans_tuple[0].start
      span_end = spans_tuple[0].end
      j = 0
      while j < spans_tuple.length
        if spans_tuple[j].start > span_end
          ruby_time += spans_tuple[j].start - span_end
          span_end = spans_tuple[j].end
        else
          if spans_tuple[j].end > span_end
            span_end = spans_tuple[j].end
          end
        end
        j += 1
      end
      if Util.ms(txn.duration) > span_end
        ruby_time += Util.ms(txn.duration) - span_end
      end
    end

    if ruby_time > 0
      ruby_time = Util.us(ruby_time)
      @txns_agg[txn.name].spans["Ruby"] = Layer.new("Ruby", "Ruby", ruby_time)
    end

    if spans_present == true || ruby_time > 0
      if Util.ms(txn.duration) >= @config.trace_threshold
        trace_txn = txn
        trace_txn.spans = @spans[txn.id] if spans_present
        trace_txn.ruby_time = ruby_time if ruby_time > 0

        if @traces_agg.length < 5
          @traces_agg.push(trace_txn)
        else
          i = rand(5)
          @traces_agg[i] = trace_txn
        end
      end
    end

    if spans_present
      @spans.delete(txn.id)
    end

    if
      !txn.context.nil? &&
      !txn.context.response.nil? &&
      !txn.context.response.status_code.nil?
    then
      status_code = txn.context.response.status_code.to_i
      ignore_status_code = false
      if status_code >= 400 && status_code != 404
        if @hostinfo_response.key?("ignoreHTTPFailurePatterns")
          @hostinfo_response['ignoreHTTPFailurePatterns'].each do |k, v|
            if txn.name.match(k)
              status_code_array_s = v
              if status_code_array_s.length == 0
                ignore_status_code = true
              else
                status_code_array_s.each do |code|
                  if code == txn.context.response.status_code.to_s
                    ignore_status_code = true
                  end
                end
              end
              break
            end
          end
        end
        if ignore_status_code == false
          if !@error_metrics_agg.key?(txn.name)
            @error_metrics_agg[txn.name] = {status_code => 1}
          else
            if !@error_metrics_agg[txn.name].key?(status_code)
              @error_metrics_agg[txn.name][status_code] = 1
            else
              @error_metrics_agg[txn.name][status_code] += 1
            end
          end

          trace_id = ""
          trace_id = txn.trace_id if !txn.trace_id.nil?
          if @error_requests_agg.length < 20
            @error_requests_agg.push({'name' => txn.name, 'txn_id' => txn.id, 'trace_id' => trace_id, 'context' => txn.context})
          else
            i = rand(20)
            @error_requests_agg[i] = {'name' => txn.name, 'txn_id' => txn.id, 'trace_id' => trace_id, 'context' => txn.context}
          end
        end
      end
    end

  end
end

#handle_forking!Object



76
77
78
79
# File 'lib/atatus/collector/base.rb', line 76

def handle_forking!
  stop
  start
end

#pid_strObject



53
54
55
# File 'lib/atatus/collector/base.rb', line 53

def pid_str
  format('[PID:%s]', Process.pid)
end

#startObject



57
58
59
60
61
# File 'lib/atatus/collector/base.rb', line 57

def start
  debug '%s: Starting collector', pid_str

  ensure_worker_running
end

#stopObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/atatus/collector/base.rb', line 63

def stop
  return unless @running
  @running = false
  if worker_active?
    debug '%s: Waiting for collector worker to exit', pid_str
    @worker.run
    @worker.join(10)
  end
rescue => e
  error format('Failed during collector stop: [%s] %s', e.class, e.message)
  error "Backtrace:\n" + e.backtrace.join("\n")
end