Sha256: 174d30691023b6ff4327457c14dfc4d3fcaf02c70bc6909bc1b9f64c13715b0c

Contents?: true

Size: 1.35 KB

Versions: 9

Compression:

Stored size: 1.35 KB

Contents

require "json"

module LogjamAgent
  class Request
    attr_reader :caller_id, :fields

    def initialize(app, env, logger, initial_fields)
      @logger = logger
      @app = app
      @env = env
      @forwarder = Forwarders.get(app, env)
      @lines = []
      @id = UUID4R::uuid(1).gsub('-','')
      @fields = initial_fields.merge(:request_id => @id, :host => LogjamAgent.hostname, :process_id => Process.pid, :lines => @lines)
      @mutex = Mutex.new
    end

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

    def caller_id
      @fields[:caller_id]
    end

    def add_line(severity, timestamp, message)
      @mutex.synchronize do
        @lines << [severity, format_time(timestamp), message.strip]
      end
    end

    def add_exception(exception)
      @mutex.synchronize do
        ((@fields[:exceptions] ||= []) << exception).uniq!
      end
    end

    def forward
      engine = @fields.delete(:engine)
      @forwarder.forward(@fields.to_json, engine)
    rescue Exception => e
      handle_forwarding_error(e)
    end

    private

    def format_time(t)
      # iso time with microseconds
      t.strftime("%Y-%m-%dT%H:%M:%S.#{"%06d" % t.usec}")
    end

    def handle_forwarding_error(exception)
      @logger.error exception.to_s
      LogjamAgent.error_handler.call(exception)
    rescue Exception
      # swallow all exceptions
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
logjam_agent-0.5.7 lib/logjam_agent/request.rb
logjam_agent-0.5.6 lib/logjam_agent/request.rb
logjam_agent-0.5.5 lib/logjam_agent/request.rb
logjam_agent-0.5.4 lib/logjam_agent/request.rb
logjam_agent-0.5.3 lib/logjam_agent/request.rb
logjam_agent-0.5.2 lib/logjam_agent/request.rb
logjam_agent-0.5.1 lib/logjam_agent/request.rb
logjam_agent-0.5.0 lib/logjam_agent/request.rb
logjam_agent-0.4.5 lib/logjam_agent/request.rb