Class: Atatus::Collector::Base
- Inherits:
-
Object
- Object
- Atatus::Collector::Base
- Includes:
- Logging
- Defined in:
- lib/atatus/collector/base.rb
Constant Summary
Constants included from Logging
Logging::LEVELS, Logging::PREFIX
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #add_analytics(txn) ⇒ Object
- #add_error(error) ⇒ Object
- #add_metrics(metricset) ⇒ Object
- #add_span(span) ⇒ Object
- #add_txn(txn) ⇒ Object
- #handle_forking! ⇒ Object
-
#initialize(config) ⇒ Base
constructor
A new instance of Base.
- #initialize_server_urls ⇒ Object
- #pid_str ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Methods included from Logging
#debug, #error, #fatal, #info, #verbose, #warn
Constructor Details
#initialize(config) ⇒ Base
Returns a new instance of Base.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/atatus/collector/base.rb', line 28 def initialize(config) # info 'Initializing Collector' @config = config initialize_server_urls @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 = [] @error_limit = 20 @metrics_lock = Mutex.new @metrics_agg = [] @analytics = @config.analytics @analytics_lock = Mutex.new @analytics_agg = [] @hostinfo_response = {} @hostinfo_response["analytics"] = true @transport = Atatus::BaseTransport.new(@config) @dt_transport = Atatus::Transport::Base.new(@config) @collect_counter = 0 @running = false @transaction_ignore_urls = @config.transaction_ignore_urls.dup end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
90 91 92 |
# File 'lib/atatus/collector/base.rb', line 90 def config @config end |
Instance Method Details
#add_analytics(txn) ⇒ Object
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 |
# File 'lib/atatus/collector/base.rb', line 199 def add_analytics(txn) analytics_txn = {} analytics_txn[:timestamp] = Util.ms(txn.).to_i analytics_txn[:txnId] = "" analytics_txn[:txnId] = txn.id if !txn.id.nil? analytics_txn[:traceId] = "" analytics_txn[:traceId] = txn.trace_id if !txn.trace_id.nil? analytics_txn[:name] = txn.name analytics_txn[:duration] = Util.ms(txn.duration) if !txn.context.nil? if !txn.context.request.nil? r = txn.context.request analytics_txn[:method] = r.method if !r.method.nil? if !r.headers.nil? analytics_txn[:requestHeaders] = r.headers analytics_txn[:userAgent] = r.headers['User-Agent'] if r.headers.key?('User-Agent') end if !r.body.nil? if !r.body.empty? if r.body != "[SKIPPED]" analytics_txn[:requestBody] = r.body.to_json end end end if !r.url.nil? analytics_txn[:url] = r.url.full end if !r.socket.nil? analytics_txn[:ip] = r.socket.remote_addr end end if defined?(txn.context.response_body) && !txn.context.response_body.nil? then analytics_txn[:responseBody] = txn.context.response_body end if !txn.context.response.nil? if !txn.context.response.headers.nil? analytics_txn[:responseHeaders] = txn.context.response.headers end if !txn.context.response.status_code.nil? analytics_txn[:statusCode] = txn.context.response.status_code.to_i end end if defined?(txn.context.custom) && !txn.context.custom.nil? && !txn.context.custom.empty? then analytics_txn[:customData] = txn.context.custom end if defined?(txn.context.user) && !txn.context.user.nil? && !txn.context.user.empty? then analytics_txn[:userId] = txn.context.user.id analytics_txn[:userEmail] = txn.context.user.email analytics_txn[:userName] = txn.context.user.username end if defined?(txn.context.company) && !txn.context.company.nil? && !txn.context.company.empty? then analytics_txn[:companyId] = txn.context.company.id end end @analytics_lock.synchronize do if @analytics_agg.length < 10000 @analytics_agg << analytics_txn end end end |
#add_error(error) ⇒ Object
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 156 157 158 159 |
# File 'lib/atatus/collector/base.rb', line 123 def add_error(error) ensure_worker_running ignore_error = false if !error.exception.nil? && !error.exception.type.nil? && !error.exception..nil? then if @hostinfo_response.key?("ignoreExceptionPatterns") @hostinfo_response['ignoreExceptionPatterns'].each do |k, v| if k.match(error.exception.type) exception_values = v if exception_values.length == 0 ignore_error = true else exception_values.each do |value| if error.exception..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 < @error_limit @errors_aggs.push(error) else i = rand(@error_limit) @errors_aggs[i] = error end end end end |
#add_metrics(metricset) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 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 |
# File 'lib/atatus/collector/base.rb', line 161 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
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/atatus/collector/base.rb', line 288 def add_span(span) if @hostinfo_response.key?("tracing") if @hostinfo_response["tracing"] == true && @config.tracing == true if @hostinfo_response["performance"] && @config.performance @dt_transport.submit(span) end end end 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
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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
# File 'lib/atatus/collector/base.rb', line 313 def add_txn(txn) if @hostinfo_response.key?("tracing") if @hostinfo_response["tracing"] == true && @config.tracing == true if @hostinfo_response["performance"] && @config.performance @dt_transport.submit(txn) end end end ensure_worker_running if txn.name.nil? || txn.id.nil? || txn..nil? || txn.duration.nil? then return end ignore_txn = false if @hostinfo_response.key?("ignoreTxnNamePatterns") @hostinfo_response['ignoreTxnNamePatterns'].each do |k| if k.match(txn.name) 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..nil? || span.duration.nil? then next end next if span.name.empty? if span. >= txn. start = Util.ms(span. - txn.) 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 k.match(txn.name) 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 if @hostinfo_response.key?("analytics") if @hostinfo_response["analytics"] == true && @analytics == true if background == false add_analytics txn end end end end end |
#handle_forking! ⇒ Object
117 118 119 120 121 |
# File 'lib/atatus/collector/base.rb', line 117 def handle_forking! stop start @dt_transport.handle_forking! end |
#initialize_server_urls ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/atatus/collector/base.rb', line 63 def initialize_server_urls url = nil if @config.notify_host != "https://apm-rx.atatus.com" url = @config.notify_host end if @config.server_url != '' url = @config.server_url end if url if @config.apm_server_url == "https://apm-rx.atatus.com" @config.apm_server_url = url end if @config.analytics_server_url == "https://an-rx.atatus.com" @config.analytics_server_url = url end if @config.traces_server_url == "https://dt-rx.atatus.com" @config.traces_server_url = url end if @config.logs_server_url == "https://log-rx.atatus.com" @config.logs_server_url = url end end end |
#pid_str ⇒ Object
92 93 94 |
# File 'lib/atatus/collector/base.rb', line 92 def pid_str format('[PID:%s]', Process.pid) end |
#start ⇒ Object
96 97 98 99 100 101 |
# File 'lib/atatus/collector/base.rb', line 96 def start @dt_transport.start debug '%s: Starting collector', pid_str ensure_worker_running end |
#stop ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/atatus/collector/base.rb', line 103 def stop return unless @running @dt_transport.stop @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.) error "Backtrace:\n" + e.backtrace.join("\n") end |