sentinel-api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b064ea2eec5598bba6f8db62610e32ae945bf7c0
4
+ data.tar.gz: e6b01bfe600a91930174388bf5140cdf21805211
5
+ SHA512:
6
+ metadata.gz: 66971e1bc851b3398ba577b8a6acc78b390a81d10c9e490bd664bbe957dabcef6e5ebdf8e2b882b94601af331c11d8db97cbbffc80d9890a2b8a4fe383388d22
7
+ data.tar.gz: 720e8b9f337175886b4c81eead1485485eb3d5c47b4e9082de6af36735a059e4a00b84186c1aeea2d8594dc5b3ab33ff0d6ac20b5234bbbc8dd60dbc17d9f12c
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .idea/
13
+ .rspec
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.4
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sentinel.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Sentinel
2
+
3
+ This is a gem for ErrorReportApi server integration.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sentinel-api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sentinel-api
20
+
21
+ ## Usage
22
+
23
+ There are 3 methods, which produce 3 log level messages:
24
+ info, warn, error.
25
+
26
+ You can use this gem in 3 ways:
27
+ * provide hash with 3 keys: message, info, tags
28
+ * provide message, exception and tags - info would be made from exception's class message and backtrace
29
+ * provide message and exception - no tags would be sent
30
+ ```ruby
31
+ Sentinel.info({ message: 'your custom error message', info: 'system info or other information', tags: ['tag1', 'tag2']})
32
+ Sentinel.warn('your message', exception, ['tag1', 'tag2'])
33
+ Sentinel.error('your message', exception)
34
+ ```
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sentinel"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/sentinel.rb ADDED
@@ -0,0 +1,28 @@
1
+ require "sentinel/version"
2
+ require 'sentinel/configuration'
3
+ require 'sentinel/notifier'
4
+ require 'sentinel/api_client'
5
+ require 'forwardable'
6
+
7
+ module Sentinel
8
+ PUBLIC_NOTIFIER_METHODS = %w(warn info error).freeze
9
+ class << self
10
+ extend Forwardable
11
+
12
+ def_delegators :notifier, *PUBLIC_NOTIFIER_METHODS
13
+
14
+ def configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ def notifier
19
+ Notifier
20
+ end
21
+
22
+ def default_logger
23
+ configuration.default_logger
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,29 @@
1
+ require 'httparty'
2
+
3
+ module Sentinel
4
+ class ApiClient
5
+ class << self
6
+ def send_to_server(record)
7
+ response = ::HTTParty.public_send(configuration.method, configuration.uri, prepare_request(record))
8
+ unless response.code == 200
9
+ Sentinel.default_logger.warn("We sent logs(#{record}) to API Server and got #{response.code} instead of status")
10
+ end
11
+ rescue => e
12
+ Sentinel.default_logger.warn("Troubles with ErrorReporter API call\n #{e.message}\n #{e.backtrace}")
13
+ end
14
+
15
+ private
16
+
17
+ def prepare_request(record)
18
+ {
19
+ headers: { 'Content-Type' => 'application/json' },
20
+ body: { request_params: record }.to_json
21
+ }
22
+ end
23
+
24
+ def configuration
25
+ Sentinel.configuration
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,50 @@
1
+ require 'logger'
2
+ module Sentinel
3
+ class Configuration
4
+ attr_accessor :uri
5
+ attr_accessor :level
6
+ attr_accessor :method
7
+
8
+ DEFAULT_URI = (ENV['sentinel_uri'] || 'http://localhost:3000/reports').freeze
9
+ DEFAULT_LEVEL = (ENV['sentinel_level'] || 'INFO').freeze
10
+ DEFAULT_METHOD = (ENV['sentinel_method'] || 'post').freeze
11
+
12
+ def initialize(uri:, level:, method:)
13
+ @uri = uri
14
+ @level = level
15
+ @method = method
16
+ end
17
+
18
+ def initialize
19
+ @uri = DEFAULT_URI
20
+ @level = DEFAULT_LEVEL
21
+ @method = DEFAULT_METHOD
22
+ end
23
+
24
+ def merge(options)
25
+ new_configuration = clone
26
+ new_configuration.merge!(options)
27
+
28
+ new_configuration
29
+ end
30
+
31
+ def merge!(options)
32
+ options.each do |name, value|
33
+ variable_name = "@#{name}"
34
+ next unless instance_variable_defined?(variable_name)
35
+
36
+ instance_variable_set(variable_name, value)
37
+ end
38
+
39
+ self
40
+ end
41
+
42
+ def default_logger
43
+ @default_logger ||= defined?(::Rails) ? ::Rails.logger : ::Logger.new(STDERR)
44
+ end
45
+
46
+ def use_ssl?
47
+ uri.match(/^https:/) ? true : false
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,66 @@
1
+ require 'active_support/core_ext/hash/indifferent_access'
2
+
3
+ module Sentinel
4
+ class Notifier
5
+ class << self
6
+
7
+ def warn(*args)
8
+ log('WARN', args)
9
+ end
10
+
11
+ def info(*args)
12
+ log('INFO', args)
13
+ end
14
+
15
+ def error(*args)
16
+ log('ERROR', args)
17
+ end
18
+
19
+ private_class_method
20
+
21
+ def log(level = nil, args)
22
+ level ||= configuration.level
23
+ message, info, tags = extract_arguments(args)
24
+
25
+ Sentinel::ApiClient.send_to_server({ level: level, message: message, info: info, tags: tags })
26
+ end
27
+
28
+ def extract_arguments(args)
29
+ message = nil
30
+ exception = nil
31
+ tags = nil
32
+ if args.count == 1 && args[0].is_a?(Hash)
33
+ result = args[0].with_indifferent_access
34
+ return [result[:message], result[:info], result[:tags]]
35
+ else
36
+ args.each do |arg|
37
+ if arg.is_a?(String)
38
+ message = arg
39
+ elsif arg.is_a?(Exception)
40
+ exception = arg
41
+ elsif RUBY_PLATFORM == 'java' && arg.is_a?(java.lang.Exception)
42
+ exception = arg
43
+ elsif arg.is_a?(Array)
44
+ tags = arg
45
+
46
+ tags = nil if tags.empty?
47
+ end
48
+ end
49
+ end
50
+
51
+ [message, prepare_info(exception), tags]
52
+ end
53
+
54
+ def prepare_info(exception)
55
+ "#{exception.class}: #{exception.message}, \n#{exception.backtrace.join("\n")}"
56
+ rescue
57
+ "#{exception.class}: #{exception.message}"
58
+ end
59
+
60
+
61
+ def configuration
62
+ Sentinel.configuration
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module Sentinel
2
+ VERSION = "0.0.1"
3
+ end
data/sentinel.gemspec ADDED
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "sentinel/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sentinel-api"
8
+ spec.version = Sentinel::VERSION
9
+ spec.authors = ["Edvard Loktev"]
10
+ spec.email = ["edvardloktev@gmail.com"]
11
+
12
+ spec.summary = %q{Gem to connect to ErrorReporterAPI}
13
+ spec.description = %q{Gem to connect to ErrorReporterAPI}
14
+ spec.homepage = "https://gitlab.com/intelllex/sentinel"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency "httparty"
35
+ spec.add_dependency 'activesupport'
36
+ spec.add_development_dependency "bundler", "~> 1.16"
37
+ spec.add_development_dependency "pry"
38
+ spec.add_development_dependency 'pry-byebug'
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sentinel-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Edvard Loktev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ description: Gem to connect to ErrorReporterAPI
112
+ email:
113
+ - edvardloktev@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/setup
126
+ - lib/sentinel.rb
127
+ - lib/sentinel/api_client.rb
128
+ - lib/sentinel/configuration.rb
129
+ - lib/sentinel/notifier.rb
130
+ - lib/sentinel/version.rb
131
+ - sentinel.gemspec
132
+ homepage: https://gitlab.com/intelllex/sentinel
133
+ licenses: []
134
+ metadata:
135
+ allowed_push_host: https://rubygems.org
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.6.13
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Gem to connect to ErrorReporterAPI
156
+ test_files: []