Class: TingYun::Agent::Collector::NoticedError

Inherits:
Object
  • Object
show all
Includes:
Support::Coerce
Defined in:
lib/ting_yun/agent/collector/error_collector/noticed_error.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::Coerce

event_params, float, int, int_or_nil, log_failure, string, url_encode

Constructor Details

#initialize(metric_name, exception, timestamp = Time.now) ⇒ NoticedError

Returns a new instance of NoticedError.



24
25
26
27
28
29
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
56
57
58
59
60
61
62
63
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 24

def initialize(metric_name, exception, timestamp = Time.now)
  @metric_name = metric_name
  @timestamp = timestamp
  @stack_trace = []
  @count_error = 1
  @exception_id = exception.object_id
  @exception_class_name = exception.is_a?(Exception) ? exception.class.name : 'Error'
  @is_external_error = exception.respond_to?(:tingyun_external)? exception.tingyun_external : false
  if @is_external_error
    @external_metric_name = exception.tingyun_klass
    @code = exception.tingyun_code
    @trace = exception.tingyun_trace
  end
  # It's critical that we not hold onto the exception class constant in this
  # class. These objects get serialized for Resque to a process that might
  # not have the original exception class loaded, so do all processing now
  # while we have the actual exception!
  @is_internal = (exception.class < TingYun::Support::Exception::InternalAgentError)

  if exception.nil?
    @message = '<no message>'
  elsif exception.respond_to?('original_exception')
    @message = (exception.original_exception || exception).to_s
  else # exception is not nil, but does not respond to original_exception
    @message = exception.to_s
  end


  unless @message.is_a?(String)
    # In pre-1.9.3, Exception.new({}).to_s.class != String
    # That is, Exception#to_s may not return a String instance if one wasn't
    # passed in upon creation of the Exception. So, try to generate a useful
    # String representation of the exception message, falling back to failsafe
    @message = String(@message.inspect) rescue '<unknown message type>'
  end

  # clamp long messages to 4k so that we don't send a lot of
  # overhead across the wire
  @message = @message[0..4095] if @message.length > 4096
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



17
18
19
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 17

def attributes
  @attributes
end

#attributes_from_notice_errorObject

Returns the value of attribute attributes_from_notice_error.



17
18
19
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 17

def attributes_from_notice_error
  @attributes_from_notice_error
end

#codeObject

Returns the value of attribute code.



17
18
19
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 17

def code
  @code
end

#count_errorObject

Returns the value of attribute count_error.



17
18
19
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 17

def count_error
  @count_error
end

#exception_class_nameObject

Returns the value of attribute exception_class_name.



17
18
19
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 17

def exception_class_name
  @exception_class_name
end

#exception_idObject (readonly)

Returns the value of attribute exception_id.



21
22
23
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 21

def exception_id
  @exception_id
end

#external_metric_nameObject

Returns the value of attribute external_metric_name.



17
18
19
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 17

def external_metric_name
  @external_metric_name
end

#is_external_errorObject

Returns the value of attribute is_external_error.



17
18
19
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 17

def is_external_error
  @is_external_error
end

#is_internalObject (readonly)

Returns the value of attribute is_internal.



21
22
23
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 21

def is_internal
  @is_internal
end

#messageObject

Returns the value of attribute message.



17
18
19
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 17

def message
  @message
end

#metric_nameObject

Returns the value of attribute metric_name.



17
18
19
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 17

def metric_name
  @metric_name
end

#stack_traceObject

Returns the value of attribute stack_trace.



17
18
19
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 17

def stack_trace
  @stack_trace
end

#timestampObject

Returns the value of attribute timestamp.



17
18
19
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 17

def timestamp
  @timestamp
end

#traceObject

Returns the value of attribute trace.



17
18
19
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 17

def trace
  @trace
end

Instance Method Details

#==(other) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 66

def ==(other)
  return true if other.respond_to?(:exception_id) && exception_id == other.exception_id

  if metric_name == other.metric_name && message == other.message
    @count_error = count_error + 1
    return true
  else
    return false
  end
end

#custom_paramsObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 115

def custom_params
  hash = {:threadName => string(attributes.agent_attributes[:threadName])}
  if is_external_error
    hash[:httpStatus] = int(code)
  else
    hash[:httpStatus] = int(attributes.agent_attributes[:httpStatus])
    hash[:referer] = string(attributes.agent_attributes[:referer]) || ''
  end
  hash
end

#error_paramsObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 102

def error_params
  hash = {
      :params => custom_params
  }
  if is_external_error
    hash[:stacktrace] = trace
  else
    hash[:stacktrace] = stack_trace
    hash[:requestParams] = request_params
  end
  hash
end

#request_paramsObject



126
127
128
129
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 126

def request_params
  return {}  unless TingYun::Agent.config['nbs.capture_params']
  attributes.request_params
end

#to_collector_array(encoder) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 79

def to_collector_array(encoder)
  if  is_external_error
    [timestamp.to_i,
     string(external_metric_name),
     int(code),
     string(exception_class_name),
     count_error,
     string(metric_name),
     encoder.encode(error_params)
    ]
  else
    [timestamp.to_i,
     string(metric_name),
     int(attributes.agent_attributes[:httpStatus]),
     string(exception_class_name),
     string(message),
     count_error,
     string(attributes.agent_attributes[:request_path]||metric_name),
     encoder.encode(error_params)
    ]
  end
end