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,41 @@
1
+ module Sentrace
2
+ class Event < ApplicationRecord
3
+ EVENT_ID_FORMAT = /\A[0-9a-f]{32}\z/
4
+
5
+ class << self
6
+ def dangerous_attribute_method?(method_name)
7
+ return false if method_name == "logger" || method_name == "logger?"
8
+
9
+ super
10
+ end
11
+ end
12
+
13
+ belongs_to :project
14
+ belongs_to :issue
15
+
16
+ enum :level, Issue::LEVELS, prefix: :level, validate: true
17
+
18
+ before_validation :normalize_event_id
19
+
20
+ validates :event_id,
21
+ presence: true,
22
+ format: { with: EVENT_ID_FORMAT, message: "must be a 32-character lowercase hex string" },
23
+ uniqueness: { scope: :project_id }
24
+ validates :occurred_at, :received_at, presence: true
25
+ validates :environment, length: { maximum: 64 }, allow_nil: true
26
+ validates :release, length: { maximum: 128 }, allow_nil: true
27
+ validates :platform, length: { maximum: 32 }, allow_nil: true
28
+ validates :logger, length: { maximum: 128 }, allow_nil: true
29
+ validates :culprit, length: { maximum: 512 }, allow_nil: true
30
+ validate :event_data_must_be_hash
31
+
32
+ private
33
+ def normalize_event_id
34
+ self.event_id = event_id.to_s.strip.delete("-").downcase if event_id.present?
35
+ end
36
+
37
+ def event_data_must_be_hash
38
+ errors.add(:event_data, "must be a JSON object") unless event_data.is_a?(Hash)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,32 @@
1
+ module Sentrace
2
+ class Issue < ApplicationRecord
3
+ STATUSES = {
4
+ unresolved: 0,
5
+ resolved: 1,
6
+ ignored: 2
7
+ }.freeze
8
+
9
+ LEVELS = {
10
+ debug: 10,
11
+ info: 20,
12
+ warning: 30,
13
+ error: 40,
14
+ fatal: 50
15
+ }.freeze
16
+
17
+ belongs_to :project
18
+ has_many :events, dependent: :destroy
19
+
20
+ enum :status, STATUSES, default: :unresolved, validate: true
21
+ enum :level, LEVELS, prefix: :level, validate: true
22
+
23
+ validates :fingerprint,
24
+ presence: true,
25
+ length: { maximum: 64 },
26
+ uniqueness: { scope: :project_id }
27
+ validates :exception_type, length: { maximum: 255 }, allow_nil: true
28
+ validates :message, length: { maximum: 1024 }, allow_nil: true
29
+ validates :first_seen_at, :last_seen_at, presence: true
30
+ validates :event_count, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
31
+ end
32
+ end
@@ -0,0 +1,82 @@
1
+ require "openssl"
2
+ require "securerandom"
3
+
4
+ module Sentrace
5
+ class McpToken < ApplicationRecord
6
+ TOKEN_PREFIX_HEADER = "snt_mcp_".freeze
7
+ PREFIX_LENGTH = 12
8
+
9
+ scope :active, -> { where(active: true, revoked_at: nil) }
10
+
11
+ validates :name, presence: true, length: { maximum: 120 }
12
+ validates :token_prefix, presence: true, length: { maximum: 16 }
13
+ validates :token_digest, presence: true, length: { is: 64 }, uniqueness: true
14
+
15
+ class << self
16
+ def digest_token(raw_token)
17
+ OpenSSL::Digest::SHA256.hexdigest(raw_token.to_s)
18
+ end
19
+
20
+ def issue!(name: "default")
21
+ normalized_name = name.to_s.strip
22
+ raise ArgumentError, "Token name is required" if normalized_name.empty?
23
+
24
+ raw_token = "#{TOKEN_PREFIX_HEADER}#{SecureRandom.hex(24)}"
25
+ record = create!(
26
+ name: normalized_name,
27
+ token_prefix: raw_token[0, PREFIX_LENGTH],
28
+ token_digest: digest_token(raw_token),
29
+ active: true,
30
+ created_at: Time.current
31
+ )
32
+ [ record, raw_token ]
33
+ end
34
+
35
+ def authenticate(raw_token)
36
+ return nil if raw_token.blank?
37
+
38
+ provided = raw_token.to_s
39
+ return nil unless provided.start_with?(TOKEN_PREFIX_HEADER)
40
+
41
+ candidate_digest = digest_token(provided)
42
+ matched = nil
43
+
44
+ active.to_a.each do |record|
45
+ if record.token_digest.bytesize == candidate_digest.bytesize &&
46
+ ActiveSupport::SecurityUtils.secure_compare(record.token_digest, candidate_digest)
47
+ matched = record
48
+ end
49
+ end
50
+
51
+ return nil unless matched
52
+
53
+ touch_last_used_at(matched)
54
+ matched
55
+ end
56
+
57
+ def rotate!(name: "default")
58
+ normalized_name = name.to_s.strip
59
+ raise ArgumentError, "Token name is required" if normalized_name.empty?
60
+
61
+ transaction do
62
+ now = Time.current
63
+ active.where(name: normalized_name).find_each do |existing|
64
+ existing.revoke!(at: now)
65
+ end
66
+ issue!(name: normalized_name)
67
+ end
68
+ end
69
+
70
+ private
71
+ def touch_last_used_at(record)
72
+ record.update_columns(last_used_at: Time.current)
73
+ rescue ActiveRecord::ActiveRecordError
74
+ nil
75
+ end
76
+ end
77
+
78
+ def revoke!(at: Time.current)
79
+ update!(active: false, revoked_at: at)
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,50 @@
1
+ module Sentrace
2
+ class Project < ApplicationRecord
3
+ SLUG_FORMAT = /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/
4
+
5
+ has_many :project_keys, dependent: :destroy
6
+ has_many :issues, dependent: :destroy
7
+ has_many :events, dependent: :destroy
8
+
9
+ validates :name, presence: true, length: { maximum: 120 }
10
+ validates :slug,
11
+ presence: true,
12
+ length: { maximum: 120 },
13
+ uniqueness: true,
14
+ format: { with: SLUG_FORMAT, message: "must be lowercase alphanumeric words separated by hyphens" }
15
+
16
+ class << self
17
+ def provision!(slug:, name: nil)
18
+ normalized_slug = slug.to_s.strip.downcase
19
+ derived_name = (name && !name.to_s.strip.empty?) ? name.to_s.strip : normalized_slug.tr("-", " ").titleize
20
+
21
+ transaction do
22
+ project = create!(slug: normalized_slug, name: derived_name)
23
+ key = project.project_keys.create!
24
+ [ project, key ]
25
+ end
26
+ end
27
+ end
28
+
29
+ def active_key
30
+ project_keys.active.order(created_at: :desc, id: :desc).first
31
+ end
32
+
33
+ def rotate_key!
34
+ with_lock do
35
+ now = Time.current
36
+ project_keys.active.find_each do |existing_key|
37
+ existing_key.revoke!(at: now)
38
+ end
39
+ project_keys.create!
40
+ end
41
+ end
42
+
43
+ def dsn(base_url:)
44
+ key = active_key
45
+ raise Error, "Project #{slug.inspect} has no active DSN key" unless key
46
+
47
+ Dsn.build(base_url: base_url, public_key: key.public_key, project_id: id).to_s
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,60 @@
1
+ require "openssl"
2
+ require "securerandom"
3
+
4
+ module Sentrace
5
+ class ProjectKey < ApplicationRecord
6
+ belongs_to :project
7
+
8
+ scope :active, -> { where(active: true, revoked_at: nil) }
9
+
10
+ before_validation :ensure_public_key, on: :create
11
+
12
+ validates :public_key, presence: true, length: { maximum: 64 }, uniqueness: true
13
+ validates :secret_digest, length: { maximum: 255 }, allow_nil: true
14
+
15
+ class << self
16
+ def digest_secret(raw_secret)
17
+ OpenSSL::Digest::SHA256.hexdigest(raw_secret.to_s)
18
+ end
19
+
20
+ def authenticate_public_key(project_id:, public_key:, secret: nil)
21
+ return nil if project_id.blank? || public_key.blank?
22
+
23
+ provided_pub = public_key.to_s
24
+ candidates = active.where(project_id: project_id).to_a
25
+
26
+ matched = candidates.find do |key|
27
+ key.public_key.bytesize == provided_pub.bytesize &&
28
+ ActiveSupport::SecurityUtils.secure_compare(key.public_key, provided_pub)
29
+ end
30
+
31
+ return nil unless matched
32
+ return nil if matched.secret_digest.present? && !matched.verify_secret(secret)
33
+
34
+ matched
35
+ end
36
+ end
37
+
38
+ def secret=(raw_secret)
39
+ @secret = nil
40
+ self.secret_digest = raw_secret.present? ? self.class.digest_secret(raw_secret) : nil
41
+ end
42
+
43
+ def verify_secret(candidate)
44
+ return false if secret_digest.blank? || candidate.blank?
45
+
46
+ candidate_digest = self.class.digest_secret(candidate)
47
+ secret_digest.bytesize == candidate_digest.bytesize &&
48
+ ActiveSupport::SecurityUtils.secure_compare(secret_digest, candidate_digest)
49
+ end
50
+
51
+ def revoke!(at: Time.current)
52
+ update!(active: false, revoked_at: at)
53
+ end
54
+
55
+ private
56
+ def ensure_public_key
57
+ self.public_key = SecureRandom.hex(16) if public_key.blank?
58
+ end
59
+ end
60
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,11 @@
1
+ Sentrace::Engine.routes.draw do
2
+ post "/api/:project_id/envelope", to: "ingestion#envelope"
3
+ match "/mcp", to: "mcp#handle", via: [ :post, :get, :delete ]
4
+
5
+ namespace :internal do
6
+ resources :projects
7
+ resources :issues, only: [ :index, :show, :update ]
8
+ resources :events, only: [ :index, :show ]
9
+ resource :stats, only: [ :show ], controller: "stats"
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ class CreateSentraceProjects < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :sentrace_projects do |t|
4
+ t.string :name, limit: 120, null: false
5
+ t.string :slug, limit: 120, null: false
6
+ t.timestamptz :created_at, null: false
7
+ t.timestamptz :updated_at, null: false
8
+ end
9
+
10
+ add_index :sentrace_projects, :slug, unique: true
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ class CreateSentraceProjectKeys < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :sentrace_project_keys do |t|
4
+ t.references :project, null: false, foreign_key: { to_table: :sentrace_projects, on_delete: :cascade }
5
+ t.string :public_key, limit: 64, null: false
6
+ t.string :secret_digest, limit: 255
7
+ t.boolean :active, null: false, default: true
8
+ t.timestamptz :created_at, null: false
9
+ t.timestamptz :revoked_at
10
+ end
11
+
12
+ add_index :sentrace_project_keys, :public_key, unique: true
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ class CreateSentraceIssues < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :sentrace_issues do |t|
4
+ t.references :project, null: false, index: false, foreign_key: { to_table: :sentrace_projects, on_delete: :cascade }
5
+ t.string :fingerprint, limit: 64, null: false
6
+ t.integer :status, limit: 2, null: false, default: 0
7
+ t.integer :level, limit: 2, null: false
8
+ t.string :exception_type, limit: 255
9
+ t.string :message, limit: 1024
10
+ t.timestamptz :first_seen_at, null: false
11
+ t.timestamptz :last_seen_at, null: false
12
+ t.bigint :event_count, null: false, default: 0
13
+ t.timestamptz :created_at, null: false
14
+ t.timestamptz :updated_at, null: false
15
+ end
16
+
17
+ add_index :sentrace_issues, [ :project_id, :fingerprint ],
18
+ unique: true,
19
+ name: "index_sentrace_issues_on_project_fingerprint"
20
+
21
+ add_index :sentrace_issues, [ :project_id, :status, :last_seen_at ],
22
+ order: { last_seen_at: :desc },
23
+ name: "index_sentrace_issues_on_project_status_last_seen"
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ class CreateSentraceEvents < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :sentrace_events do |t|
4
+ t.references :project, null: false, index: false, foreign_key: { to_table: :sentrace_projects, on_delete: :cascade }
5
+ t.references :issue, null: false, index: false, foreign_key: { to_table: :sentrace_issues, on_delete: :cascade }
6
+ t.column :event_id, "char(32)", null: false
7
+ t.timestamptz :occurred_at, null: false
8
+ t.timestamptz :received_at, null: false
9
+ t.string :environment, limit: 64
10
+ t.string :release, limit: 128
11
+ t.string :platform, limit: 32
12
+ t.integer :level, limit: 2, null: false
13
+ t.string :logger, limit: 128
14
+ t.string :culprit, limit: 512
15
+ t.jsonb :event_data, null: false, default: {}
16
+ t.timestamptz :created_at, null: false
17
+ end
18
+
19
+ add_index :sentrace_events, [ :project_id, :event_id ],
20
+ unique: true,
21
+ name: "index_sentrace_events_on_project_event_id"
22
+
23
+ add_index :sentrace_events, [ :issue_id, :occurred_at ],
24
+ order: { occurred_at: :desc },
25
+ name: "index_sentrace_events_on_issue_occurred_at"
26
+
27
+ add_index :sentrace_events, [ :project_id, :occurred_at ],
28
+ order: { occurred_at: :desc },
29
+ name: "index_sentrace_events_on_project_occurred_at"
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ class CreateSentraceMcpTokens < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :sentrace_mcp_tokens do |t|
4
+ t.string :name, limit: 120, null: false
5
+ t.string :token_prefix, limit: 16, null: false
6
+ t.string :token_digest, limit: 64, null: false
7
+ t.boolean :active, null: false, default: true
8
+ t.timestamptz :last_used_at
9
+ t.timestamptz :revoked_at
10
+ t.timestamptz :created_at, null: false
11
+ end
12
+
13
+ add_index :sentrace_mcp_tokens, :token_digest,
14
+ unique: true,
15
+ name: "index_sentrace_mcp_tokens_on_token_digest"
16
+ end
17
+ end
@@ -0,0 +1,91 @@
1
+ require "rails/generators"
2
+ require "securerandom"
3
+ require "fileutils"
4
+
5
+ module Sentrace
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("templates", __dir__)
9
+
10
+ desc "Installs Sentrace initializer, route mount, engine migrations, and project setup task."
11
+
12
+ def create_initializer
13
+ template "initializer.rb.tt", "config/initializers/sentrace.rb"
14
+ end
15
+
16
+ def mount_engine
17
+ routes_path = File.join(destination_root, "config/routes.rb")
18
+ return unless File.exist?(routes_path)
19
+
20
+ content = File.read(routes_path)
21
+ return if content.include?("Sentrace::Engine")
22
+
23
+ route 'mount Sentrace::Engine => "/sentrace"'
24
+ end
25
+
26
+ def copy_engine_migrations
27
+ source_migrations_dir = Sentrace::Engine.root.join("db/migrate")
28
+ target_migrations_dir = File.join(destination_root, "db/migrate")
29
+ FileUtils.mkdir_p(target_migrations_dir)
30
+
31
+ existing_suffixes = Dir.glob(File.join(target_migrations_dir, "*.rb")).map do |file|
32
+ File.basename(file).sub(/\A\d+_/, "")
33
+ end
34
+
35
+ timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
36
+
37
+ Dir.glob(File.join(source_migrations_dir, "*.rb")).sort.each do |source_file|
38
+ suffix = File.basename(source_file).sub(/\A\d+_/, "")
39
+ if existing_suffixes.include?(suffix)
40
+ say_status :identical, "db/migrate/#{suffix}", :blue
41
+ next
42
+ end
43
+
44
+ timestamp += 1
45
+ target_rel = "db/migrate/#{timestamp}_#{suffix}"
46
+ create_file target_rel, File.read(source_file)
47
+ end
48
+ end
49
+
50
+ def create_project_setup_task
51
+ template "sentrace_setup.rake.tt", "lib/tasks/sentrace_setup.rake"
52
+ end
53
+
54
+ def display_sdk_instructions
55
+ sample_dsn = Dsn.build(
56
+ base_url: "http://localhost:3000/sentrace",
57
+ public_key: generated_public_key,
58
+ project_id: 1
59
+ )
60
+
61
+ say "\nSentrace installed!", :green
62
+ say <<~INSTRUCTIONS
63
+
64
+ Next steps:
65
+ 1. Run migrations:
66
+ bin/rails db:migrate
67
+
68
+ 2. Bootstrap your initial project and local DSN:
69
+ bin/rails sentrace:setup
70
+
71
+ 3. Configure sentry-ruby (e.g. config/initializers/sentry.rb):
72
+ Sentry.init do |config|
73
+ config.dsn = ENV.fetch("SENTRY_DSN", "#{sample_dsn}")
74
+ config.environment = Rails.env
75
+ config.release = ENV["APP_RELEASE"]
76
+ end
77
+ INSTRUCTIONS
78
+ end
79
+
80
+ private
81
+ def generated_public_key
82
+ @generated_public_key ||= SecureRandom.hex(16)
83
+ end
84
+
85
+ def default_project_slug
86
+ app_name = File.basename(destination_root).downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-|-\z/, "")
87
+ app_name.empty? ? "app" : app_name
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,34 @@
1
+ Sentrace.configure do |config|
2
+ config.enabled = ENV.fetch("SENTRACE_ENABLED", "true") == "true"
3
+
4
+ config.retention_period = 30.days
5
+ config.orphan_issue_retention_period = 7.days
6
+
7
+ config.max_envelope_size = 1.megabyte
8
+ config.max_stack_frames = 50
9
+ config.max_breadcrumbs = 20
10
+
11
+ config.redact_fields = %w[
12
+ password
13
+ token
14
+ authorization
15
+ cookie
16
+ credit_card
17
+ ]
18
+
19
+ config.environments = {
20
+ local: {
21
+ transport: :local
22
+ }
23
+ # staging: {
24
+ # transport: :http,
25
+ # url: ENV["SENTRACE_STAGING_MCP_URL"],
26
+ # token: ENV["SENTRACE_STAGING_MCP_TOKEN"]
27
+ # },
28
+ # production: {
29
+ # transport: :http,
30
+ # url: ENV["SENTRACE_PRODUCTION_MCP_URL"],
31
+ # token: ENV["SENTRACE_PRODUCTION_MCP_TOKEN"]
32
+ # }
33
+ }
34
+ end
@@ -0,0 +1,21 @@
1
+ namespace :sentrace do
2
+ desc "Bootstrap the default Sentrace project and output local DSN credentials"
3
+ task setup: :environment do
4
+ slug = ENV.fetch("SENTRACE_PROJECT_SLUG", "<%= default_project_slug %>")
5
+ base_url = ENV.fetch("SENTRACE_BASE_URL", "http://localhost:3000/sentrace")
6
+
7
+ project = Sentrace::Project.find_by(slug: slug)
8
+ if project
9
+ key = project.active_key || project.rotate_key!
10
+ else
11
+ project, key = Sentrace::Project.provision!(
12
+ slug: slug,
13
+ name: slug.tr("-", " ").titleize
14
+ )
15
+ end
16
+
17
+ dsn = Sentrace::Dsn.build(base_url: base_url, public_key: key.public_key, project_id: project.id)
18
+ puts "Sentrace project '#{project.slug}' (id=#{project.id}) ready."
19
+ puts "SENTRY_DSN=#{dsn}"
20
+ end
21
+ end