logplex 0.0.1.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in logplex.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2012 Harold Giménez
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Harold Giménez
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Logplex
2
+
3
+ Publish and Consume Logplex messages
4
+
5
+ Logplex is the Heroku log router, and can be found [here](https://github.com/heroku/logplex).
6
+
7
+ ### Publishing messages
8
+
9
+ ```ruby
10
+ publisher = Logplex::Publisher.new(logplex_token, logplex_url)
11
+ publisher.publish("This is a log entry", process: 'worker.2',
12
+ host: 'some-host')
13
+ ```
14
+
15
+ Passing an array of messages to the `#publish` method will publish them all in one request,
16
+ so some latency optimization is possible:
17
+
18
+ ```ruby
19
+ publisher.publish [ "And as we wind on down the road",
20
+ "Our shadows taller than our soul",
21
+ "There walks a lady we all know",
22
+ "Who shines white light and wants to show"]
23
+ ```
24
+
25
+ ### Consumnig messages
26
+
27
+ TBD
28
+
29
+ ## Configuration
30
+
31
+ You can configure default values for logplex message posting:
32
+
33
+ ```ruby
34
+ Logplex.configure do |config|
35
+ config.logplex_url = 'https://logplex.example.com'
36
+ config.process = 'stats'
37
+ config.host = 'host'
38
+ end
39
+ ```
40
+
41
+ In the example above, it is now not not necessary to
42
+ specify a logplex URL, process or host when getting
43
+ a hold of a publisher and publishing messages:
44
+
45
+ ```ruby
46
+ publisher = Logplex::Publisher.new(logplex_token)
47
+ publisher.publish "And she's buying a stairway to heaven"
48
+ ```
49
+
50
+ ### License
51
+
52
+ Copyright (c) Harold Giménez. Released under the terms of the MIT License found in the LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ module Logplex
2
+ class Configuration
3
+ attr_accessor :logplex_url,
4
+ :process,
5
+ :host
6
+
7
+ def initialize
8
+ @logplex_url = 'https://east.logplex.io'
9
+ end
10
+ end
11
+
12
+ class << self
13
+ attr_accessor :configuration
14
+ end
15
+
16
+ def self.configure
17
+ self.configuration ||= Configuration.new
18
+ yield(configuration)
19
+ end
20
+
21
+ end
@@ -0,0 +1,47 @@
1
+ require 'valcro'
2
+ require 'time'
3
+ require 'logplex/configuration'
4
+
5
+ module Logplex
6
+ class Message
7
+ include Valcro
8
+
9
+ # facility = local0, priority = info, RFC5452 encoded
10
+ # syslog version 1
11
+ FACILITY_AND_PRIORITY = '<134>1'.freeze
12
+
13
+ FIELD_DISABLED = '-'.freeze
14
+
15
+ def initialize(message, opts = {})
16
+ @message = message
17
+ @token = opts.fetch(:token)
18
+ @time = opts[:time] || DateTime.now
19
+ @process = opts[:process] || Logplex.configuration.process
20
+ @host = opts[:host] || Logplex.configuration.host
21
+ @message_id = opts[:message_id] || FIELD_DISABLED
22
+ end
23
+
24
+ def syslog_frame
25
+ temp = "#{FACILITY_AND_PRIORITY} #{formatted_time} #{@host} #{@token} #{@process} #{@message_id} #{FIELD_DISABLED} #{@message}"
26
+ length = temp.length
27
+ "#{length} #{temp}"
28
+ end
29
+
30
+ def validate
31
+ super
32
+ errors.add(:message, "too long") if @message.length > 10240
33
+ errors.add(:process, "can't be nil") if @process.nil?
34
+ errors.add(:host, "can't be nil") if @host.nil?
35
+ end
36
+
37
+ private
38
+ def formatted_time
39
+ case @time.class
40
+ when String
41
+ DateTime.parse(@time).rfc3339
42
+ else
43
+ @time.to_datetime.rfc3339
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: UTF-8
2
+ require 'base64'
3
+ require 'restclient'
4
+ require 'logplex/message'
5
+
6
+ module Logplex
7
+ class Publisher
8
+ def initialize(token, logplex_url=nil)
9
+ @token = token
10
+ @logplex_url = logplex_url || Logplex.configuration.logplex_url
11
+ end
12
+
13
+ def publish(messages, opts={})
14
+ messages = Array(messages).dup
15
+ messages.map! { |m| Message.new(m, opts.merge(token: @token)) }
16
+ messages.each(&:validate)
17
+ if messages.inject(true) { |accum, m| m.valid? }
18
+ api_post(
19
+ messages.map(&:syslog_frame).join('')
20
+ )
21
+ end
22
+ end
23
+
24
+ private
25
+ def api_post(message)
26
+ auth_token = Base64.encode64("token:#{@token}")
27
+ auth = "Basic #{auth_token}"
28
+ RestClient.post("#{@logplex_url}/logs", message,
29
+ content_type: 'application/logplex-1',
30
+ content_length: message.length,
31
+ authorization: auth)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Logplex
2
+ VERSION = "0.0.1.pre"
3
+ end
data/lib/logplex.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "logplex/version"
2
+ require "logplex/configuration"
3
+ require "logplex/message"
4
+ require "logplex/publisher"
data/logplex.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'logplex/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "logplex"
8
+ gem.version = Logplex::VERSION
9
+ gem.authors = ["Harold Giménez"]
10
+ gem.email = ["harold.gimenez@gmail.com"]
11
+ gem.description = %q{Publish and Consume Logplex messages}
12
+ gem.summary = %q{Publish and Consume Logplex messages}
13
+ gem.homepage = "https://practiceovertheory.com"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency "valcro"
20
+ gem.add_dependency "rest-client"
21
+ gem.add_development_dependency "rspec"
22
+ gem.add_development_dependency "sham_rack"
23
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+ require 'logplex/configuration'
3
+
4
+ describe Logplex::Configuration, 'defaults' do
5
+ it 'defaults to the production heroku logplex url' do
6
+ Logplex.configure { |config| }
7
+
8
+ expect(
9
+ Logplex.configuration.logplex_url
10
+ ).to eq('https://east.logplex.io')
11
+ end
12
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'logplex/message'
3
+
4
+ describe Logplex::Message do
5
+ before { Logplex.configure {} }
6
+ after { restore_default_config }
7
+ it 'fills out fields of a syslog message' do
8
+ message = Logplex::Message.new(
9
+ 'my message here',
10
+ token: 't.some-token',
11
+ time: DateTime.parse("1980-08-23 05:31 00:00"),
12
+ process: 'heroku-postgres',
13
+ host: 'some-host',
14
+ message_id: '1'
15
+ )
16
+
17
+ expect(message.syslog_frame).to eq(
18
+ "91 <134>1 1980-08-23T05:31:00+00:00 some-host t.some-token heroku-postgres 1 - my message here"
19
+ )
20
+ end
21
+
22
+ it 'is invalid for messages longer than 10240 bytes' do
23
+ short = Logplex::Message.new('a' * 10240, token: 'foo',
24
+ process: 'proc',
25
+ host: 'host')
26
+ long = Logplex::Message.new('a' * 10241, token: 'foo',
27
+ process: 'proc',
28
+ host: 'host')
29
+ short.validate
30
+ long.validate
31
+
32
+ expect(short.valid?).to be_true
33
+ expect(long.valid?).to be_false
34
+ end
35
+
36
+ it 'is invalid with no process or host' do
37
+ message = Logplex::Message.new("a message", token: 't.some-token')
38
+ message.validate
39
+
40
+ expect(message.valid?).to be_false
41
+ expect(message.errors[:process]).to eq ["can't be nil"]
42
+ expect(message.errors[:host]).to eq ["can't be nil"]
43
+ end
44
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ require 'sham_rack'
3
+ require 'logplex/publisher'
4
+ require 'support/fake_logplex'
5
+
6
+ describe Logplex::Publisher, '#publish' do
7
+ before do
8
+ ShamRack.mount(FakeLogplex.new, 'logplex.example.com', 443)
9
+
10
+ Logplex.configure do |config|
11
+ config.process = "postgres"
12
+ config.host = "host"
13
+ end
14
+ end
15
+
16
+ after do
17
+ ShamRack.unmount_all
18
+ FakeLogplex.clear!
19
+ restore_default_config
20
+ end
21
+
22
+ it 'encodes a message and publishes it' do
23
+ FakeLogplex.register_token('t.some-token')
24
+
25
+ message = 'I have a message for you'
26
+ publisher = Logplex::Publisher.new('t.some-token', 'https://logplex.example.com')
27
+ publisher.publish(message)
28
+
29
+ expect(FakeLogplex).to have_received_message(message)
30
+ end
31
+
32
+ it 'sends many messages in one request when passed an array' do
33
+ FakeLogplex.register_token('t.some-token')
34
+ messages = ['I have a message for you', 'here is another', 'some final thoughts']
35
+
36
+ publisher = Logplex::Publisher.new('t.some-token', 'https://logplex.example.com')
37
+
38
+ publisher.publish(messages)
39
+
40
+ messages.each do |message|
41
+ expect(FakeLogplex).to have_received_message(message)
42
+ end
43
+
44
+ expect(FakeLogplex.requests_received).to eq(1)
45
+ end
46
+ end
@@ -0,0 +1,12 @@
1
+ RSpec.configure do |config|
2
+ config.treat_symbols_as_metadata_keys_with_true_values = true
3
+ config.run_all_when_everything_filtered = true
4
+ config.filter_run :focus
5
+
6
+ config.order = 'random'
7
+ end
8
+
9
+ def restore_default_config
10
+ Logplex.configuration = nil
11
+ Logplex.configure {}
12
+ end
@@ -0,0 +1,108 @@
1
+ class FakeLogplex
2
+ class Message
3
+
4
+ attr_reader :message, :token
5
+
6
+ def initialize(opts)
7
+ @message = opts[:message]
8
+ @token = opts[:token]
9
+ end
10
+
11
+ def self.from_syslog(syslog_message)
12
+ messages = []
13
+ anchor = 0
14
+ until anchor >= syslog_message.length
15
+ new_anchor, opts = extract_syslog_field(syslog_message, anchor)
16
+ raise "same" if anchor == new_anchor
17
+ anchor = new_anchor
18
+ messages << new(opts)
19
+ end
20
+ messages
21
+ end
22
+
23
+ def self.extract_syslog_field(syslog_message, anchor)
24
+ start = anchor
25
+ pos = start
26
+ pos, bytes = next_syslog_field(syslog_message, anchor, pos)
27
+ anchor = pos+1
28
+ pos, facility = next_syslog_field(syslog_message, anchor, pos)
29
+ anchor = pos+1
30
+ pos, time = next_syslog_field(syslog_message, anchor, pos)
31
+ anchor = pos+1
32
+ pos, host = next_syslog_field(syslog_message, anchor, pos)
33
+ anchor = pos+1
34
+ pos, token = next_syslog_field(syslog_message, anchor, pos)
35
+ anchor = pos+1
36
+ pos, process = next_syslog_field(syslog_message, anchor, pos)
37
+ anchor = pos+1
38
+ pos, message_id = next_syslog_field(syslog_message, anchor, pos)
39
+ anchor = pos+1
40
+ pos, unknown = next_syslog_field(syslog_message, anchor, pos)
41
+ anchor = pos+1
42
+
43
+ limit = start + bytes + bytes.to_s.length
44
+ message = syslog_message[anchor..limit]
45
+
46
+ [limit + 1, { message: message, token: token }]
47
+ end
48
+
49
+ def self.next_syslog_field(message, anchor, pos)
50
+ until char = message[pos+=1] and char == ' '
51
+ field = message[anchor..pos].to_i
52
+ end
53
+ [pos, field]
54
+ end
55
+ end
56
+
57
+ @@tokens = []
58
+ @@received_messages = []
59
+ @@requests_received = 0
60
+
61
+ def call(env)
62
+ @@requests_received += 1
63
+
64
+ message = env['rack.input'].read
65
+ method = env['REQUEST_METHOD']
66
+ path = env['PATH_INFO']
67
+ content_type = env['CONTENT_TYPE']
68
+ content_length = env['CONTENT_LENGTH'].to_i
69
+ auth = env['HTTP_AUTHORIZATION']
70
+
71
+ _, auth_token = auth.split(' ')
72
+ user, pass = Base64.decode64(auth_token).split(':')
73
+ if @@tokens.include?(pass)
74
+ if (method == 'POST' &&
75
+ path == '/logs' &&
76
+ content_type == 'application/logplex-1' &&
77
+ content_length == message.length)
78
+
79
+ @@received_messages << Message.from_syslog(message)
80
+ @@received_messages.flatten!
81
+ [200, {}, []]
82
+ else
83
+ [404, {}, []]
84
+ end
85
+ else
86
+ [401, {}, []]
87
+ end
88
+ end
89
+
90
+ def self.has_received_message?(message)
91
+ @@received_messages.map(&:message).include? message
92
+ end
93
+
94
+ def self.register_token(token)
95
+ @@tokens << token
96
+ end
97
+
98
+ def self.clear!
99
+ @@tokens = []
100
+ @@received_messages = []
101
+ @@requests_received = 0
102
+ end
103
+
104
+ def self.requests_received
105
+ @@requests_received
106
+ end
107
+ end
108
+
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logplex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Harold Giménez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: valcro
16
+ requirement: &70148113247780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70148113247780
25
+ - !ruby/object:Gem::Dependency
26
+ name: rest-client
27
+ requirement: &70148113247240 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70148113247240
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70148113246560 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70148113246560
47
+ - !ruby/object:Gem::Dependency
48
+ name: sham_rack
49
+ requirement: &70148113245700 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70148113245700
58
+ description: Publish and Consume Logplex messages
59
+ email:
60
+ - harold.gimenez@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - .rspec
67
+ - Gemfile
68
+ - LICENSE
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - lib/logplex.rb
73
+ - lib/logplex/configuration.rb
74
+ - lib/logplex/message.rb
75
+ - lib/logplex/publisher.rb
76
+ - lib/logplex/version.rb
77
+ - logplex.gemspec
78
+ - spec/logplex/configuration_spec.rb
79
+ - spec/logplex/message_spec.rb
80
+ - spec/logplex/publisher_spec.rb
81
+ - spec/spec_helper.rb
82
+ - spec/support/fake_logplex.rb
83
+ homepage: https://practiceovertheory.com
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>'
99
+ - !ruby/object:Gem::Version
100
+ version: 1.3.1
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.10
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Publish and Consume Logplex messages
107
+ test_files:
108
+ - spec/logplex/configuration_spec.rb
109
+ - spec/logplex/message_spec.rb
110
+ - spec/logplex/publisher_spec.rb
111
+ - spec/spec_helper.rb
112
+ - spec/support/fake_logplex.rb
113
+ has_rdoc: