class Sematext::Metrics::AsyncSender

Public Class Methods

new(token, base_uri, path) click to toggle source
# 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

Public Instance Methods

send(data) click to toggle source
# 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