semantic_logger_ecs_addon 0.1.9 → 0.1.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/rails_semantic_logger/sequel/log_subscriber.rb +89 -91
- data/lib/semantic_logger_ecs_addon/formatters/base.rb +4 -2
- data/lib/semantic_logger_ecs_addon/formatters/json.rb +2 -2
- data/lib/semantic_logger_ecs_addon/formatters/raw.rb +1 -1
- data/lib/semantic_logger_ecs_addon/identity.rb +1 -1
- data/lib/semantic_logger_ecs_addon/sequel.rb +3 -0
- data/lib/semantic_logger_ecs_addon.rb +3 -7
- data/lib/sequel/database.rb +30 -32
- data/lib/sequel/railties/controller_runtime.rb +63 -65
- data.tar.gz.sig +0 -0
- metadata +17 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce08af24030c70387cfde3b983fe760d9dffc7cb40fb474e6869c4b0e40c9e47
|
4
|
+
data.tar.gz: 92d2b5bf3a20403aac49fb04ef3f3db1b7153e44cd8137c9a4b1c9981a89e3d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 013e4c5ea2cc2b4e2dcdd7bbbd81e5b5fba592c7aa88edea58bef7968bb5e042e1be27e7895c424b0f6b3e05c3b2fb4c2281444f544789cfaef93327fb4bfa86
|
7
|
+
data.tar.gz: 5abe959f84462b47d8375711968e98e9b55ba5b2937974dbd72f80aada8ef2871924894381dc0cb101b66803afac15a9308dac630a7b7cc2fc9aa20532513c29
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
@@ -1,91 +1,89 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
#
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
# RailsSemanticLogger::Sequel::LogSubscriber.attach_to :sequel
|
91
|
-
# end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsSemanticLogger
|
4
|
+
module Sequel
|
5
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
6
|
+
class << self
|
7
|
+
attr_reader :logger
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.runtime= value
|
11
|
+
# ::ActiveRecord::RuntimeRegistry.sql_runtime = value
|
12
|
+
RequestStore.store[:sql_runtime] = value
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.runtime
|
16
|
+
# ::ActiveRecord::RuntimeRegistry.sql_runtime ||= 0
|
17
|
+
RequestStore.fetch(:sql_runtime) { 0 }
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.count= value
|
21
|
+
RequestStore.store[:sql_count] = value
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.count
|
25
|
+
RequestStore.fetch(:sql_count) { 0 }
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.reset_runtime
|
29
|
+
previous = runtime
|
30
|
+
self.runtime = 0
|
31
|
+
previous
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.reset_count
|
35
|
+
previous = count
|
36
|
+
self.count = 0
|
37
|
+
previous
|
38
|
+
end
|
39
|
+
|
40
|
+
def sql event
|
41
|
+
self.class.runtime += event.duration
|
42
|
+
self.class.count += 1
|
43
|
+
return unless logger.debug?
|
44
|
+
|
45
|
+
payload = event.payload
|
46
|
+
name = payload[:name]
|
47
|
+
|
48
|
+
log_payload = {sql: payload[:sql].squeeze(" ")}
|
49
|
+
log_payload[:binds] = bind_values payload unless (payload[:binds] || []).empty?
|
50
|
+
log_payload[:allocations] = event.allocations if event.respond_to? :allocations
|
51
|
+
log_payload[:cached] = event.payload[:cached]
|
52
|
+
|
53
|
+
log = {message: name, payload: log_payload, duration: event.duration}
|
54
|
+
|
55
|
+
# Log the location of the query itself.
|
56
|
+
if logger.send(:level_index) >= SemanticLogger.backtrace_level_index
|
57
|
+
log[:backtrace] = SemanticLogger::Utils.strip_backtrace caller
|
58
|
+
end
|
59
|
+
|
60
|
+
logger.debug log
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
@logger = SemanticLogger["Sequel"]
|
66
|
+
|
67
|
+
# When multiple values are received for a single bound field, it is converted into an array
|
68
|
+
def add_bind_value binds, key, value
|
69
|
+
key = key.downcase.to_sym unless key.nil?
|
70
|
+
value = (Array(binds[key]) << value) if binds.key? key
|
71
|
+
binds[key] = value
|
72
|
+
end
|
73
|
+
|
74
|
+
def logger
|
75
|
+
self.class.logger
|
76
|
+
end
|
77
|
+
|
78
|
+
def bind_values payload
|
79
|
+
binds = {}
|
80
|
+
binds = " " + payload[:binds].map { |col, v| [col.name, v] }
|
81
|
+
.inspect
|
82
|
+
payload[:binds].each { |col, value| add_bind_value binds, col.name, value }
|
83
|
+
binds
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
RailsSemanticLogger::Sequel::LogSubscriber.attach_to :sequel
|
@@ -164,7 +164,7 @@ module SemanticLoggerEcsAddon
|
|
164
164
|
end
|
165
165
|
|
166
166
|
def format_payload
|
167
|
-
if log.payload.respond_to?(:empty?) && !log.payload.empty?
|
167
|
+
if log.payload.respond_to?(:empty?) && !log.payload.empty? && log.payload.respond_to?(:has_key?)
|
168
168
|
self.formatted_payload = Utils::Hash[**log.payload]
|
169
169
|
|
170
170
|
rack_extract
|
@@ -176,11 +176,13 @@ module SemanticLoggerEcsAddon
|
|
176
176
|
def request
|
177
177
|
hash[:"http.request.id"] = formatted_payload.dig :request, :request_id
|
178
178
|
hash[:"http.request.body.content"] = formatted_payload.dig :request, :body
|
179
|
+
hash[:"http.request.body.content"] = hash[:"http.request.body.content"].to_json if hash[:"http.request.body.content"].respond_to?(:to_json)
|
179
180
|
hash[:"http.request.method"] = formatted_payload.dig :request, :method
|
180
181
|
end
|
181
182
|
|
182
183
|
def response
|
183
184
|
hash[:"http.response.body.content"] = formatted_payload.dig :response, :body
|
185
|
+
hash[:"http.response.body.content"] = hash[:"http.response.body.content"].to_json if hash[:"http.response.body.content"].respond_to?(:to_json)
|
184
186
|
hash[:"http.response.status_code"] = formatted_payload.dig :response, :status
|
185
187
|
end
|
186
188
|
|
@@ -203,7 +205,7 @@ module SemanticLoggerEcsAddon
|
|
203
205
|
def apm_agent_present_and_running?
|
204
206
|
return false unless defined?(::ElasticAPM)
|
205
207
|
|
206
|
-
ElasticAPM.running?
|
208
|
+
::ElasticAPM.running?
|
207
209
|
end
|
208
210
|
end
|
209
211
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "
|
3
|
+
require "oj"
|
4
4
|
|
5
5
|
module SemanticLoggerEcsAddon
|
6
6
|
module Formatters
|
@@ -12,7 +12,7 @@ module SemanticLoggerEcsAddon
|
|
12
12
|
|
13
13
|
# Returns log messages in JSON format
|
14
14
|
def call log, logger
|
15
|
-
super(log, logger)
|
15
|
+
Oj.dump(super(log, logger), nilnil: true, symbol_keys: true, escape_mode: :json, mode: :rails)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -82,7 +82,7 @@ module SemanticLoggerEcsAddon
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def extras
|
85
|
-
return unless formatted_payload.respond_to?(:empty?) && !formatted_payload.empty?
|
85
|
+
return unless formatted_payload.respond_to?(:empty?) && !formatted_payload.empty? && formatted_payload.respond_to?(:has_key?)
|
86
86
|
|
87
87
|
hash.merge! formatted_payload.except(:request, :response, :user)
|
88
88
|
end
|
@@ -5,7 +5,7 @@ module SemanticLoggerEcsAddon
|
|
5
5
|
module Identity
|
6
6
|
NAME = "semantic_logger_ecs_addon"
|
7
7
|
LABEL = "Semantic Logger Ecs Addon"
|
8
|
-
VERSION = "0.1.
|
8
|
+
VERSION = "0.1.13"
|
9
9
|
VERSION_LABEL = "#{LABEL} #{VERSION}"
|
10
10
|
SUMMARY = "A semantic logger formatter that formats the logs according to Elastic Common Schema, adds APM trace data if ElasticAPM is enabled and instruments sequel logs for rails if available."
|
11
11
|
end
|
@@ -2,15 +2,11 @@
|
|
2
2
|
|
3
3
|
require "pathname"
|
4
4
|
require "zeitwerk"
|
5
|
-
# if defined?(Sequel)
|
6
|
-
# if defined? ActiveSupport::LogSubscriber
|
7
|
-
# require_relative "rails_semantic_logger/sequel/log_subscriber"
|
8
|
-
# end
|
9
|
-
# require_relative "sequel/database" if defined?(ActiveSupport::Notifications)
|
10
|
-
# require_relative "sequel/railties/controller_runtime" if defined?(ActionController)
|
11
|
-
# end
|
12
5
|
|
13
6
|
loader = Zeitwerk::Loader.for_gem
|
7
|
+
loader.ignore "#{__dir__}/rails_semantic_logger"
|
8
|
+
loader.ignore "#{__dir__}/sequel"
|
9
|
+
loader.ignore "#{__dir__}/semantic_logger_ecs_addon/sequel.rb"
|
14
10
|
loader.setup
|
15
11
|
|
16
12
|
# Main namespace.
|
data/lib/sequel/database.rb
CHANGED
@@ -1,35 +1,33 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
# require "sequel/database/logging"
|
3
|
+
require "sequel/database/logging"
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
5
|
+
module Sequel
|
6
|
+
class Database
|
7
|
+
def log_connection_yield sql, conn, args = nil
|
8
|
+
log_connection_info = (connection_info conn if conn && log_connection_info)
|
9
|
+
log_args = ("; #{args.inspect}" if args)
|
10
|
+
sql_for_log = "#{log_connection_info}#{sql}#{log_args}"
|
11
|
+
start = Time.now
|
12
|
+
begin
|
13
|
+
::ActiveSupport::Notifications.instrument(
|
14
|
+
"sql.sequel",
|
15
|
+
sql: sql,
|
16
|
+
name: self.class,
|
17
|
+
binds: args
|
18
|
+
) do
|
19
|
+
yield
|
20
|
+
end
|
21
|
+
rescue StandardError => error
|
22
|
+
log_exception error, sql_for_log unless @loggers.empty?
|
23
|
+
raise
|
24
|
+
ensure
|
25
|
+
log_duration Time.now - start, sql_for_log unless error || @loggers.empty?
|
26
|
+
end
|
27
|
+
end
|
29
28
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
# end
|
29
|
+
def log_yield sql, args = nil, &block
|
30
|
+
log_connection_yield(sql, nil, args, &block)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,65 +1,63 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
# ActionController::API.include Sequel::Railties::ControllerRuntime
|
65
|
-
# end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/module/attr_internal"
|
4
|
+
unless defined?(RailsSemanticLogger::Sequel::LogSubscriber)
|
5
|
+
require_relative "../../rails_semantic_logger/sequel/log_subscriber"
|
6
|
+
end
|
7
|
+
|
8
|
+
module Sequel
|
9
|
+
module Railties
|
10
|
+
module ControllerRuntime
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def log_process_action payload
|
15
|
+
messages = super
|
16
|
+
db_runtime = payload[:db_runtime]
|
17
|
+
db_query_count = payload[:db_query_count]
|
18
|
+
if db_runtime && db_query_count
|
19
|
+
messages << (format "Sequel: %.1fms & %d queries", db_runtime.to_f, db_query_count)
|
20
|
+
end
|
21
|
+
messages
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
attr_internal :db_runtime, :db_query_count
|
28
|
+
|
29
|
+
def process_action action, *args
|
30
|
+
# We also need to reset the runtime before each action
|
31
|
+
# because of queries in middleware or in cases we are streaming
|
32
|
+
# and it won't be cleaned up by the method below.
|
33
|
+
RailsSemanticLogger::Sequel::LogSubscriber.reset_runtime
|
34
|
+
RailsSemanticLogger::Sequel::LogSubscriber.reset_count
|
35
|
+
super
|
36
|
+
end
|
37
|
+
|
38
|
+
def cleanup_view_runtime
|
39
|
+
if logger && logger.info?
|
40
|
+
db_rt_before_render = RailsSemanticLogger::Sequel::LogSubscriber.reset_runtime
|
41
|
+
self.db_runtime = (db_runtime || 0) + db_rt_before_render
|
42
|
+
runtime = super
|
43
|
+
db_rt_after_render = RailsSemanticLogger::Sequel::LogSubscriber.reset_runtime
|
44
|
+
self.db_runtime += db_rt_after_render
|
45
|
+
runtime - db_rt_after_render
|
46
|
+
else
|
47
|
+
super
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def append_info_to_payload payload
|
52
|
+
super
|
53
|
+
payload[:db_runtime] =
|
54
|
+
(db_runtime || 0) + RailsSemanticLogger::Sequel::LogSubscriber.reset_runtime
|
55
|
+
payload[:db_query_count] =
|
56
|
+
(db_query_count || 0) + RailsSemanticLogger::Sequel::LogSubscriber.reset_count
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
ActionController::Base.include Sequel::Railties::ControllerRuntime
|
63
|
+
ActionController::API.include Sequel::Railties::ControllerRuntime
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: semantic_logger_ecs_addon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sajeev Ramasamy
|
@@ -34,8 +34,22 @@ cert_chain:
|
|
34
34
|
vYrYpx2LhW5Xd0nPdr4aI+5BUqGdI7Ypl3BqNHZljUUlieiWu7rU0MhIZgQ1lIMa
|
35
35
|
pQreMxz5itbb+2KmpdBlkm8nuRoFv0jrL5LR+7+UBRknGsHXaEQQcQ==
|
36
36
|
-----END CERTIFICATE-----
|
37
|
-
date: 2021-11-
|
37
|
+
date: 2021-11-07 00:00:00.000000000 Z
|
38
38
|
dependencies:
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
name: oj
|
41
|
+
requirement: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - "~>"
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.13'
|
46
|
+
type: :runtime
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - "~>"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '3.13'
|
39
53
|
- !ruby/object:Gem::Dependency
|
40
54
|
name: request_store
|
41
55
|
requirement: !ruby/object:Gem::Requirement
|
@@ -95,6 +109,7 @@ files:
|
|
95
109
|
- lib/semantic_logger_ecs_addon/formatters/json.rb
|
96
110
|
- lib/semantic_logger_ecs_addon/formatters/raw.rb
|
97
111
|
- lib/semantic_logger_ecs_addon/identity.rb
|
112
|
+
- lib/semantic_logger_ecs_addon/sequel.rb
|
98
113
|
- lib/semantic_logger_ecs_addon/utils/backtrace_cleaner.rb
|
99
114
|
- lib/semantic_logger_ecs_addon/utils/hash.rb
|
100
115
|
- lib/sequel/database.rb
|
metadata.gz.sig
CHANGED
Binary file
|