Class: Logdna::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/logdna/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(request, uri, opts) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/logdna/client.rb', line 11

def initialize(request, uri, opts)
  @uri = uri

  # NOTE: buffer is in memory
  @buffer = []
  @buffer_byte_size = 0

  @side_messages = []

  @lock = Mutex.new
  @side_message_lock = Mutex.new
  @flush_limit = opts[:flush_size] || Resources::FLUSH_BYTE_LIMIT
  @flush_interval = opts[:flush_interval] || Resources::FLUSH_INTERVAL
  @flush_scheduled = false
  @exception_flag = false

  @request = request
  @retry_timeout = opts[:retry_timeout] || Resources::RETRY_TIMEOUT

  @internal_logger = Logger.new(STDOUT)
  @internal_logger.level = Logger::DEBUG
end

Instance Method Details

#exitoutObject



133
134
135
136
# File 'lib/logdna/client.rb', line 133

def exitout
  flush
  @internal_logger.debug("Exiting LogDNA logger: Logging remaining messages")
end

#flushObject



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/logdna/client.rb', line 121

def flush
  if @lock.try_lock
    @flush_scheduled = false
    if @buffer.any? || @side_messages.any?
      send_request
    end
    @lock.unlock
  else
    schedule_flush
  end
end

#process_message(msg, opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/logdna/client.rb', line 34

def process_message(msg, opts = {})
  processed_message = {
    line: msg,
    app: opts[:app],
    level: opts[:level],
    env: opts[:env],
    meta: opts[:meta],
    timestamp: Time.now.to_i,
  }
  processed_message.delete(:meta) if processed_message[:meta].nil?
  processed_message
end

#schedule_flushObject



47
48
49
50
51
52
53
# File 'lib/logdna/client.rb', line 47

def schedule_flush
  start_timer = lambda {
    sleep(@exception_flag ? @retry_timeout : @flush_interval)
    flush if @flush_scheduled
  }
  Thread.new { start_timer.call }
end

#send_requestObject

This method has to be called with @lock



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/logdna/client.rb', line 77

def send_request
  @side_message_lock.synchronize do
    @buffer.concat(@side_messages)
    @side_messages.clear
  end

  @request.body = {
    e: "ls",
    ls: @buffer
  }.to_json

  handle_exception = lambda do |message|
    @internal_logger.debug(message)
    @exception_flag = true
    @side_message_lock.synchronize do
      @side_messages.concat(@buffer)
    end
  end

  begin
    @response = Net::HTTP.start(
      @uri.hostname,
      @uri.port,
      use_ssl: @uri.scheme == "https"
    ) do |http|
      http.request(@request)
    end
    if @response.is_a?(Net::HTTPForbidden)
      @internal_logger.debug("Please provide a valid ingestion key")
    elsif !@response.is_a?(Net::HTTPSuccess)
      handle_exception.call("The response is not successful #{@response}")
    end
    @exception_flag = false
  rescue SocketError
    handle_exception.call("Network connectivity issue")
  rescue Errno::ECONNREFUSED => e
    handle_exception.call("The server is down. #{e.message}")
  rescue Timeout::Error => e
    handle_exception.call("Timeout error occurred. #{e.message}")
  ensure
    @buffer.clear
  end
end

#write_to_buffer(msg, opts) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/logdna/client.rb', line 55

def write_to_buffer(msg, opts)
  if @lock.try_lock
    processed_message = process_message(msg, opts)
    new_message_size = processed_message.to_s.bytesize
    @buffer.push(processed_message)
    @buffer_byte_size += new_message_size
    @flush_scheduled = true
    @lock.unlock

    if @flush_limit <= @buffer_byte_size
      flush
    else
      schedule_flush
    end
  else
    @side_message_lock.synchronize do
      @side_messages.push(process_message(msg, opts))
    end
  end
end