railstash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 464a34fd86b5ca71063c461a5eb065837369efa2
4
+ data.tar.gz: 3517b2f6746235e6a07195fa7437389499609482
5
+ SHA512:
6
+ metadata.gz: 0a7be782c057a099fea57cbd139146874f229060e63afee35b7838e836200930956d66f6662a756c82592adf70d2d677118da6f2a5f886e0dcf3d829cac508c5
7
+ data.tar.gz: af910c7f1193d0d2aa80eb73f92e1c2f5a14d11ae6ebd0b944a3d4a082ea4b11b0d7da357ed76791696001d42ce03863840b10ed0146367687eb89e2a0b3ca2b
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ test/dummy/db/*.sqlite3
12
+ test/dummy/db/*.sqlite3-journal
13
+ test/dummy/log/*.log
14
+ test/dummy/tmp/
15
+ test/dummy/.sass-cache
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in railstash.gemspec
4
+ gemspec
@@ -0,0 +1,35 @@
1
+ # Railstash
2
+
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'railstash'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ ## Usage
17
+
18
+ *config/initializers/railstash.rb*
19
+
20
+ ```
21
+ Railstash.configure do |c|
22
+ c.on_log { |data| data.merge!(app_name: 'dummy_app', source: 'dummy.example.com') }
23
+ c.on_log_action { |context, data| data['host'] = context.request.host }
24
+ end
25
+ ```
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/railstash.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "railstash"
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
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require "pathname"
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ load Gem.bin_path("rake", "rake")
@@ -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
@@ -0,0 +1,43 @@
1
+ require "railstash/version"
2
+ require "railstash/instrumentation"
3
+ require "railstash/subscribers"
4
+ require "railstash/railtie"
5
+
6
+ module Railstash
7
+ mattr_accessor :logger, instance_accessor: false
8
+ mattr_accessor(:on_log_callbacks, instance_accessor: false) { [] }
9
+ mattr_accessor(:on_log_action_callbacks, instance_accessor: false) { [] }
10
+
11
+ def self.configure
12
+ yield self
13
+ end
14
+
15
+ def self.on_log(&block)
16
+ self.on_log_callbacks << block
17
+ end
18
+
19
+ def self.on_log_action(&block)
20
+ self.on_log_action_callbacks << block
21
+ end
22
+
23
+ def self.log_action(context, data)
24
+ run_callbacks :on_log_action_callbacks, context, data
25
+ log(data.merge(Railstash::Instrumentation.extract_context_data(context)))
26
+ end
27
+
28
+ def self.log(data)
29
+ data[:tags] ||= ["log"]
30
+ run_callbacks :on_log_callbacks, data
31
+
32
+ _log(data)
33
+ end
34
+
35
+ def self._log(data)
36
+ logstash_event = ::LogStash::Event.new(data)
37
+ Railstash.logger << logstash_event.to_json + "\n"
38
+ end
39
+
40
+ def self.run_callbacks(chain, *args)
41
+ send(chain).each { |callback| callback.call(*args) }
42
+ end
43
+ end
@@ -0,0 +1,37 @@
1
+ module Railstash
2
+ module Instrumentation
3
+ def self.extract_context_data(context)
4
+ request = context.request
5
+ params = request.parameters
6
+ data = {}
7
+ data[:method] = request.request_method
8
+ data[:path] = request.path
9
+ data[:fullpath] = request.fullpath
10
+ data[:controller] = params["controller"]
11
+ data[:action] = params["action"]
12
+ data[:route] = "#{params["controller"]}##{params["action"]}"
13
+ data[:format] = request.format.ref
14
+ data[:ip] = request.remote_ip
15
+ data[:user_agent] = request.user_agent
16
+ data[:referer] = request.referer
17
+ data[:parameters] = request.filtered_parameters.except("utf8","authenticity_token",
18
+ *ActionController::LogSubscriber::INTERNAL_PARAMS)
19
+ if request.respond_to? :request_id
20
+ data[:request_id] = request.request_id
21
+ else
22
+ data[:request_id] = request.uuid
23
+ end
24
+ data
25
+ end
26
+
27
+ module ControllerRuntime
28
+ def append_info_to_payload(payload)
29
+ super
30
+ data = Railstash::Instrumentation.extract_context_data(self)
31
+ Railstash.run_callbacks :on_log_action_callbacks, self, data
32
+
33
+ payload[:railstash] = data
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/railtie'
2
+
3
+ module Railstash
4
+ class Railtie < Rails::Railtie
5
+ initializer "railstash.instrumentation" do
6
+ require 'logstash-event'
7
+ Railstash.logger ||= Logger.new(Rails.root + "log/railstash_#{Rails.env}.log")
8
+
9
+ ActiveSupport.on_load(:action_controller) do
10
+ include Railstash::Instrumentation::ControllerRuntime
11
+ Railstash::ControllerLogSubscriber.attach_to :action_controller
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ require 'active_support/log_subscriber'
2
+
3
+ module Railstash
4
+ class ControllerLogSubscriber < ::ActiveSupport::LogSubscriber
5
+ def process_action(event)
6
+ payload = event.payload
7
+ data = {
8
+ tags: ['request'],
9
+ status: payload[:status]
10
+ }
11
+ data.merge!(payload[:railstash])
12
+ data.merge!(runtimes(event))
13
+
14
+ Railstash.log(data)
15
+ end
16
+
17
+ private
18
+ def runtimes(event)
19
+ {
20
+ duration: event.duration,
21
+ view: event.payload[:view_runtime],
22
+ db: event.payload[:db_runtime]
23
+ }.transform_values { |value| value.to_f.round(2) if value }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Railstash
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'railstash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "railstash"
8
+ spec.version = Railstash::VERSION
9
+ spec.authors = ["Yves Senn"]
10
+ spec.email = ["yves.senn@gmail.com"]
11
+
12
+ spec.summary = %q{Writes Logstash specific Rails logs.}
13
+ spec.description = %q{Railstash provides a simple way to write Rails events into Logstash and augment them with your own information.}
14
+ spec.homepage = "https://github.com/senny/railstash"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "logstash-event", "~> 1.2"
22
+ spec.add_dependency "rails", ">= 4.0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest", "~> 5.0"
27
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: railstash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yves Senn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-24 00:00:00.000000000 Z
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
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.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.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: Railstash provides a simple way to write Rails events into Logstash and
84
+ augment them with your own information.
85
+ email:
86
+ - yves.senn@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/rake
98
+ - bin/setup
99
+ - lib/railstash.rb
100
+ - lib/railstash/instrumentation.rb
101
+ - lib/railstash/railtie.rb
102
+ - lib/railstash/subscribers.rb
103
+ - lib/railstash/version.rb
104
+ - railstash.gemspec
105
+ homepage: https://github.com/senny/railstash
106
+ licenses: []
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.4.5.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Writes Logstash specific Rails logs.
128
+ test_files: []