ichnite 0.0.5 → 0.0.6

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
- SHA1:
3
- metadata.gz: 8356832e68c689347200de1532c4b5ee6c878905
4
- data.tar.gz: 6eec0f10c9ad8f9f4cee5e337c45ff0f874b4ab4
2
+ SHA256:
3
+ metadata.gz: 677aac051c83f841ad6c88bcc59e81eec236a16f2073bfcb9ecda926a5ab526e
4
+ data.tar.gz: eac35f4f44079509b5bd05c690e0810059d6be9e0a29f5c1a54f3dbc404845f4
5
5
  SHA512:
6
- metadata.gz: 4835f9417bd793d5101e9aaebf84e65f8cd4bc4a6137ffa9b93534f445699a74a5523c1263dea2c9bf870891ad8cb32563f56ebbe7516dbb514ba645658081c5
7
- data.tar.gz: 72040b57c125facae786106773580f5adb03ad9179b35b62954fd0ebf5005d48c2975bca74fd33c6eabf45fcecff0b760b2a5294e0abb720c7c7c7a2a916802e
6
+ metadata.gz: '03299eb5ed6de16539367a015471151d5e4cf094742062e8b6f11594b27824902531fba745032d78048d96bb78bdb177da80496a98721f0dcbfd0171ee781a45'
7
+ data.tar.gz: e93689adf0c210d474291d70fc50f1ebc855730843c4fd98dc7781c85b23249d220f05a64ca617cc7536fcdd4a31bfb75e054fc71cd878cd95e0f1580754b39c
@@ -44,8 +44,8 @@ module Ichnite
44
44
 
45
45
  def self.default_logger
46
46
  @default_logger ||=
47
- if defined?(Rails)
48
- Ichnite::Logger.new(Rails.logger)
47
+ if defined?(::Rails) && ::Rails.respond_to?(:logger)
48
+ Ichnite::Logger.new(::Rails.logger)
49
49
  else
50
50
  inner = ::Logger.new($stdout)
51
51
  inner.formatter = proc { |_level, _time, _prog, msg| "#{msg}\n" }
@@ -0,0 +1,28 @@
1
+ module Ichnite
2
+ module ActionDispatch
3
+ module StructuredExceptions
4
+ def render_exception(_env, e)
5
+ begin
6
+ exception_name = e.class.name
7
+ status = ::ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_name)
8
+ if status == 500
9
+ ::Ichnite.log('request_error',
10
+ at: :error,
11
+ error: exception_name,
12
+ message: e.message[/\A.+$/].inspect
13
+ )
14
+ end
15
+ rescue Exception => e2
16
+ # never interfere with the regular exception handling
17
+ ::Rails.logger.error(e2.inspect)
18
+ end
19
+
20
+ super
21
+ end
22
+
23
+ def log_error(*)
24
+ # no-logging
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveSupport
2
+ module TaggedLogging
3
+ module Formatter
4
+ def tagged(*_args)
5
+ yield
6
+ end
7
+
8
+ def push_tags(*_args); end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ require "ichnite/rails/railtie"
@@ -0,0 +1,24 @@
1
+ require 'ichnite/action_dispatch/structured_exceptions'
2
+
3
+ module Ichnite
4
+ module Rails
5
+ class Railtie < ::Rails::Railtie
6
+ initializer 'ichnite_rails.setup' do |app|
7
+ Ichnite.default_logger = Ichnite::Logger.new(::Rails.logger)
8
+
9
+ ActiveSupport.on_load :action_controller do
10
+ if defined?(::ActionDispatch::DebugExceptions)
11
+ ::ActionDispatch::DebugExceptions.prepend Ichnite::ActionDispatch::StructuredExceptions
12
+ elsif defined?(::ActionDispatch::ShowExceptions)
13
+ ::ActionDispatch::ShowExceptions.prepend Ichnite::ActionDispatch::StructuredExceptions
14
+ end
15
+ end
16
+
17
+ if !::Rails.env.test? && !::Rails.env.development?
18
+ require 'active_support/tagged_logging'
19
+ require 'ichnite/active_support/disable_tagged_logging_patch'
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ require 'ichnite/sidekiq/middleware'
2
+
3
+ Sidekiq.logger.level = Logger::WARN
4
+
5
+ Sidekiq.configure_server do |config|
6
+ # Remove Sidekiqs default backtrace logging
7
+ if !defined?(::Rails) || ::Rails.env.production?
8
+ config.error_handlers.delete_if { |h| Sidekiq::ExceptionHandler::Logger === h }
9
+ end
10
+ end
11
+
12
+ Sidekiq.configure_client do |config|
13
+ config.client_middleware do |chain|
14
+ chain.add Sidekiq::Ichnite::ClientMiddleware
15
+ end
16
+ end
17
+
18
+ Sidekiq.configure_server do |config|
19
+ config.client_middleware do |chain|
20
+ chain.add Sidekiq::Ichnite::ClientMiddleware
21
+ end
22
+
23
+ config.server_middleware do |chain|
24
+ chain.add Sidekiq::Ichnite::ServerMiddleware
25
+ end
26
+ end
@@ -0,0 +1,108 @@
1
+ module Sidekiq
2
+ module Ichnite
3
+ def self.job_id(msg)
4
+ msg['jid']
5
+ end
6
+
7
+ def self.job_class(msg)
8
+ aj_job_class(msg) || msg['class']
9
+ end
10
+
11
+ def self.aj_job_class(msg)
12
+ msg['wrapped']
13
+ end
14
+
15
+ def self.job_args(msg)
16
+ args = msg['args']
17
+ aj_job_args(args) || args
18
+ end
19
+
20
+ def self.aj_job_args(args)
21
+ first_arg = args.first
22
+
23
+ # check if the first argument looks like a AJ metadata hash
24
+ if first_arg.is_a?(Hash) && first_arg.key?('arguments')
25
+ first_arg['arguments'].map do |arg|
26
+ if arg.is_a?(Hash) && arg.key?('_aj_globalid')
27
+ arg['_aj_globalid']
28
+ else
29
+ arg
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ class ClientMiddleware
36
+ def call(worker_class, msg, queue, _redis_pool)
37
+ context = {
38
+ queue: queue,
39
+ job_id: Sidekiq::Ichnite.job_id(msg),
40
+ job_class: Sidekiq::Ichnite.job_class(msg)
41
+ }
42
+ if at = msg['at']
43
+ context[:scheduled_at] = Time.at(at).utc
44
+ ::Ichnite.log('job_schedule', context)
45
+ else
46
+ context[:args] = Sidekiq::Ichnite.job_args(msg)
47
+ ::Ichnite.log('job_enqueue', context)
48
+ end
49
+ yield
50
+ end
51
+ end
52
+
53
+ class ServerMiddleware
54
+ def call(worker, msg, queue)
55
+ ::Ichnite.enter(
56
+ job_id: Sidekiq::Ichnite.job_id(msg),
57
+ job_class: Sidekiq::Ichnite.job_class(msg)) do
58
+ begin
59
+ context = { queue: queue }
60
+
61
+ ts = Time.now.to_f
62
+ # We are currently not logging start events.
63
+ # This would most likely blow our SumoLogic limit.
64
+ # start context, ts
65
+ yield
66
+ stop context, msg, ts
67
+ rescue => ex
68
+ error context, msg, ex, ts
69
+ raise ex
70
+ end
71
+ end
72
+ end
73
+
74
+ def start(context, _time)
75
+ ::Ichnite.log('job_start', context)
76
+ end
77
+
78
+ def error(context, msg, error, start)
79
+ data = context.merge(job_timing(msg, start))
80
+ data.merge!(
81
+ at: :error,
82
+ error: error.class.name,
83
+ message: error.message[/\A.+$/].inspect)
84
+ ::Ichnite.log('job_error', data)
85
+ end
86
+
87
+ def stop(context, msg, start)
88
+ data = context.merge(job_timing(msg, start))
89
+ ::Ichnite.log('job_stop', data)
90
+ end
91
+
92
+ def job_timing(msg, start)
93
+ # with clock drift this number can become negative.
94
+ # Let's take 0 in this case.
95
+ queued_duration = [0, duration_ms(msg['enqueued_at'], start)].max
96
+ {
97
+ queued_for: queued_duration / 1000,
98
+ duration: duration_ms(start)
99
+ }
100
+ end
101
+
102
+ def duration_ms(from, to = Time.now.to_f)
103
+ ((to - from) * 1000).round
104
+ end
105
+ end
106
+
107
+ end
108
+ end
@@ -1,3 +1,3 @@
1
1
  module Ichnite
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ichnite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yves Senn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-25 00:00:00.000000000 Z
11
+ date: 2019-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,8 +69,14 @@ files:
69
69
  - bin/setup
70
70
  - ichnite.gemspec
71
71
  - lib/ichnite.rb
72
+ - lib/ichnite/action_dispatch/structured_exceptions.rb
73
+ - lib/ichnite/active_support/disable_tagged_logging_patch.rb
72
74
  - lib/ichnite/formatters.rb
73
75
  - lib/ichnite/logger.rb
76
+ - lib/ichnite/rails.rb
77
+ - lib/ichnite/rails/railtie.rb
78
+ - lib/ichnite/sidekiq.rb
79
+ - lib/ichnite/sidekiq/middleware.rb
74
80
  - lib/ichnite/testing.rb
75
81
  - lib/ichnite/version.rb
76
82
  homepage: https://github.com/senny/ichnite
@@ -93,9 +99,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
99
  version: '0'
94
100
  requirements: []
95
101
  rubyforge_project:
96
- rubygems_version: 2.5.1
102
+ rubygems_version: 2.7.6
97
103
  signing_key:
98
104
  specification_version: 4
99
105
  summary: A structured logging library.
100
106
  test_files: []
101
- has_rdoc: