-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
377 lines (331 loc) · 11.3 KB
/
main.rs
File metadata and controls
377 lines (331 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
mod api;
mod auth;
mod auth_service;
mod background;
mod cli;
mod config;
mod crypto;
mod db;
mod encoding;
mod endpoints;
mod enrich;
mod envelope;
mod event_data;
mod extractors;
mod filter;
mod fingerprint;
mod forge;
mod html;
mod middleware;
mod models;
mod network;
mod notify;
mod providers;
mod queries;
mod server;
mod sourcemap;
mod ssrf;
mod stats;
mod sync;
mod writer;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
#[derive(Parser)]
#[command(name = "stackpit", about = "Drop-in Sentry host replacement")]
struct Cli {
/// Config file path
#[arg(short, long, default_value = "stackpit.toml")]
config: PathBuf,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Fire up the HTTP server
Serve {
/// Run ingest-only -- no admin UI or API
#[arg(long)]
ingest_only: bool,
},
/// Show known projects
Projects,
/// Show recent events
Events {
/// Only show events for this project
#[arg(short, long)]
project: Option<u64>,
/// How many to show
#[arg(short, long, default_value = "20")]
limit: u32,
},
/// Dump a single event as decompressed JSON
Event { id: String },
/// Follow new events as they come in
Tail,
/// Write a default config file
Init,
/// Show environment and configuration overview
Status,
/// Retroactively generate fingerprints and issues for old events
BackfillIssues,
/// Pull events from a remote Sentry instance
Sync {
/// Sentry org slug
#[arg(long)]
org: String,
/// API base URL
#[arg(long, default_value = "https://sentry.io")]
url: String,
/// Limit to these projects (comma-separated slugs)
#[arg(long, value_delimiter = ',')]
projects: Option<Vec<String>>,
/// Cap pages per project -- handy for testing
#[arg(long)]
max_pages: Option<u32>,
},
}
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
if let Command::Init = cli.command {
return cli::init::run(&cli.config);
}
let config = config::Config::load(&cli.config)?;
config.validate()?;
let database_url = config.storage.database_url();
match cli.command {
Command::Serve { ingest_only } => {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "stackpit=info,tower_http=info".into()),
)
.init();
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?
.block_on(server::run(config, ingest_only))?;
}
Command::Projects => {
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
rt.block_on(async {
let pool = db::create_pool(&database_url).await?;
cli::projects::run(&pool).await
})?;
}
Command::Events { project, limit } => {
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
rt.block_on(async {
let pool = db::create_pool(&database_url).await?;
cli::events::run(&pool, project, limit).await
})?;
}
Command::Event { id } => {
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
rt.block_on(async {
let pool = db::create_pool(&database_url).await?;
cli::event::run(&pool, &id).await
})?;
}
Command::Tail => {
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
rt.block_on(async {
let pool = db::create_pool(&database_url).await?;
cli::tail::run(&pool).await
})?;
}
Command::Status => {
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
rt.block_on(cli::status::run(&config))?;
}
Command::BackfillIssues => {
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
rt.block_on(async {
let pool = db::create_writer_pool(&database_url).await?;
db::run_migrations(&pool).await?;
cli::backfill::run(&pool).await
})?;
}
Command::Sync {
org,
url,
projects,
max_pages,
} => {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "stackpit=info".into()),
)
.init();
sync::run(
&database_url,
sync::SyncArgs {
org,
url,
projects,
max_pages,
},
)?;
}
Command::Init => unreachable!(),
}
Ok(())
}
// ---------------------------------------------------------------------------
// Integration tests: event ingestion → writer → query → response
// ---------------------------------------------------------------------------
#[cfg(test)]
mod integration_tests {
use crate::db;
use crate::models::{ItemType, Level, StorableEvent};
use crate::writer::{self, WriteMsg};
fn make_event(event_id: &str, project_id: u64, fingerprint: &str) -> StorableEvent {
let payload = serde_json::json!({
"event_id": event_id,
"message": format!("test error {event_id}"),
"level": "error",
"timestamp": 1700000000.0,
});
let raw = serde_json::to_vec(&payload).unwrap();
StorableEvent {
event_id: event_id.to_string(),
item_type: ItemType::Event,
payload: raw,
project_id,
public_key: "test-key".to_string(),
timestamp: 1700000000,
level: Some(Level::Error),
platform: Some("python".to_string()),
release: Some("1.0.0".to_string()),
environment: Some("production".to_string()),
server_name: None,
transaction_name: None,
title: Some(format!("test error {event_id}")),
sdk_name: None,
sdk_version: None,
fingerprint: Some(fingerprint.to_string()),
monitor_slug: None,
session_status: None,
parent_event_id: None,
user_identifier: Some("user-1".to_string()),
tags: vec![("browser".to_string(), "Chrome".to_string())],
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn ingest_event_then_query_back() {
let pool = db::open_test_pool().await;
// Need a project row in the DB for queries to work
sqlx::query("INSERT INTO projects (project_id, name) VALUES (1, 'test-project')")
.execute(&pool)
.await
.unwrap();
sqlx::query("INSERT INTO project_keys (public_key, project_id, status) VALUES ('test-key', 1, 'active')")
.execute(&pool).await.unwrap();
// Spin up the writer
let (writer, _join) = writer::spawn(
pool.clone(),
None,
None,
std::sync::Arc::new(crate::stats::IngestStats::new()),
)
.await
.unwrap();
let tx = writer.raw_sender();
// Push events through the raw channel
let event1 = make_event("evt-001", 1, "fp-001");
let event2 = make_event("evt-002", 1, "fp-001"); // same fingerprint, should land in same issue
tx.try_send(WriteMsg::Event(event1)).unwrap();
tx.try_send(WriteMsg::Event(event2)).unwrap();
// Shut down cleanly so everything gets flushed
let _ = writer.shutdown();
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
// Event should be queryable
let detail = crate::queries::events::get_event_detail(&pool, "evt-001")
.await
.unwrap();
assert!(
detail.is_some(),
"event should be queryable after ingestion"
);
let detail = detail.unwrap();
assert_eq!(detail.event_id, "evt-001");
// Issue should've been created from the fingerprint
let issue = crate::queries::issues::get_issue(&pool, "fp-001")
.await
.unwrap();
assert!(issue.is_some(), "issue should exist for the fingerprint");
let issue = issue.unwrap();
assert_eq!(issue.fingerprint, "fp-001");
assert!(
issue.event_count >= 2,
"issue should have at least 2 events"
);
// Both events should show up in the project listing
let page = crate::queries::types::Page::new(None, None);
let events = crate::queries::events::list_events(&pool, 1, &page)
.await
.unwrap();
assert!(
events.items.len() >= 2,
"project should have at least 2 events"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn ingest_and_update_issue_status() {
let pool = db::open_test_pool().await;
sqlx::query("INSERT INTO projects (project_id, name) VALUES (1, 'test-project')")
.execute(&pool)
.await
.unwrap();
sqlx::query("INSERT INTO project_keys (public_key, project_id, status) VALUES ('test-key', 1, 'active')")
.execute(&pool).await.unwrap();
let (writer, _join) = writer::spawn(
pool.clone(),
None,
None,
std::sync::Arc::new(crate::stats::IngestStats::new()),
)
.await
.unwrap();
writer
.send_event(make_event("evt-status-1", 1, "fp-status-001"))
.unwrap();
// Wait past the 1s flush interval, then send another event to
// trigger the aggregation flush that creates the issue row.
tokio::time::sleep(std::time::Duration::from_millis(1200)).await;
writer
.send_event(make_event("evt-status-2", 1, "fp-status-002"))
.unwrap();
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
// Issue should exist now -- try updating its status
let reply_rx = writer
.update_issue_status(
"fp-status-001".to_string(),
crate::queries::IssueStatus::Resolved,
)
.unwrap();
let result = reply_rx.await;
assert!(result.is_ok(), "should get a reply from writer");
let _ = writer.shutdown();
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
// Confirm the status change stuck
let issue = crate::queries::issues::get_issue(&pool, "fp-status-001")
.await
.unwrap();
assert!(issue.is_some());
let issue = issue.unwrap();
assert_eq!(issue.status, crate::queries::IssueStatus::Resolved);
}
}