Class: Logplex::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/logplex/channel.rb

Overview

Public: A logplex channel.

You generally acquire one of these objects from Logplex::Client#create_channel or Logplex::Client#channel

Instance Method Summary collapse

Constructor Details

#initialize(url, channel_id) ⇒ Channel

Public: initialize a channel object

url - the url to logplex. See Logplex::Client#new for more info. channel_id - the channel id, should be a number.

See also: Logplex::Client#create_channel and Logplex::Client#channel



32
33
34
35
36
# File 'lib/logplex/channel.rb', line 32

def initialize(url, channel_id)
  @url = url
  @channel_id = channel_id
  @backend = Logplex::Client::Backends::HTTP.new(@url)
end

Instance Method Details

#create_drain(url = nil) ⇒ Object

Public: Create a new drain for this channel.

For information on what a drain is, see Logplex::Drain

Returns a Logplex::Drain Raises # TODO(sissel): ???



101
102
103
104
105
106
# File 'lib/logplex/channel.rb', line 101

def create_drain(url=nil)
  # Just create the drain, don't add any URLs to it.
  # API call /channels/[channel-id]/drains/tokens
  result = @backend.create_drain(@channel_id, url)
  return Logplex::Drain.new(@url, @channel_id, result[:id], result[:token], url)
end

#create_session(settings = {}) ⇒ Object

Public: Create a new session for this channel.

For information on what a session is, see Logplex::Session

settings - a hash containing:

:source - the token name, like "app" or "heroku" (default: nil, optional)
:ps - the RFC5424 process id, like "web.1" (default: nil, optional)
:num - the number of events to retrieve (default: 40, optional)
:tail - true if you wish to follow the live event stream 
  (default: false, optional)

Returns a Logplex::Session Raises # TODO(sissel): ???



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/logplex/channel.rb', line 121

def create_session(settings={})
  settings = { 
    :num => 40,
    :tail => false
  }.merge(settings)

  # Currently a bug(?) in logplex that if you specify 'num' = 0, you get all
  # logs. TODO(sissel): If you specify '0.1' you get zero results, so use
  # that hack to hide the '0' bug until the bug is fixed.
  settings[:num] = 0.1 if settings[:num] == 0

  # Here's what core does: https://github.com/heroku/core/blob/master/lib/logplex.rb#L12
  # TODO(sissel): generate a random 'srv' id
  server_id = rand(1000)
  result = @backend.create_session(@channel_id, server_id, settings)
  # the foo=bar& prefix is required because of some problem with haproxy or
  # how we are using it to pin http requests to backends.
  # Additionally, sesssions also don't need authentication, so strip that out.
  url = URI.parse(@url + result[:url] + "?" + "foo=bar&srv=#{server_id}")
  url.user = nil
  url.password = nil
  return Logplex::Session.new(url.to_s, settings)
end

#create_token(name) ⇒ Object

Public: Create a new token for this channel.

name - a String name for this token. The name is meaningful and will

appear in drain or session output.

Returns a Logplex::Token



90
91
92
93
# File 'lib/logplex/channel.rb', line 90

def create_token(name)
  result = @backend.create_token(@channel_id, name)
  return Logplex::Token.new(@url, @channel_id, result[:name], result[:token])
end

#destroyObject

Public: Destroy a channel.

This will remove a channel from logplex as well as any associated tokens or drains.

Returns nothing. Raises # TODO(sissel): What errors?



45
46
47
# File 'lib/logplex/channel.rb', line 45

def destroy
  @backend.delete_channel(@channel_id)
end

#drainsObject

Public: Get a list of drains associated with this channel.

Note: To create a drain, see Logplex::Channel#create_drain

Returns an Array of Logplex::Drains Raises # TODO(sissel): ???



76
77
78
79
80
81
82
# File 'lib/logplex/channel.rb', line 76

def drains
  @info ||= @backend.get_channel(@channel_id)
  result = @info
  return result[:drains].collect do |info|
    Logplex::Drain.new(@url, @channel_id, info[:id], info[:token], info[:url])
  end
end

#idObject

Public: Get the channel id.

Returns the channel id.



52
53
54
# File 'lib/logplex/channel.rb', line 52

def id
  return @channel_id
end

#tokensObject

Public: Get a list of tokens associated with this channel.

Note: To create a token, see Logplex::Channel#create_token

Returns an Array of Logplex::Tokens Raises # TODO(sissel): ???



62
63
64
65
66
67
68
# File 'lib/logplex/channel.rb', line 62

def tokens
  @info ||= @backend.get_channel(@channel_id)
  result = @info
  return result[:tokens].collect do |info| 
    Logplex::Token.new(@url, @channel_id, info[:name], info[:token])
  end
end