Class: LogjamAgent::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/logjam_agent/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, env, initial_fields) ⇒ Request

Returns a new instance of Request.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/logjam_agent/request.rb', line 12

def initialize(app, env, initial_fields)
  @app = app
  @env = env
  @forwarder = Forwarders.get(app, env)
  @lines = []
  @uuid = LogjamAgent.generate_uuid
  @fields = initial_fields.merge(:request_id => @uuid, :host => LogjamAgent.hostname,
                                 :process_id => Process.pid, :lines => @lines)
  @fields[:trace_id] ||= @uuid
  unless (revision = LogjamAgent.application_revision).blank?
    @fields[:revision] = revision
  end
  if ENV['CLUSTER']
    @fields[:cluster] = ENV['CLUSTER']
  end
  if ENV['DATACENTER']
    @fields[:datacenter] = ENV['DATACENTER']
  end
  if ENV['NAMESPACE']
    @fields[:namespace] = ENV['NAMESPACE']
  end
  if start_time = @fields.delete(:start_time)
    self.start_time = start_time
  end
  @mutex = Mutex.new
  @ignored = false
  @bytes_all_lines = 0
  @max_bytes_all_lines = LogjamAgent.max_bytes_all_lines
  @max_line_length = LogjamAgent.max_line_length
  @lines_dropped = false
  @log_info = {}
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



9
10
11
# File 'lib/logjam_agent/request.rb', line 9

def fields
  @fields
end

#log_device_ignored_linesObject

Returns the value of attribute log_device_ignored_lines.



10
11
12
# File 'lib/logjam_agent/request.rb', line 10

def log_device_ignored_lines
  @log_device_ignored_lines
end

#log_infoObject (readonly)

Returns the value of attribute log_info.



9
10
11
# File 'lib/logjam_agent/request.rb', line 9

def log_info
  @log_info
end

#start_timeObject

Returns the value of attribute start_time.



9
10
11
# File 'lib/logjam_agent/request.rb', line 9

def start_time
  @start_time
end

#uuidObject (readonly)

Returns the value of attribute uuid.



9
10
11
# File 'lib/logjam_agent/request.rb', line 9

def uuid
  @uuid
end

Instance Method Details

#actionObject



63
64
65
# File 'lib/logjam_agent/request.rb', line 63

def action
  @fields[:action]
end

#add_exception(exception, severity = Logger::ERROR) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/logjam_agent/request.rb', line 102

def add_exception(exception, severity = Logger::ERROR)
  @mutex.synchronize do
    if LogjamAgent.split_hard_and_soft_exceptions && severity < Logger::ERROR
      ((@fields[:soft_exceptions] ||= []) << exception).uniq!
    else
      ((@fields[:exceptions] ||= []) << exception).uniq!
    end
  end
end

#add_line(severity, timestamp, message) ⇒ 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/logjam_agent/request.rb', line 79

def add_line(severity, timestamp, message)
  @mutex.synchronize do
    if @bytes_all_lines > @max_bytes_all_lines
      unless @lines_dropped
        @lines << [severity, format_time(timestamp), "... [LINES DROPPED]"]
        @lines_dropped = true
      end
      return
    end
    message = message.strip
    line_too_long = message.size > @max_line_length
    if line_too_long && severity < Logger::ERROR
      message[(@max_line_length-21)..-1] = " ... [LINE TRUNCATED]"
    end
    if (@bytes_all_lines += message.bytesize) > @max_bytes_all_lines
      if line_too_long
        message[(@max_line_length-21)..-1] = " ... [LINE TRUNCATED]"
      end
    end
    @lines << [severity, format_time(timestamp), message]
  end
end

#caller_actionObject



71
72
73
# File 'lib/logjam_agent/request.rb', line 71

def caller_action
  @fields[:caller_action]
end

#caller_idObject



67
68
69
# File 'lib/logjam_agent/request.rb', line 67

def caller_id
  @fields[:caller_id]
end

#forwardObject



112
113
114
115
116
117
118
119
# File 'lib/logjam_agent/request.rb', line 112

def forward
  return if @ignored || LogjamAgent.disabled
  engine = @fields.delete(:engine)
  sync = @fields.delete(:sync)
  @forwarder.forward(@fields, :engine => engine, :sync => sync)
rescue Exception => e
  handle_forwarding_error(e)
end

#idObject



59
60
61
# File 'lib/logjam_agent/request.rb', line 59

def id
  "#{@app}-#{@env}-#{@uuid}"
end

#ignore!(reason = nil) ⇒ Object



51
52
53
# File 'lib/logjam_agent/request.rb', line 51

def ignore!(reason = nil)
  @ignored = reason || :unknown
end

#ignored?(reason = nil) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/logjam_agent/request.rb', line 55

def ignored?(reason = nil)
  reason ? @ignored == reason : @ignored
end

#trace_idObject



75
76
77
# File 'lib/logjam_agent/request.rb', line 75

def trace_id
  @fields[:trace_id]
end