eventhub-components 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cbba5765cb10c12c223815b1934db8b5e1d9e172
4
+ data.tar.gz: e6b43f9c39845f88e426445f9ef3a80e384f6509
5
+ SHA512:
6
+ metadata.gz: 8c6e9699a28d8927b8fb25f2b7f49712b13a8d11510c3bfd2bdddf0ab4a100812695b6edcac842472e7784ae48779da1727e313ce3e3d9cae4d3a604ec9f5d82
7
+ data.tar.gz: 7e74d8599751dc01b6df9e7463fc39a8cc8050105cd39348cdbb2a4a199620ba642e16f31ea3abf5bab8185e3272ee249693c094c20adc44fdbed51f438278b3
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in eventhub-components.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Pascal Betz
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,38 @@
1
+ # Eventhub::Components
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'eventhub-components'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install eventhub-components
18
+
19
+ ## Usage
20
+
21
+ This Gem provides shared code for processors and the console app.
22
+
23
+ ### Logger
24
+
25
+ Provides a second argument (a hash) to the log methods (debug, info, ...)
26
+
27
+ ''''
28
+ logger = Eventhub::Components::Logger.new(some_other_logger, 'app_name' => 'my fancy app', 'env' => 'staging')
29
+ logger.info("my message", :foo => 1, :bar => 2)
30
+ ''''
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( https://github.com/[my-github-username]/eventhub-components/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'eventhub/components/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "eventhub-components"
8
+ spec.version = Eventhub::Components::VERSION
9
+ spec.authors = ["Novartis"]
10
+ spec.email = ["pascal.betz@simplificator.com"]
11
+ spec.summary = %q{Eventhub Components utilities}
12
+ spec.description = %q{Eventhub Components utilities}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec', "~> 3.1.0"
24
+ end
@@ -0,0 +1,8 @@
1
+ module Eventhub
2
+ module Components
3
+ end
4
+ end
5
+
6
+ require_relative "components/version"
7
+ require_relative "components/logger"
8
+
@@ -0,0 +1,74 @@
1
+ require 'socket'
2
+ require 'thread'
3
+
4
+ # A wrapper for loggers to enrich the log message with structured data (a Hash).
5
+ # All methods besides debug/info/warn/error/fatal are forwarded to the target by the means of method_missing.
6
+ class Eventhub::Components::Logger < BasicObject
7
+ SEVERITY_DEBUG = 7
8
+ SEVERITY_INFO = 6
9
+ SEVERITY_WARNING = 4
10
+ SEVERITY_ERROR = 3
11
+ SEVERITY_FATAL = 0
12
+
13
+ attr_reader :target, :options
14
+ # Create a new Logger
15
+ # target: an onject which behaves like a ruby logger.
16
+ # options (keys are Strings):
17
+ # * app_name: String, mandatory
18
+ # * env: String, mandatory
19
+ # * pid: Number, optional, defaults to Process.pid
20
+ # * hostname, String, optional, defaults to Socket.gethostname
21
+ # additional keys are possible and will be sent along. The keys :data and :message are reserved and must not be used.
22
+ #
23
+ def initialize(target, options)
24
+ @target = target
25
+ @options = options
26
+ @options['pid'] ||= ::Process.pid
27
+ @options['hostname'] ||= ::Socket.gethostname
28
+ verify_options!
29
+ end
30
+
31
+ def debug(message, structured_data = nil)
32
+ target.debug(build_message(SEVERITY_DEBUG, message, structured_data))
33
+ end
34
+
35
+ def info(message, structured_data = nil)
36
+ target.info(build_message(SEVERITY_INFO, message, structured_data))
37
+ end
38
+
39
+ def warn(message, structured_data = nil)
40
+ target.warn(build_message(SEVERITY_WARNING, message, structured_data))
41
+ end
42
+
43
+ def error(message, structured_data = nil)
44
+ target.error(build_message(SEVERITY_ERROR, message, structured_data))
45
+ end
46
+
47
+ # fatal <=> emergeny <=> alert
48
+ def fatal(message, structured_data = nil)
49
+ target.fatal(build_message(SEVERITY_FATAL, message, structured_data))
50
+ end
51
+
52
+ private
53
+
54
+ def method_missing(method, *args, &block)
55
+ target.send(method, *args, &block)
56
+ end
57
+
58
+ def build_message(severity, message, structured_data)
59
+ options.merge({
60
+ 'severity' => severity,
61
+ 'data' => structured_data,
62
+ 'message' => message
63
+ })
64
+ end
65
+
66
+ def verify_options!
67
+ raise ::ArgumentError.new('target must not be nil') if target.nil?
68
+ raise ::ArgumentError.new('data is a reserved key') if options.has_key?('data')
69
+ raise ::ArgumentError.new('message is a reserved key') if options.has_key?('message')
70
+ raise ::ArgumentError.new('app_name is required') if !options['app_name']
71
+ raise ::ArgumentError.new('env is required') if !options['env']
72
+ end
73
+
74
+ end
@@ -0,0 +1,5 @@
1
+ module Eventhub
2
+ module Components
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Eventhub
2
+ module Components
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,81 @@
1
+ require_relative '../spec_helper'
2
+
3
+ RSpec.describe Eventhub::Components::Logger do
4
+ context 'initalize' do
5
+ it 'no raise when all required arguments are given' do
6
+ expect {
7
+ Eventhub::Components::Logger.new("something not nil", 'app_name' => 'an app', 'env' => 'test')
8
+ }.to_not raise_error
9
+ end
10
+ it 'requires app_name' do
11
+ expect {
12
+ Eventhub::Components::Logger.new("something not nil", 'env' => 'test')
13
+ }.to raise_error(ArgumentError)
14
+ end
15
+ it 'requires app_name' do
16
+ expect {
17
+ Eventhub::Components::Logger.new("something not nil", 'app_name' => 'an app')
18
+ }.to raise_error(ArgumentError)
19
+ end
20
+
21
+ it 'adds the default options' do
22
+ logger = Eventhub::Components::Logger.new("something not nil", 'app_name' => 'an app', 'env' => 'test')
23
+ expect(logger.options['pid']).to eq(::Process.pid)
24
+ expect(logger.options['hostname']).to eq(::Socket.gethostname)
25
+ end
26
+ end
27
+
28
+ context 'proxy' do
29
+ let(:logger) do
30
+ # fake logger that stores a log message
31
+ # in hash
32
+ fake_logger = Hash.new
33
+ def l.debug(message); self[:debug] = message; end
34
+ def l.info(message); self[:info] = message; end
35
+ def l.warn(message); self[:warn] = message; end
36
+ def l.error(message); self[:error] = message; end
37
+ def l.fatal(message); self[:fatal] = message; end
38
+ fake_logger
39
+ end
40
+ subject {
41
+ Eventhub::Components::Logger.new(logger, 'app_name' => 'an app', 'env' => 'test')
42
+ }
43
+
44
+ it 'forwards calls to the target when not related to log methods' do
45
+ expect(subject.class).to eq(logger.class)
46
+ end
47
+
48
+ it 'enriches and forwards debug calls' do
49
+ subject.debug("my message debug", {'some' => 'debug'})
50
+ expectation = {'app_name' => 'an app', 'data' => {'some' => 'debug'}, 'env' => 'test', 'hostname' => Socket.gethostname, 'message' => 'my message debug', 'pid' => Process.pid, 'severity' => 7}
51
+ expect(logger[:debug]).to eq(expectation)
52
+ end
53
+
54
+ it 'enriches and forwards info calls' do
55
+ subject.info("my message info", {'some' => 'info'})
56
+ expectation = {'app_name' => 'an app', 'data' => {'some' => 'info'}, 'env' => 'test', 'hostname' => Socket.gethostname, 'message' => 'my message info', 'pid' => Process.pid, 'severity' => 6}
57
+ expect(logger[:info]).to eq(expectation)
58
+ end
59
+
60
+ it 'enriches and forwards warn calls' do
61
+ subject.warn("my message warn", {'some' => 'warn'})
62
+ expectation = {'app_name' => 'an app', 'data' => {'some' => 'warn'}, 'env' => 'test', 'hostname' => Socket.gethostname, 'message' => 'my message warn', 'pid' => Process.pid, 'severity' => 4}
63
+ expect(logger[:warn]).to eq(expectation)
64
+ end
65
+
66
+ it 'enriches and forwards error calls' do
67
+ subject.error("my message error", {'some' => 'error'})
68
+ expectation = {'app_name' => 'an app', 'data' => {'some' => 'error'}, 'env' => 'test', 'hostname' => Socket.gethostname, 'message' => 'my message error', 'pid' => Process.pid, 'severity' => 3}
69
+ expect(logger[:error]).to eq(expectation)
70
+ end
71
+
72
+ it 'enriches and forwards fatal calls' do
73
+ subject.fatal("my message fatal", {'some' => 'fatal'})
74
+ expectation = {'app_name' => 'an app', 'data' => {'some' => 'fatal'}, 'env' => 'test', 'hostname' => Socket.gethostname, 'message' => 'my message fatal', 'pid' => Process.pid, 'severity' => 0}
75
+ expect(logger[:fatal]).to eq(expectation)
76
+ end
77
+
78
+ end
79
+
80
+
81
+ end
@@ -0,0 +1,9 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+ require 'rspec'
4
+ require_relative '../lib/eventhub/components'
5
+
6
+ RSpec.configure do |config|
7
+ #config.mock_with :rspec
8
+ end
9
+
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eventhub-components
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Novartis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.0
55
+ description: Eventhub Components utilities
56
+ email:
57
+ - pascal.betz@simplificator.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - eventhub-components.gemspec
68
+ - lib/eventhub/components.rb
69
+ - lib/eventhub/components/logger.rb
70
+ - lib/eventhub/components/version.rb
71
+ - lib/eventhub/version.rb
72
+ - spec/components/logger_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Eventhub Components utilities
98
+ test_files:
99
+ - spec/components/logger_spec.rb
100
+ - spec/spec_helper.rb