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.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/stackify_apm/agent.rb', line 78

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.



90
91
92
# File 'lib/stackify_apm/agent.rb', line 90

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.



90
91
92
# File 'lib/stackify_apm/agent.rb', line 90

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.



90
91
92
# File 'lib/stackify_apm/agent.rb', line 90

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.



90
91
92
# File 'lib/stackify_apm/agent.rb', line 90

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.



90
91
92
# File 'lib/stackify_apm/agent.rb', line 90

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.



90
91
92
# File 'lib/stackify_apm/agent.rb', line 90

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.



90
91
92
# File 'lib/stackify_apm/agent.rb', line 90

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.



90
91
92
# File 'lib/stackify_apm/agent.rb', line 90

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.



90
91
92
# File 'lib/stackify_apm/agent.rb', line 90

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)


74
75
76
# File 'lib/stackify_apm/agent.rb', line 74

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.



191
192
193
194
195
196
197
# File 'lib/stackify_apm/agent.rb', line 191

def self.squish(str)
  str = str.sub(/\A[[:space:]]+/, '')
  str = str.sub(/[[:space:]]+\z/, '')
  str = str.sub(/[[:space:]]+/, ' ')
  str = str.strip.sub(/\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/PerceivedComplexity rubocop:disable Metrics/CyclomaticComplexity



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
# File 'lib/stackify_apm/agent.rb', line 30

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

  config = Config.new(config) unless config.is_a?(Config)
  case config.transport.downcase
  # For unix socket we don't create a trace log file
  when StackifyRubyAPM::TRACE_LOG
    # For Default transport let's create initial trace log file
    pid = $PID || Process.pid
    host_name = (config.hostname || `hostname`).strip
    date_now = Time.now
    current_trace_file = config.log_trace_path + squish(host_name) + '#' + squish(pid.to_s)
    if ENV['STACKIFY_RUBY_ENV'] != 'rspec'
      config.tracer_logger = StackifyLogger.new(current_trace_file, config.filenum_rotate, config.logger_byte_size)
      config.debug_logger
      config.logtime_created = date_now.strftime('%H:%M')
      Util::TraceLogWatcher.delete_trace_logs(config)
    end
  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 rubocop:enable Metrics/PerceivedComplexity



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

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



163
164
165
# File 'lib/stackify_apm/agent.rb', line 163

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



147
148
149
# File 'lib/stackify_apm/agent.rb', line 147

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.



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

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



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/stackify_apm/agent.rb', line 120

def enqueue_transaction(transaction)
  if !@config.queue
    return @trace_logger.post([transaction])
  end

  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



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

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.



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

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)


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

def should_flush_transactions?
  return true unless config.flush_interval_seconds
  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.



157
158
159
# File 'lib/stackify_apm/agent.rb', line 157

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.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/stackify_apm/agent.rb', line 100

def start
  info '[Agent] start()'
  info '[Agent] transport type: ' + @config.transport
  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 if ENV['STACKIFY_TRANSPORT_LOG_LEVEL'] == '0'
  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.



68
69
70
71
72
# File 'lib/stackify_apm/agent.rb', line 68

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



153
154
155
# File 'lib/stackify_apm/agent.rb', line 153

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