foam-ruby 0.1.0.alpha3 → 0.1.0.alpha5

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: 4757e034451eb4a545eb37b3d9fefcb4d1efc64748ce9a09984f27bd11882666
4
- data.tar.gz: 4794bba41f510f12bf74f54a59b48ce5f7d9756eb76836ba8bc5835fc24a2abe
3
+ metadata.gz: 57eba0f562797a93af9440bf8cc80c2e80009200837d5ac647e01c832a85e07e
4
+ data.tar.gz: 9c68ec68a95f652a90840cb21c285746ca317724c92b10ba272e25708210104e
5
5
  SHA512:
6
- metadata.gz: 75b987b723c9566e71e38acc0f75b92ae4893cb5de83ebf1667629d0fd5b5cb7937e95e36ddbc66744a97439bdaa6b9227bfbb09431d6f5b97445f031b75d4d9
7
- data.tar.gz: 3abca311a34a3d1cf780416fb6f362369c4a797d87f9513f1244d6e021b485bc063ec224868196512e79f326c6bfc32e26342f9f9789b8e5cd0a7cb7735678ac
6
+ metadata.gz: 2f8f50ce67167cdb2e48382c6166abd767b22aa86b6ff01cc3762fa5fa8305cea5ce766b06877c6a257878b6e52c64cb063fc521e2f7b3a3cf41645e9fd306e7
7
+ data.tar.gz: 8ab6d387a971d5111e3ee7b864715eb15872ab976e5ab7f06a215b0d3e6c943e401050a883f725f35bfb46d4c8e6db1760eb40d2236da586d78cf76b267b8e9e
data/README.md CHANGED
@@ -35,17 +35,6 @@ That's it. The gem will:
35
35
 
36
36
  ## Configuration
37
37
 
38
- ### Block syntax
39
-
40
- ```ruby
41
- Foam::Ruby.configure do |c|
42
- c.token = ENV["FOAM_API_TOKEN"]
43
- c.service_name = "my-rails-app"
44
- c.traces_enabled = true
45
- c.logs_enabled = true
46
- end
47
- ```
48
-
49
38
  ### Environment variables
50
39
 
51
40
  | Variable | Description | Default |
@@ -95,20 +84,13 @@ rescue => e
95
84
  end
96
85
  ```
97
86
 
98
- ### Additional Instrumentations
87
+ ### Sentry Integration
99
88
 
100
- Pass extra OpenTelemetry instrumentations:
89
+ If Sentry is in your Gemfile, the gem automatically bridges `Sentry.capture_exception` to forward errors to Foam. No configuration needed.
101
90
 
102
- ```ruby
103
- Foam::Ruby.init(
104
- token: ENV["FOAM_API_TOKEN"],
105
- service_name: "my-rails-app",
106
- instrumentations: [
107
- "OpenTelemetry::Instrumentation::Sidekiq",
108
- "OpenTelemetry::Instrumentation::Redis",
109
- ]
110
- )
111
- ```
91
+ ### Production Only
92
+
93
+ The gem only initializes in production (`RAILS_ENV=production` or `RACK_ENV=production`). In development and test, all calls are silent no-ops — zero overhead, zero side effects.
112
94
 
113
95
  ## Rails Compatibility
114
96
 
@@ -6,8 +6,7 @@ module Foam
6
6
  FOAM_OTEL_ENDPOINT = "https://otel.api.foam.ai"
7
7
 
8
8
  attr_accessor :token, :service_name, :endpoint,
9
- :traces_enabled, :logs_enabled,
10
- :additional_instrumentations
9
+ :traces_enabled, :logs_enabled
11
10
 
12
11
  def initialize
13
12
  @token = ENV["FOAM_API_TOKEN"]
@@ -15,7 +14,6 @@ module Foam
15
14
  @endpoint = ENV.fetch("FOAM_OTEL_ENDPOINT", FOAM_OTEL_ENDPOINT)
16
15
  @traces_enabled = true
17
16
  @logs_enabled = true
18
- @additional_instrumentations = []
19
17
  end
20
18
 
21
19
  def otel_headers
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Foam
4
4
  module Ruby
5
- VERSION = "0.1.0.alpha3"
5
+ VERSION = "0.1.0.alpha5"
6
6
  end
7
7
  end
data/lib/foam/ruby.rb CHANGED
@@ -1,45 +1,45 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "opentelemetry/sdk"
4
- require "opentelemetry-logs-api"
5
-
6
3
  require_relative "ruby/version"
7
4
  require_relative "ruby/configuration"
8
- require_relative "ruby/otel_setup"
9
- require_relative "ruby/log_subscriber"
10
5
 
11
6
  module Foam
12
7
  module Ruby
13
8
  class << self
14
- def configuration
15
- @configuration ||= Configuration.new
16
- end
17
-
18
- def configure
19
- yield(configuration) if block_given?
20
- configuration
9
+ def initialized?
10
+ @initialized == true
21
11
  end
22
12
 
23
13
  def init(token: nil, service_name: nil, endpoint: nil, **options)
24
- configure do |c|
25
- c.token = token if token
26
- c.service_name = service_name if service_name
27
- c.endpoint = endpoint if endpoint
28
- c.traces_enabled = options.fetch(:traces, true)
29
- c.logs_enabled = options.fetch(:logs, true)
30
- c.additional_instrumentations = options[:instrumentations] || []
31
- end
14
+ return unless production?
15
+
16
+ config = configuration
17
+ config.token = token if token
18
+ config.service_name = service_name if service_name
19
+ config.endpoint = endpoint if endpoint
20
+ config.traces_enabled = options.fetch(:traces, true)
21
+ config.logs_enabled = options.fetch(:logs, true)
32
22
 
33
- unless configuration.valid?
23
+ unless config.valid?
34
24
  warn "[foam-ruby] Missing token — set FOAM_API_TOKEN or pass token: to Foam::Ruby.init"
35
25
  return
36
26
  end
37
27
 
38
- OtelSetup.configure!(configuration)
39
- schedule_logger_attach! if configuration.logs_enabled
28
+ require_relative "ruby/otel_setup"
29
+ require_relative "ruby/log_subscriber"
30
+
31
+ OtelSetup.configure!(config)
32
+ schedule_logger_attach! if config.logs_enabled
33
+ bridge_sentry!
34
+ @initialized = true
35
+ rescue Exception => e # rubocop:disable Lint/RescueException
36
+ warn "[foam-ruby] init failed: #{e.message}"
37
+ @initialized = false
40
38
  end
41
39
 
42
40
  def capture_exception(error, attributes: {})
41
+ return unless initialized?
42
+
43
43
  span = OpenTelemetry::Trace.current_span
44
44
  return unless span && span.context.valid?
45
45
 
@@ -55,12 +55,25 @@ module Foam
55
55
 
56
56
  span.record_exception(error, attributes: attrs)
57
57
  span.status = OpenTelemetry::Trace::Status.error(error.message)
58
- rescue StandardError => e
59
- warn "[foam-ruby] capture_exception failed: #{e.message}"
58
+ rescue Exception # rubocop:disable Lint/RescueException
59
+ nil
60
60
  end
61
61
 
62
62
  private
63
63
 
64
+ def configuration
65
+ @configuration ||= Configuration.new
66
+ rescue Exception # rubocop:disable Lint/RescueException
67
+ Configuration.new
68
+ end
69
+
70
+ def production?
71
+ env = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
72
+ env.downcase == "production"
73
+ rescue Exception # rubocop:disable Lint/RescueException
74
+ false
75
+ end
76
+
64
77
  def schedule_logger_attach!
65
78
  return unless defined?(::Rails)
66
79
 
@@ -69,15 +82,31 @@ module Foam
69
82
  else
70
83
  do_attach_otel_logger!
71
84
  end
72
- rescue StandardError => e
73
- warn "[foam-ruby] Failed to schedule OTel logger attach: #{e.message}"
85
+ rescue Exception # rubocop:disable Lint/RescueException
86
+ nil
87
+ end
88
+
89
+ def bridge_sentry!
90
+ return unless defined?(::Rails) && ::Rails.respond_to?(:application) && ::Rails.application
91
+
92
+ ::Rails.application.config.after_initialize do
93
+ next unless defined?(::Sentry)
94
+
95
+ ::Sentry.singleton_class.prepend(Module.new do
96
+ def capture_exception(exception, **options, &block)
97
+ Foam::Ruby.capture_exception(exception) if exception.is_a?(::Exception)
98
+ super
99
+ end
100
+ end)
101
+ end
102
+ rescue Exception # rubocop:disable Lint/RescueException
103
+ nil
74
104
  end
75
105
 
76
106
  def do_attach_otel_logger!
77
107
  return unless ::Rails.respond_to?(:logger) && ::Rails.logger
78
108
 
79
109
  unless OpenTelemetry.logger_provider.is_a?(OpenTelemetry::SDK::Logs::LoggerProvider)
80
- warn "[foam-ruby] OTel logger provider not configured — call Foam::Ruby.init before attaching logger"
81
110
  return
82
111
  end
83
112
 
@@ -88,8 +117,8 @@ module Foam
88
117
  elsif defined?(::ActiveSupport::Logger) && ::ActiveSupport::Logger.respond_to?(:broadcast)
89
118
  ::Rails.logger.extend(::ActiveSupport::Logger.broadcast(otel_logger))
90
119
  end
91
- rescue StandardError => e
92
- warn "[foam-ruby] Failed to attach OTel logger to Rails.logger: #{e.message}"
120
+ rescue Exception # rubocop:disable Lint/RescueException
121
+ nil
93
122
  end
94
123
  end
95
124
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foam-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.alpha3
4
+ version: 0.1.0.alpha5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Foam