bugstack 1.0.2 → 1.1.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.
@@ -1,97 +1,97 @@
1
- # frozen_string_literal: true
2
-
3
- require "digest/sha2"
4
-
5
- module Bugstack
6
- module Fingerprint
7
- # Generate a stable SHA-256 fingerprint for an error.
8
- #
9
- # @param exception_type [String]
10
- # @param file [String]
11
- # @param function [String]
12
- # @param line [Integer, nil]
13
- # @return [String] 16-char hex fingerprint
14
- def self.generate(exception_type, file, function, line = nil)
15
- parts = [exception_type, file, function]
16
- parts << line.to_s if line
17
- key = parts.join(":")
18
- Digest::SHA256.hexdigest(key)[0, 16]
19
- end
20
-
21
- # Extract file, function, and line from an exception's backtrace.
22
- #
23
- # @param exception [Exception]
24
- # @return [Array<(String, String, String, Integer)>]
25
- # [exception_type, file, function, line]
26
- def self.extract_location(exception)
27
- exception_type = exception.class.name
28
-
29
- bt = exception.backtrace
30
- return [exception_type, "", "", nil] if bt.nil? || bt.empty?
31
-
32
- # Parse the first backtrace line: "file:line:in `method'"
33
- first_line = bt.first
34
- if first_line =~ /\A(.+):(\d+):in [`'](.+)'\z/
35
- file = Regexp.last_match(1)
36
- line = Regexp.last_match(2).to_i
37
- function = Regexp.last_match(3)
38
- [exception_type, file, function, line]
39
- else
40
- [exception_type, first_line, "", nil]
41
- end
42
- end
43
-
44
- # Format an exception's backtrace as a string.
45
- #
46
- # @param exception [Exception]
47
- # @return [String]
48
- def self.format_backtrace(exception)
49
- bt = exception.backtrace
50
- return "#{exception.class}: #{exception.message}" if bt.nil? || bt.empty?
51
-
52
- lines = ["#{exception.class}: #{exception.message}"]
53
- bt.each { |frame| lines << " from #{frame}" }
54
- lines.join("\n")
55
- end
56
- end
57
-
58
- # Client-side error deduplicator.
59
- # Prevents the same error (by fingerprint) from being reported
60
- # more than once within a configurable time window.
61
- class Deduplicator
62
- def initialize(window: 300.0)
63
- @cache = {}
64
- @window = window
65
- @mutex = Mutex.new
66
- end
67
-
68
- # Check if an error should be sent. Thread-safe.
69
- #
70
- # @param fingerprint [String]
71
- # @return [Boolean]
72
- def should_send?(fingerprint)
73
- now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
74
-
75
- @mutex.synchronize do
76
- last_sent = @cache[fingerprint]
77
- if last_sent && (now - last_sent) < @window
78
- return false
79
- end
80
-
81
- @cache[fingerprint] = now
82
- cleanup(now)
83
- true
84
- end
85
- end
86
-
87
- def clear
88
- @mutex.synchronize { @cache.clear }
89
- end
90
-
91
- private
92
-
93
- def cleanup(now)
94
- @cache.delete_if { |_, ts| (now - ts) >= @window }
95
- end
96
- end
97
- end
1
+ # frozen_string_literal: true
2
+
3
+ require "digest/sha2"
4
+
5
+ module Bugstack
6
+ module Fingerprint
7
+ # Generate a stable SHA-256 fingerprint for an error.
8
+ #
9
+ # @param exception_type [String]
10
+ # @param file [String]
11
+ # @param function [String]
12
+ # @param line [Integer, nil]
13
+ # @return [String] 16-char hex fingerprint
14
+ def self.generate(exception_type, file, function, line = nil)
15
+ parts = [exception_type, file, function]
16
+ parts << line.to_s if line
17
+ key = parts.join(":")
18
+ Digest::SHA256.hexdigest(key)[0, 16]
19
+ end
20
+
21
+ # Extract file, function, and line from an exception's backtrace.
22
+ #
23
+ # @param exception [Exception]
24
+ # @return [Array<(String, String, String, Integer)>]
25
+ # [exception_type, file, function, line]
26
+ def self.extract_location(exception)
27
+ exception_type = exception.class.name
28
+
29
+ bt = exception.backtrace
30
+ return [exception_type, "", "", nil] if bt.nil? || bt.empty?
31
+
32
+ # Parse the first backtrace line: "file:line:in `method'"
33
+ first_line = bt.first
34
+ if first_line =~ /\A(.+):(\d+):in [`'](.+)'\z/
35
+ file = Regexp.last_match(1)
36
+ line = Regexp.last_match(2).to_i
37
+ function = Regexp.last_match(3)
38
+ [exception_type, file, function, line]
39
+ else
40
+ [exception_type, first_line, "", nil]
41
+ end
42
+ end
43
+
44
+ # Format an exception's backtrace as a string.
45
+ #
46
+ # @param exception [Exception]
47
+ # @return [String]
48
+ def self.format_backtrace(exception)
49
+ bt = exception.backtrace
50
+ return "#{exception.class}: #{exception.message}" if bt.nil? || bt.empty?
51
+
52
+ lines = ["#{exception.class}: #{exception.message}"]
53
+ bt.each { |frame| lines << " from #{frame}" }
54
+ lines.join("\n")
55
+ end
56
+ end
57
+
58
+ # Client-side error deduplicator.
59
+ # Prevents the same error (by fingerprint) from being reported
60
+ # more than once within a configurable time window.
61
+ class Deduplicator
62
+ def initialize(window: 300.0)
63
+ @cache = {}
64
+ @window = window
65
+ @mutex = Mutex.new
66
+ end
67
+
68
+ # Check if an error should be sent. Thread-safe.
69
+ #
70
+ # @param fingerprint [String]
71
+ # @return [Boolean]
72
+ def should_send?(fingerprint)
73
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
74
+
75
+ @mutex.synchronize do
76
+ last_sent = @cache[fingerprint]
77
+ if last_sent && (now - last_sent) < @window
78
+ return false
79
+ end
80
+
81
+ @cache[fingerprint] = now
82
+ cleanup(now)
83
+ true
84
+ end
85
+ end
86
+
87
+ def clear
88
+ @mutex.synchronize { @cache.clear }
89
+ end
90
+
91
+ private
92
+
93
+ def cleanup(now)
94
+ @cache.delete_if { |_, ts| (now - ts) >= @window }
95
+ end
96
+ end
97
+ end
@@ -1,41 +1,41 @@
1
- # frozen_string_literal: true
2
-
3
- module Bugstack
4
- module Integrations
5
- # Generic Ruby integration that hooks into at_exit
6
- # to capture unhandled exceptions.
7
- #
8
- # Usage:
9
- # require "bugstack"
10
- # require "bugstack/integrations/generic"
11
- #
12
- # Bugstack.init(api_key: "bs_live_...")
13
- # Bugstack::Integrations::Generic.install!
14
- module Generic
15
- @installed = false
16
-
17
- # Install global exception hooks.
18
- def self.install!
19
- return if @installed
20
-
21
- @installed = true
22
-
23
- at_exit do
24
- if $! && !$!.is_a?(SystemExit)
25
- Bugstack.capture_exception($!)
26
- end
27
- end
28
- end
29
-
30
- # Check if hooks are installed.
31
- def self.installed?
32
- @installed
33
- end
34
-
35
- # Reset for testing.
36
- def self.reset!
37
- @installed = false
38
- end
39
- end
40
- end
41
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Bugstack
4
+ module Integrations
5
+ # Generic Ruby integration that hooks into at_exit
6
+ # to capture unhandled exceptions.
7
+ #
8
+ # Usage:
9
+ # require "bugstack"
10
+ # require "bugstack/integrations/generic"
11
+ #
12
+ # Bugstack.init(api_key: "bs_live_...")
13
+ # Bugstack::Integrations::Generic.install!
14
+ module Generic
15
+ @installed = false
16
+
17
+ # Install global exception hooks.
18
+ def self.install!
19
+ return if @installed
20
+
21
+ @installed = true
22
+
23
+ at_exit do
24
+ if $! && !$!.is_a?(SystemExit)
25
+ Bugstack.capture_exception($!)
26
+ end
27
+ end
28
+ end
29
+
30
+ # Check if hooks are installed.
31
+ def self.installed?
32
+ @installed
33
+ end
34
+
35
+ # Reset for testing.
36
+ def self.reset!
37
+ @installed = false
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,61 +1,61 @@
1
- # frozen_string_literal: true
2
-
3
- module Bugstack
4
- module Integrations
5
- # Rails integration via Railtie for automatic setup
6
- # and Rack middleware for exception capture.
7
- #
8
- # Add to your Gemfile:
9
- # gem "bugstack"
10
- #
11
- # Configure in an initializer:
12
- # # config/initializers/bugstack.rb
13
- # Bugstack.init do |config|
14
- # config.api_key = Rails.application.credentials.bugstack_api_key
15
- # config.environment = Rails.env
16
- # config.auto_fix = true
17
- # end
18
- class Railtie < ::Rails::Railtie
19
- initializer "bugstack.middleware" do |app|
20
- app.middleware.insert(0, Bugstack::Integrations::RackMiddleware)
21
- end
22
- end if defined?(::Rails::Railtie)
23
-
24
- # Rack middleware that captures unhandled exceptions.
25
- class RackMiddleware
26
- def initialize(app)
27
- @app = app
28
- end
29
-
30
- def call(env)
31
- @app.call(env)
32
- rescue Exception => e # rubocop:disable Lint/RescueException
33
- capture_from_rack(e, env)
34
- raise
35
- end
36
-
37
- private
38
-
39
- def capture_from_rack(exception, env)
40
- client = Bugstack.client
41
- return unless client
42
-
43
- request = build_request_context(env)
44
- client.capture_exception(
45
- exception,
46
- request: request,
47
- metadata: { "framework" => "rails" }
48
- )
49
- rescue => inner
50
- warn "[BugStack] Error capturing exception: #{inner.message}" if client&.config&.debug
51
- end
52
-
53
- def build_request_context(env)
54
- {
55
- route: env["PATH_INFO"].to_s,
56
- method: env["REQUEST_METHOD"].to_s
57
- }
58
- end
59
- end
60
- end
61
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Bugstack
4
+ module Integrations
5
+ # Rails integration via Railtie for automatic setup
6
+ # and Rack middleware for exception capture.
7
+ #
8
+ # Add to your Gemfile:
9
+ # gem "bugstack"
10
+ #
11
+ # Configure in an initializer:
12
+ # # config/initializers/bugstack.rb
13
+ # Bugstack.init do |config|
14
+ # config.api_key = Rails.application.credentials.bugstack_api_key
15
+ # config.environment = Rails.env
16
+ # config.auto_fix = true
17
+ # end
18
+ class Railtie < ::Rails::Railtie
19
+ initializer "bugstack.middleware" do |app|
20
+ app.middleware.insert(0, Bugstack::Integrations::RackMiddleware)
21
+ end
22
+ end if defined?(::Rails::Railtie)
23
+
24
+ # Rack middleware that captures unhandled exceptions.
25
+ class RackMiddleware
26
+ def initialize(app)
27
+ @app = app
28
+ end
29
+
30
+ def call(env)
31
+ @app.call(env)
32
+ rescue Exception => e # rubocop:disable Lint/RescueException
33
+ capture_from_rack(e, env)
34
+ raise
35
+ end
36
+
37
+ private
38
+
39
+ def capture_from_rack(exception, env)
40
+ client = Bugstack.client
41
+ return unless client
42
+
43
+ request = build_request_context(env)
44
+ client.capture_exception(
45
+ exception,
46
+ request: request,
47
+ metadata: { "framework" => "rails" }
48
+ )
49
+ rescue => inner
50
+ warn "[BugStack] Error capturing exception: #{inner.message}" if client&.config&.debug
51
+ end
52
+
53
+ def build_request_context(env)
54
+ {
55
+ route: env["PATH_INFO"].to_s,
56
+ method: env["REQUEST_METHOD"].to_s
57
+ }
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,41 +1,41 @@
1
- # frozen_string_literal: true
2
-
3
- module Bugstack
4
- module Integrations
5
- # Sinatra integration for capturing unhandled exceptions.
6
- #
7
- # Usage:
8
- # require "sinatra"
9
- # require "bugstack"
10
- # require "bugstack/integrations/sinatra"
11
- #
12
- # Bugstack.init(api_key: "bs_live_...")
13
- #
14
- # class MyApp < Sinatra::Base
15
- # register Bugstack::Integrations::Sinatra
16
- #
17
- # get "/" do
18
- # "Hello!"
19
- # end
20
- # end
21
- module Sinatra
22
- def self.registered(app)
23
- app.error do |exception|
24
- client = Bugstack.client
25
- if client
26
- client.capture_exception(
27
- exception,
28
- request: {
29
- route: request.path_info,
30
- method: request.request_method
31
- },
32
- metadata: { "framework" => "sinatra" }
33
- )
34
- end
35
-
36
- raise exception
37
- end
38
- end
39
- end
40
- end
41
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Bugstack
4
+ module Integrations
5
+ # Sinatra integration for capturing unhandled exceptions.
6
+ #
7
+ # Usage:
8
+ # require "sinatra"
9
+ # require "bugstack"
10
+ # require "bugstack/integrations/sinatra"
11
+ #
12
+ # Bugstack.init(api_key: "bs_live_...")
13
+ #
14
+ # class MyApp < Sinatra::Base
15
+ # register Bugstack::Integrations::Sinatra
16
+ #
17
+ # get "/" do
18
+ # "Hello!"
19
+ # end
20
+ # end
21
+ module Sinatra
22
+ def self.registered(app)
23
+ app.error do |exception|
24
+ client = Bugstack.client
25
+ if client
26
+ client.capture_exception(
27
+ exception,
28
+ request: {
29
+ route: request.path_info,
30
+ method: request.request_method
31
+ },
32
+ metadata: { "framework" => "sinatra" }
33
+ )
34
+ end
35
+
36
+ raise exception
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end