Class: StackifyRubyAPM::Agent Private

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/stackify_apm/agent.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

LOCK =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Mutex.new

Constants included from Log

Log::PREFIX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

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

Constructor Details

#initialize(config) ⇒ Agent

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Agent.



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/stackify_apm/agent.rb', line 71

def initialize(config)
  @config = config
  @trace_logger = TraceLogger.new(config)
  @messages = Queue.new
  @pending_transactions = Queue.new
  @instrumenter = Instrumenter.new(self)
  @context_builder = ContextBuilder.new(self)
  @error_builder = ErrorBuilder.new(self)
  @stacktrace_builder = StacktraceBuilder.new(self)
  @error_serializer = Serializers::Errors.new(config)
end

Instance Attribute Details

#configObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
# File 'lib/stackify_apm/agent.rb', line 83

def config
  @config
end

#context_builderObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
# File 'lib/stackify_apm/agent.rb', line 83

def context_builder
  @context_builder
end

#error_builderObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
# File 'lib/stackify_apm/agent.rb', line 83

def error_builder
  @error_builder
end

#error_serializerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
# File 'lib/stackify_apm/agent.rb', line 83

def error_serializer
  @error_serializer
end

#instrumenterObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
# File 'lib/stackify_apm/agent.rb', line 83

def instrumenter
  @instrumenter
end

#messagesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
# File 'lib/stackify_apm/agent.rb', line 83

def messages
  @messages
end

#pending_transactionsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
# File 'lib/stackify_apm/agent.rb', line 83

def pending_transactions
  @pending_transactions
end

#stacktrace_builderObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
# File 'lib/stackify_apm/agent.rb', line 83

def stacktrace_builder
  @stacktrace_builder
end

#trace_loggerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
# File 'lib/stackify_apm/agent.rb', line 83

def trace_logger
  @trace_logger
end

Class Method Details

.instanceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Style/TrivialAccessors



24
25
26
# File 'lib/stackify_apm/agent.rb', line 24

def self.instance # rubocop:disable Style/TrivialAccessors
  @instance
end

.running?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


67
68
69
# File 'lib/stackify_apm/agent.rb', line 67

def self.running?
  !@instance.nil?
end

.squish(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method will strip and remove garbage character and multiple spaces.



179
180
181
182
183
184
185
# File 'lib/stackify_apm/agent.rb', line 179

def self.squish(str)
  str = str.gsub(/\A[[:space:]]+/, '')
  str = str.gsub(/[[:space:]]+\z/, '')
  str = str.gsub(/[[:space:]]+/, ' ')
  str = str.strip.gsub(/\s+/, '')
  str
end

.start(config) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/CyclomaticComplexity



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

def self.start(config)
  return @instance if @instance

  config = Config.new(config) unless config.is_a?(Config)
  pid = $PID || Process.pid
  host_name = config.hostname || `hostname`
  date_now = Time.now
  current_trace_file = config.log_trace_path + squish(host_name) + '#' + squish(pid.to_s) + '.log'

  if ENV['STACKIFY_RUBY_ENV'] != 'rspec'
    config.tracer_logger = StackifyLogger.new(current_trace_file, config.filenum_rotate, config.logger_byte_size)
    config.logtime_created = date_now.strftime('%H:%M')
    Util::TraceLogWatcher.delete_trace_logs(config)
  end

  LOCK.synchronize do
    return @instance if @instance

    @instance = new(config).start
  end
end

.stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:enable Metrics/CyclomaticComplexity



52
53
54
55
56
57
58
59
# File 'lib/stackify_apm/agent.rb', line 52

def self.stop
  LOCK.synchronize do
    return unless @instance

    @instance.stop
    @instance = nil
  end
end

Instance Method Details

#build_context(rack_env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Responsible for building the transaction’s context



151
152
153
# File 'lib/stackify_apm/agent.rb', line 151

def build_context(rack_env)
  @context_builder.build(rack_env)
end

#current_transactionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instrumentation



135
136
137
# File 'lib/stackify_apm/agent.rb', line 135

def current_transaction
  instrumenter.current_transaction
end

#enqueue_error(error) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



127
128
129
130
131
# File 'lib/stackify_apm/agent.rb', line 127

def enqueue_error(error)
  boot_worker unless worker_running?

  messages.push(Worker::ErrorMsg.new(error))
end

#enqueue_transaction(transaction) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

queues Stores transaction in queue



112
113
114
115
116
117
118
# File 'lib/stackify_apm/agent.rb', line 112

def enqueue_transaction(transaction)
  boot_worker unless worker_running?
  pending_transactions.push(transaction)
  return unless should_flush_transactions?

  messages.push(Worker::FlushMsg.new)
end

#report(exception, handled: true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

errors



157
158
159
160
161
162
163
164
165
166
# File 'lib/stackify_apm/agent.rb', line 157

def report(exception, handled: true)
  return if config.filter_exception_types.include?(exception.class.to_s)

  error = @error_builder.build_exception(
    exception,
    handled: handled
  )
  current_error = @error_serializer.build(error)
  current_transaction.add_exception(current_error) if current_transaction
end

#report_message(message, backtrace: nil, **attrs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



168
169
170
171
172
173
174
175
# File 'lib/stackify_apm/agent.rb', line 168

def report_message(message, backtrace: nil, **attrs)
  error = @error_builder.build_log(
    message,
    backtrace: backtrace,
    **attrs
  )
  enqueue_error error
end

#should_flush_transactions?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


120
121
122
123
124
125
# File 'lib/stackify_apm/agent.rb', line 120

def should_flush_transactions?
  return true unless config.flush_interval
  return true if pending_transactions.length >= config.max_queue_size

  false
end

#span(*args, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



145
146
147
# File 'lib/stackify_apm/agent.rb', line 145

def span(*args, &block)
  instrumenter.span(*args, &block)
end

#startObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/stackify_apm/agent.rb', line 93

def start
  info '[Agent] start()'
  spies_name = ''
  # If the rake task is detected as being ran then we don't load the spies
  StackifyRubyAPM::Util.apm_disabled_in_rake
  return false unless @config.instrument
  config.enabled_spies.each do |lib|
    spies_name = spies_name + ', ' + lib.inspect.to_s

    require "stackify_apm/spies/#{lib}"
  end

  debug '[Agent] Loaded spies:' + spies_name
  self
end

#stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



61
62
63
64
65
# File 'lib/stackify_apm/agent.rb', line 61

def stop
  @instrumenter.stop
  kill_worker
  self
end

#transaction(*args, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Loads transaction



141
142
143
# File 'lib/stackify_apm/agent.rb', line 141

def transaction(*args, &block)
  instrumenter.transaction(*args, &block)
end