railswatch_gem 0.2.2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab1600349c2d7702f4e7972b165296254f2aefcb5b4714bb7a45dd286f180065
4
- data.tar.gz: d1a43e912c06c23a56d32872bc9ccf31bb691ff66960c49dd02c6c736ad3b7e4
3
+ metadata.gz: '02985088f2afd6a08aeb35ac5f6b5d95d7e65287b8d9be0848b01d654ee1253c'
4
+ data.tar.gz: 1274fcc8d855b6ca31a97539b88b4ccc4bacc629bbaf0b8cd26640927ff506c2
5
5
  SHA512:
6
- metadata.gz: 57cec176ed6376bc0ea49d46fe04c2c288e1fef27b7d67c97ea8b2451b2a15d03133ab010bcab193a59e0f550435407206c29a9127aeb33f670a66832c245bc7
7
- data.tar.gz: 981333528bdad9a2e4fb5eaaeff5c155f2d3655a672a66afc6d516686f823429255e2e95942b975dbc91e501e119f8947f68333ae34b441fd0b430c45b827726
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailswatchGem
4
- VERSION = "0.2.2"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -29,6 +29,9 @@ module RailswatchGem
29
29
  return unless config.enabled
30
30
  return if @started
31
31
 
32
+ ENV['OTEL_EXPORTER_OTLP_PROTOCOL'] = 'http/json'
33
+ ENV['OTEL_EXPORTER_OTLP_COMPRESSION'] = 'gzip'
34
+
32
35
  OpenTelemetry::SDK.configure do |c|
33
36
  c.service_name = config.service_name
34
37
 
@@ -36,9 +39,7 @@ module RailswatchGem
36
39
  OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
37
40
  OpenTelemetry::Exporter::OTLP::Exporter.new(
38
41
  endpoint: config.ingest_url,
39
- headers: { "X-Railswatch-Token" => config.api_key },
40
- protocol: :http_json,
41
- compression: :gzip
42
+ headers: { "X-Railswatch-Token" => config.api_key }
42
43
  )
43
44
  )
44
45
  )
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.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Hammett