rocketgraph-ruby 0.1.0 → 1.0.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: 624703e22bb90bdaaec8169c2ecf587dacf20ec891ba7cd4ac0176ca0b0e5980
4
- data.tar.gz: 627c062c86f22b402bd95092f31bdbfacd4f3e585ea09c531978abd57494d102
3
+ metadata.gz: cae82234830f1586ab278ccbab7ad3f12a60b231e7390c88945169186191f5ae
4
+ data.tar.gz: 6e43d9b348e2fe7f7a83c14e5e89dce369b717402e6b1594827880926150f5c8
5
5
  SHA512:
6
- metadata.gz: ab252ee37e0214e327620654b1c965436b384bf500932596d26a7a63eb92713ad0ae92181762f19f18ade74aea3e7f5292780335f56d7a99187a3dacf7467857
7
- data.tar.gz: d5e61fe6457e8df9bc9958c9b0c63304bcf617c2632cabda05505b865a81c54efb70ce483da5e5bd93c3f236e0d91450d1943007d4429f158be4ed79ecd2e8d5
6
+ metadata.gz: 062c9700deb9d9e9b50b0cf63969e200e0ab875532bf4ef0603ddfafcafbb9ea671e3e4650284eadc98d8bce41d1828eb7b6d826709c1ed0cfb2302d0017c94a
7
+ data.tar.gz: 13f3900cad8755576b81f24e659f9f77366a1a5e1b10fcf37f2d41f80b3cc01d14c0cabeab3c1d5bfce3b6e313b0b9ca6efd17b40f6e661b5170dfcbac269325
@@ -1,3 +1,3 @@
1
1
  module Rocketgraph
2
- VERSION = '0.1.0'
2
+ VERSION = '1.0.0'
3
3
  end
data/lib/rocketgraph.rb CHANGED
@@ -1,19 +1,20 @@
1
1
  require 'httparty'
2
2
  require 'rocketgraph/version'
3
+ require 'logger'
3
4
 
4
5
  module Rocketgraph
5
6
  class ErrorReportingMiddleware
6
- def initialize(app, endpoint)
7
+ def initialize(app, endpoint, api_key)
7
8
  @app = app
8
9
  @endpoint = endpoint
10
+ @api_key = api_key
11
+ @custom_logger = nil
12
+ setup_logger_interception
9
13
  end
10
14
 
11
15
  def call(env)
12
16
  begin
13
- # Call the application
14
17
  status, headers, body = @app.call(env)
15
-
16
- # Check for error status codes (e.g., 400, 404, 500)
17
18
  if status >= 400
18
19
  report_error(
19
20
  error: StandardError.new("HTTP #{status}"),
@@ -21,74 +22,146 @@ module Rocketgraph
21
22
  status_code: status
22
23
  )
23
24
  end
24
-
25
25
  [status, headers, body]
26
26
  rescue StandardError => e
27
- # Catch any unhandled exceptions
28
27
  report_error(error: e, env: env, status_code: 500)
29
- raise # Re-raise to let the app handle the response
28
+ raise
30
29
  end
31
30
  end
32
31
 
33
32
  private
34
33
 
34
+ def setup_logger_interception
35
+ # Initialize custom logger with null output
36
+ @custom_logger = Logger.new(STDOUT)
37
+ @custom_logger.formatter = proc do |severity, datetime, progname, msg|
38
+ puts "DEBUG: Formatter called with severity=#{severity}, msg=#{msg}" # Debug
39
+ log_details = build_log_details(
40
+ severity: severity,
41
+ message: msg,
42
+ timestamp: datetime,
43
+ progname: progname
44
+ )
45
+ Thread.new do
46
+ puts "DEBUG: Sending log to endpoint: #{log_details.inspect}" # Debug
47
+ send_to_networks_endpoint(log_details)
48
+ end
49
+ msg
50
+ end
51
+
52
+ # Find and wrap the STDOUT logger
53
+ stdout_logger = nil
54
+ ObjectSpace.each_object(Logger) do |logger|
55
+ next if logger == @custom_logger
56
+ logdev = logger.instance_variable_get(:@logdev)
57
+ if logdev && logdev.dev == STDOUT
58
+ stdout_logger = logger
59
+ puts "DEBUG: Found STDOUT logger: #{logger.inspect}" # Debug
60
+ break
61
+ end
62
+ end
63
+
64
+ if stdout_logger
65
+ wrap_logger(stdout_logger)
66
+ puts "DEBUG: Wrapped STDOUT logger" # Debug
67
+ else
68
+ puts "DEBUG: No STDOUT logger found during initialization" # Debug
69
+ # Schedule a retry to catch late-initialized loggers
70
+ Thread.new do
71
+ sleep 1 # Wait for app to initialize
72
+ ObjectSpace.each_object(Logger) do |logger|
73
+ next if logger == @custom_logger
74
+ logdev = logger.instance_variable_get(:@logdev)
75
+ if logdev && logdev.dev == STDOUT
76
+ wrap_logger(logger)
77
+ puts "DEBUG: Wrapped late-initialized STDOUT logger: #{logger.inspect}" # Debug
78
+ break
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ def wrap_logger(logger)
86
+ middleware = self
87
+ original_add = logger.method(:add)
88
+
89
+ logger.define_singleton_method(:add) do |severity, message = nil, progname = nil, &block|
90
+ puts "DEBUG: Intercepted log - severity=#{severity}, message=#{message}, progname=#{progname}" # Debug
91
+ result = original_add.call(severity, message, progname, &block)
92
+ if middleware.instance_variable_get(:@custom_logger)
93
+ message = block.call if block
94
+ message ||= progname
95
+ middleware.instance_variable_get(:@custom_logger).add(severity, message, progname)
96
+ end
97
+ result
98
+ end
99
+ end
100
+
35
101
  def report_error(error:, env:, status_code:)
36
102
  error_details = build_error_details(error, env, status_code)
37
- send_to_endpoint(error_details)
103
+ send_to_networks_endpoint(error_details)
38
104
  rescue StandardError
39
- # Silently fail to avoid affecting the app
105
+ puts "DEBUG: Failed to report error: #{error_details.inspect}" # Debug
40
106
  end
41
107
 
42
108
  def build_error_details(error, env, status_code)
43
- # request = Rack::Request.new(env)
44
- # {
45
- # timestamp: Time.now.utc.iso8601,
46
- # status_code: status_code,
47
- # error_class: error.class.name,
48
- # message: error.message,
49
- # backtrace: error.backtrace&.first(5),
50
- # request: {
51
- # method: request.request_method,
52
- # path: request.path_info,
53
- # ip: request.ip,
54
- # params: request.params
55
- # }
56
- # }
57
-
58
- # Add more info
59
109
  request = Rack::Request.new(env)
60
110
  {
61
- timestamp: Time.now.utc.iso8601,
62
- status_code: status_code,
63
- error_class: error.class.name,
64
- message: error.message,
65
- backtrace: error.backtrace&.first(5),
66
- request: {
67
- method: request.request_method,
68
- path: request.path_info,
69
- ip: request.ip,
70
- params: request.params,
71
- user_agent: env['HTTP_USER_AGENT'] || '', # User-Agent header
72
- referer: env['HTTP_REFERER'], # Referer header
73
- accept: env['HTTP_ACCEPT'], # Accept header
74
- accept_language: env['HTTP_ACCEPT_LANGUAGE'], # Accept-Language header
75
- accept_encoding: env['HTTP_ACCEPT_ENCODING'], # Accept-Encoding header
76
- connection: env['HTTP_CONNECTION'], # Connection header
77
- has_session: !env['rack.session'].nil?, # Presence of session cookie
78
- request_timestamp: Time.now.to_f # Precise timestamp for rate analysis
111
+ type: "network_request",
112
+ api_key: @api_key,
113
+ data: {
114
+ timestamp: Time.now.utc.iso8601,
115
+ status_code: status_code,
116
+ error_class: error.class.name,
117
+ message: error.message,
118
+ backtrace: error.backtrace&.first(5),
119
+ request: {
120
+ method: request.request_method,
121
+ path: request.path_info,
122
+ ip: request.ip,
123
+ params: request.params,
124
+ user_agent: env['HTTP_USER_AGENT'] || '',
125
+ referer: env['HTTP_REFERER'],
126
+ accept: env['HTTP_ACCEPT'],
127
+ accept_language: env['HTTP_ACCEPT_LANGUAGE'],
128
+ accept_encoding: env['HTTP_ACCEPT_ENCODING'],
129
+ connection: env['HTTP_CONNECTION'],
130
+ has_session: !env['rack.session'].nil?,
131
+ request_timestamp: Time.now.to_f
132
+ }
133
+ }
134
+ }
135
+ end
136
+
137
+ def build_log_details(severity:, message:, timestamp:, progname:)
138
+ {
139
+ type: "application_log",
140
+ api_key: @api_key,
141
+ data: {
142
+ timestamp: timestamp.utc.iso8601,
143
+ severity: severity,
144
+ message: message.to_s,
145
+ program_name: progname,
146
+ request_timestamp: Time.now.to_f
79
147
  }
80
148
  }
81
149
  end
82
150
 
83
- def send_to_endpoint(details)
151
+ def send_to_networks_endpoint(details)
152
+ networks_endpoint = "#{@endpoint}/traces"
153
+ puts "\n=== #{details[:type].capitalize} Details ===" # Original puts
84
154
  HTTParty.post(
85
- @endpoint,
155
+ networks_endpoint,
86
156
  body: details.to_json,
87
- headers: { 'Content-Type' => 'application/json' },
157
+ headers: {
158
+ 'Content-Type' => 'application/json',
159
+ 'Origin' => 'https://rocketgraph.io'
160
+ },
88
161
  timeout: 5
89
162
  )
90
- rescue HTTParty::Error, Timeout::Error
91
- # Silently fail
163
+ rescue HTTParty::Error, Timeout::Error => e
164
+ puts "DEBUG: Failed to send to endpoint: #{e.message}" # Debug
92
165
  end
93
166
  end
94
167
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rocketgraph-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - Your Name
8
- autorequire:
7
+ - Kaushik Varanasi
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2025-04-26 00:00:00.000000000 Z
10
+ date: 2025-07-14 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: httparty
@@ -69,19 +68,18 @@ dependencies:
69
68
  description: Rocketgraph Ruby intercepts HTTP errors (e.g., 404, 500) in Rack-based
70
69
  applications and sends detailed reports to a specified backend service.
71
70
  email:
72
- - your.email@example.com
71
+ - kaushik.varanasi1@gmail.com
73
72
  executables: []
74
73
  extensions: []
75
74
  extra_rdoc_files: []
76
75
  files:
77
76
  - lib/rocketgraph.rb
78
77
  - lib/rocketgraph/version.rb
79
- homepage: https://github.com/yourusername/rocketgraph-ruby
78
+ homepage: https://github.com/kaushik94/rocketgraph-ruby
80
79
  licenses:
81
80
  - MIT
82
81
  metadata:
83
- source_code_uri: https://github.com/yourusername/rocketgraph-ruby
84
- post_install_message:
82
+ source_code_uri: https://github.com/kaushik94/rocketgraph-ruby
85
83
  rdoc_options: []
86
84
  require_paths:
87
85
  - lib
@@ -96,8 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
94
  - !ruby/object:Gem::Version
97
95
  version: '0'
98
96
  requirements: []
99
- rubygems_version: 3.0.3.1
100
- signing_key:
97
+ rubygems_version: 3.6.2
101
98
  specification_version: 4
102
99
  summary: A Ruby gem to catch HTTP errors and report them to a custom endpoint
103
100
  test_files: []