apm_bro 0.1.11 → 0.1.12
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 +4 -4
- data/lib/apm_bro/error_middleware.rb +2 -1
- data/lib/apm_bro/job_sql_tracking_middleware.rb +2 -0
- data/lib/apm_bro/job_subscriber.rb +4 -2
- data/lib/apm_bro/logger.rb +128 -0
- data/lib/apm_bro/sql_tracking_middleware.rb +3 -0
- data/lib/apm_bro/subscriber.rb +4 -2
- data/lib/apm_bro/version.rb +1 -1
- data/lib/apm_bro.rb +6 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: caf0f22a5f4a72ddab15c8ec54dd82eb421786dd02568706eab5a906e2543a6c
|
|
4
|
+
data.tar.gz: 8b944014790250fc71107ebe01e789b882f9f9d836904f30edcc03ebb734fd41
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 750db0f63cd9e0d6bb01f3c45db1efcd81922a5b532ed0226d9948dcf0ae4b709f6cec442403f469064bc334b45bcae9d203b4df4a90cefc6ba334a0b4add919
|
|
7
|
+
data.tar.gz: 626c32ee6ac593b581a1fbd3e9a5086382b877212280d8265f774600bd6fea79c87957aa6c50c6c3725baa1835cc899f1a3fa1af7f9de0713e3bf63b34fb7d0f
|
|
@@ -5,6 +5,8 @@ module ApmBro
|
|
|
5
5
|
def self.subscribe!
|
|
6
6
|
# Start SQL tracking when a job begins - use the start event, not the complete event
|
|
7
7
|
ActiveSupport::Notifications.subscribe("perform_start.active_job") do |name, started, finished, _unique_id, data|
|
|
8
|
+
# Clear logs for this job
|
|
9
|
+
ApmBro.logger.clear
|
|
8
10
|
ApmBro::SqlSubscriber.start_request_tracking
|
|
9
11
|
end
|
|
10
12
|
rescue StandardError
|
|
@@ -34,7 +34,8 @@ module ApmBro
|
|
|
34
34
|
rails_env: safe_rails_env,
|
|
35
35
|
host: safe_host,
|
|
36
36
|
memory_usage: memory_usage_mb,
|
|
37
|
-
gc_stats: gc_stats
|
|
37
|
+
gc_stats: gc_stats,
|
|
38
|
+
logs: ApmBro.logger.logs
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
client.post_metric(event_name: name, payload: payload)
|
|
@@ -70,7 +71,8 @@ module ApmBro
|
|
|
70
71
|
rails_env: safe_rails_env,
|
|
71
72
|
host: safe_host,
|
|
72
73
|
memory_usage: memory_usage_mb,
|
|
73
|
-
gc_stats: gc_stats
|
|
74
|
+
gc_stats: gc_stats,
|
|
75
|
+
logs: ApmBro.logger.logs
|
|
74
76
|
}
|
|
75
77
|
|
|
76
78
|
event_name = exception&.class&.name || "ActiveJob::Exception"
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ApmBro
|
|
4
|
+
class Logger
|
|
5
|
+
SEVERITY_LEVELS = {
|
|
6
|
+
debug: 0,
|
|
7
|
+
info: 1,
|
|
8
|
+
warn: 2,
|
|
9
|
+
error: 3,
|
|
10
|
+
fatal: 4
|
|
11
|
+
}.freeze
|
|
12
|
+
|
|
13
|
+
# ANSI color codes
|
|
14
|
+
COLOR_RESET = "\033[0m".freeze
|
|
15
|
+
COLOR_DEBUG = "\033[36m".freeze # Cyan
|
|
16
|
+
COLOR_INFO = "\033[32m".freeze # Green
|
|
17
|
+
COLOR_WARN = "\033[33m".freeze # Yellow
|
|
18
|
+
COLOR_ERROR = "\033[31m".freeze # Red
|
|
19
|
+
COLOR_FATAL = "\033[35m".freeze # Magenta
|
|
20
|
+
|
|
21
|
+
def initialize
|
|
22
|
+
@thread_logs_key = :apm_bro_logs
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def debug(message)
|
|
26
|
+
log(:debug, message)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def info(message)
|
|
30
|
+
log(:info, message)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def warn(message)
|
|
34
|
+
log(:warn, message)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def error(message)
|
|
38
|
+
log(:error, message)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def fatal(message)
|
|
42
|
+
log(:fatal, message)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Get all logs for the current thread
|
|
46
|
+
def logs
|
|
47
|
+
Thread.current[@thread_logs_key] || []
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Clear logs for the current thread
|
|
51
|
+
def clear
|
|
52
|
+
Thread.current[@thread_logs_key] = []
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def log(severity, message)
|
|
58
|
+
timestamp = Time.now.utc
|
|
59
|
+
log_entry = {
|
|
60
|
+
sev: severity.to_s,
|
|
61
|
+
msg: message.to_s,
|
|
62
|
+
time: timestamp.iso8601(3) # Include milliseconds for better precision
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
# Store in thread-local storage
|
|
66
|
+
Thread.current[@thread_logs_key] ||= []
|
|
67
|
+
Thread.current[@thread_logs_key] << log_entry
|
|
68
|
+
|
|
69
|
+
# Print the message immediately
|
|
70
|
+
print_log(severity, message, timestamp)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def print_log(severity, message, timestamp)
|
|
74
|
+
formatted_message = format_log_message(severity, message, timestamp)
|
|
75
|
+
|
|
76
|
+
if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
|
|
77
|
+
# Use Rails logger if available (Rails handles its own color formatting)
|
|
78
|
+
case severity
|
|
79
|
+
when :debug
|
|
80
|
+
Rails.logger.debug(formatted_message)
|
|
81
|
+
when :info
|
|
82
|
+
Rails.logger.info(formatted_message)
|
|
83
|
+
when :warn
|
|
84
|
+
Rails.logger.warn(formatted_message)
|
|
85
|
+
when :error
|
|
86
|
+
Rails.logger.error(formatted_message)
|
|
87
|
+
when :fatal
|
|
88
|
+
Rails.logger.fatal(formatted_message)
|
|
89
|
+
end
|
|
90
|
+
else
|
|
91
|
+
# Fallback to stdout with colors
|
|
92
|
+
colored_message = format_log_message_with_color(severity, message, timestamp)
|
|
93
|
+
$stdout.puts(colored_message)
|
|
94
|
+
end
|
|
95
|
+
rescue StandardError
|
|
96
|
+
# Never let logging break the application
|
|
97
|
+
$stdout.puts("[ApmBro] #{severity.to_s.upcase}: #{message}")
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def format_log_message(severity, message, timestamp)
|
|
101
|
+
"[ApmBro] #{timestamp.iso8601(3)} #{severity.to_s.upcase}: #{message}"
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def format_log_message_with_color(severity, message, timestamp)
|
|
105
|
+
color = color_for_severity(severity)
|
|
106
|
+
severity_str = severity.to_s.upcase
|
|
107
|
+
"#{color}[ApmBro] #{timestamp.iso8601(3)} #{severity_str}: #{message}#{COLOR_RESET}"
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def color_for_severity(severity)
|
|
111
|
+
case severity
|
|
112
|
+
when :debug
|
|
113
|
+
COLOR_DEBUG
|
|
114
|
+
when :info
|
|
115
|
+
COLOR_INFO
|
|
116
|
+
when :warn
|
|
117
|
+
COLOR_WARN
|
|
118
|
+
when :error
|
|
119
|
+
COLOR_ERROR
|
|
120
|
+
when :fatal
|
|
121
|
+
COLOR_FATAL
|
|
122
|
+
else
|
|
123
|
+
COLOR_RESET
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
@@ -7,6 +7,9 @@ module ApmBro
|
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
def call(env)
|
|
10
|
+
# Clear logs for this request
|
|
11
|
+
ApmBro.logger.clear
|
|
12
|
+
|
|
10
13
|
# Start SQL tracking for this request
|
|
11
14
|
if defined?(ApmBro::SqlSubscriber)
|
|
12
15
|
puts "Starting SQL tracking for request: #{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
|
data/lib/apm_bro/subscriber.rb
CHANGED
|
@@ -95,7 +95,8 @@ module ApmBro
|
|
|
95
95
|
exception_class: (exception_class || exception_obj&.class&.name),
|
|
96
96
|
message: (exception_message || exception_obj&.message).to_s[0, 1000],
|
|
97
97
|
backtrace: backtrace,
|
|
98
|
-
error: true
|
|
98
|
+
error: true,
|
|
99
|
+
logs: ApmBro.logger.logs
|
|
99
100
|
}
|
|
100
101
|
|
|
101
102
|
event_name = (exception_class || exception_obj&.class&.name || "exception").to_s
|
|
@@ -134,7 +135,8 @@ module ApmBro
|
|
|
134
135
|
view_events: view_events,
|
|
135
136
|
view_performance: view_performance,
|
|
136
137
|
memory_events: memory_events,
|
|
137
|
-
memory_performance: memory_performance
|
|
138
|
+
memory_performance: memory_performance,
|
|
139
|
+
logs: ApmBro.logger.logs
|
|
138
140
|
}
|
|
139
141
|
client.post_metric(event_name: name, payload: payload)
|
|
140
142
|
end
|
data/lib/apm_bro/version.rb
CHANGED
data/lib/apm_bro.rb
CHANGED
|
@@ -18,6 +18,7 @@ module ApmBro
|
|
|
18
18
|
autoload :MemoryHelpers, "apm_bro/memory_helpers"
|
|
19
19
|
autoload :JobSubscriber, "apm_bro/job_subscriber"
|
|
20
20
|
autoload :JobSqlTrackingMiddleware, "apm_bro/job_sql_tracking_middleware"
|
|
21
|
+
autoload :Logger, "apm_bro/logger"
|
|
21
22
|
begin
|
|
22
23
|
require "apm_bro/railtie"
|
|
23
24
|
rescue LoadError
|
|
@@ -45,4 +46,9 @@ module ApmBro
|
|
|
45
46
|
SecureRandom.uuid
|
|
46
47
|
end
|
|
47
48
|
end
|
|
49
|
+
|
|
50
|
+
# Returns the logger instance for storing and retrieving log messages
|
|
51
|
+
def self.logger
|
|
52
|
+
@logger ||= Logger.new
|
|
53
|
+
end
|
|
48
54
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: apm_bro
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.12
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Emanuel Comsa
|
|
@@ -29,6 +29,7 @@ files:
|
|
|
29
29
|
- lib/apm_bro/job_sql_tracking_middleware.rb
|
|
30
30
|
- lib/apm_bro/job_subscriber.rb
|
|
31
31
|
- lib/apm_bro/lightweight_memory_tracker.rb
|
|
32
|
+
- lib/apm_bro/logger.rb
|
|
32
33
|
- lib/apm_bro/memory_helpers.rb
|
|
33
34
|
- lib/apm_bro/memory_leak_detector.rb
|
|
34
35
|
- lib/apm_bro/memory_tracking_subscriber.rb
|