Class: TingYun::Agent::Collector::ErrorCollector

Inherits:
Object
  • Object
show all
Includes:
Metric, Tag
Defined in:
lib/ting_yun/agent/collector/error_collector.rb

Defined Under Namespace

Modules: Metric, Tag

Constant Summary collapse

ERRORS_ACTION =
"Errors/Count/".freeze
ERRORS_ALL =
"Errors/Count/All".freeze
ERRORS_ALL_WEB =
"Errors/Count/AllWeb".freeze
ERRORS_ALL_BACK_GROUND =
"Errors/Count/AllBackground".freeze
ERRORS_TYPE =
"Errors/Type:".freeze
EXCEPTIONS_ACTION =
"Exception/Count/".freeze
EXCEPTIONS_ALL =
"Exception/Count/All".freeze
EXCEPTIONS_ALL_WEB =
"Exception/Count/AllWeb".freeze
EXCEPTIONS_ALL_BACK_GROUND =
"Exception/Count/AllBackground".freeze
EXCEPTIONS_TYPE =
"Exception/Type:".freeze
MAX_ERROR_QUEUE_LENGTH =

Maximum possible length of the queue - defaults to 20, may be

20
EMPTY_STRING =
''.freeze

Constants included from Tag

Tag::EXCEPTION_TAG_IVAR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Metric

#action_exception_metric_names, #action_metric_names, #aggregated_exception_metric_names, #aggregated_exception_type_count, #aggregated_metric_names, #aggregated_type_count

Methods included from Tag

#exception_tagged?, #tag_exception

Constructor Details

#initializeErrorCollector

Returns a new instance of ErrorCollector.



109
110
111
112
113
# File 'lib/ting_yun/agent/collector/error_collector.rb', line 109

def initialize
  @error_trace_array = ::TingYun::Agent::Collector::ErrorTraceArray.new(MAX_ERROR_QUEUE_LENGTH)
  @external_error_array = ::TingYun::Agent::Collector::ErrorTraceArray.new(MAX_ERROR_QUEUE_LENGTH)
  @exception_error_array = ::TingYun::Agent::Collector::ErrorTraceArray.new(MAX_ERROR_QUEUE_LENGTH)
end

Instance Attribute Details

#error_trace_arrayObject (readonly)

Returns the value of attribute error_trace_array.



107
108
109
# File 'lib/ting_yun/agent/collector/error_collector.rb', line 107

def error_trace_array
  @error_trace_array
end

#exception_error_arrayObject (readonly)

Returns the value of attribute exception_error_array.



107
108
109
# File 'lib/ting_yun/agent/collector/error_collector.rb', line 107

def exception_error_array
  @exception_error_array
end

#external_error_arrayObject (readonly)

Returns the value of attribute external_error_array.



107
108
109
# File 'lib/ting_yun/agent/collector/error_collector.rb', line 107

def external_error_array
  @external_error_array
end

Instance Method Details

#create_noticed_error(exception, options) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/ting_yun/agent/collector/error_collector.rb', line 167

def create_noticed_error(exception, options)
  if options[:type] && options[:type]==:exception
    attributes = options[:attributes]
    error_metric = options[:attributes].agent_attributes[:metric_name] || EMPTY_STRING
    noticed_error = TingYun::Agent::Collector::NoticedError.new(error_metric, exception)
    noticed_error.attributes  = attributes
    noticed_error.stack_trace = extract_stack_trace(exception) if ::TingYun::Agent.config[:'nbs.exception.stack_enabled']
    noticed_error.type = options[:type]
    noticed_error
  else
    attributes = options[:attributes]
    error_metric = attributes.agent_attributes[:metric_name] || EMPTY_STRING
    noticed_error = TingYun::Agent::Collector::NoticedError.new(error_metric, exception)
    noticed_error.attributes  = attributes
    noticed_error.stack_trace = extract_stack_trace(exception)
    noticed_error.type = options[:type]
    noticed_error.code = attributes.agent_attributes[:httpStatus]
    noticed_error
  end

end

#extract_stack_trace(exception) ⇒ Object

extracts a stack trace from the exception for debugging purposes



202
203
204
205
# File 'lib/ting_yun/agent/collector/error_collector.rb', line 202

def extract_stack_trace(exception)
  actual_exception = sense_method(exception, 'original_exception') || exception
  sense_method(actual_exception, 'backtrace') || '<no stack trace>'
end

#increment_error_count(exception, state, type) ⇒ Object

Notice the error with the given available options:

  • :type => default nil; :exception,:error



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ting_yun/agent/collector/error_collector.rb', line 144

def increment_error_count(exception,state, type)
  txn = state.current_transaction
  if type && type==:exception
    exception_metric_names = aggregated_exception_metric_names(txn)
    exception_metric_names.concat aggregated_exception_type_count(exception,txn)
    exception_metric_names << action_exception_metric_names(txn)
    stats_engine = TingYun::Agent.agent.stats_engine
    stats_engine.record_unscoped_metrics(state, exception_metric_names) do |stats|
      stats.increment_count
    end
  else
    metric_names = aggregated_metric_names(txn)
    metric_names.concat aggregated_type_count(exception,txn)
    metric_names.concat action_metric_names(txn,exception)
    stats_engine = TingYun::Agent.agent.stats_engine
    stats_engine.record_unscoped_metrics(state, metric_names) do |stats|
      stats.increment_count
    end
  end
end

#notice_agent_error(exception) ⇒ Object

*Use sparingly for difficult to track bugs.*

Track internal agent errors for communication back to TingYun To use, make a specific subclass of TingYun::Support::Exception::InternalAgentError, then pass an instance of it to this method when your problem occurs.

Limits are treated differently for these errors. We only gather one per class per harvest, disregarding (and not impacting) the app error queue limit.



216
217
218
# File 'lib/ting_yun/agent/collector/error_collector.rb', line 216

def notice_agent_error(exception)
  error_trace_array.notice_agent_error(exception)
end

#notice_error(exception, options = {}) ⇒ Object

See TingYun::Agent.notice_error for options and commentary



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ting_yun/agent/collector/error_collector.rb', line 116

def notice_error(exception, options={})
  tag_exception(exception)
  state = ::TingYun::Agent::TransactionState.tl_get
  increment_error_count(exception,state,options[:type])
  _error = create_noticed_error(exception, options)
  noticed_error = _error.clone
  if noticed_error.is_external_error
    noticed_error.is_external_error = nil
    external_error_array.add_to_error_queue(_error)
  end
  if noticed_error.type && noticed_error.type == :exception
     exception_error_array.add_to_error_queue(noticed_error)
  else
    error_trace_array.add_to_error_queue(noticed_error)
  end


rescue => e
  ::TingYun::Agent.logger.warn("Failure when capturing error '#{exception}':", e)
  nil
end

#reset!Object



220
221
222
223
224
# File 'lib/ting_yun/agent/collector/error_collector.rb', line 220

def reset!
  @error_trace_array.reset!
  @external_error_array.reset!
  nil
end

#sense_method(object, method) ⇒ Object

calls a method on an object, if it responds to it - used for detection and soft fail-safe. Returns nil if the method does not exist



197
198
199
# File 'lib/ting_yun/agent/collector/error_collector.rb', line 197

def sense_method(object, method)
  object.send(method) if object.respond_to?(method)
end

#skip_notice_error?(exception) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/ting_yun/agent/collector/error_collector.rb', line 190

def skip_notice_error?(exception)
  exception_tagged?(exception)
end