errorgap 0.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.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/exe/errorgap +15 -0
- data/lib/errorgap/configuration.rb +42 -0
- data/lib/errorgap/notice.rb +104 -0
- data/lib/errorgap/notifier.rb +72 -0
- data/lib/errorgap/rack_middleware.rb +111 -0
- data/lib/errorgap/rails/railtie.rb +29 -0
- data/lib/errorgap/span_collector.rb +52 -0
- data/lib/errorgap/transacter.rb +55 -0
- data/lib/errorgap/transaction.rb +41 -0
- data/lib/errorgap/version.rb +5 -0
- data/lib/errorgap.rb +45 -0
- data/lib/generators/errorgap/install_generator.rb +28 -0
- metadata +88 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9fc092f673d279e6295144e0bcd302fabf729ff193b00b14ed651706f9cbb388
|
|
4
|
+
data.tar.gz: e89ffdc6f5eaab044376725d9baa3b8584a428d7f4799a89f5c22c13b9a96b30
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b93c2e7bbd4c4b29364a98db702c7463ac21906d73a5ace7fd779fecc209187284bc666badd6a5fe70854ae4a8f3367bbf73b8b49d0ac9e9673248e5c86ec83a
|
|
7
|
+
data.tar.gz: c5563c43afef3637222dc69b82388bd5cb42fb44daa5494c83a79d5c053da778c1fa15dc85915a6eea0ef6e7b079a002c8b1e3a6b7a5be447c2e37be5f2e7f58
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Errorgap
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Errorgap Ruby
|
|
2
|
+
|
|
3
|
+
Ruby notifier for Errorgap. It captures exceptions, normalizes backtraces, and sends notices to a Errorgap server.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add the gem to your application:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem "errorgap"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Then install:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
bundle install
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Configuration
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
require "errorgap"
|
|
23
|
+
|
|
24
|
+
Errorgap.configure do |config|
|
|
25
|
+
config.endpoint = ENV.fetch("ERRORGAP_ENDPOINT", "http://127.0.0.1:3030")
|
|
26
|
+
config.project_slug = ENV["ERRORGAP_PROJECT_SLUG"]
|
|
27
|
+
config.project_id = ENV["ERRORGAP_PROJECT_ID"]
|
|
28
|
+
config.api_key = ENV["ERRORGAP_API_KEY"]
|
|
29
|
+
config.environment = ENV.fetch("RAILS_ENV", ENV.fetch("RACK_ENV", "development"))
|
|
30
|
+
end
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Manual Notification
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
begin
|
|
37
|
+
risky_operation
|
|
38
|
+
rescue => error
|
|
39
|
+
Errorgap.notify(error, context: { component: "billing" })
|
|
40
|
+
raise
|
|
41
|
+
end
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Rack
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
use Errorgap::RackMiddleware
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Rails
|
|
51
|
+
|
|
52
|
+
In Rails, requiring the gem installs the Rack middleware automatically through the Railtie.
|
|
53
|
+
|
|
54
|
+
Generate an initializer:
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
rails generate errorgap:install
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Development
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
bundle install
|
|
64
|
+
bundle exec rake
|
|
65
|
+
```
|
data/exe/errorgap
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "errorgap"
|
|
5
|
+
|
|
6
|
+
command = ARGV.shift
|
|
7
|
+
|
|
8
|
+
case command
|
|
9
|
+
when "test"
|
|
10
|
+
Errorgap.notify(RuntimeError.new("Errorgap test exception"), sync: true)
|
|
11
|
+
puts "Sent test exception to Errorgap."
|
|
12
|
+
else
|
|
13
|
+
warn "Usage: errorgap test"
|
|
14
|
+
exit 1
|
|
15
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Errorgap
|
|
4
|
+
class Configuration
|
|
5
|
+
attr_accessor :endpoint,
|
|
6
|
+
:project_id,
|
|
7
|
+
:project_slug,
|
|
8
|
+
:api_key,
|
|
9
|
+
:environment,
|
|
10
|
+
:root_directory,
|
|
11
|
+
:async,
|
|
12
|
+
:logger,
|
|
13
|
+
:filter_keys,
|
|
14
|
+
:apm_enabled,
|
|
15
|
+
:apm_sample_rate
|
|
16
|
+
|
|
17
|
+
def initialize
|
|
18
|
+
@endpoint = ENV.fetch("ERRORGAP_ENDPOINT", "http://127.0.0.1:3030")
|
|
19
|
+
@project_id = ENV["ERRORGAP_PROJECT_ID"]
|
|
20
|
+
@project_slug = ENV["ERRORGAP_PROJECT_SLUG"]
|
|
21
|
+
@api_key = ENV["ERRORGAP_API_KEY"]
|
|
22
|
+
@environment = ENV.fetch("RAILS_ENV", ENV.fetch("RACK_ENV", "development"))
|
|
23
|
+
@root_directory = Dir.pwd
|
|
24
|
+
@async = true
|
|
25
|
+
@filter_keys = %w[password password_confirmation token secret api_key authorization cookie]
|
|
26
|
+
@apm_enabled = false
|
|
27
|
+
@apm_sample_rate = 1.0
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def validate!
|
|
31
|
+
raise ArgumentError, "Errorgap project_slug is required" if blank?(project_slug)
|
|
32
|
+
|
|
33
|
+
true
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def blank?(value)
|
|
39
|
+
value.nil? || value.to_s.strip.empty?
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
module Errorgap
|
|
6
|
+
class Notice
|
|
7
|
+
def self.from_exception(error, configuration:, context: {}, environment: {}, session: {}, params: {})
|
|
8
|
+
new(
|
|
9
|
+
error: error,
|
|
10
|
+
configuration: configuration,
|
|
11
|
+
context: context,
|
|
12
|
+
environment: environment,
|
|
13
|
+
session: session,
|
|
14
|
+
params: params
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def initialize(error:, configuration:, context:, environment:, session:, params:)
|
|
19
|
+
@error = error
|
|
20
|
+
@configuration = configuration
|
|
21
|
+
@context = context || {}
|
|
22
|
+
@environment = environment || {}
|
|
23
|
+
@session = session || {}
|
|
24
|
+
@params = params || {}
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def to_h
|
|
28
|
+
{
|
|
29
|
+
project_id: @configuration.project_id,
|
|
30
|
+
received_at: Time.now.utc.iso8601,
|
|
31
|
+
errors: [
|
|
32
|
+
{
|
|
33
|
+
type: @error.class.name,
|
|
34
|
+
message: @error.message.to_s,
|
|
35
|
+
backtrace: backtrace_frames
|
|
36
|
+
}
|
|
37
|
+
],
|
|
38
|
+
context: default_context.merge(stringify_hash(@context)),
|
|
39
|
+
environment: stringify_hash(@environment),
|
|
40
|
+
session: stringify_hash(@session),
|
|
41
|
+
params: filter_hash(@params)
|
|
42
|
+
}
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def default_context
|
|
48
|
+
{
|
|
49
|
+
notifier: "errorgap-ruby",
|
|
50
|
+
notifier_version: Errorgap::VERSION,
|
|
51
|
+
environment: @configuration.environment,
|
|
52
|
+
root_directory: @configuration.root_directory
|
|
53
|
+
}
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def backtrace_frames
|
|
57
|
+
Array(@error.backtrace).map.with_index do |line, index|
|
|
58
|
+
file, line_number, function = parse_backtrace_line(line)
|
|
59
|
+
{
|
|
60
|
+
file: relative_file(file),
|
|
61
|
+
line: line_number,
|
|
62
|
+
function: function,
|
|
63
|
+
in_app: in_app?(file),
|
|
64
|
+
index: index
|
|
65
|
+
}.compact
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def parse_backtrace_line(line)
|
|
70
|
+
match = line.match(/\A(.+?):(\d+)(?::in `(.*)')?\z/)
|
|
71
|
+
return [line, nil, nil] unless match
|
|
72
|
+
|
|
73
|
+
[match[1], match[2].to_i, match[3]]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def relative_file(file)
|
|
77
|
+
root = @configuration.root_directory.to_s
|
|
78
|
+
return file if root.empty?
|
|
79
|
+
|
|
80
|
+
file.to_s.sub(%r{\A#{Regexp.escape(root)}/?}, "")
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def in_app?(file)
|
|
84
|
+
root = @configuration.root_directory.to_s
|
|
85
|
+
!root.empty? && file.to_s.start_with?(root)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def filter_hash(hash)
|
|
89
|
+
stringify_hash(hash).each_with_object({}) do |(key, value), filtered|
|
|
90
|
+
filtered[key] = sensitive?(key) ? "[FILTERED]" : value
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def sensitive?(key)
|
|
95
|
+
@configuration.filter_keys.any? { |filter| key.to_s.downcase.include?(filter.downcase) }
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def stringify_hash(hash)
|
|
99
|
+
hash.each_with_object({}) do |(key, value), result|
|
|
100
|
+
result[key.to_s] = value.is_a?(Hash) ? stringify_hash(value) : value
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Errorgap
|
|
8
|
+
class Notifier
|
|
9
|
+
Response = Struct.new(:status, :body, :error, keyword_init: true) do
|
|
10
|
+
def success?
|
|
11
|
+
error.nil? && status.to_i.between?(200, 299)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def initialize(configuration)
|
|
16
|
+
configure(configuration)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def configure(configuration)
|
|
20
|
+
@configuration = configuration
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def notify(error, context: {}, environment: {}, session: {}, params: {}, sync: false)
|
|
24
|
+
@configuration.validate!
|
|
25
|
+
notice = Notice.from_exception(
|
|
26
|
+
error,
|
|
27
|
+
configuration: @configuration,
|
|
28
|
+
context: context,
|
|
29
|
+
environment: environment,
|
|
30
|
+
session: session,
|
|
31
|
+
params: params
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
if sync || !@configuration.async
|
|
35
|
+
deliver(notice)
|
|
36
|
+
else
|
|
37
|
+
Thread.new { deliver(notice) }
|
|
38
|
+
Response.new(status: 202, body: "queued")
|
|
39
|
+
end
|
|
40
|
+
rescue StandardError => exception
|
|
41
|
+
log(exception)
|
|
42
|
+
Response.new(error: exception)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def deliver(notice)
|
|
46
|
+
uri = URI.join(@configuration.endpoint.end_with?("/") ? @configuration.endpoint : "#{@configuration.endpoint}/", "api/projects/#{@configuration.project_slug}/notices")
|
|
47
|
+
request = Net::HTTP::Post.new(uri)
|
|
48
|
+
request["Content-Type"] = "application/json"
|
|
49
|
+
request["User-Agent"] = "errorgap-ruby/#{Errorgap::VERSION}"
|
|
50
|
+
request["X-Errorgap-Project-Key"] = @configuration.api_key if present?(@configuration.api_key)
|
|
51
|
+
request.body = JSON.generate(notice.to_h)
|
|
52
|
+
|
|
53
|
+
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
|
|
54
|
+
http.request(request)
|
|
55
|
+
end
|
|
56
|
+
Response.new(status: response.code.to_i, body: response.body)
|
|
57
|
+
rescue StandardError => exception
|
|
58
|
+
log(exception)
|
|
59
|
+
Response.new(error: exception)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def log(exception)
|
|
65
|
+
@configuration.logger&.warn("[errorgap] #{exception.class}: #{exception.message}")
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def present?(value)
|
|
69
|
+
!value.nil? && !value.to_s.empty?
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Errorgap
|
|
4
|
+
class RackMiddleware
|
|
5
|
+
def initialize(app)
|
|
6
|
+
@app = app
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def call(env)
|
|
10
|
+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
11
|
+
SpanCollector.start if apm_enabled?
|
|
12
|
+
|
|
13
|
+
status, headers, body = @app.call(env)
|
|
14
|
+
[status, headers, body]
|
|
15
|
+
rescue Exception => exception # rubocop:disable Lint/RescueException
|
|
16
|
+
notify_once(env, exception)
|
|
17
|
+
raise
|
|
18
|
+
ensure
|
|
19
|
+
if apm_enabled?
|
|
20
|
+
elapsed_ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000.0
|
|
21
|
+
record_transaction(env, status || 500, elapsed_ms)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def apm_enabled?
|
|
28
|
+
Errorgap.configuration.apm_enabled
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def record_transaction(env, status_code, elapsed_ms)
|
|
32
|
+
spans = SpanCollector.flush
|
|
33
|
+
txn = Transaction.new(
|
|
34
|
+
kind: "web",
|
|
35
|
+
method: env["REQUEST_METHOD"],
|
|
36
|
+
path: route_pattern(env),
|
|
37
|
+
path_raw: env["PATH_INFO"],
|
|
38
|
+
status_code: status_code,
|
|
39
|
+
duration_ms: elapsed_ms.round(2),
|
|
40
|
+
environment: Errorgap.configuration.environment,
|
|
41
|
+
occurred_at: Time.now,
|
|
42
|
+
spans: spans
|
|
43
|
+
)
|
|
44
|
+
Errorgap.transacter.deliver_async(txn)
|
|
45
|
+
rescue StandardError => exception
|
|
46
|
+
Errorgap.configuration.logger&.warn("[errorgap] APM record error: #{exception.message}")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def route_pattern(env)
|
|
50
|
+
# Rails: use controller#action as the normalised route pattern
|
|
51
|
+
if (params = env["action_dispatch.request.path_parameters"])
|
|
52
|
+
controller = params[:controller]
|
|
53
|
+
action = params[:action]
|
|
54
|
+
return "#{controller}##{action}" if controller && action
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Sinatra: env['sinatra.route'] is "GET /path/:id"
|
|
58
|
+
if (sinatra_route = env["sinatra.route"])
|
|
59
|
+
return sinatra_route.split(" ", 2).last
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Plain Rack fallback
|
|
63
|
+
env["PATH_INFO"]
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def notify_once(env, exception)
|
|
67
|
+
notified = env["errorgap.notified_exception_ids"] ||= {}
|
|
68
|
+
return if notified[exception.object_id]
|
|
69
|
+
|
|
70
|
+
notified[exception.object_id] = true
|
|
71
|
+
Errorgap.notify(
|
|
72
|
+
exception,
|
|
73
|
+
context: rack_context(env),
|
|
74
|
+
environment: rack_environment(env),
|
|
75
|
+
session: rack_session(env),
|
|
76
|
+
params: rack_params(env),
|
|
77
|
+
sync: true
|
|
78
|
+
)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def rack_context(env)
|
|
82
|
+
{
|
|
83
|
+
url: "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['PATH_INFO']}",
|
|
84
|
+
component: env["SCRIPT_NAME"],
|
|
85
|
+
action: env["REQUEST_METHOD"]
|
|
86
|
+
}
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def rack_environment(env)
|
|
90
|
+
{
|
|
91
|
+
method: env["REQUEST_METHOD"],
|
|
92
|
+
path: env["PATH_INFO"],
|
|
93
|
+
query_string: env["QUERY_STRING"],
|
|
94
|
+
user_agent: env["HTTP_USER_AGENT"],
|
|
95
|
+
remote_addr: env["REMOTE_ADDR"]
|
|
96
|
+
}
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def rack_session(env)
|
|
100
|
+
session = env["rack.session"]
|
|
101
|
+
session.respond_to?(:to_hash) ? session.to_hash : {}
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def rack_params(env)
|
|
105
|
+
request = defined?(Rack::Request) ? Rack::Request.new(env) : nil
|
|
106
|
+
request ? request.params : {}
|
|
107
|
+
rescue StandardError
|
|
108
|
+
{}
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/railtie"
|
|
4
|
+
|
|
5
|
+
module Errorgap
|
|
6
|
+
module Rails
|
|
7
|
+
class Railtie < ::Rails::Railtie
|
|
8
|
+
initializer "errorgap.configure_rails" do |app|
|
|
9
|
+
Errorgap.configure do |config|
|
|
10
|
+
config.root_directory = app.root.to_s
|
|
11
|
+
config.environment = ::Rails.env.to_s
|
|
12
|
+
config.logger = ::Rails.logger
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
initializer "errorgap.insert_middleware" do |app|
|
|
17
|
+
app.config.middleware.use Errorgap::RackMiddleware
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
initializer "errorgap.install_span_collector" do
|
|
21
|
+
SpanCollector.install if Errorgap.configuration.apm_enabled
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
generators do
|
|
25
|
+
require "generators/errorgap/install_generator"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Errorgap
|
|
4
|
+
class SpanCollector
|
|
5
|
+
THREAD_KEY = :errorgap_spans
|
|
6
|
+
|
|
7
|
+
# Quoted strings and standalone numeric literals → ?
|
|
8
|
+
NORMALIZE_PATTERN = /
|
|
9
|
+
'(?:[^'\\]|\\.)*' # single-quoted string literals
|
|
10
|
+
|
|
|
11
|
+
\b\d+(?:\.\d+)?\b # integer and float literals
|
|
12
|
+
/x
|
|
13
|
+
|
|
14
|
+
SKIP_NAMES = %w[SCHEMA EXPLAIN CACHE].freeze
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
# Call once at boot (from Railtie) when APM is enabled.
|
|
18
|
+
def install
|
|
19
|
+
return unless defined?(ActiveSupport::Notifications)
|
|
20
|
+
|
|
21
|
+
ActiveSupport::Notifications.subscribe("sql.active_record") do |*, payload|
|
|
22
|
+
next unless Thread.current[THREAD_KEY]
|
|
23
|
+
next if SKIP_NAMES.any? { |n| payload[:name]&.start_with?(n) }
|
|
24
|
+
next if payload[:cached]
|
|
25
|
+
|
|
26
|
+
duration_ms = payload[:duration] || 0.0
|
|
27
|
+
sql = normalize_sql(payload[:sql].to_s)
|
|
28
|
+
|
|
29
|
+
store << Span.new(kind: "db", sql: sql, duration_ms: duration_ms.round(3))
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def start
|
|
34
|
+
Thread.current[THREAD_KEY] = []
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def flush
|
|
38
|
+
spans = Thread.current[THREAD_KEY] || []
|
|
39
|
+
Thread.current[THREAD_KEY] = nil
|
|
40
|
+
spans
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def store
|
|
44
|
+
Thread.current[THREAD_KEY] ||= []
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def normalize_sql(sql)
|
|
48
|
+
sql.gsub(NORMALIZE_PATTERN, "?").gsub(/\s+/, " ").strip
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Errorgap
|
|
8
|
+
class Transacter
|
|
9
|
+
def initialize(configuration)
|
|
10
|
+
configure(configuration)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def configure(configuration)
|
|
14
|
+
@configuration = configuration
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def deliver_async(transaction)
|
|
18
|
+
return unless should_sample?
|
|
19
|
+
|
|
20
|
+
Thread.new { deliver(transaction) }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def deliver(transaction)
|
|
24
|
+
uri = URI.join(
|
|
25
|
+
@configuration.endpoint.end_with?("/") ? @configuration.endpoint : "#{@configuration.endpoint}/",
|
|
26
|
+
"api/projects/#{@configuration.project_slug}/transactions"
|
|
27
|
+
)
|
|
28
|
+
request = Net::HTTP::Post.new(uri)
|
|
29
|
+
request["Content-Type"] = "application/json"
|
|
30
|
+
request["User-Agent"] = "errorgap-ruby/#{Errorgap::VERSION}"
|
|
31
|
+
request["X-Errorgap-Project-Key"] = @configuration.api_key if present?(@configuration.api_key)
|
|
32
|
+
request.body = JSON.generate(transaction.to_h)
|
|
33
|
+
|
|
34
|
+
Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
|
|
35
|
+
http.request(request)
|
|
36
|
+
end
|
|
37
|
+
rescue StandardError => exception
|
|
38
|
+
@configuration.logger&.warn("[errorgap] APM delivery error: #{exception.class}: #{exception.message}")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def should_sample?
|
|
44
|
+
rate = @configuration.apm_sample_rate.to_f
|
|
45
|
+
return true if rate >= 1.0
|
|
46
|
+
return false if rate <= 0.0
|
|
47
|
+
|
|
48
|
+
rand < rate
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def present?(value)
|
|
52
|
+
!value.nil? && !value.to_s.empty?
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
module Errorgap
|
|
6
|
+
Span = Struct.new(:kind, :sql, :file, :line, :fn_name, :duration_ms, keyword_init: true) do
|
|
7
|
+
def to_h
|
|
8
|
+
{
|
|
9
|
+
kind: kind,
|
|
10
|
+
sql: sql,
|
|
11
|
+
file: file,
|
|
12
|
+
line: line,
|
|
13
|
+
fn_name: fn_name,
|
|
14
|
+
duration_ms: duration_ms
|
|
15
|
+
}.compact
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
Transaction = Struct.new(
|
|
20
|
+
:kind, :method, :path, :path_raw, :status_code,
|
|
21
|
+
:duration_ms, :environment, :occurred_at, :spans,
|
|
22
|
+
:job_class, :queue,
|
|
23
|
+
keyword_init: true
|
|
24
|
+
) do
|
|
25
|
+
def to_h
|
|
26
|
+
{
|
|
27
|
+
kind: kind,
|
|
28
|
+
method: method,
|
|
29
|
+
path: path,
|
|
30
|
+
path_raw: path_raw,
|
|
31
|
+
status_code: status_code,
|
|
32
|
+
duration_ms: duration_ms,
|
|
33
|
+
environment: environment,
|
|
34
|
+
occurred_at: occurred_at&.utc&.iso8601(3),
|
|
35
|
+
spans: Array(spans).map(&:to_h),
|
|
36
|
+
job_class: job_class,
|
|
37
|
+
queue: queue
|
|
38
|
+
}.compact
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
data/lib/errorgap.rb
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "errorgap/configuration"
|
|
4
|
+
require_relative "errorgap/notifier"
|
|
5
|
+
require_relative "errorgap/notice"
|
|
6
|
+
require_relative "errorgap/transaction"
|
|
7
|
+
require_relative "errorgap/span_collector"
|
|
8
|
+
require_relative "errorgap/transacter"
|
|
9
|
+
require_relative "errorgap/rack_middleware"
|
|
10
|
+
require_relative "errorgap/version"
|
|
11
|
+
|
|
12
|
+
module Errorgap
|
|
13
|
+
class << self
|
|
14
|
+
def configure
|
|
15
|
+
yield(configuration)
|
|
16
|
+
notifier.configure(configuration)
|
|
17
|
+
transacter.configure(configuration)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def configuration
|
|
21
|
+
@configuration ||= Configuration.new
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def notifier
|
|
25
|
+
@notifier ||= Notifier.new(configuration)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def transacter
|
|
29
|
+
@transacter ||= Transacter.new(configuration)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def notify(error, context: {}, environment: {}, session: {}, params: {}, sync: false)
|
|
33
|
+
notifier.notify(
|
|
34
|
+
error,
|
|
35
|
+
context: context,
|
|
36
|
+
environment: environment,
|
|
37
|
+
session: session,
|
|
38
|
+
params: params,
|
|
39
|
+
sync: sync
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
require_relative "errorgap/rails/railtie" if defined?(Rails::Railtie)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/generators"
|
|
4
|
+
|
|
5
|
+
module Errorgap
|
|
6
|
+
class InstallGenerator < ::Rails::Generators::Base
|
|
7
|
+
def create_initializer
|
|
8
|
+
create_file "config/initializers/errorgap.rb", <<~RUBY
|
|
9
|
+
# frozen_string_literal: true
|
|
10
|
+
|
|
11
|
+
Errorgap.configure do |config|
|
|
12
|
+
config.endpoint = ENV.fetch("ERRORGAP_ENDPOINT", "http://127.0.0.1:3030")
|
|
13
|
+
config.project_slug = ENV["ERRORGAP_PROJECT_SLUG"]
|
|
14
|
+
config.project_id = ENV["ERRORGAP_PROJECT_ID"]
|
|
15
|
+
config.api_key = ENV["ERRORGAP_API_KEY"]
|
|
16
|
+
config.environment = Rails.env
|
|
17
|
+
config.root_directory = Rails.root.to_s
|
|
18
|
+
config.logger = Rails.logger
|
|
19
|
+
|
|
20
|
+
# Enable APM performance monitoring (opt-in).
|
|
21
|
+
# Records HTTP request timing and DB query spans via ActiveSupport::Notifications.
|
|
22
|
+
# config.apm_enabled = true
|
|
23
|
+
# config.apm_sample_rate = 1.0 # 0.0–1.0; sample a fraction of requests
|
|
24
|
+
end
|
|
25
|
+
RUBY
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: errorgap
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Errorgap
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-07-07 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rake
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '13.0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '13.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: minitest
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '5.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '5.0'
|
|
41
|
+
description: Captures Ruby exceptions and sends them to a Errorgap server.
|
|
42
|
+
email:
|
|
43
|
+
- support@errorgap.com
|
|
44
|
+
executables:
|
|
45
|
+
- errorgap
|
|
46
|
+
extensions: []
|
|
47
|
+
extra_rdoc_files: []
|
|
48
|
+
files:
|
|
49
|
+
- LICENSE.txt
|
|
50
|
+
- README.md
|
|
51
|
+
- exe/errorgap
|
|
52
|
+
- lib/errorgap.rb
|
|
53
|
+
- lib/errorgap/configuration.rb
|
|
54
|
+
- lib/errorgap/notice.rb
|
|
55
|
+
- lib/errorgap/notifier.rb
|
|
56
|
+
- lib/errorgap/rack_middleware.rb
|
|
57
|
+
- lib/errorgap/rails/railtie.rb
|
|
58
|
+
- lib/errorgap/span_collector.rb
|
|
59
|
+
- lib/errorgap/transacter.rb
|
|
60
|
+
- lib/errorgap/transaction.rb
|
|
61
|
+
- lib/errorgap/version.rb
|
|
62
|
+
- lib/generators/errorgap/install_generator.rb
|
|
63
|
+
homepage: https://github.com/errorgaphq/errorgap-ruby
|
|
64
|
+
licenses:
|
|
65
|
+
- MIT
|
|
66
|
+
metadata:
|
|
67
|
+
homepage_uri: https://github.com/errorgaphq/errorgap-ruby
|
|
68
|
+
source_code_uri: https://github.com/errorgaphq/errorgap-ruby
|
|
69
|
+
post_install_message:
|
|
70
|
+
rdoc_options: []
|
|
71
|
+
require_paths:
|
|
72
|
+
- lib
|
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - ">="
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: '2.7'
|
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
requirements: []
|
|
84
|
+
rubygems_version: 3.5.22
|
|
85
|
+
signing_key:
|
|
86
|
+
specification_version: 4
|
|
87
|
+
summary: Ruby notifier for Errorgap error tracking.
|
|
88
|
+
test_files: []
|