sentrace 0.0.1

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.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +192 -0
  4. data/app/controllers/sentrace/application_controller.rb +4 -0
  5. data/app/controllers/sentrace/ingestion_controller.rb +169 -0
  6. data/app/controllers/sentrace/internal/base_controller.rb +47 -0
  7. data/app/controllers/sentrace/internal/events_controller.rb +25 -0
  8. data/app/controllers/sentrace/internal/issues_controller.rb +36 -0
  9. data/app/controllers/sentrace/internal/projects_controller.rb +58 -0
  10. data/app/controllers/sentrace/internal/stats_controller.rb +10 -0
  11. data/app/controllers/sentrace/mcp_controller.rb +74 -0
  12. data/app/jobs/sentrace/retention_cleanup_job.rb +9 -0
  13. data/app/models/sentrace/application_record.rb +5 -0
  14. data/app/models/sentrace/event.rb +41 -0
  15. data/app/models/sentrace/issue.rb +32 -0
  16. data/app/models/sentrace/mcp_token.rb +82 -0
  17. data/app/models/sentrace/project.rb +50 -0
  18. data/app/models/sentrace/project_key.rb +60 -0
  19. data/config/routes.rb +11 -0
  20. data/db/migrate/20261116000001_create_sentrace_projects.rb +12 -0
  21. data/db/migrate/20261116000002_create_sentrace_project_keys.rb +14 -0
  22. data/db/migrate/20261116000003_create_sentrace_issues.rb +25 -0
  23. data/db/migrate/20261116000004_create_sentrace_events.rb +31 -0
  24. data/db/migrate/20261116000005_create_sentrace_mcp_tokens.rb +17 -0
  25. data/lib/generators/sentrace/install/install_generator.rb +91 -0
  26. data/lib/generators/sentrace/install/templates/initializer.rb.tt +34 -0
  27. data/lib/generators/sentrace/install/templates/sentrace_setup.rake.tt +21 -0
  28. data/lib/sentrace/benchmark/storage_runner.rb +289 -0
  29. data/lib/sentrace/configuration.rb +198 -0
  30. data/lib/sentrace/dsn.rb +123 -0
  31. data/lib/sentrace/engine.rb +10 -0
  32. data/lib/sentrace/errors.rb +20 -0
  33. data/lib/sentrace/ingestion/envelope_parser.rb +143 -0
  34. data/lib/sentrace/ingestion/event_normalizer.rb +315 -0
  35. data/lib/sentrace/ingestion/processor.rb +90 -0
  36. data/lib/sentrace/ingestion/sanitizer.rb +293 -0
  37. data/lib/sentrace/issues/fingerprint.rb +177 -0
  38. data/lib/sentrace/issues/grouper.rb +184 -0
  39. data/lib/sentrace/mcp/dispatcher.rb +23 -0
  40. data/lib/sentrace/mcp/environment_router.rb +266 -0
  41. data/lib/sentrace/mcp/local_backend.rb +425 -0
  42. data/lib/sentrace/mcp/remote_http_client.rb +310 -0
  43. data/lib/sentrace/mcp/server.rb +417 -0
  44. data/lib/sentrace/mcp/stdio_runner.rb +55 -0
  45. data/lib/sentrace/mcp/token_manager.rb +73 -0
  46. data/lib/sentrace/project_manager.rb +64 -0
  47. data/lib/sentrace/retention/cleaner.rb +105 -0
  48. data/lib/sentrace/storage_stats.rb +136 -0
  49. data/lib/sentrace/version.rb +3 -0
  50. data/lib/sentrace.rb +40 -0
  51. data/lib/tasks/sentrace_benchmark.rake +9 -0
  52. data/lib/tasks/sentrace_mcp.rake +36 -0
  53. data/lib/tasks/sentrace_projects.rake +22 -0
  54. data/lib/tasks/sentrace_retention.rake +16 -0
  55. metadata +148 -0
@@ -0,0 +1,293 @@
1
+ require "json"
2
+ require "openssl"
3
+ require "uri"
4
+
5
+ module Sentrace
6
+ module Ingestion
7
+ class Sanitizer
8
+ REDACTED = "[REDACTED]"
9
+
10
+ FORBIDDEN_CONTEXT_KEYS = %w[
11
+ env
12
+ environment
13
+ environment_variables
14
+ rack_env
15
+ headers
16
+ cookies
17
+ session
18
+ ].freeze
19
+
20
+ ALWAYS_REDACT_PATTERNS = %w[
21
+ password
22
+ passwd
23
+ secret
24
+ token
25
+ authorization
26
+ cookie
27
+ session_id
28
+ credit_card
29
+ card_number
30
+ cvv
31
+ cvc
32
+ api_key
33
+ ip_address
34
+ remote_addr
35
+ remote_ip
36
+ client_ip
37
+ ].freeze
38
+
39
+ BEARER_HEADER_REGEX = /\bBearer\s+[A-Za-z0-9\-._~+\/]+=*/i
40
+ PAN_CANDIDATE_REGEX = /\b(?:\d[ -]*?){13,19}\b/
41
+
42
+ class << self
43
+ def sanitize(normalized_event, project_id:, config: Sentrace.configuration, secret_base: nil)
44
+ new(config: config, secret_base: secret_base).sanitize(normalized_event, project_id: project_id)
45
+ end
46
+ end
47
+
48
+ def initialize(config: Sentrace.configuration, secret_base: nil)
49
+ @config = config
50
+ @secret_base = secret_base
51
+ @redact_terms = (ALWAYS_REDACT_PATTERNS + Array(@config.redact_fields).map { |f| f.to_s.downcase }).uniq
52
+ end
53
+
54
+ def sanitize(normalized_event, project_id:)
55
+ base_data = deep_dup(normalized_event.event_data)
56
+
57
+ base_data["request"] = sanitize_request(normalized_event.raw_request)
58
+ base_data["user"] = sanitize_user(normalized_event.raw_user, project_id: project_id)
59
+ base_data["tags"] = scrub_hash(base_data["tags"] || {})
60
+ base_data["breadcrumbs"] = scrub_breadcrumbs(base_data["breadcrumbs"] || [])
61
+ base_data["contexts"] = scrub_contexts(base_data["contexts"] || {})
62
+ base_data["message"] = scrub_text(base_data["message"]) if base_data["message"]
63
+
64
+ if base_data["exception"].is_a?(Hash)
65
+ exc = base_data["exception"].dup
66
+ exc["value"] = scrub_text(exc["value"]) if exc["value"]
67
+ base_data["exception"] = exc
68
+ end
69
+
70
+ if @config.sanitizer
71
+ custom_result = @config.sanitizer.call(deep_dup(base_data))
72
+ unless custom_result.is_a?(Hash)
73
+ raise Error, "Custom Sentrace sanitizer must return a Hash"
74
+ end
75
+ base_data = enforce_post_sanitizer_bounds(custom_result)
76
+ end
77
+
78
+ issue_summary_source = base_data.dig("exception", "value") || base_data["message"]
79
+ sanitized_issue_message = issue_summary_source ? issue_summary_source.to_s[0, 1024] : nil
80
+
81
+ normalized_event.with(
82
+ issue_message: sanitized_issue_message,
83
+ event_data: base_data,
84
+ raw_user: nil,
85
+ raw_request: nil
86
+ )
87
+ end
88
+
89
+ private
90
+ def sanitize_request(raw_request)
91
+ return nil unless raw_request.is_a?(Hash)
92
+
93
+ method = raw_request["method"]&.to_s&.strip&.upcase
94
+ method = nil if method && method.empty?
95
+ method = method[0, 16] if method
96
+
97
+ url = sanitize_url(raw_request["url"], raw_request["query_string"])
98
+ return nil if url.nil? && method.nil?
99
+
100
+ { "url" => url, "method" => method }.compact
101
+ end
102
+
103
+ def sanitize_url(raw_url, raw_query_string)
104
+ return nil if raw_url.nil? || raw_url.to_s.strip.empty?
105
+
106
+ uri = URI.parse(raw_url.to_s.strip)
107
+ uri.user = nil
108
+ uri.password = nil
109
+
110
+ combined_query = [ uri.query, raw_query_string.is_a?(String) ? raw_query_string : nil ]
111
+ .compact
112
+ .reject(&:empty?)
113
+ .join("&")
114
+
115
+ if !combined_query.empty?
116
+ scrubbed_pairs = URI.decode_www_form(combined_query).map do |k, v|
117
+ [ k, sensitive_key?(k) ? REDACTED : scrub_text(v) ]
118
+ end
119
+ uri.query = URI.encode_www_form(scrubbed_pairs)
120
+ else
121
+ uri.query = nil
122
+ end
123
+
124
+ EventNormalizer.truncate_utf8_bytes(uri.to_s, 1024)
125
+ rescue URI::InvalidURIError, ArgumentError
126
+ # Strip query string and credentials if URL could not be cleanly parsed
127
+ fallback = raw_url.to_s.split("?").first.to_s.sub(%r{//[^/@]+@}, "//")
128
+ EventNormalizer.truncate_utf8_bytes(fallback, 1024)
129
+ end
130
+
131
+ def sanitize_user(raw_user, project_id:)
132
+ return nil unless raw_user.is_a?(Hash)
133
+
134
+ raw_id = raw_user["id"] || raw_user[:id] ||
135
+ raw_user["username"] || raw_user[:username] ||
136
+ raw_user["email"] || raw_user[:email]
137
+ return nil if raw_id.nil? || raw_id.to_s.strip.empty?
138
+
139
+ key_material = "#{effective_secret_base}:sentrace:project:#{project_id}"
140
+ digest = OpenSSL::HMAC.hexdigest("SHA256", key_material, raw_id.to_s)
141
+ { "id_hash" => digest }
142
+ end
143
+
144
+ def effective_secret_base
145
+ return @secret_base if @secret_base && !@secret_base.empty?
146
+ return @config.user_hash_secret if @config.user_hash_secret && !@config.user_hash_secret.empty?
147
+
148
+ if defined?(Rails) && Rails.application && Rails.application.respond_to?(:secret_key_base) && Rails.application.secret_key_base.present?
149
+ Rails.application.secret_key_base
150
+ else
151
+ "sentrace-default-local-secret"
152
+ end
153
+ end
154
+
155
+ def scrub_contexts(contexts_hash)
156
+ return {} unless contexts_hash.is_a?(Hash)
157
+
158
+ contexts_hash.each_with_object({}) do |(k, v), acc|
159
+ next if FORBIDDEN_CONTEXT_KEYS.include?(k.to_s.downcase)
160
+
161
+ if sensitive_key?(k)
162
+ acc[k.to_s] = REDACTED
163
+ elsif v.is_a?(Hash)
164
+ acc[k.to_s] = scrub_hash(v)
165
+ end
166
+ end
167
+ end
168
+
169
+ def scrub_breadcrumbs(crumbs)
170
+ return [] unless crumbs.is_a?(Array)
171
+
172
+ crumbs.map do |crumb|
173
+ next unless crumb.is_a?(Hash)
174
+
175
+ cleaned = crumb.dup
176
+ cleaned["message"] = scrub_text(cleaned["message"]) if cleaned["message"]
177
+ if cleaned["data"].is_a?(Hash)
178
+ cleaned["data"] = scrub_hash(cleaned["data"])
179
+ end
180
+ cleaned
181
+ end.compact
182
+ end
183
+
184
+ def scrub_hash(hash)
185
+ hash.each_with_object({}) do |(k, v), acc|
186
+ key_str = k.to_s
187
+ if FORBIDDEN_CONTEXT_KEYS.include?(key_str.downcase)
188
+ next
189
+ elsif sensitive_key?(key_str)
190
+ acc[key_str] = REDACTED
191
+ else
192
+ acc[key_str] = scrub_value(v)
193
+ end
194
+ end
195
+ end
196
+
197
+ def scrub_value(val)
198
+ case val
199
+ when Hash
200
+ scrub_hash(val)
201
+ when Array
202
+ val.map { |item| scrub_value(item) }
203
+ when String
204
+ scrub_text(val)
205
+ else
206
+ val
207
+ end
208
+ end
209
+
210
+ def sensitive_key?(key)
211
+ downcased = key.to_s.downcase
212
+ @redact_terms.any? { |term| downcased.include?(term) }
213
+ end
214
+
215
+ def scrub_text(text)
216
+ return text unless text.is_a?(String)
217
+
218
+ cleaned = text.gsub(BEARER_HEADER_REGEX, "Bearer #{REDACTED}")
219
+ cleaned.gsub(PAN_CANDIDATE_REGEX) do |match|
220
+ digits = match.gsub(/\D/, "")
221
+ luhn_valid?(digits) ? REDACTED : match
222
+ end
223
+ end
224
+
225
+ def luhn_valid?(digits)
226
+ return false unless digits.length.between?(13, 19)
227
+ return false if digits.chars.uniq.one?
228
+
229
+ sum = 0
230
+ digits.reverse.chars.each_with_index do |ch, idx|
231
+ n = ch.to_i
232
+ if idx.odd?
233
+ n *= 2
234
+ n -= 9 if n > 9
235
+ end
236
+ sum += n
237
+ end
238
+ (sum % 10).zero?
239
+ end
240
+
241
+ def enforce_post_sanitizer_bounds(data)
242
+ bounded = data.dup
243
+ if bounded["message"].is_a?(String)
244
+ bounded["message"] = EventNormalizer.truncate_utf8_bytes(bounded["message"], @config.max_message_bytes)
245
+ end
246
+
247
+ if bounded["tags"].is_a?(Hash)
248
+ bounded["tags"] = bounded["tags"].first(@config.max_tags).to_h do |k, v|
249
+ [
250
+ EventNormalizer.truncate_utf8_bytes(k.to_s, @config.max_tag_key_bytes),
251
+ v.nil? ? nil : EventNormalizer.truncate_utf8_bytes(v.to_s, @config.max_tag_value_bytes)
252
+ ]
253
+ end
254
+ end
255
+
256
+ if bounded["breadcrumbs"].is_a?(Array)
257
+ bounded["breadcrumbs"] = bounded["breadcrumbs"].last(@config.max_breadcrumbs)
258
+ end
259
+
260
+ if bounded.dig("exception", "frames").is_a?(Array)
261
+ bounded["exception"] = bounded["exception"].merge(
262
+ "frames" => bounded.dig("exception", "frames").last(@config.max_stack_frames)
263
+ )
264
+ end
265
+
266
+ if bounded["contexts"].is_a?(Hash)
267
+ kept = {}
268
+ bounded["contexts"].each do |k, v|
269
+ break if kept.size >= @config.max_context_entries
270
+ candidate = kept.merge(k.to_s => v)
271
+ kept = candidate if JSON.generate(candidate).bytesize <= @config.max_context_bytes
272
+ end
273
+ bounded["contexts"] = kept
274
+ end
275
+
276
+ bounded
277
+ end
278
+
279
+ def deep_dup(obj)
280
+ case obj
281
+ when Hash
282
+ obj.transform_values { |v| deep_dup(v) }
283
+ when Array
284
+ obj.map { |v| deep_dup(v) }
285
+ when String
286
+ obj.dup
287
+ else
288
+ obj
289
+ end
290
+ end
291
+ end
292
+ end
293
+ end
@@ -0,0 +1,177 @@
1
+ require "json"
2
+ require "openssl"
3
+
4
+ module Sentrace
5
+ module Issues
6
+ class Fingerprint
7
+ DEFAULT_PLACEHOLDER = "{{ default }}"
8
+
9
+ GEM_PATH_REGEX = %r{(?:\A|/)gems/([^/]+?)(?:-\d[\w.-]*)?/(.+)\z}
10
+ RELEASE_PATH_REGEX = %r{(?:\A|/)(?:releases/[^/]+|current)/(.+)\z}
11
+ APP_ROOT_SUBDIR_REGEX = %r{(?:\A|/)((?:app|config|db|lib)/.+)\z}
12
+
13
+ OBJECT_ADDRESS_REGEX = /0x[0-9a-f]{6,}/i
14
+ ISO_TIMESTAMP_REGEX = /\b\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?\b/
15
+ UUID_REGEX = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/i
16
+ LONG_HEX_REGEX = /\b[0-9a-f]{12,}\b/i
17
+ INTEGER_REGEX = /\b\d+\b/
18
+
19
+ class << self
20
+ def compute(normalized_event)
21
+ new.compute(normalized_event)
22
+ end
23
+
24
+ def normalize_filename(raw_filename)
25
+ return "" if raw_filename.nil?
26
+
27
+ cleaned = raw_filename.to_s.tr("\\", "/").strip.gsub(%r{/+}, "/")
28
+ return "" if cleaned.empty?
29
+
30
+ if (gem_match = cleaned.match(GEM_PATH_REGEX))
31
+ "gem:#{gem_match[1]}/#{gem_match[2]}"
32
+ elsif (rel_match = cleaned.match(RELEASE_PATH_REGEX))
33
+ rel_match[1]
34
+ elsif (app_match = cleaned.match(APP_ROOT_SUBDIR_REGEX))
35
+ app_match[1]
36
+ else
37
+ cleaned.sub(%r{\A/+}, "")
38
+ end
39
+ end
40
+
41
+ def normalize_message(raw_message)
42
+ return "" if raw_message.nil?
43
+
44
+ raw_message.to_s
45
+ .gsub(ISO_TIMESTAMP_REGEX, "<timestamp>")
46
+ .gsub(UUID_REGEX, "<uuid>")
47
+ .gsub(OBJECT_ADDRESS_REGEX, "<hex>")
48
+ .gsub(LONG_HEX_REGEX, "<hex>")
49
+ .gsub(INTEGER_REGEX, "<int>")
50
+ .gsub(/\s+/, " ")
51
+ .strip
52
+ end
53
+ end
54
+
55
+ def compute(normalized_event)
56
+ tokens = build_tokens(normalized_event)
57
+ canonical_payload = JSON.generate(tokens)
58
+ OpenSSL::Digest::SHA256.hexdigest(canonical_payload)
59
+ end
60
+
61
+ private
62
+ def build_tokens(normalized_event)
63
+ explicit = extract_explicit_fingerprint(normalized_event)
64
+ if explicit && explicit != [ DEFAULT_PLACEHOLDER ]
65
+ return expand_explicit_fingerprint(explicit, normalized_event)
66
+ end
67
+
68
+ default_tokens(normalized_event)
69
+ end
70
+
71
+ def extract_explicit_fingerprint(normalized_event)
72
+ fp =
73
+ if normalized_event.respond_to?(:fingerprint)
74
+ normalized_event.fingerprint
75
+ elsif normalized_event.is_a?(Hash)
76
+ normalized_event["fingerprint"] || normalized_event[:fingerprint]
77
+ end
78
+
79
+ return nil unless fp.is_a?(Array)
80
+
81
+ cleaned = fp.filter_map do |item|
82
+ str = item.to_s.strip
83
+ str unless str.empty?
84
+ end
85
+ cleaned.empty? ? nil : cleaned
86
+ end
87
+
88
+ def expand_explicit_fingerprint(explicit, normalized_event)
89
+ if explicit.include?(DEFAULT_PLACEHOLDER)
90
+ defaults = default_tokens(normalized_event)
91
+ expanded = explicit.flat_map { |part| part == DEFAULT_PLACEHOLDER ? defaults : part }
92
+ [ "hybrid", *expanded ]
93
+ else
94
+ [ "explicit", *explicit ]
95
+ end
96
+ end
97
+
98
+ def default_tokens(normalized_event)
99
+ event_data = extract_event_data(normalized_event)
100
+ exception_hash = event_data["exception"].is_a?(Hash) ? event_data["exception"] : nil
101
+ exception_type = extract_exception_type(normalized_event, exception_hash)
102
+ frames = exception_hash && exception_hash["frames"].is_a?(Array) ? exception_hash["frames"] : []
103
+
104
+ usable_frames = select_frames(frames)
105
+ if usable_frames.any?
106
+ frame_tokens = usable_frames.map { |frame| normalize_frame(frame) }
107
+ [ "exception-frames", exception_type.to_s, *frame_tokens ]
108
+ else
109
+ raw_msg =
110
+ (exception_hash && exception_hash["value"]) ||
111
+ event_data["message"] ||
112
+ extract_issue_message(normalized_event)
113
+ [ "message", exception_type.to_s, self.class.normalize_message(raw_msg) ]
114
+ end
115
+ end
116
+
117
+ def select_frames(frames)
118
+ valid_frames = frames.select do |f|
119
+ f.is_a?(Hash) && (!f["filename"].to_s.strip.empty? || !f["function"].to_s.strip.empty?)
120
+ end
121
+ return [] if valid_frames.empty?
122
+
123
+ in_app_frames = valid_frames.select { |f| f["in_app"] == true }
124
+ in_app_frames.any? ? in_app_frames : valid_frames
125
+ end
126
+
127
+ def normalize_frame(frame)
128
+ file = self.class.normalize_filename(frame["filename"])
129
+ func = normalize_function(frame["function"])
130
+ if !func.empty?
131
+ "#{file}:#{func}"
132
+ elsif frame["lineno"]
133
+ "#{file}:line:#{frame["lineno"]}"
134
+ else
135
+ file
136
+ end
137
+ end
138
+
139
+ def normalize_function(raw_function)
140
+ return "" if raw_function.nil?
141
+
142
+ raw_function.to_s
143
+ .gsub(OBJECT_ADDRESS_REGEX, "<hex>")
144
+ .gsub(/\s+/, " ")
145
+ .strip
146
+ end
147
+
148
+ def extract_event_data(normalized_event)
149
+ if normalized_event.respond_to?(:event_data) && normalized_event.event_data.is_a?(Hash)
150
+ normalized_event.event_data
151
+ elsif normalized_event.is_a?(Hash)
152
+ (normalized_event["event_data"] || normalized_event[:event_data] || normalized_event).to_h
153
+ else
154
+ {}
155
+ end
156
+ end
157
+
158
+ def extract_exception_type(normalized_event, exception_hash)
159
+ if normalized_event.respond_to?(:exception_type) && normalized_event.exception_type
160
+ normalized_event.exception_type
161
+ elsif exception_hash && exception_hash["type"]
162
+ exception_hash["type"]
163
+ elsif normalized_event.is_a?(Hash)
164
+ normalized_event["exception_type"] || normalized_event[:exception_type]
165
+ end
166
+ end
167
+
168
+ def extract_issue_message(normalized_event)
169
+ if normalized_event.respond_to?(:issue_message)
170
+ normalized_event.issue_message
171
+ elsif normalized_event.is_a?(Hash)
172
+ normalized_event["issue_message"] || normalized_event[:issue_message] || normalized_event["message"]
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,184 @@
1
+ require "json"
2
+
3
+ module Sentrace
4
+ module Issues
5
+ class Grouper
6
+ PersistResult = Data.define(:event_id, :event_record_id, :issue_id, :duplicate)
7
+
8
+ class << self
9
+ def persist!(project_id:, normalized_event:, fingerprint:)
10
+ new.persist!(
11
+ project_id: project_id,
12
+ normalized_event: normalized_event,
13
+ fingerprint: fingerprint
14
+ )
15
+ end
16
+ end
17
+
18
+ def persist!(project_id:, normalized_event:, fingerprint:)
19
+ level_int = Issue::LEVELS.fetch(normalized_event.level.to_sym, Issue::LEVELS[:error])
20
+ now = Time.current.utc
21
+ occurred_at = normalized_event.occurred_at.utc
22
+ received_at = normalized_event.received_at.utc
23
+
24
+ Issue.transaction do
25
+ conn = Issue.connection
26
+
27
+ issue_id = upsert_issue_shell(
28
+ conn: conn,
29
+ project_id: project_id,
30
+ fingerprint: fingerprint,
31
+ level_int: level_int,
32
+ exception_type: normalized_event.exception_type,
33
+ message: normalized_event.issue_message,
34
+ occurred_at: occurred_at,
35
+ now: now
36
+ )
37
+
38
+ event_record_id = insert_event_if_unique(
39
+ conn: conn,
40
+ project_id: project_id,
41
+ issue_id: issue_id,
42
+ normalized_event: normalized_event,
43
+ level_int: level_int,
44
+ occurred_at: occurred_at,
45
+ received_at: received_at,
46
+ now: now
47
+ )
48
+
49
+ if event_record_id.nil?
50
+ next PersistResult.new(
51
+ event_id: normalized_event.event_id,
52
+ event_record_id: nil,
53
+ issue_id: issue_id,
54
+ duplicate: true
55
+ )
56
+ end
57
+
58
+ apply_issue_occurrence!(
59
+ conn: conn,
60
+ issue_id: issue_id,
61
+ level_int: level_int,
62
+ exception_type: normalized_event.exception_type,
63
+ message: normalized_event.issue_message,
64
+ occurred_at: occurred_at,
65
+ now: now
66
+ )
67
+
68
+ PersistResult.new(
69
+ event_id: normalized_event.event_id,
70
+ event_record_id: event_record_id,
71
+ issue_id: issue_id,
72
+ duplicate: false
73
+ )
74
+ end
75
+ end
76
+
77
+ private
78
+ def upsert_issue_shell(conn:, project_id:, fingerprint:, level_int:, exception_type:, message:, occurred_at:, now:)
79
+ sql = Issue.sanitize_sql_array([
80
+ <<~SQL.squish,
81
+ INSERT INTO sentrace_issues (
82
+ project_id,
83
+ fingerprint,
84
+ status,
85
+ level,
86
+ exception_type,
87
+ message,
88
+ first_seen_at,
89
+ last_seen_at,
90
+ event_count,
91
+ created_at,
92
+ updated_at
93
+ )
94
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?)
95
+ ON CONFLICT (project_id, fingerprint)
96
+ DO UPDATE SET updated_at = sentrace_issues.updated_at
97
+ RETURNING id
98
+ SQL
99
+ project_id,
100
+ fingerprint,
101
+ Issue::STATUSES.fetch(:unresolved),
102
+ level_int,
103
+ exception_type,
104
+ message,
105
+ occurred_at,
106
+ occurred_at,
107
+ now,
108
+ now
109
+ ])
110
+
111
+ conn.select_value(sql).to_i
112
+ end
113
+
114
+ def insert_event_if_unique(conn:, project_id:, issue_id:, normalized_event:, level_int:, occurred_at:, received_at:, now:)
115
+ sql = Event.sanitize_sql_array([
116
+ <<~SQL.squish,
117
+ INSERT INTO sentrace_events (
118
+ project_id,
119
+ issue_id,
120
+ event_id,
121
+ occurred_at,
122
+ received_at,
123
+ environment,
124
+ release,
125
+ platform,
126
+ level,
127
+ logger,
128
+ culprit,
129
+ event_data,
130
+ created_at
131
+ )
132
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?::jsonb, ?)
133
+ ON CONFLICT (project_id, event_id) DO NOTHING
134
+ RETURNING id
135
+ SQL
136
+ project_id,
137
+ issue_id,
138
+ normalized_event.event_id,
139
+ occurred_at,
140
+ received_at,
141
+ normalized_event.environment,
142
+ normalized_event.release,
143
+ normalized_event.platform,
144
+ level_int,
145
+ normalized_event.logger,
146
+ normalized_event.culprit,
147
+ JSON.generate(normalized_event.event_data),
148
+ now
149
+ ])
150
+
151
+ raw_id = conn.select_value(sql)
152
+ raw_id ? raw_id.to_i : nil
153
+ end
154
+
155
+ def apply_issue_occurrence!(conn:, issue_id:, level_int:, exception_type:, message:, occurred_at:, now:)
156
+ sql = Issue.sanitize_sql_array([
157
+ <<~SQL.squish,
158
+ UPDATE sentrace_issues
159
+ SET event_count = event_count + 1,
160
+ first_seen_at = LEAST(first_seen_at, ?),
161
+ last_seen_at = GREATEST(last_seen_at, ?),
162
+ level = ?,
163
+ exception_type = COALESCE(?, exception_type),
164
+ message = COALESCE(?, message),
165
+ status = CASE WHEN status = ? THEN ? ELSE status END,
166
+ updated_at = ?
167
+ WHERE id = ?
168
+ SQL
169
+ occurred_at,
170
+ occurred_at,
171
+ level_int,
172
+ exception_type,
173
+ message,
174
+ Issue::STATUSES.fetch(:resolved),
175
+ Issue::STATUSES.fetch(:unresolved),
176
+ now,
177
+ issue_id
178
+ ])
179
+
180
+ conn.execute(sql)
181
+ end
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,23 @@
1
+ module Sentrace
2
+ module Mcp
3
+ class Dispatcher
4
+ DEFAULT_REMOTE_CLIENT = Object.new.freeze
5
+ private_constant :DEFAULT_REMOTE_CLIENT
6
+
7
+ def initialize(config: Sentrace.configuration, remote_client: DEFAULT_REMOTE_CLIENT, router: nil)
8
+ @config = config
9
+ resolved_client = remote_client.equal?(DEFAULT_REMOTE_CLIENT) ? RemoteHttpClient.new : remote_client
10
+ @remote_client = resolved_client
11
+ @router = router || EnvironmentRouter.new(config: @config, remote_client: @remote_client)
12
+ end
13
+
14
+ def list_environments
15
+ @router.list_environments
16
+ end
17
+
18
+ def dispatch(operation, environment: nil, **kwargs)
19
+ @router.route(operation, environment: environment, **kwargs)
20
+ end
21
+ end
22
+ end
23
+ end