logstasher 2.0.0 → 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e438a36935fc002e2de28f362171174d01908e321a432a45c0b6d5ba607593ff
4
- data.tar.gz: 2076d9f4e694686bafc239e90f3c426feee7fcc74a9904352700704b1c8d2bd5
3
+ metadata.gz: 33158a121af27be0e9bd17ac2c3e9ac3e40823a52db857ad9954d370d339eb63
4
+ data.tar.gz: cf62d43543a0b3b6948c1c29233c5eedfd21188f9f0cb6f98fdf9276f933b54f
5
5
  SHA512:
6
- metadata.gz: 7b8face2936949f9cf382336ef8e88da96ff848ea8f088f70eeedba7d6e76f923cb206b06b9cbb10b03ad28dda22cea70415d1686f662617a8175c9f6b7b2aa2
7
- data.tar.gz: 7aa9af505594a1109b438c85a5196cfa5d46ac9591c53a4d3425790ed40d02f812c067a760d387b222bebbce8bae74585437d2a2e47cc8baa92e908d24c9aaee
6
+ metadata.gz: 9f403b9a4d329f51c981ed6b57ab1ff2e6ef4ecc58f0943c59eb6d136b9cbdda39d8a904f02511258972fb99597a66ff75a8f53ac17906e837867400a83d66d7
7
+ data.tar.gz: 57e572d794e472c73496f163023b3f61379998d0e80a4e9cc73d7c37fbbbf2a1999dd53697b897e0a8034d6362184cd86020162409406ecfd68716014bda9bf0
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'logstasher/version'
2
4
  require 'logstasher/active_support/log_subscriber'
3
5
  require 'logstasher/active_support/mailer_log_subscriber'
@@ -36,7 +38,9 @@ module LogStasher
36
38
  unsubscribe(:action_mailer, subscriber)
37
39
  when 'ActiveRecord::LogSubscriber'
38
40
  unsubscribe(:active_record, subscriber)
39
- when 'ActiveJob::Logging::LogSubscriber'
41
+ when 'ActiveJob::LogSubscriber' # For Rails 6
42
+ unsubscribe(:active_job, subscriber)
43
+ when 'ActiveJob::Logging::LogSubscriber' # For Rails 5
40
44
  unsubscribe(:active_job, subscriber)
41
45
  end
42
46
  end
@@ -89,7 +93,6 @@ module LogStasher
89
93
  end
90
94
 
91
95
  def setup_before(config)
92
- require 'logstash-event'
93
96
  self.enabled = config.enabled
94
97
  LogStasher::ActiveSupport::LogSubscriber.attach_to :action_controller if config.controller_enabled
95
98
  LogStasher::ActiveSupport::MailerLogSubscriber.attach_to :action_mailer if config.mailer_enabled
@@ -108,8 +111,8 @@ module LogStasher
108
111
  self.logger = config.logger || new_logger(logger_path)
109
112
  logger.level = config.log_level || Logger::WARN
110
113
  self.source = config.source unless config.source.nil?
111
- self.log_controller_parameters = !!config.log_controller_parameters
112
- self.backtrace = !!config.backtrace unless config.backtrace.nil?
114
+ self.log_controller_parameters = !config.log_controller_parameters.nil?
115
+ self.backtrace = !config.backtrace.nil? unless config.backtrace.nil?
113
116
  set_data_for_rake
114
117
  set_data_for_console
115
118
  self.field_renaming = Hash(config.field_renaming)
@@ -124,7 +127,7 @@ module LogStasher
124
127
  end
125
128
 
126
129
  def called_as_rake?
127
- File.basename($0) == 'rake'
130
+ File.basename($PROGRAM_NAME) == 'rake'
128
131
  end
129
132
 
130
133
  def called_as_console?
@@ -159,7 +162,7 @@ module LogStasher
159
162
  # LogStasher.info("message", timing:1234)
160
163
  # LogStasher.info(custom1:"yes", custom2:"no")
161
164
  def log(severity, message, additional_fields = {})
162
- if logger && logger.send("#{severity}?")
165
+ if logger&.send("#{severity}?")
163
166
 
164
167
  data = { 'level' => severity }
165
168
  if message.respond_to?(:to_hash)
@@ -172,7 +175,7 @@ module LogStasher
172
175
  tags = Array(additional_fields.delete(:tags) || 'log')
173
176
 
174
177
  data.merge!(additional_fields)
175
- logger << build_logstash_event(data, tags).to_json + "\n"
178
+ logger << "#{build_logstash_event(data, tags).to_json}\n"
176
179
 
177
180
  end
178
181
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/notifications'
2
4
  require 'action_view/log_subscriber'
3
5
  require 'logstasher/custom_fields'
@@ -31,7 +33,7 @@ module LogStasher
31
33
 
32
34
  tags = []
33
35
  tags.push('exception') if data[:exception]
34
- logger << LogStasher.build_logstash_event(data, tags).to_json + "\n"
36
+ logger << "#{LogStasher.build_logstash_event(data, tags).to_json}\n"
35
37
  end
36
38
 
37
39
  def extract_data(data)
@@ -1,91 +1,98 @@
1
- begin
2
- # `rescue nil` didn't work for some Ruby versions
1
+ # frozen_string_literal: true
2
+
3
+ # For Rails 6.0 or below, require the logging module which contains LogSubscriber
4
+ if ActiveJob::VERSION::MAJOR < 6 || (ActiveJob::VERSION::MAJOR == 6 && ActiveJob::VERSION::MINOR.zero?)
3
5
  require 'active_job/logging'
4
- rescue LoadError
6
+ else
7
+ require 'active_job/log_subscriber'
5
8
  end
6
9
 
7
- if defined?(::ActiveJob::Logging::LogSubscriber)
8
- module LogStasher
9
- module ActiveJob
10
- class LogSubscriber < ::ActiveJob::Logging::LogSubscriber
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
10
+ module LogStasher
11
+ module ActiveJob
12
+ BASE_SUBSCRIBER = if defined?(::ActiveJob::LogSubscriber)
13
+ ::ActiveJob::LogSubscriber
14
+ else
15
+ ::ActiveJob::Logging::LogSubscriber
16
+ end
17
+
18
+ class LogSubscriber < BASE_SUBSCRIBER
19
+ def enqueue(event)
20
+ process_event(event, 'enqueue')
21
+ end
22
+
23
+ def enqueue_at(event)
24
+ process_event(event, 'enqueue_at')
25
+ end
26
+
27
+ def perform(event)
28
+ process_event(event, 'perform')
29
+
30
+ # Revert the request id back, in the event that the inline adapter is being used or a
31
+ # perform_now was used.
32
+ LogStasher.request_context[:request_id] = Thread.current[:old_request_id]
33
+ Thread.current[:old_request_id] = nil
34
+ end
35
+
36
+ def perform_start(event)
37
+ # Use the job_id as the request id, so that any custom logging done for a job
38
+ # shares a request id, and has the job id in each log line.
39
+ #
40
+ # It's not being set when the job is enqueued, so enqueuing a job will have it's default
41
+ # request_id. In a lot of cases, it will be because of a web request.
42
+ #
43
+ # Hang onto the old request id, so we can revert after the job is done being performed.
44
+ Thread.current[:old_request_id] = LogStasher.request_context[:request_id]
45
+ LogStasher.request_context[:request_id] = event.payload[:job].job_id
46
+
47
+ process_event(event, 'perform_start')
48
+ end
49
+
50
+ def logger
51
+ LogStasher.logger
52
+ end
53
+
54
+ private
55
+
56
+ def process_event(event, type)
57
+ data = extract_metadata(event)
58
+ data.merge! extract_exception(event)
59
+ data.merge! extract_scheduled_at(event) if type == 'enqueue_at'
60
+ data.merge! extract_duration(event) if type == 'perform'
61
+ data.merge! request_context
62
+
63
+ tags = ['job', type]
64
+ tags.push('exception') if data[:exception]
65
+ logger << "#{LogStasher.build_logstash_event(data, tags).to_json}\n"
66
+ end
67
+
68
+ def extract_metadata(event)
69
+ {
70
+ job_id: event.payload[:job].job_id,
71
+ queue_name: queue_name(event),
72
+ job_class: event.payload[:job].class.to_s,
73
+ job_args: args_info(event.payload[:job])
74
+ }
75
+ end
76
+
77
+ def extract_duration(event)
78
+ { duration: event.duration.to_f.round(2) }
79
+ end
80
+
81
+ def extract_exception(event)
82
+ event.payload.slice(:exception)
83
+ end
84
+
85
+ def extract_scheduled_at(event)
86
+ { scheduled_at: scheduled_at(event) }
87
+ end
88
+
89
+ def request_context
90
+ LogStasher.request_context
91
+ end
92
+
93
+ # The default args_info makes a string. We need objects to turn into JSON.
94
+ def args_info(job)
95
+ ::ActiveJob::Arguments.serialize(job.arguments)
89
96
  end
90
97
  end
91
98
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/notifications'
2
4
  require 'active_record/log_subscriber'
3
5
  require 'logstasher/custom_fields'
@@ -9,7 +11,7 @@ module LogStasher
9
11
 
10
12
  def identity(event)
11
13
  lsevent = logstash_event(event)
12
- logger << lsevent.to_json + "\n" if logger && lsevent
14
+ logger << "#{lsevent.to_json}\n" if logger && lsevent
13
15
  end
14
16
  alias sql identity
15
17
 
@@ -21,9 +23,13 @@ module LogStasher
21
23
 
22
24
  def logstash_event(event)
23
25
  self.class.runtime += event.duration
24
- data = event.payload
26
+ data = event.payload.dup
27
+
28
+ return if data[:name] == 'SCHEMA'
25
29
 
26
- return if 'SCHEMA' == data[:name]
30
+ # A connection cannot be converted to JSON as it fails with
31
+ # SystemStackError when running against ActiveSupport JSON patches.
32
+ data.delete(:connection)
27
33
 
28
34
  data.merge! runtimes(event)
29
35
  data.merge! extract_sql(data)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/core_ext/class/attribute'
2
4
  require 'active_support/log_subscriber'
3
5
  require 'logstasher/custom_fields'
@@ -20,7 +22,7 @@ module LogStasher
20
22
 
21
23
  tags = ['request']
22
24
  tags.push('exception') if payload[:exception]
23
- logger << LogStasher.build_logstash_event(data, tags).to_json + "\n"
25
+ logger << "#{LogStasher.build_logstash_event(data, tags).to_json}\n"
24
26
  end
25
27
 
26
28
  def redirect_to(event)
@@ -48,11 +50,7 @@ module LogStasher
48
50
  end
49
51
 
50
52
  def extract_format(payload)
51
- if ::ActionPack::VERSION::MAJOR == 3 && ::ActionPack::VERSION::MINOR == 0
52
- payload[:formats].first
53
- else
54
- payload[:format]
55
- end
53
+ payload[:format]
56
54
  end
57
55
 
58
56
  def extract_status(payload)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/core_ext/class/attribute'
2
4
  require 'active_support/log_subscriber'
3
5
 
@@ -10,6 +12,11 @@ module LogStasher
10
12
  process_event(event, %w[mailer deliver])
11
13
  end
12
14
 
15
+ # This method will only be invoked on Rails 6.0 and prior.
16
+ # Starting in Rails 6.0 the receive method was deprecated in
17
+ # favor of ActionMailbox. The receive method was removed
18
+ # from ActionMailer in Rails 6.1, and there doesn't appear to
19
+ # be corresponding instrumentation for ActionMailbox.
13
20
  def receive(event)
14
21
  process_event(event, %w[mailer receive])
15
22
  end
@@ -26,7 +33,7 @@ module LogStasher
26
33
 
27
34
  def process_event(event, tags)
28
35
  data = LogStasher.request_context.merge(extract_metadata(event.payload))
29
- logger << LogStasher.build_logstash_event(data, tags).to_json + "\n"
36
+ logger << "#{LogStasher.build_logstash_event(data, tags).to_json}\n"
30
37
  end
31
38
 
32
39
  def extract_metadata(payload)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module LogStasher
2
4
  module CustomFields
3
5
  module LogSubscriber
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'redis'
2
4
 
3
5
  module LogStasher
@@ -22,7 +24,7 @@ module LogStasher
22
24
  def redis_options
23
25
  unless @redis_options
24
26
  default_keys = default_options.keys
25
- @redis_options = options.select { |k, _v| !default_keys.include?(k) }
27
+ @redis_options = options.reject { |k, _v| default_keys.include?(k) }
26
28
  end
27
29
 
28
30
  @redis_options
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json'
2
4
  require 'time'
3
5
  require 'date'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module LogStasher
2
4
  module ActionController
3
5
  module Instrumentation
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActionController
2
4
  module Instrumentation
3
5
  alias orig_process_action process_action
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails/rack/logger'
2
4
 
3
5
  module Rails
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails/railtie'
2
4
  require 'action_view/log_subscriber'
3
5
  require 'action_controller/log_subscriber'
@@ -20,7 +22,7 @@ module LogStasher
20
22
  config_file = File.expand_path './config/logstasher.yml'
21
23
 
22
24
  # Load and ERB templating of YAML files
23
- LOGSTASHER = File.exist?(config_file) ? YAML.load(ERB.new(File.read(config_file)).result).symbolize_keys : nil
25
+ LOGSTASHER = File.exist?(config_file) ? YAML.safe_load(ERB.new(File.read(config_file)).result).symbolize_keys : nil
24
26
 
25
27
  initializer :logstasher, before: :load_config_initializers do |app|
26
28
  if LOGSTASHER.present?
@@ -42,7 +44,7 @@ module LogStasher
42
44
  end
43
45
 
44
46
  def rack_cache_hashlike?(app)
45
- app.config.action_dispatch.rack_cache && app.config.action_dispatch.rack_cache.respond_to?(:[]=)
47
+ app.config.action_dispatch.rack_cache&.respond_to?(:[]=)
46
48
  end
47
49
  end
48
50
 
@@ -55,7 +57,7 @@ module LogStasher
55
57
  # get all available IP address lists and use the first one.
56
58
  # This will always be `127.0.0.1`.
57
59
  address_info = Socket.ip_address_list.first
58
- address_info && address_info.ip_address
60
+ address_info&.ip_address
59
61
  else
60
62
  IPSocket.getaddress(Socket.gethostname)
61
63
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module LogStasher
2
- VERSION = '2.0.0'.freeze
4
+ VERSION = '2.1.2'
3
5
  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: 2.0.0
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shadab Ahmed
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-27 00:00:00.000000000 Z
11
+ date: 2020-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,28 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
19
+ version: '5.2'
20
20
  type: :runtime
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: '5.0'
27
- - !ruby/object:Gem::Dependency
28
- name: logstash-event
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: 1.2.0
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: 1.2.0
26
+ version: '5.2'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: request_store
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,20 +38,6 @@ dependencies:
52
38
  - - ">="
53
39
  - !ruby/object:Gem::Version
54
40
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: activerecord
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '5.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '5.0'
69
41
  - !ruby/object:Gem::Dependency
70
42
  name: bundler
71
43
  requirement: !ruby/object:Gem::Requirement
@@ -86,14 +58,14 @@ dependencies:
86
58
  requirements:
87
59
  - - ">="
88
60
  - !ruby/object:Gem::Version
89
- version: '5.0'
61
+ version: '5.2'
90
62
  type: :development
91
63
  prerelease: false
92
64
  version_requirements: !ruby/object:Gem::Requirement
93
65
  requirements:
94
66
  - - ">="
95
67
  - !ruby/object:Gem::Version
96
- version: '5.0'
68
+ version: '5.2'
97
69
  - !ruby/object:Gem::Dependency
98
70
  name: rspec
99
71
  requirement: !ruby/object:Gem::Requirement
@@ -148,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
120
  - !ruby/object:Gem::Version
149
121
  version: '0'
150
122
  requirements: []
151
- rubygems_version: 3.0.3
123
+ rubygems_version: 3.1.4
152
124
  signing_key:
153
125
  specification_version: 4
154
126
  summary: Awesome rails logs