railswatch_gem 0.2.3 → 0.3.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 +15 -1
- data/lib/railswatch_gem/railtie.rb +32 -2
- 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: '02985088f2afd6a08aeb35ac5f6b5d95d7e65287b8d9be0848b01d654ee1253c'
|
|
4
|
+
data.tar.gz: 1274fcc8d855b6ca31a97539b88b4ccc4bacc629bbaf0b8cd26640927ff506c2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 73a7baa455c8ab7c2e527daf48fc48dd2bff19b4ca9d67584c424351fd2ff92092b15ee6655571ea2c43641d623c1645b0af1265370d8e28cb84c7554b75838e
|
|
7
|
+
data.tar.gz: 0d06b74786c73c587dc3f1841f534b3ab86e4f14241f5476eb7e7012d9cd13b98f5b56e02f0edc9f9fc9dff1ef203737f13fc439d4567199558bc7739128f894
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
module RailswatchGem
|
|
4
4
|
class Configuration
|
|
5
|
-
attr_accessor :api_key, :service_name, :ingest_url, :enabled
|
|
5
|
+
attr_accessor :api_key, :service_name, :ingest_url, :enabled,
|
|
6
|
+
:track_users, :identify_user
|
|
6
7
|
|
|
7
8
|
def initialize
|
|
8
9
|
@api_key = ENV["RAILSWATCH_API_KEY"]
|
|
@@ -10,6 +11,19 @@ module RailswatchGem
|
|
|
10
11
|
@service_name = ENV["RAILSWATCH_SERVICE_NAME"] || default_name
|
|
11
12
|
@ingest_url = ENV["RAILSWATCH_INGEST_URL"] || "https://ingest.railswatch.com/api/v1/ingest"
|
|
12
13
|
@enabled = true
|
|
14
|
+
@track_users = true
|
|
15
|
+
@identify_user = DEFAULT_IDENTIFY_USER
|
|
13
16
|
end
|
|
17
|
+
|
|
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
|
+
DEFAULT_IDENTIFY_USER = ->(user) {
|
|
22
|
+
attrs = {}
|
|
23
|
+
attrs["enduser.id"] = user.id.to_s if user.respond_to?(:id)
|
|
24
|
+
attrs["enduser.email"] = user.email.to_s if user.respond_to?(:email)
|
|
25
|
+
attrs["enduser.role"] = user.role.to_s if user.respond_to?(:role)
|
|
26
|
+
attrs
|
|
27
|
+
}
|
|
14
28
|
end
|
|
15
29
|
end
|
|
@@ -2,14 +2,44 @@
|
|
|
2
2
|
|
|
3
3
|
module RailswatchGem
|
|
4
4
|
class Railtie < ::Rails::Railtie
|
|
5
|
-
# Automatically boot the instrumentation when Rails finishes initializing.
|
|
6
5
|
config.after_initialize do
|
|
7
|
-
# Check if properly configured (e.g. via ENV vars or initializer)
|
|
8
6
|
if RailswatchGem.config.api_key.present?
|
|
9
7
|
RailswatchGem.start!
|
|
10
8
|
else
|
|
11
9
|
puts "[Railswatch] API Key not found. Observability disabled."
|
|
12
10
|
end
|
|
13
11
|
end
|
|
12
|
+
|
|
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
|
+
initializer "railswatch.user_identification" do
|
|
17
|
+
ActiveSupport.on_load(:action_controller_base) do
|
|
18
|
+
around_action :railswatch_identify_user
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def railswatch_identify_user
|
|
23
|
+
yield
|
|
24
|
+
ensure
|
|
25
|
+
return unless RailswatchGem.config.track_users
|
|
26
|
+
return unless respond_to?(:current_user, true)
|
|
27
|
+
|
|
28
|
+
begin
|
|
29
|
+
user = current_user
|
|
30
|
+
return unless user
|
|
31
|
+
|
|
32
|
+
span = OpenTelemetry::Trace.current_span
|
|
33
|
+
return unless span&.recording?
|
|
34
|
+
|
|
35
|
+
attrs = RailswatchGem.config.identify_user.call(user)
|
|
36
|
+
attrs.each { |key, value| span.set_attribute(key, value) if value.present? }
|
|
37
|
+
rescue => e
|
|
38
|
+
# Never break the app for instrumentation
|
|
39
|
+
Rails.logger.debug { "[Railswatch] User identification skipped: #{e.message}" } if defined?(Rails.logger)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
14
44
|
end
|
|
15
45
|
end
|