newrelic_logger_extension 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: 47b4bac116ae08eddbd49abfb642f504bd857322
4
+ data.tar.gz: 690e8dcec9f206835fcbd13938ded8587074f048
5
+ SHA512:
6
+ metadata.gz: 5801bded28b2c026fc4895f5e6fffcc450a0821192b8b22bd81617739c571cc606afdb562268a48f41ad27a8cbf719473bf4664d53bed97cd2192f3444eb2da6
7
+ data.tar.gz: 0d8c898bceee4ce13b6ccce55095a3a3370f98c9fd63d885c27b5a6d226264ec67d868ef90d10347686cf52c01b963e885570cdf5a9443dbd160c67300f225f9
data/.codeqa.rb ADDED
@@ -0,0 +1,16 @@
1
+ Codeqa.configure do |config|
2
+ config.excludes = ['coverage/*',
3
+ 'pkg/*']
4
+
5
+ config.enabled_checker.delete 'CheckRubySyntax'
6
+ config.enabled_checker << 'RubocopLint'
7
+ config.enabled_checker << 'RubocopFormatter'
8
+
9
+ config.rubocop_formatter_cops << 'AlignHash'
10
+ config.rubocop_formatter_cops << 'SignalException'
11
+ config.rubocop_formatter_cops << 'DeprecatedClassMethods'
12
+ config.rubocop_formatter_cops << 'RedundantBegin'
13
+ config.rubocop_formatter_cops << 'RedundantSelf'
14
+ config.rubocop_formatter_cops << 'RedundantReturn'
15
+ config.rubocop_formatter_cops << 'CollectionMethods'
16
+ end
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+
2
+ Metrics/LineLength:
3
+ Max: 99
4
+
5
+ Style/Documentation:
6
+ Enabled: false
7
+ Style/RegexpLiteral:
8
+ Enabled: false
9
+ Style/PerlBackrefs:
10
+ Enabled: false
11
+
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in newrelic_logger_extension.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rubocop', require: false
8
+ gem 'codeqa', require: false
9
+
10
+ gem 'guard-rspec'
11
+ gem 'guard-rubocop'
12
+ gem 'rb-inotify', require: false
13
+ end
14
+
15
+ group :test do
16
+ gem 'simplecov', require: false
17
+ gem 'coveralls', require: false
18
+ end
data/Guardfile ADDED
@@ -0,0 +1,20 @@
1
+ notification :tmux,
2
+ display_message: true,
3
+ timeout: 3 # in seconds
4
+
5
+ # ignore /doc/
6
+
7
+ group :red_green_refactor, halt_on_fail: true do
8
+ guard 'rspec', cmd: 'bundle exec rspec --color --format p', all_after_pass: true do
9
+ watch(%r{^spec/.+_spec\.rb$})
10
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
11
+ watch('spec/spec_helper.rb') { 'spec' }
12
+ watch(%r{^spec/support/(.+)\.rb}) { 'spec' }
13
+ watch(%r{^spec/fixtures/(.+)}) { 'spec' }
14
+ end
15
+
16
+ guard :rubocop, cli: %w(--display-cop-names --auto-correct) do
17
+ watch(%r{.+\.rb$})
18
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
19
+ end
20
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Andreas Eger
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,42 @@
1
+ # Newrelic Logger Extension
2
+
3
+ patch a logger instance to send a specified servity level as custom metric to
4
+ newrelic. The original logger handling is kept unchanged.
5
+
6
+ The login message is not send to newrelic only the location where the logging
7
+ happend is sent.
8
+
9
+ Also keep in mind that newrelic as a limit of ~2000 custom metrics which means
10
+ if you have to many locations where logging happend this might become an issue.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'newrelic_logger_extension'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ to use this in for example an Rails application, first make sure newrelic is
25
+ setup and then create a new initilizer like this
26
+
27
+ ``` ruby
28
+ %w(info warn error).each do |level|
29
+ NewrelicLoggerExtension.inject_logger(Rails.logger, level)\
30
+ end
31
+ ```
32
+
33
+ This will add the newrelic custom metric logging to the servity level `info`,
34
+ `warn` and `error`.
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( https://github.com/[my-github-username]/newrelic_logger_extension/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ desc 'Run all specs'
4
+ task :specs do
5
+ opts = %w(rspec -c)
6
+ opts += ['--require', File.join(File.dirname(__FILE__), 'spec', 'spec_helper')]
7
+ # opts += ['-I', YARD::ROOT]
8
+ if ENV['DEBUG']
9
+ $DEBUG = true
10
+ opts += ['-d']
11
+ end
12
+ opts += FileList['spec/**/*_spec.rb'].sort
13
+ cmd = opts.join(' ')
14
+ puts cmd if Rake.application.options.trace
15
+ system(cmd)
16
+ fail "Command failed with status (#{$CHILD_STATUS.to_i}): #{cmd}" if $CHILD_STATUS.to_i != 0
17
+ end
18
+ task spec: :specs
19
+ task default: :specs
@@ -0,0 +1,35 @@
1
+ require 'newrelic_logger_extension/version'
2
+ # require 'newrelic_rpm'
3
+
4
+ module NewrelicLoggerExtension
5
+ SEPARATOR = '__'
6
+ def self.inject_logger(logger, level = 'warn')
7
+ return unless logger.respond_to? level.to_sym
8
+ inject_servity_method(logger, level)
9
+ inject_shared_methods(logger) unless logger.respond_to? :log_to_newrelic
10
+ end
11
+ def self.inject_servity_method(logger, level)
12
+ logger.singleton_class.class_eval do
13
+ define_method("#{level}_with_newrelic") do |*args|
14
+ send("#{level}_without_newrelic", *args)
15
+ log_to_newrelic("#{level.capitalize}", caller_locations(1).first)
16
+ end
17
+
18
+ alias_method "#{level}_without_newrelic", level
19
+ alias_method level, "#{level}_with_newrelic"
20
+ end
21
+ end
22
+ def self.inject_shared_methods(logger)
23
+ logger.singleton_class.class_eval do
24
+ def log_to_newrelic(category, kaller)
25
+ ::NewRelic::Agent.increment_metric("Custom/#{category}/#{format_caller(kaller)}")
26
+ end
27
+
28
+ def format_caller(kaller)
29
+ (kaller.path.split(/\/|\./)[-4..-2] << kaller.lineno).join(SEPARATOR)
30
+ rescue
31
+ File.basename(kaller.path)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module NewrelicLoggerExtension
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'newrelic_logger_extension/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'newrelic_logger_extension'
8
+ spec.version = NewrelicLoggerExtension::VERSION
9
+ spec.authors = ['Andreas Eger']
10
+ spec.email = ['andreas.eger@experteer.com']
11
+ spec.summary = 'extend Logger instance with newrelic custom metrics'
12
+ spec.description = <<-EOS
13
+ extend a Logger instance to send warnings, errors and info to newrelic as
14
+ custom metric
15
+ EOS
16
+ spec.homepage = 'https://www.github.com/experteer/newrelic_logger_extension'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0")
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'newrelic_rpm', '>= 3.9'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.6'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec', '>=3.0'
29
+ end
@@ -0,0 +1,34 @@
1
+ require 'logger'
2
+
3
+ module NewRelic
4
+ end
5
+ describe NewrelicLoggerExtension do
6
+ SPEC_LOG_PATH = 'tmp/newrelic_logger_extension_spec.log'
7
+ let(:logger) { Logger.new(SPEC_LOG_PATH) }
8
+ after(:each) do
9
+ File.delete(SPEC_LOG_PATH)
10
+ ::NewRelic.send(:remove_const, 'Agent')
11
+ end
12
+
13
+ before(:each) do
14
+ ::NewRelic::Agent = double('Agent', increment_metric: true)
15
+ NewrelicLoggerExtension.inject_logger(logger, 'warn')
16
+ end
17
+
18
+ it 'should not interfere with the actuall logging' do
19
+ logger.warn('test warning')
20
+ filelog = IO.read(SPEC_LOG_PATH)
21
+ expect(filelog).to match 'WARN -- :'
22
+ expect(filelog).to match 'test warning'
23
+ end
24
+ it 'should call :increment_metric on the NewRelic::Agent' do
25
+ expect(::NewRelic::Agent).to receive(:increment_metric)
26
+ logger.warn('test warning')
27
+ end
28
+ it 'should call correctly set the custom metric nameing' do
29
+ expect(::NewRelic::Agent)
30
+ .to receive(:increment_metric)
31
+ .with('Custom/Warn/spec__lib__newrelic_logger_extension_spec__32')
32
+ logger.warn('test warning') # this is line 32
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ require 'simplecov'
5
+ require 'coveralls'
6
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
7
+ SimpleCov::Formatter::HTMLFormatter,
8
+ Coveralls::SimpleCov::Formatter
9
+ ]
10
+ SimpleCov.start
11
+
12
+ Bundler.require(:default, :test)
13
+
14
+ require 'newrelic_logger_extension'
15
+ RSpec.configure do |config|
16
+ config.run_all_when_everything_filtered = true
17
+ config.filter_run :focus
18
+
19
+ config.order = 'random'
20
+ end
21
+ # Dir['./spec/support/**/*.rb'].sort.each{ |f| require f }
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: newrelic_logger_extension
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andreas Eger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: newrelic_rpm
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: |
70
+ extend a Logger instance to send warnings, errors and info to newrelic as
71
+ custom metric
72
+ email:
73
+ - andreas.eger@experteer.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".codeqa.rb"
79
+ - ".gitignore"
80
+ - ".rspec"
81
+ - ".rubocop.yml"
82
+ - Gemfile
83
+ - Guardfile
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - lib/newrelic_logger_extension.rb
88
+ - lib/newrelic_logger_extension/version.rb
89
+ - newrelic_logger_extension.gemspec
90
+ - spec/lib/newrelic_logger_extension_spec.rb
91
+ - spec/spec_helper.rb
92
+ homepage: https://www.github.com/experteer/newrelic_logger_extension
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.2.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: extend Logger instance with newrelic custom metrics
116
+ test_files:
117
+ - spec/lib/newrelic_logger_extension_spec.rb
118
+ - spec/spec_helper.rb