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
# 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)
  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
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

#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



61
62
63
# File 'lib/logjam_agent/request.rb', line 61

def action
  @fields[:action]
end

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



93
94
95
96
97
98
99
100
101
# File 'lib/logjam_agent/request.rb', line 93

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/logjam_agent/request.rb', line 73

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
    if message.size > @max_line_length && severity < Logger::ERROR
      message[(@max_line_length-21)..-1] = " ... [LINE TRUNCATED]"
    end
    if (@bytes_all_lines += message.bytesize) > @max_bytes_all_lines
      message[(@max_line_length-21)..-1] = " ... [LINE TRUNCATED]"
    end
    @lines << [severity, format_time(timestamp), message]
  end
end

#caller_actionObject



69
70
71
# File 'lib/logjam_agent/request.rb', line 69

def caller_action
  @fields[:caller_action]
end

#caller_idObject



65
66
67
# File 'lib/logjam_agent/request.rb', line 65

def caller_id
  @fields[:caller_id]
end

#forwardObject



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

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



57
58
59
# File 'lib/logjam_agent/request.rb', line 57

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

#ignore!Object



49
50
51
# File 'lib/logjam_agent/request.rb', line 49

def ignore!
  @ignored = true
end

#ignored?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/logjam_agent/request.rb', line 53

def ignored?
  @ignored
end