Module: TraceViewBase

Extended by:
TraceView::ThreadLocal
Included in:
Oboe, TraceView
Defined in:
lib/traceview/base.rb

Overview

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)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TraceView::ThreadLocal

thread_local

Instance Attribute Details

#loadedObject

Returns the value of attribute loaded.



40
41
42
# File 'lib/traceview/base.rb', line 40

def loaded
  @loaded
end

#reporterObject

Returns the value of attribute reporter.



39
40
41
# File 'lib/traceview/base.rb', line 39

def reporter
  @reporter
end

Class Method Details

.extended(cls) ⇒ Object

extended

Invoked when this module is extended. e.g. extend TraceViewBase



80
81
82
83
84
85
86
87
88
89
90
91
# 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

Instance Method Details

#always?Boolean

Returns true if the tracing_mode is set to always. False otherwise

Returns:

  • (Boolean)


162
163
164
# File 'lib/traceview/base.rb', line 162

def always?
  TraceView::Config[:tracing_mode].to_sym == :always
end

#entry_layer?(layer) ⇒ Boolean

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.

Returns:

  • (Boolean)


154
155
156
# 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

#forking_webserver?Boolean

Determines if we are running under a forking webserver

Returns:

  • (Boolean)


211
212
213
214
215
216
217
218
# 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

#framework?Boolean

Indicates whether a supported framework is in use or not

Returns:

  • (Boolean)


252
253
254
# File 'lib/traceview/base.rb', line 252

def framework?
  defined?(::Rails) || defined?(::Sinatra) || defined?(::Padrino) || defined?(::Grape)
end

#heroku?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/traceview/base.rb', line 204

def heroku?
  ENV.key?('TRACEVIEW_URL')
end

#log(_layer, _label, _options = {}) ⇒ Object



199
200
201
202
# 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

#never?Boolean

Returns true if the tracing_mode is set to never. False otherwise

Returns:

  • (Boolean)


170
171
172
# File 'lib/traceview/base.rb', line 170

def never?
  TraceView::Config[:tracing_mode].to_sym == :never
end

#passthrough?Boolean

Returns true if the tracing_mode is set to always or through. False otherwise

Returns:

  • (Boolean)


178
179
180
# File 'lib/traceview/base.rb', line 178

def passthrough?
  [:always, :through].include?(TraceView::Config[:tracing_mode])
end

#pickup_context?(xtrace) ⇒ Boolean

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.

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
# 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

#pry!Object

Debugging helper method



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# 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

#sample?(_opts = {}) ⇒ Boolean

These methods should be implemented by the descendants (Oboe_metal, JOboe_metal (JRuby), Heroku_metal)

Returns:

  • (Boolean)


260
261
262
# File 'lib/traceview/base.rb', line 260

def sample?(_opts = {})
  fail 'sample? should be implemented by metal layer.'
end

#set_sample_rate(_rate) ⇒ Object



272
273
274
# File 'lib/traceview/base.rb', line 272

def set_sample_rate(_rate)
  fail 'set_sample_rate should be implemented by metal layer.'
end

#set_tracing_mode(_mode) ⇒ Object



268
269
270
# File 'lib/traceview/base.rb', line 268

def set_tracing_mode(_mode)
  fail 'set_tracing_mode should be implemented by metal layer.'
end

#through?Boolean

Returns true if the tracing_mode is set to through. False otherwise

Returns:

  • (Boolean)


186
187
188
# File 'lib/traceview/base.rb', line 186

def through?
  TraceView::Config[:tracing_mode].to_sym == :through
end

#tracing?Boolean

Returns true if we are currently tracing a request False otherwise

Returns:

  • (Boolean)


194
195
196
197
# File 'lib/traceview/base.rb', line 194

def tracing?
  return false if !TraceView.loaded || TraceView.never?
  TraceView::Context.isValid
end

#tracing_layer?(layer) ⇒ Boolean

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.

Returns:

  • (Boolean)


120
121
122
# File 'lib/traceview/base.rb', line 120

def tracing_layer?(layer)
  TraceView.layer == layer.to_sym
end

#tracing_layer_op?(operation) ⇒ Boolean

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.

Returns:

  • (Boolean)


136
137
138
139
140
141
142
# 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