logstasher 0.9.0 → 1.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91e7c9a0ab40af2e0f9e71ea61fde24dfc2dfd9d
4
- data.tar.gz: 4a89eb5bcf8cba52d1ae269ffc00cf9df733211c
3
+ metadata.gz: b8e249b498916037326b3263726f4c4ef055b5de
4
+ data.tar.gz: 7b1900cebe4e73585233a77ba59115901c174911
5
5
  SHA512:
6
- metadata.gz: 055f322b5f2abdab76f784844970691e7845cc3e216f4f37f35a79f61192049c3ced249b6eed9b8477c3c776c81bbba52ff7f0ac0c29652eabdedfee2541f332
7
- data.tar.gz: 3076cb4c8bf1e8e8dc0cefaa348f6ca7350d36b984cf01da6ee7b57ce92d697ca1f141b6406b15f5034a025ed5cff405f7f242f9c205c3e32fda9e4b88f68162
6
+ metadata.gz: 99773b3a67a1b19a67c6cee76f635fa776ced016fac2c015cb9e65d95b5cd1bd3891609947c4eb72c49d1a38fd4a551e770984296fd4eb02ebd17eae1f8a9228
7
+ data.tar.gz: db8b75e39aff2ed4e1c5a2e4cdf2e18b2978728df71119d7eb65045dea4986a3f01716a591c6687f0c2a49309e21f728f0c87d263611f7d178629e75af4bc362
@@ -3,7 +3,9 @@ require 'logstasher/active_support/log_subscriber'
3
3
  require 'logstasher/active_support/mailer_log_subscriber'
4
4
  require 'logstasher/active_record/log_subscriber'
5
5
  require 'logstasher/action_view/log_subscriber'
6
+ require 'logstasher/active_job/log_subscriber'
6
7
  require 'logstasher/rails_ext/action_controller/base'
8
+ require 'logstasher/custom_fields'
7
9
  require 'request_store'
8
10
  require 'active_support/core_ext/module/attribute_accessors'
9
11
  require 'active_support/core_ext/string/inflections'
@@ -32,6 +34,8 @@ module LogStasher
32
34
  unsubscribe(:action_mailer, subscriber)
33
35
  when 'ActiveRecord::LogSubscriber'
34
36
  unsubscribe(:active_record, subscriber)
37
+ when 'ActiveJob::Logging::LogSubscriber'
38
+ unsubscribe(:active_job, subscriber)
35
39
  end
36
40
  end
37
41
  end
@@ -51,16 +55,16 @@ module LogStasher
51
55
  payload[:ip] = request.remote_ip
52
56
  payload[:route] = "#{request.params[:controller]}##{request.params[:action]}"
53
57
  payload[:request_id] = request.env['action_dispatch.request_id']
54
- self.custom_fields += [:ip, :route, :request_id]
58
+ LogStasher::CustomFields.add(:ip, :route, :request_id)
55
59
  if self.log_controller_parameters
56
60
  payload[:parameters] = payload[:params].except(*::ActionController::LogSubscriber::INTERNAL_PARAMS)
57
- self.custom_fields += [:parameters]
61
+ LogStasher::CustomFields.add(:parameters)
58
62
  end
59
63
  end
60
64
 
61
65
  def add_custom_fields(&block)
62
66
  wrapped_block = Proc.new do |fields|
63
- LogStasher.custom_fields.concat(LogStasher.store.keys)
67
+ LogStasher::CustomFields.add(*LogStasher.store.keys)
64
68
  instance_exec(fields, &block)
65
69
  end
66
70
  ::ActionController::Metal.send(:define_method, :logtasher_add_custom_fields_to_payload, &wrapped_block)
@@ -70,7 +74,7 @@ module LogStasher
70
74
  def add_custom_fields_to_request_context(&block)
71
75
  wrapped_block = Proc.new do |fields|
72
76
  instance_exec(fields, &block)
73
- LogStasher.custom_fields.concat(fields.keys)
77
+ LogStasher::CustomFields.add(*fields.keys)
74
78
  end
75
79
  ::ActionController::Metal.send(:define_method, :logstasher_add_custom_fields_to_request_context, &wrapped_block)
76
80
  ::ActionController::Base.send(:define_method, :logstasher_add_custom_fields_to_request_context, &wrapped_block)
@@ -91,6 +95,7 @@ module LogStasher
91
95
  LogStasher::ActiveSupport::MailerLogSubscriber.attach_to :action_mailer if config.mailer_enabled
92
96
  LogStasher::ActiveRecord::LogSubscriber.attach_to :active_record if config.record_enabled
93
97
  LogStasher::ActionView::LogSubscriber.attach_to :action_view if config.view_enabled
98
+ LogStasher::ActiveJob::LogSubscriber.attach_to :active_job if has_active_job? && config.job_enabled
94
99
  end
95
100
 
96
101
  def setup(config)
@@ -125,6 +130,10 @@ module LogStasher
125
130
  defined?(Rails::Console) && true || false
126
131
  end
127
132
 
133
+ def has_active_job?
134
+ Rails::VERSION::MAJOR > 4 || (Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR >= 2)
135
+ end
136
+
128
137
  def suppress_app_logs(config)
129
138
  if configured_to_suppress_app_logs?(config)
130
139
  require 'logstasher/rails_ext/rack/logger'
@@ -137,14 +146,6 @@ module LogStasher
137
146
  !!(config.suppress_app_log.nil? ? config.supress_app_log : config.suppress_app_log)
138
147
  end
139
148
 
140
- def custom_fields
141
- Thread.current[:logstasher_custom_fields] ||= []
142
- end
143
-
144
- def custom_fields=(val)
145
- Thread.current[:logstasher_custom_fields] = val
146
- end
147
-
148
149
  # Log an arbitrary message.
149
150
  #
150
151
  # Usually invoked by the level-based wrapper methods defined below.
@@ -1,9 +1,12 @@
1
1
  require 'active_support/notifications'
2
2
  require 'action_view/log_subscriber'
3
+ require 'logstasher/custom_fields'
3
4
 
4
5
  module LogStasher
5
6
  module ActionView
6
7
  class LogSubscriber < ::ActionView::LogSubscriber
8
+ include CustomFields::LogSubscriber
9
+
7
10
  def render_template(event)
8
11
  logstash_event(event)
9
12
  end
@@ -57,12 +60,6 @@ module LogStasher
57
60
  runtimes[name] = runtime.to_f.round(2) if runtime
58
61
  runtimes
59
62
  end
60
-
61
- end
62
-
63
- def extract_custom_fields(data)
64
- custom_fields = (!LogStasher.custom_fields.empty? && data.extract!(*LogStasher.custom_fields)) || {}
65
- custom_fields
66
63
  end
67
64
  end
68
65
  end
@@ -0,0 +1,92 @@
1
+ begin
2
+ # `rescue nil` didn't work for some Ruby versions
3
+ require 'active_job/logging'
4
+ rescue LoadError
5
+ end
6
+
7
+ module LogStasher
8
+ module ActiveJob
9
+ class LogSubscriber < ::ActiveJob::Logging::LogSubscriber
10
+
11
+ def enqueue(event)
12
+ process_event(event, 'enqueue')
13
+ end
14
+
15
+ def enqueue_at(event)
16
+ process_event(event, 'enqueue_at')
17
+ end
18
+
19
+ def perform(event)
20
+ process_event(event, 'perform')
21
+
22
+ # Revert the request id back, in the event that the inline adapter is being used or a
23
+ # perform_now was used.
24
+ LogStasher.request_context[:request_id] = Thread.current[:old_request_id]
25
+ Thread.current[:old_request_id] = nil
26
+ end
27
+
28
+ def perform_start(event)
29
+ # Use the job_id as the request id, so that any custom logging done for a job
30
+ # shares a request id, and has the job id in each log line.
31
+ #
32
+ # It's not being set when the job is enqueued, so enqueuing a job will have it's default
33
+ # request_id. In a lot of cases, it will be because of a web request.
34
+ #
35
+ # Hang onto the old request id, so we can revert after the job is done being performed.
36
+ Thread.current[:old_request_id] = LogStasher.request_context[:request_id]
37
+ LogStasher.request_context[:request_id] = event.payload[:job].job_id
38
+
39
+ process_event(event, 'perform_start')
40
+ end
41
+
42
+ def logger
43
+ LogStasher.logger
44
+ end
45
+
46
+ private
47
+
48
+ def process_event(event, type)
49
+ data = extract_metadata(event)
50
+ data.merge! extract_exception(event)
51
+ data.merge! extract_scheduled_at(event) if type == 'enqueue_at'
52
+ data.merge! extract_duration(event) if type == 'perform'
53
+ data.merge! request_context
54
+
55
+ tags = ['job', type]
56
+ tags.push('exception') if data[:exception]
57
+ logger << LogStasher.build_logstash_event(data, tags).to_json + "\n"
58
+ end
59
+
60
+ def extract_metadata(event)
61
+ {
62
+ :job_id => event.payload[:job].job_id,
63
+ :queue_name => queue_name(event),
64
+ :job_class => event.payload[:job].class.to_s,
65
+ :job_args => args_info(event.payload[:job]),
66
+ }
67
+ end
68
+
69
+ def extract_duration(event)
70
+ { :duration => event.duration.to_f.round(2) }
71
+ end
72
+
73
+ def extract_exception(event)
74
+ event.payload.slice(:exception)
75
+ end
76
+
77
+ def extract_scheduled_at(event)
78
+ { :scheduled_at => scheduled_at(event) }
79
+ end
80
+
81
+ def request_context
82
+ LogStasher.request_context
83
+ end
84
+
85
+ # The default args_info makes a string. We need objects to turn into JSON.
86
+ def args_info(job)
87
+ job.arguments.map { |arg| arg.try(:to_global_id).try(:to_s) || arg }
88
+ end
89
+
90
+ end
91
+ end
92
+ end if defined?(::ActiveJob::Logging::LogSubscriber)
@@ -1,9 +1,12 @@
1
1
  require 'active_support/notifications'
2
2
  require 'active_record/log_subscriber'
3
+ require 'logstasher/custom_fields'
3
4
 
4
5
  module LogStasher
5
6
  module ActiveRecord
6
7
  class LogSubscriber < ::ActiveRecord::LogSubscriber
8
+ include CustomFields::LogSubscriber
9
+
7
10
  def identity(event)
8
11
  lsevent = logstash_event(event)
9
12
  if logger && lsevent
@@ -48,11 +51,6 @@ module LogStasher
48
51
  def extract_sql(data)
49
52
  { sql: data[:sql].squeeze(' ') }
50
53
  end
51
-
52
- def extract_custom_fields(data)
53
- custom_fields = (!LogStasher.custom_fields.empty? && data.extract!(*LogStasher.custom_fields)) || {}
54
- custom_fields
55
- end
56
54
  end
57
55
  end
58
56
  end
@@ -1,9 +1,12 @@
1
1
  require 'active_support/core_ext/class/attribute'
2
2
  require 'active_support/log_subscriber'
3
+ require 'logstasher/custom_fields'
3
4
 
4
5
  module LogStasher
5
6
  module ActiveSupport
6
7
  class LogSubscriber < ::ActiveSupport::LogSubscriber
8
+ include CustomFields::LogSubscriber
9
+
7
10
  def process_action(event)
8
11
  payload = event.payload
9
12
 
@@ -95,11 +98,6 @@ module LogStasher
95
98
  {}
96
99
  end
97
100
  end
98
-
99
- def extract_custom_fields(payload)
100
- custom_fields = (!LogStasher.custom_fields.empty? && payload.extract!(*LogStasher.custom_fields)) || {}
101
- custom_fields
102
- end
103
101
  end
104
102
  end
105
103
  end
@@ -0,0 +1,25 @@
1
+ module LogStasher
2
+ module CustomFields
3
+ module LogSubscriber
4
+ def extract_custom_fields(data)
5
+ (!CustomFields.custom_fields.empty? && data.extract!(*CustomFields.custom_fields)) || {}
6
+ end
7
+ end
8
+
9
+ def self.clear
10
+ Thread.current[:logstasher_custom_fields] = []
11
+ end
12
+
13
+ def self.add(*fields)
14
+ custom_fields.concat(fields).uniq!
15
+ end
16
+
17
+ def self.custom_fields
18
+ Thread.current[:logstasher_custom_fields] ||= []
19
+ end
20
+
21
+ def self.custom_fields=(val)
22
+ Thread.current[:logstasher_custom_fields] = val
23
+ end
24
+ end
25
+ end
@@ -7,6 +7,7 @@ module LogStasher
7
7
  LogStasher.add_default_fields_to_request_context(request)
8
8
 
9
9
  super(*args)
10
+ LogStasher::CustomFields.clear
10
11
  end
11
12
 
12
13
  private
@@ -23,7 +24,7 @@ module LogStasher
23
24
  logtasher_add_custom_fields_to_payload(payload)
24
25
  after_keys = payload.keys
25
26
  # Store all extra keys added to payload hash in payload itself. This is a thread safe way
26
- LogStasher.custom_fields += after_keys - before_keys
27
+ LogStasher::CustomFields.add(*(after_keys - before_keys))
27
28
  end
28
29
 
29
30
  payload[:status] = response.status
@@ -42,10 +42,10 @@ module ActionController
42
42
  LogStasher.request_context.each do |key, value|
43
43
  payload[key] = value
44
44
  end
45
+ LogStasher::CustomFields.clear
45
46
  result
46
47
  end
47
48
  end
48
49
  alias :logstasher_process_action :process_action
49
-
50
50
  end
51
51
  end
@@ -13,6 +13,7 @@ module LogStasher
13
13
  config.logstasher.mailer_enabled = true
14
14
  config.logstasher.record_enabled = false
15
15
  config.logstasher.view_enabled = true
16
+ config.logstasher.job_enabled = true
16
17
 
17
18
  # Try loading the config/logstasher.yml if present
18
19
  env = Rails.env.to_sym || :development
@@ -47,6 +48,7 @@ module LogStasher
47
48
  config.mailer_enabled = yml_config[:mailer_enabled] if yml_config.key? :mailer_enabled
48
49
  config.record_enabled = yml_config[:record_enabled] if yml_config.key? :record_enabled
49
50
  config.view_enabled = yml_config[:view_enabled] if yml_config.key? :view_enabled
51
+ config.job_enabled = yml_config[:job_enabled] if yml_config.key? :job_enabled
50
52
  #
51
53
  # # This line is optional if you do not want to suppress app logs in your <environment>.log
52
54
  config.suppress_app_log = yml_config[:suppress_app_log] if yml_config.key? :suppress_app_log
@@ -1,3 +1,3 @@
1
1
  module LogStasher
2
- VERSION = "0.9.0"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstasher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shadab Ahmed
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-18 00:00:00.000000000 Z
11
+ date: 2016-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logstash-event
@@ -44,28 +44,28 @@ dependencies:
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '4.0'
48
48
  type: :runtime
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.0'
54
+ version: '4.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: activerecord
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '3.0'
61
+ version: '4.0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '3.0'
68
+ version: '4.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: '3.0'
103
+ version: '4.0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
- version: '3.0'
110
+ version: '4.0'
111
111
  description: Awesome rails logs
112
112
  email:
113
113
  - shadab.ansari@gmail.com
@@ -117,9 +117,11 @@ extra_rdoc_files: []
117
117
  files:
118
118
  - lib/logstasher.rb
119
119
  - lib/logstasher/action_view/log_subscriber.rb
120
+ - lib/logstasher/active_job/log_subscriber.rb
120
121
  - lib/logstasher/active_record/log_subscriber.rb
121
122
  - lib/logstasher/active_support/log_subscriber.rb
122
123
  - lib/logstasher/active_support/mailer_log_subscriber.rb
124
+ - lib/logstasher/custom_fields.rb
123
125
  - lib/logstasher/device/redis.rb
124
126
  - lib/logstasher/rails_ext/action_controller/base.rb
125
127
  - lib/logstasher/rails_ext/action_controller/metal/instrumentation.rb
@@ -146,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
148
  version: '0'
147
149
  requirements: []
148
150
  rubyforge_project:
149
- rubygems_version: 2.4.8
151
+ rubygems_version: 2.5.1
150
152
  signing_key:
151
153
  specification_version: 4
152
154
  summary: Awesome rails logs