module TraceViewBase
This module is the base module for the various implementations of TraceView
reporting. Current variations as of 2014-09-10 are a c-extension, JRuby (using TraceView
Java instrumentation) and a Heroku c-extension (with embedded tracelyzer)
Attributes
Public Class Methods
extended
Invoked when this module is extended. e.g. extend TraceViewBase
# File lib/traceview/base.rb, line 80 def self.extended(cls) cls.loaded = true # This gives us pretty accessors with questions marks at the end # e.g. is_continued_trace --> is_continued_trace? TraceView.methods.select { |m| m =~ /^is_|^has_/ }.each do |c| unless c =~ /\?$|=$/ # TraceView.logger.debug "aliasing #{c}? to #{c}" alias_method "#{c}?", c end end end
Public Instance Methods
Returns true if the tracing_mode is set to always. False otherwise
# File lib/traceview/base.rb, line 162 def always? TraceView::Config[:tracing_mode].to_sym == :always end
entry_layer?
Determines if the passed layer is an entry only layer where we would want to use smart tracing.
Entry only layers are layers that only start traces and doesn't directly receive incoming context such as DelayedJob or Sidekiq workers.
# File lib/traceview/base.rb, line 154 def entry_layer?(layer) %w(delayed_job-worker sidekiq-worker resque-worker rabbitmq-consumer).include?(layer.to_s) end
Determines if we are running under a forking webserver
# File lib/traceview/base.rb, line 211 def forking_webserver? if (defined?(::Unicorn) && ($PROGRAM_NAME =~ /unicorn/i)) || (defined?(::Puma) && ($PROGRAM_NAME =~ /puma/i)) true else false end end
Indicates whether a supported framework is in use or not
# File lib/traceview/base.rb, line 252 def framework? defined?(::Rails) || defined?(::Sinatra) || defined?(::Padrino) || defined?(::Grape) end
# File lib/traceview/base.rb, line 204 def heroku? ENV.key?('TRACEVIEW_URL') end
# File lib/traceview/base.rb, line 199 def log(layer, label, options = {}) # WARN: TraceView.log will be deprecated in a future release. Please use TraceView::API.log instead. TraceView::API.log(layer, label, options) end
Returns true if the tracing_mode is set to never. False otherwise
# File lib/traceview/base.rb, line 170 def never? TraceView::Config[:tracing_mode].to_sym == :never end
Returns true if the tracing_mode is set to always or through. False otherwise
# File lib/traceview/base.rb, line 178 def passthrough? [:always, :through].include?(TraceView::Config[:tracing_mode]) end
pickup_context
Determines whether we should pickup context from an incoming X-Trace request header. The answer is generally yes but there are cases in JRuby under Tomcat (or Glassfish etc.) where tracing may have been already started by the Java instrumentation (Joboe) in which case we don't want to do this.
# File lib/traceview/base.rb, line 103 def pickup_context?(xtrace) return false unless TraceView::XTrace.valid?(xtrace) if defined?(JRUBY_VERSION) && TraceView.tracing? return false else return true end end
Debugging helper method
# File lib/traceview/base.rb, line 223 def pry! # Only valid for development or test environments env = ENV['RACK_ENV'] || ENV['RAILS_ENV'] return unless %w(development, test).include? env if RUBY_VERSION > '1.8.7' require 'pry-byebug' if defined?(PryByebug) Pry.commands.alias_command 'c', 'continue' Pry.commands.alias_command 's', 'step' Pry.commands.alias_command 'n', 'next' Pry.commands.alias_command 'f', 'finish' Pry::Commands.command(/^$/, 'repeat last command') do _pry_.run_command Pry.history.to_a.last end end binding.pry else require 'ruby-debug'; debugger end end
These methods should be implemented by the descendants (Oboe_metal
, JOboe_metal (JRuby), Heroku_metal)
# File lib/traceview/base.rb, line 260 def sample?(_opts = {}) fail 'sample? should be implemented by metal layer.' end
# File lib/traceview/base.rb, line 272 def set_sample_rate(_rate) fail 'set_sample_rate should be implemented by metal layer.' end
# File lib/traceview/base.rb, line 268 def set_tracing_mode(_mode) fail 'set_tracing_mode should be implemented by metal layer.' end
Returns true if the tracing_mode is set to through. False otherwise
# File lib/traceview/base.rb, line 186 def through? TraceView::Config[:tracing_mode].to_sym == :through end
Returns true if we are currently tracing a request False otherwise
# File lib/traceview/base.rb, line 194 def tracing? return false if !TraceView.loaded || TraceView.never? TraceView::Context.isValid end
tracing_layer?
Queries the thread local variable about the current layer being traced. This is used in cases of recursive operation tracing or one instrumented operation calling another.
# File lib/traceview/base.rb, line 120 def tracing_layer?(layer) TraceView.layer == layer.to_sym end
tracing_layer_op?
Queries the thread local variable about the current operation being traced. This is used in cases of recursive operation tracing or one instrumented operation calling another.
<operation> can be a single symbol or an array of symbols that will be checked against.
In such cases, we only want to trace the outermost operation.
# File lib/traceview/base.rb, line 136 def tracing_layer_op?(operation) if operation.is_a?(Array) return operation.include?(TraceView.layer_op) else return TraceView.layer_op == operation.to_sym end end