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.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +192 -0
- data/app/controllers/sentrace/application_controller.rb +4 -0
- data/app/controllers/sentrace/ingestion_controller.rb +169 -0
- data/app/controllers/sentrace/internal/base_controller.rb +47 -0
- data/app/controllers/sentrace/internal/events_controller.rb +25 -0
- data/app/controllers/sentrace/internal/issues_controller.rb +36 -0
- data/app/controllers/sentrace/internal/projects_controller.rb +58 -0
- data/app/controllers/sentrace/internal/stats_controller.rb +10 -0
- data/app/controllers/sentrace/mcp_controller.rb +74 -0
- data/app/jobs/sentrace/retention_cleanup_job.rb +9 -0
- data/app/models/sentrace/application_record.rb +5 -0
- data/app/models/sentrace/event.rb +41 -0
- data/app/models/sentrace/issue.rb +32 -0
- data/app/models/sentrace/mcp_token.rb +82 -0
- data/app/models/sentrace/project.rb +50 -0
- data/app/models/sentrace/project_key.rb +60 -0
- data/config/routes.rb +11 -0
- data/db/migrate/20261116000001_create_sentrace_projects.rb +12 -0
- data/db/migrate/20261116000002_create_sentrace_project_keys.rb +14 -0
- data/db/migrate/20261116000003_create_sentrace_issues.rb +25 -0
- data/db/migrate/20261116000004_create_sentrace_events.rb +31 -0
- data/db/migrate/20261116000005_create_sentrace_mcp_tokens.rb +17 -0
- data/lib/generators/sentrace/install/install_generator.rb +91 -0
- data/lib/generators/sentrace/install/templates/initializer.rb.tt +34 -0
- data/lib/generators/sentrace/install/templates/sentrace_setup.rake.tt +21 -0
- data/lib/sentrace/benchmark/storage_runner.rb +289 -0
- data/lib/sentrace/configuration.rb +198 -0
- data/lib/sentrace/dsn.rb +123 -0
- data/lib/sentrace/engine.rb +10 -0
- data/lib/sentrace/errors.rb +20 -0
- data/lib/sentrace/ingestion/envelope_parser.rb +143 -0
- data/lib/sentrace/ingestion/event_normalizer.rb +315 -0
- data/lib/sentrace/ingestion/processor.rb +90 -0
- data/lib/sentrace/ingestion/sanitizer.rb +293 -0
- data/lib/sentrace/issues/fingerprint.rb +177 -0
- data/lib/sentrace/issues/grouper.rb +184 -0
- data/lib/sentrace/mcp/dispatcher.rb +23 -0
- data/lib/sentrace/mcp/environment_router.rb +266 -0
- data/lib/sentrace/mcp/local_backend.rb +425 -0
- data/lib/sentrace/mcp/remote_http_client.rb +310 -0
- data/lib/sentrace/mcp/server.rb +417 -0
- data/lib/sentrace/mcp/stdio_runner.rb +55 -0
- data/lib/sentrace/mcp/token_manager.rb +73 -0
- data/lib/sentrace/project_manager.rb +64 -0
- data/lib/sentrace/retention/cleaner.rb +105 -0
- data/lib/sentrace/storage_stats.rb +136 -0
- data/lib/sentrace/version.rb +3 -0
- data/lib/sentrace.rb +40 -0
- data/lib/tasks/sentrace_benchmark.rake +9 -0
- data/lib/tasks/sentrace_mcp.rake +36 -0
- data/lib/tasks/sentrace_projects.rake +22 -0
- data/lib/tasks/sentrace_retention.rake +16 -0
- metadata +148 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 79d8d70678e4b21d323c5893d691f000c5694935c5bf32b2a40931a50d3c265e
|
|
4
|
+
data.tar.gz: 75d7375fd5c405d907553b836f20bd60c8f9f6c4dc9063877cfdf82844e0549b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3ec67bc3842bf8d5076ed14888fc9c1d0e148d9106b57e31a44857106213a5975630d07b616acc5520e6e0dd128fc0834bf353220b45b8a352084d8b81fc5273
|
|
7
|
+
data.tar.gz: 4a376a0eabdd3929ea5bc54ff990249341e2204137df675eca85dd5d3d8f2fe95ec9d2d68038896cef985c0363b0aa401a239cb4696e9be7ca6ce0ee831c52e2
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kemal Elmizan
|
|
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,192 @@
|
|
|
1
|
+
# Sentrace
|
|
2
|
+
|
|
3
|
+
**Sentry-compatible error tracking for Ruby on Rails, with a federated Model Context Protocol (MCP) interface.**
|
|
4
|
+
|
|
5
|
+
Sentrace is a self-hosted, isolated Rails Engine (`Sentrace::Engine`) backed solely by PostgreSQL. It ingests application errors via the Sentry v7 envelope protocol (`sentry-ruby`), sanitizes sensitive fields before persistence, normalizes and deduplicates events, groups related occurrences into deterministic fingerprinted issues, and exposes issues, events, and trends across `local`, `staging`, and `production` environments through local `stdio` and remote Streamable HTTP MCP transports.
|
|
6
|
+
|
|
7
|
+
See [SPEC.md](SPEC.md) for the complete product specification and architecture.
|
|
8
|
+
|
|
9
|
+
## Architecture
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
Rails application (sentry-ruby)
|
|
13
|
+
|
|
|
14
|
+
| POST /sentrace/api/:project_id/envelope (raw or gzip)
|
|
15
|
+
v
|
|
16
|
+
Sentrace::Ingestion::Processor
|
|
17
|
+
-> EnvelopeParser -> EventNormalizer -> Sanitizer -> Issues::Fingerprint -> Issues::Grouper
|
|
18
|
+
|
|
|
19
|
+
v
|
|
20
|
+
PostgreSQL (sentrace_projects, sentrace_project_keys, sentrace_mcp_tokens, sentrace_issues, sentrace_events)
|
|
21
|
+
|
|
|
22
|
+
+---> Local MCP Server (stdio: bin/rails sentrace:mcp)
|
|
23
|
+
| \
|
|
24
|
+
| +---> EnvironmentRouter ---> Remote Streamable HTTP (/sentrace/mcp on staging & production)
|
|
25
|
+
|
|
|
26
|
+
+---> Retention::Cleaner (ActiveJob / Rake) & StorageStats
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Requirements
|
|
30
|
+
|
|
31
|
+
- Ruby `>= 3.2` (local development standardized on `3.3.13`)
|
|
32
|
+
- Rails `>= 7.1`
|
|
33
|
+
- PostgreSQL `>= 15`
|
|
34
|
+
- Official [`mcp`](https://rubygems.org/gems/mcp) Ruby SDK (`~> 1.0`)
|
|
35
|
+
- Client SDK compatibility: verified against official [`sentry-ruby`](https://github.com/getsentry/sentry-ruby) `5.28.x` (`5.28.1` pinned in integration specs; Envelope protocol v7, raw & `gzip` transport, exceptions, messages, breadcrumbs, stack traces, and safe zero-write discarding of unsupported performance/session items)
|
|
36
|
+
|
|
37
|
+
## Host Application Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
bundle add sentrace
|
|
41
|
+
bin/rails generate sentrace:install
|
|
42
|
+
bin/rails db:migrate
|
|
43
|
+
bin/rails sentrace:setup
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Configure `sentry-ruby` with the generated DSN:
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
Sentry.init do |config|
|
|
50
|
+
config.dsn = ENV.fetch("SENTRY_DSN")
|
|
51
|
+
config.environment = Rails.env
|
|
52
|
+
config.release = ENV["APP_RELEASE"]
|
|
53
|
+
end
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Configuration Reference
|
|
57
|
+
|
|
58
|
+
Configure Sentrace in `config/initializers/sentrace.rb`:
|
|
59
|
+
|
|
60
|
+
| Option | Default | Description |
|
|
61
|
+
|---|---|---|
|
|
62
|
+
| `enabled` | `true` | Enable or disable Sentrace envelope ingestion (`202 Accepted` no-op when `false`). |
|
|
63
|
+
| `deployment_environment` | `Rails.env` | Environment attribution label stamped on remote `/sentrace/mcp` responses. |
|
|
64
|
+
| `retention_period` | `30.days` | How long normalized `sentrace_events` rows are retained before batch pruning. |
|
|
65
|
+
| `orphan_issue_retention_period` | `7.days` | Grace period before deleting `sentrace_issues` with zero remaining events. |
|
|
66
|
+
| `retention_batch_size` | `1000` | Maximum rows deleted per SQL statement during retention cleanup. |
|
|
67
|
+
| `max_envelope_size` | `1.megabyte` | Pre-parse compressed and decompressed envelope byte ceiling (`413` if exceeded). |
|
|
68
|
+
| `max_message_bytes` | `4.kilobytes` | Maximum UTF-8 byte size for normalized exception/event messages. |
|
|
69
|
+
| `max_stack_frames` | `50` | Maximum stack frames retained per event (preserving innermost frames). |
|
|
70
|
+
| `max_breadcrumbs` | `20` | Maximum trailing breadcrumbs retained per event. |
|
|
71
|
+
| `max_tags` | `32` | Maximum tags retained per event (`max_tag_key_bytes = 64`, `max_tag_value_bytes = 256`). |
|
|
72
|
+
| `max_context_entries` | `16` | Maximum keys in `contexts` (`max_context_bytes = 8.kilobytes`). |
|
|
73
|
+
| `redact_fields` | `%w[password token authorization cookie credit_card]` | Case-insensitive key substrings replaced with `"[REDACTED]"` before persistence. |
|
|
74
|
+
| `sanitizer` | `nil` | Optional callable `->(normalized_event_hash) { ... }` invoked before persistence. |
|
|
75
|
+
| `user_hash_secret` | `nil` | Secret key mixed with `project_id` to compute one-way HMAC-SHA256 `user.id_hash`. |
|
|
76
|
+
| `remote_timeout` | `5.0` | Default timeout (seconds) for remote `:http` MCP federation requests. |
|
|
77
|
+
| `environments` | `{ local: { transport: :local } }` | Map of `:local` and `:http` MCP environments for federation. |
|
|
78
|
+
| `internal_authenticator` | `nil` | Optional callable `->(request) { boolean }` for `/sentrace/internal/*` routes (defaults to MCP bearer token auth). |
|
|
79
|
+
|
|
80
|
+
## Project & Credential Management
|
|
81
|
+
|
|
82
|
+
Ingestion DSN public keys and remote MCP bearer tokens are strictly isolated credentials:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
# Ingestion DSN keys (for sentry-ruby SDKs)
|
|
86
|
+
bin/rails sentrace:projects
|
|
87
|
+
bin/rails "sentrace:projects:create[my-app]"
|
|
88
|
+
bin/rails "sentrace:projects:rotate_key[my-app]"
|
|
89
|
+
|
|
90
|
+
# Dedicated MCP bearer tokens (stored as SHA-256 digests, separate from DSNs)
|
|
91
|
+
bin/rails sentrace:mcp:tokens
|
|
92
|
+
bin/rails "sentrace:mcp:tokens:create[staging]"
|
|
93
|
+
bin/rails "sentrace:mcp:tokens:rotate[staging]"
|
|
94
|
+
bin/rails "sentrace:mcp:tokens:revoke[1]"
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Local MCP Server (`stdio`) & Environment Federation
|
|
98
|
+
|
|
99
|
+
Run the standalone Sentrace MCP server over `stdio` (no running Rails web server required):
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
bin/rails sentrace:mcp
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Example `.mcp.json` configuration (Claude Code / Cursor / Antigravity):
|
|
106
|
+
|
|
107
|
+
```json
|
|
108
|
+
{
|
|
109
|
+
"mcpServers": {
|
|
110
|
+
"sentrace": {
|
|
111
|
+
"command": "bin/rails",
|
|
112
|
+
"args": ["sentrace:mcp"]
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Each remote Rails deployment (`staging`, `production`) exposes a stateless Streamable HTTP MCP endpoint at `POST /sentrace/mcp` (enforcing HTTPS on non-loopback hosts and constant-time `Authorization: Bearer <snt_mcp_token>` authentication). Configure `config.environments` locally to federate across deployments concurrently with per-environment timeouts and cross-environment fingerprint correlation:
|
|
119
|
+
|
|
120
|
+
```ruby
|
|
121
|
+
Sentrace.configure do |config|
|
|
122
|
+
config.deployment_environment = ENV.fetch("SENTRACE_DEPLOYMENT_ENV", Rails.env)
|
|
123
|
+
config.remote_timeout = 5.0
|
|
124
|
+
|
|
125
|
+
config.environments = {
|
|
126
|
+
local: { transport: :local },
|
|
127
|
+
staging: {
|
|
128
|
+
transport: :http,
|
|
129
|
+
url: "https://staging.example.com/sentrace/mcp",
|
|
130
|
+
token: ENV["SENTRACE_STAGING_MCP_TOKEN"],
|
|
131
|
+
timeout: 3.0
|
|
132
|
+
},
|
|
133
|
+
production: {
|
|
134
|
+
transport: :http,
|
|
135
|
+
url: "https://errors.example.com/sentrace/mcp",
|
|
136
|
+
token: ENV["SENTRACE_PROD_MCP_TOKEN"],
|
|
137
|
+
timeout: 5.0
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
end
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Exposed MCP Capabilities
|
|
144
|
+
- **Tools**: `list_environments`, `list_projects`, `list_issues`, `get_issue`, `update_issue_status`, `list_events`, `get_event`, `get_error_trends`
|
|
145
|
+
- **Resources & Templates**: `sentrace://environments`, `sentrace://projects`, `sentrace://projects/{project_id}/issues`, `sentrace://issues/{issue_id}`, `sentrace://events/{event_id}`
|
|
146
|
+
- **Prompts**: `investigate_issue` (structured 8-step debugging workflow separating **Observed Evidence** from **Testable Hypotheses**)
|
|
147
|
+
|
|
148
|
+
## Retention, Storage Observability & Benchmarking
|
|
149
|
+
|
|
150
|
+
Run bounded batch retention via Rake or enqueue `Sentrace::RetentionCleanupJob` using any Rails ActiveJob adapter (no Redis required):
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
# Prune expired events and aged orphan issues
|
|
154
|
+
bin/rails sentrace:retention:cleanup
|
|
155
|
+
|
|
156
|
+
# Inspect PostgreSQL relation sizes, retention counts, and daily aggregates
|
|
157
|
+
bin/rails sentrace:stats
|
|
158
|
+
|
|
159
|
+
# Run the repeatable PostgreSQL storage, throughput, and latency benchmark
|
|
160
|
+
EVENTS=500 bin/rails sentrace:benchmark:storage
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Reference benchmark on PostgreSQL 18 / Ruby 3.3.13 (`EVENTS=500`, 8 stack frames + 8 breadcrumbs + tags/contexts per event):
|
|
164
|
+
- **Average PostgreSQL storage per event**: `~1.7 KB` (`~29.8%` B-tree index share across all 5 engine tables)
|
|
165
|
+
- **Envelope parse latency**: `~0.02 ms` (`SPEC.md` target `< 50 ms`)
|
|
166
|
+
- **Single-event persist latency**: `~0.8 ms` / `~1,300+ events/sec` (`SPEC.md` target `< 100 ms`)
|
|
167
|
+
- **Issue/event lookup & MCP `list_issues` latency**: `~0.3–1.6 ms` (`SPEC.md` targets `< 100 ms` / `< 500 ms`)
|
|
168
|
+
- **Batch retention deletion throughput**: `> 100,000 events/sec`
|
|
169
|
+
|
|
170
|
+
## Internal Administrative JSON API
|
|
171
|
+
|
|
172
|
+
`Sentrace::Engine` also mounts `/internal/projects`, `/internal/issues`, `/internal/events`, and `/internal/stats` for administration and integration testing, protected by default with MCP bearer tokens or a host-provided `config.internal_authenticator` policy.
|
|
173
|
+
|
|
174
|
+
## Development Workflow (OpenSpec)
|
|
175
|
+
|
|
176
|
+
This repository uses [OpenSpec](https://github.com/Fission-AI/OpenSpec) (`spec-driven` schema):
|
|
177
|
+
|
|
178
|
+
- `SPEC.md` — Product vision, architecture, and MVP specification reference.
|
|
179
|
+
- `openspec/config.yaml` — Project-wide OpenSpec constraints, stack conventions, and workflow rules.
|
|
180
|
+
- `openspec/specs/` — Living source of truth for all implemented capabilities.
|
|
181
|
+
- `openspec/changes/archive/` — Historical record of completed specification-driven phases.
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
bin/setup
|
|
185
|
+
bundle exec rspec
|
|
186
|
+
bundle exec rubocop
|
|
187
|
+
openspec validate --all --strict --no-interactive
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## License
|
|
191
|
+
|
|
192
|
+
Released under the [MIT License](LICENSE.txt).
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
require "stringio"
|
|
2
|
+
require "zlib"
|
|
3
|
+
|
|
4
|
+
module Sentrace
|
|
5
|
+
class IngestionController < ApplicationController
|
|
6
|
+
GZIP_CHUNK_BYTES = 16 * 1024
|
|
7
|
+
HEADER_PEEK_BYTES = 16 * 1024
|
|
8
|
+
|
|
9
|
+
def envelope
|
|
10
|
+
config = Sentrace.configuration
|
|
11
|
+
unless config.enabled
|
|
12
|
+
return render json: { id: nil }, status: :accepted
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
max_size = config.max_envelope_size
|
|
16
|
+
if request.content_length && request.content_length > max_size
|
|
17
|
+
return render json: { error: "Payload too large" }, status: :content_too_large
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
raw_bytes = read_bounded_stream(request.body, max_size)
|
|
21
|
+
matched_key = authenticate_request(raw_bytes, max_size: max_size)
|
|
22
|
+
unless matched_key
|
|
23
|
+
return render json: { error: "Unauthorized" }, status: :unauthorized
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
envelope_bytes = gzip_encoded? ? inflate_bounded_gzip(raw_bytes, max_size) : raw_bytes
|
|
27
|
+
|
|
28
|
+
result = Ingestion::Processor.process_raw_envelope(
|
|
29
|
+
envelope_bytes,
|
|
30
|
+
project: matched_key.project_id,
|
|
31
|
+
config: config
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
render json: { id: result.primary_event_id }, status: :ok
|
|
35
|
+
rescue PayloadTooLargeError
|
|
36
|
+
render json: { error: "Payload too large" }, status: :content_too_large
|
|
37
|
+
rescue EnvelopeParseError
|
|
38
|
+
render json: { error: "Malformed envelope" }, status: :bad_request
|
|
39
|
+
rescue StandardError => e
|
|
40
|
+
Rails.logger&.error("[Sentrace] Ingestion error (#{e.class})")
|
|
41
|
+
render json: { error: "Internal ingestion error" }, status: :internal_server_error
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
def read_bounded_stream(io, max_size)
|
|
46
|
+
return "".b if io.nil?
|
|
47
|
+
|
|
48
|
+
io.rewind if io.respond_to?(:rewind)
|
|
49
|
+
buffer = "".b
|
|
50
|
+
while (chunk = io.read(GZIP_CHUNK_BYTES))
|
|
51
|
+
buffer << chunk.b
|
|
52
|
+
if buffer.bytesize > max_size
|
|
53
|
+
raise PayloadTooLargeError, "Request body exceeds max_envelope_size"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
buffer
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def gzip_encoded?
|
|
60
|
+
encoding = request.headers["Content-Encoding"].to_s.downcase
|
|
61
|
+
encoding.include?("gzip")
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def inflate_bounded_gzip(compressed_bytes, max_size)
|
|
65
|
+
if compressed_bytes.nil? || compressed_bytes.empty?
|
|
66
|
+
raise EnvelopeParseError, "Empty gzip envelope"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
reader = Zlib::GzipReader.new(StringIO.new(compressed_bytes))
|
|
70
|
+
inflated = "".b
|
|
71
|
+
begin
|
|
72
|
+
until reader.eof?
|
|
73
|
+
chunk = reader.read(GZIP_CHUNK_BYTES)
|
|
74
|
+
break if chunk.nil?
|
|
75
|
+
|
|
76
|
+
inflated << chunk.b
|
|
77
|
+
if inflated.bytesize > max_size
|
|
78
|
+
raise PayloadTooLargeError, "Decompressed envelope exceeds max_envelope_size"
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
ensure
|
|
82
|
+
reader.close unless reader.closed?
|
|
83
|
+
end
|
|
84
|
+
inflated
|
|
85
|
+
rescue Zlib::Error
|
|
86
|
+
raise EnvelopeParseError, "Malformed gzip stream"
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def authenticate_request(raw_bytes, max_size:)
|
|
90
|
+
project_id = Integer(params[:project_id].to_s, 10)
|
|
91
|
+
return nil if project_id <= 0
|
|
92
|
+
|
|
93
|
+
pub_key, secret_key = extract_credentials(raw_bytes, project_id: project_id, max_size: max_size)
|
|
94
|
+
return nil if pub_key.blank?
|
|
95
|
+
|
|
96
|
+
ProjectKey.authenticate_public_key(
|
|
97
|
+
project_id: project_id,
|
|
98
|
+
public_key: pub_key,
|
|
99
|
+
secret: secret_key
|
|
100
|
+
)
|
|
101
|
+
rescue ArgumentError, TypeError
|
|
102
|
+
nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def extract_credentials(raw_bytes, project_id:, max_size:)
|
|
106
|
+
auth_header = request.headers["X-Sentry-Auth"].presence || sentry_authorization_header
|
|
107
|
+
if auth_header
|
|
108
|
+
parsed = parse_sentry_auth_header(auth_header)
|
|
109
|
+
return [ parsed["sentry_key"], parsed["sentry_secret"] ] if parsed["sentry_key"].present?
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
if request.query_parameters["sentry_key"].present?
|
|
113
|
+
return [
|
|
114
|
+
request.query_parameters["sentry_key"].to_s,
|
|
115
|
+
request.query_parameters["sentry_secret"]&.to_s
|
|
116
|
+
]
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
dsn_string = extract_dsn_from_envelope_prefix(raw_bytes, max_size: max_size)
|
|
120
|
+
return [ nil, nil ] if dsn_string.blank?
|
|
121
|
+
|
|
122
|
+
parsed_dsn = Dsn.parse(dsn_string)
|
|
123
|
+
return [ nil, nil ] unless parsed_dsn.project_id.to_i == project_id
|
|
124
|
+
|
|
125
|
+
[ parsed_dsn.public_key, parsed_dsn.secret_key ]
|
|
126
|
+
rescue InvalidDsnError
|
|
127
|
+
[ nil, nil ]
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def sentry_authorization_header
|
|
131
|
+
raw = request.headers["Authorization"].to_s
|
|
132
|
+
raw if raw.match?(/\ASentry\s+/i)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def parse_sentry_auth_header(raw_header)
|
|
136
|
+
stripped = raw_header.to_s.sub(/\ASentry\s+/i, "")
|
|
137
|
+
stripped.split(",").each_with_object({}) do |pair, acc|
|
|
138
|
+
key, val = pair.split("=", 2)
|
|
139
|
+
next if key.nil? || val.nil?
|
|
140
|
+
|
|
141
|
+
acc[key.strip] = val.strip
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def extract_dsn_from_envelope_prefix(raw_bytes, max_size:)
|
|
146
|
+
peek_limit = [ HEADER_PEEK_BYTES, max_size ].min
|
|
147
|
+
prefix =
|
|
148
|
+
if gzip_encoded?
|
|
149
|
+
peek_gzip_prefix(raw_bytes, peek_limit)
|
|
150
|
+
else
|
|
151
|
+
raw_bytes.byteslice(0, peek_limit)
|
|
152
|
+
end
|
|
153
|
+
Ingestion::EnvelopeParser.extract_header_dsn(prefix)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def peek_gzip_prefix(compressed_bytes, limit)
|
|
157
|
+
return nil if compressed_bytes.nil? || compressed_bytes.empty?
|
|
158
|
+
|
|
159
|
+
reader = Zlib::GzipReader.new(StringIO.new(compressed_bytes))
|
|
160
|
+
begin
|
|
161
|
+
reader.read(limit)
|
|
162
|
+
ensure
|
|
163
|
+
reader.close unless reader.closed?
|
|
164
|
+
end
|
|
165
|
+
rescue Zlib::Error
|
|
166
|
+
nil
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Sentrace
|
|
2
|
+
module Internal
|
|
3
|
+
class BaseController < ApplicationController
|
|
4
|
+
before_action :authenticate_internal_request!
|
|
5
|
+
|
|
6
|
+
rescue_from ActiveRecord::RecordNotFound do |err|
|
|
7
|
+
render json: { error: err.message }, status: :not_found
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
rescue_from ActiveRecord::RecordInvalid do |err|
|
|
11
|
+
render json: { error: err.record.errors.full_messages.join(", ") }, status: :unprocessable_content
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
rescue_from McpNotFoundError do |err|
|
|
15
|
+
render json: { error: err.message }, status: :not_found
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
rescue_from McpInvalidParamsError do |err|
|
|
19
|
+
render json: { error: err.message }, status: :unprocessable_content
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
def authenticate_internal_request!
|
|
24
|
+
custom_auth = Sentrace.configuration.internal_authenticator
|
|
25
|
+
if custom_auth
|
|
26
|
+
return if custom_auth.call(request)
|
|
27
|
+
|
|
28
|
+
render json: { error: "Unauthorized" }, status: :unauthorized
|
|
29
|
+
return
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
auth_header = request.headers["Authorization"].to_s
|
|
33
|
+
bearer = auth_header[/\ABearer\s+(\S+)\z/i, 1]
|
|
34
|
+
return if McpToken.authenticate(bearer)
|
|
35
|
+
|
|
36
|
+
response.set_header("WWW-Authenticate", 'Bearer realm="sentrace-internal"')
|
|
37
|
+
render json: { error: "Unauthorized" }, status: :unauthorized
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def local_backend
|
|
41
|
+
@local_backend ||= Mcp::LocalBackend.new(
|
|
42
|
+
environment_name: Sentrace.configuration.deployment_environment.to_s
|
|
43
|
+
)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module Sentrace
|
|
2
|
+
module Internal
|
|
3
|
+
class EventsController < BaseController
|
|
4
|
+
def index
|
|
5
|
+
payload = local_backend.list_events(
|
|
6
|
+
project_id: params[:project_id],
|
|
7
|
+
issue_id: params[:issue_id],
|
|
8
|
+
since: params[:since],
|
|
9
|
+
until: params[:until],
|
|
10
|
+
limit: params[:limit],
|
|
11
|
+
cursor: params[:cursor]
|
|
12
|
+
)
|
|
13
|
+
render json: payload
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def show
|
|
17
|
+
payload = local_backend.get_event(
|
|
18
|
+
event_id: params[:id],
|
|
19
|
+
project_id: params[:project_id]
|
|
20
|
+
)
|
|
21
|
+
render json: payload
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module Sentrace
|
|
2
|
+
module Internal
|
|
3
|
+
class IssuesController < BaseController
|
|
4
|
+
def index
|
|
5
|
+
payload = local_backend.list_issues(
|
|
6
|
+
project_id: params[:project_id],
|
|
7
|
+
status: params[:status],
|
|
8
|
+
level: params[:level],
|
|
9
|
+
release: params[:release],
|
|
10
|
+
since: params[:since],
|
|
11
|
+
until: params[:until],
|
|
12
|
+
limit: params[:limit],
|
|
13
|
+
cursor: params[:cursor]
|
|
14
|
+
)
|
|
15
|
+
render json: payload
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def show
|
|
19
|
+
payload = local_backend.get_issue(
|
|
20
|
+
issue_id: params[:id],
|
|
21
|
+
project_id: params[:project_id]
|
|
22
|
+
)
|
|
23
|
+
render json: payload
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def update
|
|
27
|
+
payload = local_backend.update_issue_status(
|
|
28
|
+
issue_id: params[:id],
|
|
29
|
+
status: params[:status],
|
|
30
|
+
project_id: params[:project_id]
|
|
31
|
+
)
|
|
32
|
+
render json: payload
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
module Sentrace
|
|
2
|
+
module Internal
|
|
3
|
+
class ProjectsController < BaseController
|
|
4
|
+
def index
|
|
5
|
+
projects = Project.includes(:project_keys).order(:id).map { |project| serialize_project(project) }
|
|
6
|
+
render json: { projects: projects }
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def show
|
|
10
|
+
project = Project.includes(:project_keys).find(params[:id])
|
|
11
|
+
render json: { project: serialize_project(project) }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def create
|
|
15
|
+
slug = params[:slug].to_s.strip
|
|
16
|
+
if slug.empty?
|
|
17
|
+
return render json: { error: "slug is required" }, status: :unprocessable_content
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
project, key = Project.provision!(slug: slug, name: params[:name])
|
|
21
|
+
render json: { project: serialize_project(project, key_override: key) }, status: :created
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def update
|
|
25
|
+
project = Project.find(params[:id])
|
|
26
|
+
attrs = {}
|
|
27
|
+
attrs[:name] = params[:name] if params.key?(:name)
|
|
28
|
+
attrs[:slug] = params[:slug] if params.key?(:slug)
|
|
29
|
+
project.update!(attrs) if attrs.any?
|
|
30
|
+
|
|
31
|
+
render json: { project: serialize_project(project) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def destroy
|
|
35
|
+
project = Project.find(params[:id])
|
|
36
|
+
project.destroy!
|
|
37
|
+
head :no_content
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
def serialize_project(project, key_override: nil)
|
|
42
|
+
active_key = key_override || project.active_key
|
|
43
|
+
base_url = "#{request.base_url}#{Sentrace::Engine.routes.find_script_name({})}"
|
|
44
|
+
dsn = active_key ? Dsn.build(base_url: base_url, public_key: active_key.public_key, project_id: project.id).to_s : nil
|
|
45
|
+
|
|
46
|
+
{
|
|
47
|
+
id: project.id,
|
|
48
|
+
slug: project.slug,
|
|
49
|
+
name: project.name,
|
|
50
|
+
public_key: active_key&.public_key,
|
|
51
|
+
dsn: dsn,
|
|
52
|
+
created_at: project.created_at.utc.iso8601,
|
|
53
|
+
updated_at: project.updated_at.utc.iso8601
|
|
54
|
+
}
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
require "mcp"
|
|
2
|
+
require "rack"
|
|
3
|
+
|
|
4
|
+
module Sentrace
|
|
5
|
+
class McpController < ApplicationController
|
|
6
|
+
MAX_MCP_REQUEST_BYTES = 1.megabyte
|
|
7
|
+
|
|
8
|
+
def handle
|
|
9
|
+
unless https_or_loopback?
|
|
10
|
+
return render json: { error: "HTTPS is required for non-local MCP endpoints" }, status: :forbidden
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
token_record = authenticate_bearer_token
|
|
14
|
+
unless token_record
|
|
15
|
+
response.set_header("WWW-Authenticate", 'Bearer realm="sentrace-mcp"')
|
|
16
|
+
return render json: { error: "Unauthorized" }, status: :unauthorized
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
mcp_server = Mcp::Server.build(dispatcher: build_node_dispatcher(Sentrace.configuration))
|
|
20
|
+
transport = ::MCP::Server::Transports::StreamableHTTPTransport.new(
|
|
21
|
+
mcp_server,
|
|
22
|
+
stateless: true,
|
|
23
|
+
enable_json_response: true,
|
|
24
|
+
dns_rebinding_protection: false,
|
|
25
|
+
serve_subscriptions_listen: false,
|
|
26
|
+
max_request_bytes: MAX_MCP_REQUEST_BYTES
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
rack_status, rack_headers, rack_body = transport.handle_request(Rack::Request.new(request.env))
|
|
30
|
+
write_rack_response(rack_status, rack_headers, rack_body)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
def https_or_loopback?
|
|
35
|
+
return true if request.ssl?
|
|
36
|
+
|
|
37
|
+
Configuration::LOOPBACK_HOSTS.include?(request.host.to_s.downcase)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def authenticate_bearer_token
|
|
41
|
+
auth_header = request.headers["Authorization"].to_s
|
|
42
|
+
match = auth_header.match(/\ABearer\s+(\S+)\z/i)
|
|
43
|
+
return nil unless match
|
|
44
|
+
|
|
45
|
+
McpToken.authenticate(match[1])
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def build_node_dispatcher(config)
|
|
49
|
+
env_label = config.deployment_environment.to_s.strip
|
|
50
|
+
env_label = "local" if env_label.empty?
|
|
51
|
+
|
|
52
|
+
node_config = config.dup
|
|
53
|
+
node_config.environments = { env_label.to_sym => { transport: :local } }
|
|
54
|
+
Mcp::Dispatcher.new(config: node_config, remote_client: nil)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def write_rack_response(rack_status, rack_headers, rack_body)
|
|
58
|
+
rack_headers.each do |key, value|
|
|
59
|
+
next if key.to_s.casecmp("content-length").zero?
|
|
60
|
+
|
|
61
|
+
response.set_header(key, value)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
chunks = []
|
|
65
|
+
rack_body.each { |chunk| chunks << chunk.to_s }
|
|
66
|
+
rack_body.close if rack_body.respond_to?(:close)
|
|
67
|
+
|
|
68
|
+
content_type = rack_headers["content-type"] || rack_headers["Content-Type"]
|
|
69
|
+
self.content_type = content_type if content_type
|
|
70
|
+
self.status = rack_status
|
|
71
|
+
self.response_body = chunks.join
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|