railswatch_gem 0.3.0 → 0.4.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 +4 -4
- data/lib/railswatch_gem/configuration.rb +3 -4
- data/lib/railswatch_gem/railtie.rb +26 -4
- data/lib/railswatch_gem/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 51d84f78da3a10be4c22945395c3802e6d7e6622fca6da46db2f51a59c73af13
|
|
4
|
+
data.tar.gz: '018bf2d1fbaed1fdc4f4e97caf4de19ea4d4a04f70b3e8c149a058c5137d4430'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 296307ae6dfc447f56e0c9614d69c236324f23bf1209a2e6a23b1f09ce51885644216b7d99ed5b01fd35b163ac0bc72c161d021a218eba2735b22d9afac7705a
|
|
7
|
+
data.tar.gz: 499075bcc71e108cfd42f18c23337a05a32ee1f624ac5e8b1faf9b63bb7addb404e8773646f7fa0ee9799e99cd8bb2819d7cd1fdbe66c4017088b0622a6d505d
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
module RailswatchGem
|
|
4
4
|
class Configuration
|
|
5
5
|
attr_accessor :api_key, :service_name, :ingest_url, :enabled,
|
|
6
|
-
:track_users, :identify_user
|
|
6
|
+
:track_users, :identify_user,
|
|
7
|
+
:track_query_source
|
|
7
8
|
|
|
8
9
|
def initialize
|
|
9
10
|
@api_key = ENV["RAILSWATCH_API_KEY"]
|
|
@@ -13,11 +14,9 @@ module RailswatchGem
|
|
|
13
14
|
@enabled = true
|
|
14
15
|
@track_users = true
|
|
15
16
|
@identify_user = DEFAULT_IDENTIFY_USER
|
|
17
|
+
@track_query_source = true
|
|
16
18
|
end
|
|
17
19
|
|
|
18
|
-
# Default strategy: extract id and email if available.
|
|
19
|
-
# Customers can override with any proc that receives a user object
|
|
20
|
-
# and returns a Hash of OTel attribute key => value pairs.
|
|
21
20
|
DEFAULT_IDENTIFY_USER = ->(user) {
|
|
22
21
|
attrs = {}
|
|
23
22
|
attrs["enduser.id"] = user.id.to_s if user.respond_to?(:id)
|
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
module RailswatchGem
|
|
4
4
|
class Railtie < ::Rails::Railtie
|
|
5
|
+
# Paths that belong to the host application (not gems or Ruby internals)
|
|
6
|
+
APP_PATH_PATTERN = %r{/(app|lib|config)/}.freeze
|
|
7
|
+
IGNORE_PATH_PATTERN = %r{/vendor/|/railswatch_gem/}.freeze
|
|
8
|
+
MAX_STACKTRACE_FRAMES = 10
|
|
9
|
+
|
|
5
10
|
config.after_initialize do
|
|
6
11
|
if RailswatchGem.config.api_key.present?
|
|
7
12
|
RailswatchGem.start!
|
|
@@ -10,9 +15,6 @@ module RailswatchGem
|
|
|
10
15
|
end
|
|
11
16
|
end
|
|
12
17
|
|
|
13
|
-
# Auto-inject user identification into all controllers.
|
|
14
|
-
# Sets enduser.id (and optionally email/role) on the current OTel span
|
|
15
|
-
# so RailsWatch can group requests by user on the Users tab.
|
|
16
18
|
initializer "railswatch.user_identification" do
|
|
17
19
|
ActiveSupport.on_load(:action_controller_base) do
|
|
18
20
|
around_action :railswatch_identify_user
|
|
@@ -35,11 +37,31 @@ module RailswatchGem
|
|
|
35
37
|
attrs = RailswatchGem.config.identify_user.call(user)
|
|
36
38
|
attrs.each { |key, value| span.set_attribute(key, value) if value.present? }
|
|
37
39
|
rescue => e
|
|
38
|
-
# Never break the app for instrumentation
|
|
39
40
|
Rails.logger.debug { "[Railswatch] User identification skipped: #{e.message}" } if defined?(Rails.logger)
|
|
40
41
|
end
|
|
41
42
|
end
|
|
42
43
|
end
|
|
43
44
|
end
|
|
45
|
+
|
|
46
|
+
initializer "railswatch.query_source_location" do
|
|
47
|
+
ActiveSupport.on_load(:active_record) do
|
|
48
|
+
ActiveSupport::Notifications.subscribe("sql.active_record") do |*, payload|
|
|
49
|
+
next unless RailswatchGem.config.enabled
|
|
50
|
+
next unless RailswatchGem.config.track_query_source
|
|
51
|
+
|
|
52
|
+
span = OpenTelemetry::Trace.current_span
|
|
53
|
+
next unless span&.recording?
|
|
54
|
+
|
|
55
|
+
frames = caller_locations(0, 40)
|
|
56
|
+
.select { |f| f.path.match?(APP_PATH_PATTERN) && !f.path.match?(IGNORE_PATH_PATTERN) }
|
|
57
|
+
.first(MAX_STACKTRACE_FRAMES)
|
|
58
|
+
.map { |f| "#{f.path}:#{f.lineno} in `#{f.label}`" }
|
|
59
|
+
|
|
60
|
+
span.set_attribute("code.stacktrace", frames.join("\n")) if frames.any?
|
|
61
|
+
rescue => e
|
|
62
|
+
Rails.logger.debug { "[Railswatch] Query source capture skipped: #{e.message}" } if defined?(Rails.logger)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
44
66
|
end
|
|
45
67
|
end
|