logasm 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3101253198b60ee5b25bb1630c9e921adb38aeb
4
- data.tar.gz: 6bd9c7299915e79a8e66e435203814055d02975e
3
+ metadata.gz: 95ef4c8dd10a76b6775e23d3ed9788b8fd0c566e
4
+ data.tar.gz: c42a97f69f78953749d22008b8dd8dadab680ba8
5
5
  SHA512:
6
- metadata.gz: d79512c5dfae753ff1b23afa09d847b4eea76d2c659b496f4b45d1646113af73683c808cb7a756c472dd8eb3befbcf4e2532847180d42512adb9eb8b42db475d
7
- data.tar.gz: 85187d7037e0df502f3c25e2c792d1769a4583f39d3876534069365eacea671b092dcb56f37f30216dc89c47a4d6a774223a55ec651f36124124b69efdffc177
6
+ metadata.gz: 71b8cd77787497193c97508a269c0d538c3fd2a38a8940eaff94174f5918222ff3d59ec8f1fbb1cd7b1f7c90f1147ea08e776762f5919a1eeb20f005b74cd460
7
+ data.tar.gz: a766efc45eff5765e72353fc49c6680f31722e3bd81d710ceb0f64ef130901c8823349b6556f0bba3e33b07806e69cde3dd842937a621522b78d56af84961029
data/Gemfile.lock CHANGED
@@ -1,17 +1,26 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- logasm (0.2.1)
4
+ logasm (0.2.2)
5
5
  inflecto
6
- logstash-event (~> 1.2)
7
6
 
8
7
  GEM
9
8
  remote: https://rubygems.org/
10
9
  specs:
10
+ amq-protocol (2.0.0)
11
+ atomic (1.1.99)
12
+ bunny (1.6.3)
13
+ amq-protocol (>= 1.9.2)
11
14
  coderay (1.1.0)
12
15
  diff-lcs (1.2.5)
16
+ freddy (0.4.1)
17
+ bunny (= 1.6.3)
18
+ hamster (~> 1.0.1.pre.rc3)
19
+ symbolizer
20
+ thread (~> 0.2)
21
+ hamster (1.0.1.pre.rc3)
22
+ atomic (~> 1.1)
13
23
  inflecto (0.0.2)
14
- logstash-event (1.2.02)
15
24
  method_source (0.8.2)
16
25
  pry (0.10.1)
17
26
  coderay (~> 1.1.0)
@@ -32,12 +41,15 @@ GEM
32
41
  rspec-support (~> 3.3.0)
33
42
  rspec-support (3.3.0)
34
43
  slop (3.6.0)
44
+ symbolizer (0.0.1)
45
+ thread (0.2.2)
35
46
 
36
47
  PLATFORMS
37
48
  ruby
38
49
 
39
50
  DEPENDENCIES
40
51
  bundler (~> 1.3)
52
+ freddy (~> 0.4.1)
41
53
  logasm!
42
54
  pry
43
55
  rake
@@ -2,33 +2,14 @@ class Logasm
2
2
  module Adapters
3
3
  class LogstashAdapter
4
4
  class Formatter < ::Logger::Formatter
5
- HOST = ::Socket.gethostname
6
-
7
5
  def initialize(service_name)
8
6
  @service_name = service_name
9
7
  end
10
8
 
11
- def call(severity, time, _progname, message)
12
- event = build_event(message, severity, time)
9
+ def call(level, _time, _progname, message)
10
+ event = Utils.build_event(message, level, @service_name)
13
11
  "#{event.to_json}\n"
14
12
  end
15
-
16
- private
17
-
18
- def build_event(metadata, level, time)
19
- event = LogStash::Event.new(metadata.merge("@timestamp" => time))
20
-
21
- event['application'] = @service_name if @service_name
22
- event['level'] = level.downcase
23
- event['host'] ||= HOST
24
-
25
- # In case Time#to_json has been overridden
26
- if event.timestamp.is_a?(Time)
27
- event.timestamp = event.timestamp.iso8601(3)
28
- end
29
-
30
- event
31
- end
32
13
  end
33
14
  end
34
15
  end
@@ -1,5 +1,4 @@
1
1
  require 'socket'
2
- require 'logstash-event'
3
2
  require_relative 'logstash_adapter/formatter'
4
3
 
5
4
  class Logasm
@@ -7,8 +7,6 @@ rescue LoadError => e
7
7
  raise exception
8
8
  end
9
9
 
10
- require_relative 'rabbitmq_adapter/message_builder'
11
-
12
10
  class Logasm
13
11
  module Adapters
14
12
  class RabbitmqAdapter
@@ -16,18 +14,18 @@ class Logasm
16
14
 
17
15
  CONFIGURATION_KEYS = [:host, :user, :pass, :port]
18
16
 
19
- def initialize(level, service, arguments = {})
17
+ def initialize(level, service_name, arguments = {})
20
18
  config = arguments.select { |key, value| CONFIGURATION_KEYS.include?(key) }
21
19
  logger = Logger.new(STDOUT)
22
20
  @queue = arguments.fetch(:queue, 'logstash-queue')
23
21
  @level = level
24
- @message_builder = MessageBuilder.new(service)
22
+ @service_name = service_name
25
23
  @freddy = Freddy.build(logger, config.merge({recover_from_connection_close: true}))
26
24
  end
27
25
 
28
26
  def log(level, metadata = {})
29
27
  if meets_threshold?(level)
30
- message = @message_builder.build_message metadata, level
28
+ message = Utils.build_event(metadata, level, @service_name)
31
29
  deliver_message message
32
30
  end
33
31
  end
@@ -0,0 +1,35 @@
1
+ require 'socket'
2
+
3
+ class Logasm
4
+ module Utils
5
+ HOST = ::Socket.gethostname
6
+
7
+ # Build logstash json compatible event
8
+ #
9
+ # @param [Hash] metadata
10
+ # @param [#to_s] level
11
+ # @param [String] service_name
12
+ #
13
+ # @return [Hash]
14
+ def self.build_event(metadata, level, service_name)
15
+ metadata.merge(
16
+ application: application_name(service_name),
17
+ level: level.to_s.downcase,
18
+ host: HOST,
19
+ :@timestamp => Time.now.utc.iso8601(3)
20
+ )
21
+ end
22
+
23
+ # Return application name
24
+ #
25
+ # Returns lower snake case application name. This allows the
26
+ # application value to be used in the elasticsearch index name.
27
+ #
28
+ # @param [String] service_name
29
+ #
30
+ # @return [String]
31
+ def self.application_name(service_name)
32
+ Inflecto.underscore(service_name)
33
+ end
34
+ end
35
+ end
data/lib/logasm.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'inflecto'
2
2
  require 'logger'
3
+ require 'json'
3
4
  require_relative 'logasm/adapters'
5
+ require_relative 'logasm/utils'
4
6
 
5
7
  class Logasm
6
8
  def self.build(service_name, loggers_config)
data/logasm.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "logasm"
7
- gem.version = '0.2.1'
7
+ gem.version = '0.2.2'
8
8
  gem.authors = ["Salemove"]
9
9
  gem.email = ["support@salemove.com"]
10
10
  gem.description = %q{It's logasmic}
@@ -16,7 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
17
  gem.require_paths = ["lib"]
18
18
 
19
- gem.add_dependency 'logstash-event', '~> 1.2'
20
19
  gem.add_dependency 'inflecto'
21
20
 
22
21
  gem.add_development_dependency "bundler", "~> 1.3"
@@ -2,37 +2,19 @@ require 'spec_helper'
2
2
  require_relative '../../../lib/logasm/adapters/logstash_adapter/formatter'
3
3
 
4
4
  describe Logasm::Adapters::LogstashAdapter::Formatter do
5
- subject(:event) { JSON.parse(formatter.call(severity, time, nil, message)) }
5
+ subject(:event) { formatter.call(severity, Time.now, nil, message) }
6
6
 
7
- let(:formatter) { described_class.new(service_name) }
7
+ let(:formatter) { described_class.new(service_name) }
8
8
  let(:service_name) { 'test_service' }
9
- let(:severity) { 'INFO' }
10
- let(:time) { Time.now }
11
- let(:message) { {} }
12
-
13
- context 'when service name is present' do
14
- it 'includes it in the event as application' do
15
- expect(event['application']).to eq('test_service')
16
- end
17
- end
18
-
19
- context 'when service name is not present' do
20
- let(:service_name) { nil }
21
-
22
- it 'includes does not include the application key' do
23
- expect(event).to_not have_key('application')
24
- end
25
- end
26
-
27
- it 'includes severity as lowercase level' do
28
- expect(event['level']).to eq('info')
29
- end
30
-
31
- it 'includes timestamp' do
32
- expect(event['@timestamp']).to match(/\d{4}.*/)
33
- end
34
-
35
- it 'includes the host' do
36
- expect(event['host']).to be_a(String)
9
+ let(:severity) { 'INFO' }
10
+ let(:message) { {x: 'y'} }
11
+
12
+ it 'returns correct json' do
13
+ hash = JSON.parse(subject)
14
+ expect(hash['x']).to eq('y')
15
+ expect(hash['application']).to eq('test_service')
16
+ expect(hash['level']).to eq('info')
17
+ expect(hash['host']).to be_a(String)
18
+ expect(hash['@timestamp']).to match(/\d{4}-\d{2}-\d{2}T.*/)
37
19
  end
38
20
  end
@@ -2,21 +2,33 @@ require 'spec_helper'
2
2
  require_relative '../../lib/logasm/adapters/rabbitmq_adapter'
3
3
 
4
4
  describe Logasm::Adapters::RabbitmqAdapter do
5
- let(:adapter) { described_class.new(log_level, nil, { host: 'localhost',
6
- user: 'guest',
7
- pass: 'guest',
8
- port: '5672' }
9
- ) }
5
+ let(:adapter) do
6
+ described_class.new(log_level, 'TestService', {
7
+ host: 'localhost', user: 'guest', pass: 'guest', port: '5672'
8
+ })
9
+ end
10
10
  let(:freddy) { adapter.freddy }
11
11
 
12
+ before do
13
+ allow(Freddy).to receive(:build) { FreddyMock.new }
14
+ end
15
+
12
16
  describe '#log' do
13
17
  context 'when logging a message' do
14
18
  let(:log_level) { 0 }
15
19
 
16
20
  it 'delegates to freddy' do
17
- expect(freddy).to receive(:deliver)
18
-
19
21
  adapter.log :info, message: 'test'
22
+
23
+ expect(freddy.deliveries.size).to eq(1)
24
+
25
+ queue, event = freddy.deliveries[0]
26
+ expect(queue).to eq('logstash-queue')
27
+ expect(event[:message]).to eq('test')
28
+ expect(event[:application]).to eq('test_service')
29
+ expect(event[:level]).to eq('info')
30
+ expect(event[:host]).to be_a(String)
31
+ expect(event[:@timestamp]).to match(/\d{4}-\d{2}-\d{2}T.*/)
20
32
  end
21
33
  end
22
34
 
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,14 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'pry'
4
+ Bundler.setup
5
+
6
+ require 'logasm'
7
+
8
+ Dir[File.dirname(__FILE__) + '/support/*.rb'].each {|f| require f}
9
+
1
10
  RSpec.configure do |config|
2
11
  config.run_all_when_everything_filtered = true
3
12
  config.filter_run :focus
4
13
  config.order = 'random'
5
14
  end
6
-
7
- require 'pry'
8
- require 'securerandom'
9
-
10
- require 'logasm'
@@ -0,0 +1,11 @@
1
+ class FreddyMock
2
+ attr_reader :deliveries
3
+
4
+ def initialize
5
+ @deliveries = []
6
+ end
7
+
8
+ def deliver(queue, params)
9
+ @deliveries << [queue, params]
10
+ end
11
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Logasm::Utils do
4
+ describe '.build_event' do
5
+ subject(:event) { described_class.build_event(metadata, level, service_name) }
6
+
7
+ let(:service_name) { 'test_service' }
8
+ let(:level) { 'INFO' }
9
+ let(:metadata) { {x: 'y'} }
10
+
11
+ context 'when service name is in correct format' do
12
+ it 'includes it in the event as application' do
13
+ expect(event[:application]).to eq('test_service')
14
+ end
15
+ end
16
+
17
+ context 'when service name is in camelcase' do
18
+ let(:service_name) { 'InformationService' }
19
+
20
+ it 'includes it in the event as lower snake case' do
21
+ expect(event[:application]).to eq('information_service')
22
+ end
23
+ end
24
+
25
+ it 'includes level as a lower case string' do
26
+ expect(event[:level]).to eq('info')
27
+ end
28
+
29
+ it 'includes timestamp' do
30
+ expect(event[:@timestamp]).to match(/\d{4}.*/)
31
+ end
32
+
33
+ it 'includes the host' do
34
+ expect(event[:host]).to be_a(String)
35
+ end
36
+ end
37
+ end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salemove
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-31 00:00:00.000000000 Z
11
+ date: 2015-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: logstash-event
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.2'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.2'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: inflecto
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -100,16 +86,17 @@ files:
100
86
  - lib/logasm/adapters/logstash_adapter.rb
101
87
  - lib/logasm/adapters/logstash_adapter/formatter.rb
102
88
  - lib/logasm/adapters/rabbitmq_adapter.rb
103
- - lib/logasm/adapters/rabbitmq_adapter/message_builder.rb
104
89
  - lib/logasm/adapters/stdout_adapter.rb
90
+ - lib/logasm/utils.rb
105
91
  - logasm.gemspec
106
92
  - spec/adapters/logstash_adapter/formatter_spec.rb
107
93
  - spec/adapters/logstash_adapter_spec.rb
108
- - spec/adapters/rabbitmq_adapter/message_builder_spec.rb
109
94
  - spec/adapters/rabbitmq_adapter_spec.rb
110
95
  - spec/adapters/stdout_adapter_spec.rb
111
96
  - spec/logasm_spec.rb
112
97
  - spec/spec_helper.rb
98
+ - spec/support/freddy_mock.rb
99
+ - spec/utils_spec.rb
113
100
  homepage:
114
101
  licenses:
115
102
  - MIT
@@ -130,15 +117,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
117
  version: '0'
131
118
  requirements: []
132
119
  rubyforge_project:
133
- rubygems_version: 2.4.3
120
+ rubygems_version: 2.4.5.1
134
121
  signing_key:
135
122
  specification_version: 4
136
123
  summary: What description said
137
124
  test_files:
138
125
  - spec/adapters/logstash_adapter/formatter_spec.rb
139
126
  - spec/adapters/logstash_adapter_spec.rb
140
- - spec/adapters/rabbitmq_adapter/message_builder_spec.rb
141
127
  - spec/adapters/rabbitmq_adapter_spec.rb
142
128
  - spec/adapters/stdout_adapter_spec.rb
143
129
  - spec/logasm_spec.rb
144
130
  - spec/spec_helper.rb
131
+ - spec/support/freddy_mock.rb
132
+ - spec/utils_spec.rb
@@ -1,22 +0,0 @@
1
- require 'socket'
2
-
3
- class Logasm
4
- module Adapters
5
- class RabbitmqAdapter
6
- class MessageBuilder
7
- HOST = ::Socket.gethostname
8
-
9
- def initialize(service_name)
10
- @service_name = service_name
11
- end
12
-
13
- def build_message(metadata, level)
14
- metadata.merge({ application: @service_name,
15
- level: level,
16
- host: HOST,
17
- :@timestamp => Time.now.utc.iso8601(3) })
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
- require_relative '../../../lib/logasm/adapters/rabbitmq_adapter/message_builder'
3
-
4
- describe Logasm::Adapters::RabbitmqAdapter::MessageBuilder do
5
- subject { message_builder.build_message(metadata, level) }
6
-
7
- let(:message_builder) { described_class.new(service_name) }
8
- let(:service_name) { 'test_service' }
9
- let(:level) { :info }
10
- let(:metadata) { {message: 'test'} }
11
-
12
- it 'adds necessary arguments' do
13
- expect(subject[:message]).to eq(metadata[:message])
14
- expect(subject[:application]).to eq(service_name)
15
- expect(subject[:level]).to eq(level)
16
- expect(subject).to have_key(:host)
17
- expect(subject).to have_key(:@timestamp)
18
- end
19
- end