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
64
# 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)? exteneral_error?(exception)? "External #{exception.tingyun_code}" : exception.class.name : 'Error'
  @is_external_error = exception.respond_to?(:tingyun_external)? exception.tingyun_external : false
  @code = 0
  if @is_external_error
    @external_metric_name = exception.tingyun_klass
    @http_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

#http_codeObject

Returns the value of attribute http_code.



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

def http_code
  @http_code
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

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



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

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



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

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

#error_paramsObject



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

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

#exteneral_error?(exception) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
136
# File 'lib/ting_yun/agent/collector/error_collector/noticed_error.rb', line 130

def exteneral_error? exception
  if defined? ::Thrift::ApplicationException
   exception.is_a?(TingYun::Support::Exception::InternalServerError) or exception.is_a?(::Thrift::ApplicationException)
  else
    exception.is_a?(TingYun::Support::Exception::InternalServerError)
  end
end

#request_paramsObject



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

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

#to_collector_array(encoder) ⇒ Object



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

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