railswatch_gem 0.2.3 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bf72315894a0b5340f7b847d0a4c377137e2a9bae3940c6498dbb9f5bd7d642
4
- data.tar.gz: 690b95a780c37b2ab7b368c8e1b8462681f4bfcdca650c56237823985286e95f
3
+ metadata.gz: 51d84f78da3a10be4c22945395c3802e6d7e6622fca6da46db2f51a59c73af13
4
+ data.tar.gz: '018bf2d1fbaed1fdc4f4e97caf4de19ea4d4a04f70b3e8c149a058c5137d4430'
5
5
  SHA512:
6
- metadata.gz: 8968464f6cb0887b97fdc5796cc6c15dd02b6dd451542d545b1c58f9726b1036415a0fc702fdd01f4e3e60b165dde57d4f98d703873e21ffcfd952b2a6888c77
7
- data.tar.gz: 229a9d48d69681f448e24f798b1a7549258e604adc15bfd8c94f734193f8fe3813a3e830bb9850e9f8de53b1d764208b9a661601c8266a99dd942f3c07958f04
6
+ metadata.gz: 296307ae6dfc447f56e0c9614d69c236324f23bf1209a2e6a23b1f09ce51885644216b7d99ed5b01fd35b163ac0bc72c161d021a218eba2735b22d9afac7705a
7
+ data.tar.gz: 499075bcc71e108cfd42f18c23337a05a32ee1f624ac5e8b1faf9b63bb7addb404e8773646f7fa0ee9799e99cd8bb2819d7cd1fdbe66c4017088b0622a6d505d
@@ -2,7 +2,9 @@
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,
7
+ :track_query_source
6
8
 
7
9
  def initialize
8
10
  @api_key = ENV["RAILSWATCH_API_KEY"]
@@ -10,6 +12,17 @@ module RailswatchGem
10
12
  @service_name = ENV["RAILSWATCH_SERVICE_NAME"] || default_name
11
13
  @ingest_url = ENV["RAILSWATCH_INGEST_URL"] || "https://ingest.railswatch.com/api/v1/ingest"
12
14
  @enabled = true
15
+ @track_users = true
16
+ @identify_user = DEFAULT_IDENTIFY_USER
17
+ @track_query_source = true
13
18
  end
19
+
20
+ DEFAULT_IDENTIFY_USER = ->(user) {
21
+ attrs = {}
22
+ attrs["enduser.id"] = user.id.to_s if user.respond_to?(:id)
23
+ attrs["enduser.email"] = user.email.to_s if user.respond_to?(:email)
24
+ attrs["enduser.role"] = user.role.to_s if user.respond_to?(:role)
25
+ attrs
26
+ }
14
27
  end
15
28
  end
@@ -2,14 +2,66 @@
2
2
 
3
3
  module RailswatchGem
4
4
  class Railtie < ::Rails::Railtie
5
- # Automatically boot the instrumentation when Rails finishes initializing.
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
+
6
10
  config.after_initialize do
7
- # Check if properly configured (e.g. via ENV vars or initializer)
8
11
  if RailswatchGem.config.api_key.present?
9
12
  RailswatchGem.start!
10
13
  else
11
14
  puts "[Railswatch] API Key not found. Observability disabled."
12
15
  end
13
16
  end
17
+
18
+ initializer "railswatch.user_identification" do
19
+ ActiveSupport.on_load(:action_controller_base) do
20
+ around_action :railswatch_identify_user
21
+
22
+ private
23
+
24
+ def railswatch_identify_user
25
+ yield
26
+ ensure
27
+ return unless RailswatchGem.config.track_users
28
+ return unless respond_to?(:current_user, true)
29
+
30
+ begin
31
+ user = current_user
32
+ return unless user
33
+
34
+ span = OpenTelemetry::Trace.current_span
35
+ return unless span&.recording?
36
+
37
+ attrs = RailswatchGem.config.identify_user.call(user)
38
+ attrs.each { |key, value| span.set_attribute(key, value) if value.present? }
39
+ rescue => e
40
+ Rails.logger.debug { "[Railswatch] User identification skipped: #{e.message}" } if defined?(Rails.logger)
41
+ end
42
+ end
43
+ end
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
14
66
  end
15
67
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailswatchGem
4
- VERSION = "0.2.3"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railswatch_gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Hammett