Class: Logplex::Client::Backends::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/logplex/client/backends/http.rb

Overview

A client implementation that speaks to the logplex http interface.

Defined Under Namespace

Classes: Error, NotFound, Unauthorized

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ HTTP

A new logplex backend that uses HTTP

url - (string) url to logplex, like “user:[email protected]/”



22
23
24
# File 'lib/logplex/client/backends/http.rb', line 22

def initialize(url)
  @url = url
end

Instance Method Details

#create_channel(name, tokens = []) ⇒ Object

Returns a hash with form:

{:channel_id=>3639270, :tokens=>[]}


28
29
30
# File 'lib/logplex/client/backends/http.rb', line 28

def create_channel(name, tokens = [])
  json_post("/channels", {:name => name, :tokens => tokens})
end

#create_drain(channel_id, url) ⇒ Object

Public: Creates a drain for a given channel.



51
52
53
# File 'lib/logplex/client/backends/http.rb', line 51

def create_drain(channel_id, url)
  json_post("/v2/channels/#{channel_id}/drains", :url => url)
end

#create_session(channel_id, server_id, opts = {}) ⇒ Object

Creates a event stream session on a channel.

channel_id - the channel id (number) server_id - the server id to use in this request

Note: The ‘server_id’ is just used for request routing purposes. When creating and reading a session, you must use the same server_id in both requests. This lets any load balancer in front of logplex pin the session create and read to the same backend logplex session.



81
82
83
84
85
86
87
88
89
90
# File 'lib/logplex/client/backends/http.rb', line 81

def create_session(channel_id, server_id, opts = {})
  # The 'foo=bar&' part is due to how we use haproxy for pinning
  # Also channel_id should be a string, also.
  opts = opts.merge(:channel_id => channel_id.to_s)
  # num needs to be a string
  opts[:num] = opts[:num].to_s if opts.has_key?(:num)
  # If the 'tail' opt is false, remove it.
  opts.delete(:tail) if !opts[:tail]
  json_post("/v2/sessions?foo=bar&srv=#{server_id}", opts)
end

#create_token(channel_id, token_name) ⇒ Object

Public: Creates a token on a given channel



46
47
48
# File 'lib/logplex/client/backends/http.rb', line 46

def create_token(channel_id, token_name)
  json_post("/v2/channels/#{channel_id}/tokens", {:name => token_name})
end

#delete_channel(channel_id) ⇒ Object

Public: Deletes a channel



41
42
43
# File 'lib/logplex/client/backends/http.rb', line 41

def delete_channel(channel_id)
  delete("/v2/channels/#{channel_id}")
end

#delete_drain(channel_id, drain_id) ⇒ Object

Public: Delete a drain

channel_id - the channel id (number) drain_id - the drain id (string, given from #create_drain)



68
69
70
# File 'lib/logplex/client/backends/http.rb', line 68

def delete_drain(channel_id, drain_id)
  delete("/v2/channels/#{channel_id}/drains/#{drain_id}")
end

#get_channel(channel_id) ⇒ Object

Public: Creates a channel.

Returns a hash of the json response in the form:

{:channel_id=>3639270, :tokens=>[], :drains=>[]}


36
37
38
# File 'lib/logplex/client/backends/http.rb', line 36

def get_channel(channel_id) 
  json_get("/v2/channels/#{channel_id}")
end

#set_drain_url(channel_id, drain_id, url) ⇒ Object

Public: Sets the drain url for a channel+drain.

channel_id - the channel id (number) drain_id - the drain id (string, given from #create_drain) url - the url to drain events to, like “syslog://host:port/”



60
61
62
# File 'lib/logplex/client/backends/http.rb', line 60

def set_drain_url(channel_id, drain_id, url)
  json_post("/v2/channels/#{channel_id}/drains/#{drain_id}", :url => url)
end