Class: Sematext::Metrics::AsyncSender

Inherits:
Object
  • Object
show all
Defined in:
lib/sematext/metrics/async_sender.rb

Instance Method Summary collapse

Constructor Details

#initialize(token, base_uri, path) ⇒ AsyncSender

Returns a new instance of AsyncSender.



18
19
20
21
22
23
24
25
# File 'lib/sematext/metrics/async_sender.rb', line 18

def initialize(token, base_uri, path)
  @token = token
  @path = path || Settings::RECEIVER_PATH
  @base_uri = base_uri || Settings::RECEIVER_URI
  if defined?(EventMachine)
    require 'em-http-request'
  end
end

Instance Method Details

#send(data) ⇒ Object



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
# File 'lib/sematext/metrics/async_sender.rb', line 27

def send data
  deferrable = EventMachine::DefaultDeferrable.new

  post_options = {
    :path => @path,
    :query => {:token => @token},
    :body => data,
    :head => {
      'Content-Type' => 'text/plain',
      'User-Agent' => Settings::USER_AGENT
    }
  }
  
  http = EventMachine::HttpRequest.new(@base_uri).post post_options
  http.errback { 
    deferrable.fail{{:status => :failed}}
  }
  http.callback {
    if http.response_header.status == 201 then
      deferrable.succeed({:status => :succeed})
    else
      deferrable.fail({:status => :failed, :response => http.response})
    end
  }
  deferrable
end