eventhub-components 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/cd.yml +32 -0
- data/.github/workflows/ci.yml +52 -0
- data/.gitignore +1 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +1 -1
- data/README.md +12 -2
- data/Rakefile +7 -4
- data/eventhub-components.gemspec +20 -19
- data/lib/eventhub/components/exception_writer.rb +12 -20
- data/lib/eventhub/components/log_formatter.rb +1 -1
- data/lib/eventhub/components/logger.rb +4 -5
- data/lib/eventhub/components/multi_logger.rb +9 -9
- data/lib/eventhub/components/pid_file.rb +6 -10
- data/lib/eventhub/components/structured_data_logger.rb +15 -14
- data/lib/eventhub/components/version.rb +1 -1
- data/lib/eventhub/components.rb +3 -4
- data/spec/components/exception_writer_spec.rb +12 -14
- data/spec/components/multi_logger_spec.rb +39 -26
- data/spec/components/pid_file_spec.rb +15 -15
- data/spec/components/structured_data_logger_spec.rb +50 -38
- data/spec/spec_helper.rb +6 -5
- metadata +41 -17
- data/.travis.yml +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 75c13a6d27b15f58b9c88d3dd73b1496d05de7330fd542b71e65d9bda69d8d30
|
4
|
+
data.tar.gz: 2dde6f21b96c326d7ee0861f939f1536a7e27114eaa56e2d6943dd7e5081766b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63e3bb6c10993627d2467ed62ca557cd20c2f507a46e1ba10fb685f4abadcd43d49ac923199e43d0a323cb0445ae190dfeca2ee6bce16599b942b3ff55076eb6
|
7
|
+
data.tar.gz: b6f302e90cc517109683195c795bc54c24b9019769eead8719ffd0a4c73e1c83f63f49bf640fa3db33e84b37f9e63fcb67de09250cfeaea4b834b9956ce09521
|
@@ -0,0 +1,32 @@
|
|
1
|
+
name: cd
|
2
|
+
|
3
|
+
on:
|
4
|
+
workflow_dispatch:
|
5
|
+
|
6
|
+
jobs:
|
7
|
+
|
8
|
+
build:
|
9
|
+
runs-on: ubuntu-latest
|
10
|
+
|
11
|
+
steps:
|
12
|
+
- name: Checkout current code
|
13
|
+
uses: actions/checkout@v2
|
14
|
+
|
15
|
+
- name: Set up Ruby
|
16
|
+
uses: ruby/setup-ruby@v1
|
17
|
+
with:
|
18
|
+
ruby-version: '3.1'
|
19
|
+
bundler-cache: true
|
20
|
+
cache-version: 1
|
21
|
+
|
22
|
+
- name: Push to Rubygems
|
23
|
+
env:
|
24
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
25
|
+
run: |
|
26
|
+
git config --global user.email "thomas.steiner@ikey.ch"
|
27
|
+
git config --global user.name "thomis"
|
28
|
+
mkdir ~/.gem
|
29
|
+
echo -e "---\n:rubygems_api_key: ${{ secrets.RUBYGEMS_API_KEY }}" > ~/.gem/credentials
|
30
|
+
chmod 600 ~/.gem/credentials
|
31
|
+
bundle exec rake release
|
32
|
+
rm ~/.gem/credentials
|
@@ -0,0 +1,52 @@
|
|
1
|
+
name: ci
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- "*"
|
7
|
+
|
8
|
+
pull_request:
|
9
|
+
branches:
|
10
|
+
- "*"
|
11
|
+
|
12
|
+
schedule:
|
13
|
+
- cron: 0 2 * * *
|
14
|
+
|
15
|
+
# Allows you to run this workflow manually from the Actions tab
|
16
|
+
workflow_dispatch:
|
17
|
+
|
18
|
+
jobs:
|
19
|
+
build:
|
20
|
+
runs-on: ubuntu-latest
|
21
|
+
env:
|
22
|
+
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
|
23
|
+
strategy:
|
24
|
+
fail-fast: false
|
25
|
+
matrix:
|
26
|
+
ruby: [ '3.1', '3.0', '2.7', '2.6']
|
27
|
+
|
28
|
+
name: Ruby ${{ matrix.ruby }}
|
29
|
+
steps:
|
30
|
+
- uses: actions/checkout@v1
|
31
|
+
|
32
|
+
- name: Set up Ruby
|
33
|
+
uses: ruby/setup-ruby@v1
|
34
|
+
with:
|
35
|
+
ruby-version: ${{ matrix.ruby }}
|
36
|
+
bundler: latest
|
37
|
+
bundler-cache: true
|
38
|
+
cache-version: 1
|
39
|
+
|
40
|
+
- name: Code Climate setup test reporter
|
41
|
+
run: |
|
42
|
+
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
43
|
+
chmod +x ./cc-test-reporter
|
44
|
+
./cc-test-reporter before-build
|
45
|
+
|
46
|
+
- name: Run default task
|
47
|
+
run: |
|
48
|
+
bundle exec rake
|
49
|
+
|
50
|
+
- name: Code Climate publish test coverage
|
51
|
+
run: |
|
52
|
+
./cc-test-reporter after-build
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,11 +1,21 @@
|
|
1
|
-
[![
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/eventhub-components.svg)](https://badge.fury.io/rb/eventhub-components)
|
2
2
|
[![Maintainability](https://api.codeclimate.com/v1/badges/3f6c9e1a8d32388df0fa/maintainability)](https://codeclimate.com/github/thomis/eventhub-components/maintainability)
|
3
|
-
[![
|
3
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/3f6c9e1a8d32388df0fa/test_coverage)](https://codeclimate.com/github/thomis/eventhub-components/test_coverage)
|
4
|
+
[![ci](https://github.com/thomis/eventhub-components/actions/workflows/ci.yml/badge.svg)](https://github.com/thomis/eventhub-components/actions/workflows/ci.yml)
|
4
5
|
|
5
6
|
# EventHub::Components
|
6
7
|
|
7
8
|
Incldues logging, exception writing and pid file facilities for event hub processors.
|
8
9
|
|
10
|
+
## Supported Ruby Versions
|
11
|
+
|
12
|
+
Currently supported and tested ruby versions are:
|
13
|
+
|
14
|
+
- 3.1
|
15
|
+
- 3.0
|
16
|
+
- 2.7
|
17
|
+
- 2.6
|
18
|
+
|
9
19
|
## Installation
|
10
20
|
|
11
21
|
Add this line to your application's Gemfile:
|
data/Rakefile
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require "standard/rake"
|
3
4
|
|
4
|
-
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
6
|
+
t.verbose = false
|
7
|
+
end
|
5
8
|
|
6
|
-
task default: :spec
|
9
|
+
task default: [:spec, :standard]
|
data/eventhub-components.gemspec
CHANGED
@@ -1,25 +1,26 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
3
|
+
require "eventhub/components/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name
|
8
|
-
spec.version
|
9
|
-
spec.authors
|
10
|
-
spec.email
|
11
|
-
spec.summary
|
12
|
-
spec.description
|
13
|
-
spec.homepage
|
14
|
-
spec.license
|
6
|
+
spec.name = "eventhub-components"
|
7
|
+
spec.version = EventHub::Components::VERSION
|
8
|
+
spec.authors = ["Steiner, Thomas"]
|
9
|
+
spec.email = ["thomas.steiner@ikey.ch"]
|
10
|
+
spec.summary = "Additional eventhub components"
|
11
|
+
spec.description = "Additional eventhub components"
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
15
14
|
|
16
|
-
spec.files
|
17
|
-
spec.executables
|
18
|
-
spec.
|
19
|
-
spec.require_paths = ['lib']
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.require_paths = ["lib"]
|
20
18
|
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.
|
19
|
+
spec.add_development_dependency "bundler", "~> 2.1"
|
20
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
21
|
+
spec.add_development_dependency "rspec", "~> 3.11"
|
22
|
+
spec.add_development_dependency "standard", "~> 1.14"
|
23
|
+
spec.add_development_dependency "simplecov", "~> 0.21"
|
24
|
+
|
25
|
+
spec.add_runtime_dependency "logstash-logger", "~> 0.26"
|
25
26
|
end
|
@@ -1,16 +1,14 @@
|
|
1
1
|
class EventHub::Components::ExceptionWriter
|
2
|
-
|
3
2
|
MAX_EXCEPTIONS_FILES = 500
|
4
3
|
|
5
4
|
attr_accessor :folder, :max_files
|
6
5
|
|
7
6
|
def initialize(base = nil, max_files = MAX_EXCEPTIONS_FILES)
|
8
|
-
base
|
9
|
-
@folder = File.join(base,
|
7
|
+
base ||= Dir.pwd
|
8
|
+
@folder = File.join(base, "exceptions")
|
10
9
|
@max_files = max_files
|
11
10
|
end
|
12
11
|
|
13
|
-
|
14
12
|
def write(exception, message = nil)
|
15
13
|
time = Time.now
|
16
14
|
stamp = "#{time.strftime("%Y%m%d_%H%M%S")}_%i" % time.usec
|
@@ -26,19 +24,16 @@ class EventHub::Components::ExceptionWriter
|
|
26
24
|
stamp
|
27
25
|
end
|
28
26
|
|
29
|
-
|
30
27
|
private
|
31
28
|
|
32
29
|
def restrict_to_max_files
|
33
|
-
exception_files = Dir.glob(File.join(folder,
|
30
|
+
exception_files = Dir.glob(File.join(folder, "*.log"))
|
34
31
|
if exception_files.size > max_files
|
35
|
-
exception_files.reverse[max_files
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
rescue
|
41
|
-
end
|
32
|
+
exception_files.reverse[max_files..].each do |file|
|
33
|
+
File.delete(file)
|
34
|
+
raw = File.join(File.dirname(file), File.basename(file, ".*"), ".msg.raw")
|
35
|
+
File.delete(raw)
|
36
|
+
rescue
|
42
37
|
end
|
43
38
|
end
|
44
39
|
end
|
@@ -46,19 +41,16 @@ class EventHub::Components::ExceptionWriter
|
|
46
41
|
def write_exception(filename, exception)
|
47
42
|
File.open("#{folder}/#{filename}", "w") do |output|
|
48
43
|
output.write("#{exception}\n\n")
|
49
|
-
output.write("Exception: #{exception.class
|
44
|
+
output.write("Exception: #{exception.class}\n\n")
|
50
45
|
output.write("Call Stack:\n")
|
51
|
-
exception.backtrace
|
46
|
+
exception.backtrace&.each do |line|
|
52
47
|
output.write("#{line}\n")
|
53
|
-
end
|
48
|
+
end
|
54
49
|
end
|
55
50
|
end
|
56
51
|
|
57
52
|
def write_message(filename, message)
|
58
53
|
return unless message
|
59
|
-
File.
|
60
|
-
output.write(message)
|
61
|
-
end
|
54
|
+
File.binwrite("#{folder}/#{filename}", message)
|
62
55
|
end
|
63
|
-
|
64
56
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# format adaptation
|
2
2
|
class EventHub::Components::LogFormatter
|
3
3
|
def call(severity, time, progname, msg)
|
4
|
-
time_in_string = "#{time.strftime("%Y-%m-%d %H:%M:%S")}.#{"%04d" % (time.usec/100)}"
|
4
|
+
time_in_string = "#{time.strftime("%Y-%m-%d %H:%M:%S")}.#{"%04d" % (time.usec / 100)}"
|
5
5
|
[time_in_string, Process.pid, severity, msg].join("\t") + "\n"
|
6
6
|
end
|
7
7
|
end
|
@@ -1,17 +1,16 @@
|
|
1
1
|
class EventHub::Components::Logger
|
2
|
-
|
3
2
|
def self.stdout
|
4
|
-
logger = Logger.new(
|
3
|
+
logger = Logger.new($stdout)
|
5
4
|
logger.formatter = proc do |severity, time, progname, msg|
|
6
|
-
time_in_string = "#{time.strftime("%Y-%m-%d %H:%M:%S")}.#{"%04d" % (time.usec/100)}"
|
5
|
+
time_in_string = "#{time.strftime("%Y-%m-%d %H:%M:%S")}.#{"%04d" % (time.usec / 100)}"
|
7
6
|
"#{time_in_string}: #{"%10s" % severity} - #{msg}\n"
|
8
7
|
end
|
9
8
|
logger
|
10
9
|
end
|
11
10
|
|
12
11
|
def self.logstash(processor_name, environment)
|
13
|
-
#configure logstash with custom fields
|
14
|
-
|
12
|
+
# configure logstash with custom fields
|
13
|
+
LogStashLogger.configure do |config|
|
15
14
|
config.customize_event do |event|
|
16
15
|
event["app_name"] = processor_name
|
17
16
|
event["env"] = environment
|
@@ -1,8 +1,7 @@
|
|
1
1
|
class EventHub::Components::MultiLogger
|
2
|
-
|
3
2
|
attr_accessor :devices
|
4
3
|
|
5
|
-
def initialize(folder=nil)
|
4
|
+
def initialize(folder = nil)
|
6
5
|
@devices = []
|
7
6
|
end
|
8
7
|
|
@@ -12,17 +11,18 @@ class EventHub::Components::MultiLogger
|
|
12
11
|
self
|
13
12
|
end
|
14
13
|
|
15
|
-
|
16
14
|
private
|
17
15
|
|
18
16
|
def method_missing(method, *args, &block)
|
19
17
|
devices.map do |target|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
e
|
25
|
-
end
|
18
|
+
target.send(method, *args, &block)
|
19
|
+
rescue => e
|
20
|
+
warn "WARNING: Could not call #{method} in #{target} with #{args} because of #{e.message}"
|
21
|
+
e
|
26
22
|
end
|
27
23
|
end
|
24
|
+
|
25
|
+
def respond_to_missing?(method)
|
26
|
+
true
|
27
|
+
end
|
28
28
|
end
|
@@ -13,19 +13,15 @@ class EventHub::Components::Pidfile
|
|
13
13
|
|
14
14
|
# Try to delete file, ignore all errors
|
15
15
|
def delete
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
# ignore
|
20
|
-
end
|
16
|
+
File.delete(file_name)
|
17
|
+
rescue
|
18
|
+
# ignore
|
21
19
|
end
|
22
20
|
|
23
21
|
# Read the PID from the file
|
24
22
|
def read
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
# ignore, will return nil
|
29
|
-
end
|
23
|
+
File.read(file_name)
|
24
|
+
rescue Errno::ENOENT
|
25
|
+
# ignore, will return nil
|
30
26
|
end
|
31
27
|
end
|
@@ -1,6 +1,4 @@
|
|
1
|
-
require
|
2
|
-
require 'thread'
|
3
|
-
|
1
|
+
require "socket"
|
4
2
|
# A wrapper for loggers to enrich the log message with structured data (a Hash).
|
5
3
|
# All methods besides debug/info/warn/error/fatal are forwarded to the target by the means of method_missing.
|
6
4
|
class EventHub::Components::StructuredDataLogger
|
@@ -23,8 +21,8 @@ class EventHub::Components::StructuredDataLogger
|
|
23
21
|
def initialize(target, options)
|
24
22
|
@target = target
|
25
23
|
@options = options
|
26
|
-
@options[
|
27
|
-
@options[
|
24
|
+
@options["pid"] ||= ::Process.pid
|
25
|
+
@options["hostname"] ||= ::Socket.gethostname
|
28
26
|
verify_options!
|
29
27
|
end
|
30
28
|
|
@@ -55,20 +53,23 @@ class EventHub::Components::StructuredDataLogger
|
|
55
53
|
target.send(method, *args, &block)
|
56
54
|
end
|
57
55
|
|
56
|
+
def respond_to_missing?(method)
|
57
|
+
true
|
58
|
+
end
|
59
|
+
|
58
60
|
def build_message(severity, message, structured_data)
|
59
61
|
options.merge({
|
60
|
-
|
61
|
-
|
62
|
-
|
62
|
+
"severity" => severity,
|
63
|
+
"data" => structured_data,
|
64
|
+
"message" => message
|
63
65
|
})
|
64
66
|
end
|
65
67
|
|
66
68
|
def verify_options!
|
67
|
-
raise ::ArgumentError.new(
|
68
|
-
raise ::ArgumentError.new(
|
69
|
-
raise ::ArgumentError.new(
|
70
|
-
raise ::ArgumentError.new(
|
71
|
-
raise ::ArgumentError.new(
|
69
|
+
raise ::ArgumentError.new("target must not be nil") if target.nil?
|
70
|
+
raise ::ArgumentError.new("data is a reserved key") if options.has_key?("data")
|
71
|
+
raise ::ArgumentError.new("message is a reserved key") if options.has_key?("message")
|
72
|
+
raise ::ArgumentError.new("app_name is required") if !options["app_name"]
|
73
|
+
raise ::ArgumentError.new("env is required") if !options["env"]
|
72
74
|
end
|
73
|
-
|
74
75
|
end
|
data/lib/eventhub/components.rb
CHANGED
@@ -3,9 +3,9 @@ module EventHub
|
|
3
3
|
end
|
4
4
|
end
|
5
5
|
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
6
|
+
require "fileutils"
|
7
|
+
require "logger"
|
8
|
+
require "logstash-logger"
|
9
9
|
|
10
10
|
require_relative "components/version"
|
11
11
|
require_relative "components/log_formatter"
|
@@ -14,4 +14,3 @@ require_relative "components/multi_logger"
|
|
14
14
|
require_relative "components/structured_data_logger"
|
15
15
|
require_relative "components/exception_writer"
|
16
16
|
require_relative "components/pid_file"
|
17
|
-
|
@@ -1,8 +1,7 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "../spec_helper"
|
2
2
|
|
3
3
|
RSpec.describe EventHub::Components::StructuredDataLogger do
|
4
|
-
context
|
5
|
-
|
4
|
+
context "logger" do
|
6
5
|
before(:each) do
|
7
6
|
remove_all_exception_files
|
8
7
|
end
|
@@ -12,28 +11,28 @@ RSpec.describe EventHub::Components::StructuredDataLogger do
|
|
12
11
|
end
|
13
12
|
|
14
13
|
let(:writer) {
|
15
|
-
EventHub::Components::ExceptionWriter.new(
|
14
|
+
EventHub::Components::ExceptionWriter.new("tmp", 5)
|
16
15
|
}
|
17
16
|
|
18
|
-
it
|
17
|
+
it "writes an exception" do
|
19
18
|
begin
|
20
19
|
raise
|
21
20
|
rescue => e
|
22
|
-
|
21
|
+
writer.write(e)
|
23
22
|
end
|
24
|
-
expect(Dir[
|
23
|
+
expect(Dir["tmp/exceptions/*"].size).to eq(1)
|
25
24
|
end
|
26
25
|
|
27
|
-
it
|
26
|
+
it "writes an exception and a message" do
|
28
27
|
begin
|
29
28
|
raise
|
30
29
|
rescue => e
|
31
|
-
|
30
|
+
writer.write(e, "hi")
|
32
31
|
end
|
33
|
-
expect(Dir[
|
32
|
+
expect(Dir["tmp/exceptions/*"].size).to eq(2)
|
34
33
|
end
|
35
34
|
|
36
|
-
it
|
35
|
+
it "limits number of files" do
|
37
36
|
begin
|
38
37
|
raise
|
39
38
|
rescue => e
|
@@ -41,13 +40,12 @@ RSpec.describe EventHub::Components::StructuredDataLogger do
|
|
41
40
|
writer.write(e)
|
42
41
|
end
|
43
42
|
end
|
44
|
-
expect(Dir[
|
43
|
+
expect(Dir["tmp/exceptions/*"].size).to eq(5)
|
45
44
|
end
|
46
|
-
|
47
45
|
end
|
48
46
|
|
49
47
|
def remove_all_exception_files
|
50
|
-
Dir[
|
48
|
+
Dir["tmp/exceptions/*"].each do |file|
|
51
49
|
File.delete(file)
|
52
50
|
end
|
53
51
|
end
|
@@ -1,47 +1,60 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "../spec_helper"
|
2
2
|
|
3
3
|
RSpec.describe EventHub::Components::StructuredDataLogger do
|
4
|
-
context
|
5
|
-
|
4
|
+
context "logger" do
|
6
5
|
let(:logger) {
|
7
6
|
EventHub::Components::MultiLogger.new
|
8
7
|
}
|
9
8
|
|
9
|
+
it "forwards calls to ALL devices" do
|
10
|
+
device_1 = ""
|
11
|
+
device_2 = ""
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
# expect(device_1).to receive(:info).and_return(1)
|
14
|
-
# device_2 = stub(:device_2)
|
15
|
-
# expect(device_2).to receive(:info).and_return(2)
|
13
|
+
allow(device_1).to receive(:info).and_return(1)
|
14
|
+
allow(device_2).to receive(:info).and_return(2)
|
16
15
|
|
17
|
-
|
18
|
-
|
16
|
+
logger.add_device(device_1)
|
17
|
+
logger.add_device(device_2)
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
result = logger.info("hans")
|
20
|
+
expect(result).to eq([1, 2])
|
21
|
+
end
|
23
22
|
|
23
|
+
it "forwards calls to ALL devices even if one raises" do
|
24
|
+
device_1 = ""
|
25
|
+
device_2 = ""
|
24
26
|
|
25
|
-
|
26
|
-
|
27
|
-
# expect(device_1).to receive(:info).and_raise
|
28
|
-
# device_2 = stub(:device_2)
|
29
|
-
# expect(device_2).to receive(:info).and_return(2)
|
27
|
+
allow(device_1).to receive(:info).and_raise
|
28
|
+
allow(device_2).to receive(:info).and_return(2)
|
30
29
|
|
31
|
-
|
32
|
-
|
30
|
+
logger.add_device(device_1)
|
31
|
+
logger.add_device(device_2)
|
33
32
|
|
34
|
-
|
35
|
-
|
33
|
+
logger.info("hans")
|
34
|
+
end
|
36
35
|
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
it "returns self after adding device" do
|
37
|
+
expect(logger.add_device("something")).to eq(logger)
|
38
|
+
end
|
40
39
|
|
41
|
-
it
|
40
|
+
it "does not allow nil as device" do
|
42
41
|
expect {
|
43
42
|
logger.add_device nil
|
44
43
|
}.to raise_error(ArgumentError)
|
45
44
|
end
|
45
|
+
|
46
|
+
it "logs to json lines" do
|
47
|
+
logger.add_device(EventHub::Components::Logger.logstash("processor", "development"))
|
48
|
+
logger.info("Yes, it works!")
|
49
|
+
expect(File.read("logs/ruby/processor.log")).to match(/Yes, it works!/)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "logs to json lines and console" do
|
53
|
+
logger.add_device(EventHub::Components::Logger.logstash("processor", "development"))
|
54
|
+
logger.add_device(EventHub::Components::Logger.stdout)
|
55
|
+
|
56
|
+
logger.info("Yes, it works2!")
|
57
|
+
expect(File.read("logs/ruby/processor.log")).to match(/Yes, it works2!/)
|
58
|
+
end
|
46
59
|
end
|
47
60
|
end
|
@@ -1,47 +1,47 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "spec_helper"
|
2
|
+
require "fileutils"
|
3
3
|
|
4
4
|
describe EventHub::Components::Pidfile do
|
5
5
|
before(:each) do
|
6
|
-
FileUtils.remove_dir(
|
6
|
+
FileUtils.remove_dir("/tmp/eventhub_pid_test", true)
|
7
7
|
end
|
8
8
|
|
9
9
|
after(:each) do
|
10
|
-
FileUtils.remove_dir(
|
10
|
+
FileUtils.remove_dir("/tmp/eventhub_pid_test", true)
|
11
11
|
end
|
12
12
|
|
13
|
-
let(:pidfile) {
|
13
|
+
let(:pidfile) { EventHub::Components::Pidfile.new("/tmp/eventhub_pid_test/some.pid") }
|
14
14
|
|
15
|
-
it
|
15
|
+
it "creates the folders if not existing" do
|
16
16
|
pidfile.write(1234)
|
17
|
-
expect(File.directory?(
|
17
|
+
expect(File.directory?("/tmp/eventhub_pid_test")).to be true
|
18
18
|
end
|
19
19
|
|
20
|
-
it
|
20
|
+
it "writes the content to the file" do
|
21
21
|
pidfile.write(1234)
|
22
|
-
expect(IO.read(
|
22
|
+
expect(IO.read("/tmp/eventhub_pid_test/some.pid")).to eq("1234")
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
25
|
+
it "deletes the file" do
|
26
26
|
pidfile.write(1234)
|
27
27
|
pidfile.delete
|
28
|
-
expect(File.file?(
|
28
|
+
expect(File.file?("/tmp/eventhub_pid_test/some.pid")).to be false
|
29
29
|
end
|
30
30
|
|
31
|
-
it
|
31
|
+
it "does not choke when deleting a non-existing pid file" do
|
32
32
|
pidfile.delete
|
33
33
|
end
|
34
34
|
|
35
|
-
it
|
35
|
+
it "does not choke when reading a non-existing pid file" do
|
36
36
|
expect(pidfile.read).to eq(nil)
|
37
37
|
end
|
38
38
|
|
39
|
-
it
|
39
|
+
it "reads the pid written to the file" do
|
40
40
|
pidfile.write(1234)
|
41
41
|
expect(pidfile.read).to eq("1234")
|
42
42
|
end
|
43
43
|
|
44
|
-
it
|
44
|
+
it "it writes the currents process pid as default" do
|
45
45
|
pidfile.write
|
46
46
|
expect(pidfile.read).to eq(Process.pid.to_s)
|
47
47
|
end
|
@@ -1,77 +1,89 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "../spec_helper"
|
2
2
|
|
3
3
|
RSpec.describe EventHub::Components::StructuredDataLogger do
|
4
|
-
context
|
5
|
-
it
|
4
|
+
context "initalize" do
|
5
|
+
it "no raise when all required arguments are given" do
|
6
6
|
expect {
|
7
|
-
EventHub::Components::StructuredDataLogger.new("something not nil",
|
7
|
+
EventHub::Components::StructuredDataLogger.new("something not nil", "app_name" => "an app", "env" => "test")
|
8
8
|
}.to_not raise_error
|
9
9
|
end
|
10
|
-
it
|
10
|
+
it "requires app_name" do
|
11
11
|
expect {
|
12
|
-
EventHub::Components::StructuredDataLogger.new("something not nil",
|
12
|
+
EventHub::Components::StructuredDataLogger.new("something not nil", "env" => "test")
|
13
13
|
}.to raise_error(ArgumentError)
|
14
14
|
end
|
15
|
-
it
|
15
|
+
it "requires app_name" do
|
16
16
|
expect {
|
17
|
-
EventHub::Components::StructuredDataLogger.new("something not nil",
|
17
|
+
EventHub::Components::StructuredDataLogger.new("something not nil", "app_name" => "an app")
|
18
18
|
}.to raise_error(ArgumentError)
|
19
19
|
end
|
20
20
|
|
21
|
-
it
|
22
|
-
logger = EventHub::Components::StructuredDataLogger.new("something not nil",
|
23
|
-
expect(logger.options[
|
24
|
-
expect(logger.options[
|
21
|
+
it "adds the default options" do
|
22
|
+
logger = EventHub::Components::StructuredDataLogger.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
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
context
|
28
|
+
context "proxy" do
|
29
29
|
let(:logger) do
|
30
30
|
# fake logger that stores a log message
|
31
31
|
# in hash
|
32
|
-
fake_logger =
|
33
|
-
def fake_logger.debug(message)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
def fake_logger.
|
32
|
+
fake_logger = {}
|
33
|
+
def fake_logger.debug(message)
|
34
|
+
self[:debug] = message
|
35
|
+
end
|
36
|
+
|
37
|
+
def fake_logger.info(message)
|
38
|
+
self[:info] = message
|
39
|
+
end
|
40
|
+
|
41
|
+
def fake_logger.warn(message)
|
42
|
+
self[:warn] = message
|
43
|
+
end
|
44
|
+
|
45
|
+
def fake_logger.error(message)
|
46
|
+
self[:error] = message
|
47
|
+
end
|
48
|
+
|
49
|
+
def fake_logger.fatal(message)
|
50
|
+
self[:fatal] = message
|
51
|
+
end
|
52
|
+
|
38
53
|
fake_logger
|
39
54
|
end
|
40
55
|
subject {
|
41
|
-
EventHub::Components::StructuredDataLogger.new(logger,
|
56
|
+
EventHub::Components::StructuredDataLogger.new(logger, "app_name" => "an app", "env" => "test")
|
42
57
|
}
|
43
58
|
|
44
|
-
it
|
45
|
-
subject.debug("my message debug", {
|
46
|
-
expectation = {
|
59
|
+
it "enriches and forwards debug calls" do
|
60
|
+
subject.debug("my message debug", {"some" => "debug"})
|
61
|
+
expectation = {"app_name" => "an app", "data" => {"some" => "debug"}, "env" => "test", "hostname" => Socket.gethostname, "message" => "my message debug", "pid" => Process.pid, "severity" => 7}
|
47
62
|
expect(logger[:debug]).to eq(expectation)
|
48
63
|
end
|
49
64
|
|
50
|
-
it
|
51
|
-
subject.info("my message info", {
|
52
|
-
expectation = {
|
65
|
+
it "enriches and forwards info calls" do
|
66
|
+
subject.info("my message info", {"some" => "info"})
|
67
|
+
expectation = {"app_name" => "an app", "data" => {"some" => "info"}, "env" => "test", "hostname" => Socket.gethostname, "message" => "my message info", "pid" => Process.pid, "severity" => 6}
|
53
68
|
expect(logger[:info]).to eq(expectation)
|
54
69
|
end
|
55
70
|
|
56
|
-
it
|
57
|
-
subject.warn("my message warn", {
|
58
|
-
expectation = {
|
71
|
+
it "enriches and forwards warn calls" do
|
72
|
+
subject.warn("my message warn", {"some" => "warn"})
|
73
|
+
expectation = {"app_name" => "an app", "data" => {"some" => "warn"}, "env" => "test", "hostname" => Socket.gethostname, "message" => "my message warn", "pid" => Process.pid, "severity" => 4}
|
59
74
|
expect(logger[:warn]).to eq(expectation)
|
60
75
|
end
|
61
76
|
|
62
|
-
it
|
63
|
-
subject.error("my message error", {
|
64
|
-
expectation = {
|
77
|
+
it "enriches and forwards error calls" do
|
78
|
+
subject.error("my message error", {"some" => "error"})
|
79
|
+
expectation = {"app_name" => "an app", "data" => {"some" => "error"}, "env" => "test", "hostname" => Socket.gethostname, "message" => "my message error", "pid" => Process.pid, "severity" => 3}
|
65
80
|
expect(logger[:error]).to eq(expectation)
|
66
81
|
end
|
67
82
|
|
68
|
-
it
|
69
|
-
subject.fatal("my message fatal", {
|
70
|
-
expectation = {
|
83
|
+
it "enriches and forwards fatal calls" do
|
84
|
+
subject.fatal("my message fatal", {"some" => "fatal"})
|
85
|
+
expectation = {"app_name" => "an app", "data" => {"some" => "fatal"}, "env" => "test", "hostname" => Socket.gethostname, "message" => "my message fatal", "pid" => Process.pid, "severity" => 0}
|
71
86
|
expect(logger[:fatal]).to eq(expectation)
|
72
87
|
end
|
73
|
-
|
74
88
|
end
|
75
|
-
|
76
|
-
|
77
89
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
require
|
1
|
+
require "simplecov"
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
require "bundler/setup"
|
2
5
|
Bundler.setup
|
3
|
-
require
|
4
|
-
require_relative
|
6
|
+
require "rspec"
|
7
|
+
require_relative "../lib/eventhub/components"
|
5
8
|
|
6
9
|
RSpec.configure do |config|
|
7
|
-
#config.mock_with :rspec
|
8
10
|
end
|
9
|
-
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eventhub-components
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steiner, Thomas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-07-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,42 +16,70 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1
|
19
|
+
version: '2.1'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1
|
26
|
+
version: '2.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '13.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '13.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 3.
|
47
|
+
version: '3.11'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 3.
|
54
|
+
version: '3.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: standard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.14'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.14'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.21'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.21'
|
55
83
|
- !ruby/object:Gem::Dependency
|
56
84
|
name: logstash-logger
|
57
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -73,8 +101,10 @@ executables: []
|
|
73
101
|
extensions: []
|
74
102
|
extra_rdoc_files: []
|
75
103
|
files:
|
104
|
+
- ".github/dependabot.yml"
|
105
|
+
- ".github/workflows/cd.yml"
|
106
|
+
- ".github/workflows/ci.yml"
|
76
107
|
- ".gitignore"
|
77
|
-
- ".travis.yml"
|
78
108
|
- CHANGELOG.md
|
79
109
|
- Gemfile
|
80
110
|
- LICENSE.txt
|
@@ -113,14 +143,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
143
|
- !ruby/object:Gem::Version
|
114
144
|
version: '0'
|
115
145
|
requirements: []
|
116
|
-
|
117
|
-
rubygems_version: 2.6.14
|
146
|
+
rubygems_version: 3.3.7
|
118
147
|
signing_key:
|
119
148
|
specification_version: 4
|
120
149
|
summary: Additional eventhub components
|
121
|
-
test_files:
|
122
|
-
- spec/components/exception_writer_spec.rb
|
123
|
-
- spec/components/multi_logger_spec.rb
|
124
|
-
- spec/components/pid_file_spec.rb
|
125
|
-
- spec/components/structured_data_logger_spec.rb
|
126
|
-
- spec/spec_helper.rb
|
150
|
+
test_files: []
|