use std::{
collections::{BTreeMap, VecDeque},
convert::Infallible,
error::Error,
fmt, io,
net::{IpAddr, Ipv4Addr, SocketAddr},
path::{Path, PathBuf},
sync::{
Arc, Mutex, Weak,
atomic::{AtomicBool, AtomicU64, Ordering},
},
time::Duration,
};
use axum::{
Json, Router,
body::{Body, Bytes},
extract::{Query, Request, State},
http::{HeaderMap, HeaderValue, StatusCode, header::HOST},
middleware::{self, Next},
response::{
IntoResponse, Response,
sse::{Event as SseEvent, KeepAlive, Sse},
},
routing::get,
};
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
use dbgflow_protocol::{
CURRENT_PROJECTION_VERSION, CURRENT_SCHEMA_VERSION, EventEnvelope, EventKind, Payload,
PayloadEncoding, ProjectedSpan, ProjectionCheckpoint, ProjectionLimits, Sequence,
};
use dbgflow_storage::{
CURRENT_FORMAT_VERSION, DurableMetadata, DurableTailReader, Manifest, RecordingActivity,
RecordingReader, RecordingState, RetainedPrefix, StorageError, recording_activity,
};
use serde::{Deserialize, Serialize};
use tokio::{
net::TcpListener,
sync::{mpsc, watch},
task::JoinHandle,
};
use tokio_stream::wrappers::ReceiverStream;
pub const API_VERSION: u32 = 1;
pub const HARD_MAX_PAGE_LIMIT: usize = 100_000;
pub const HARD_MAX_PAGE_BYTES: usize = 64 * 1024 * 1024;
const MIN_PAGE_BYTES: usize = 1024;
const EVENT_RESPONSE_OVERHEAD_BYTES: usize = 512;
const LAST_EVENT_ID: &str = "last-event-id";
const LAST_DURABLE_CURSOR: &str = "x-dbgflow-last-durable-cursor";
const MAX_LIVE_CATCHUP_EVENTS: usize = 64;
const MAX_LIVE_CATCHUP_BYTES: usize = 1024 * 1024;
#[derive(Clone, Debug)]
pub struct ServerConfig {
pub bind_addr: SocketAddr,
pub ui_dir: Option<PathBuf>,
pub default_page_limit: usize,
pub max_page_limit: usize,
pub max_page_bytes: usize,
pub live_batch_limit: usize,
pub live_buffer_events: usize,
pub live_buffer_bytes: usize,
pub live_poll_interval: Duration,
pub keep_alive_interval: Duration,
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
bind_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0),
ui_dir: None,
default_page_limit: 500,
max_page_limit: 5_000,
max_page_bytes: 8 * 1024 * 1024,
live_batch_limit: 500,
live_buffer_events: 128,
live_buffer_bytes: 8 * 1024 * 1024,
live_poll_interval: Duration::from_millis(200),
keep_alive_interval: Duration::from_secs(15),
}
}
}
impl ServerConfig {
fn validate(&self) -> Result<(), ServerError> {
if !self.bind_addr.ip().is_loopback() {
return Err(ServerError::InvalidConfig(
"bind address must be loopback".into(),
));
}
if self.default_page_limit == 0 || self.default_page_limit > self.max_page_limit {
return Err(ServerError::InvalidConfig(
"default_page_limit must be in 1..=max_page_limit".into(),
));
}
if self.max_page_limit == 0
|| self.max_page_limit > HARD_MAX_PAGE_LIMIT
|| self.live_batch_limit == 0
|| self.live_batch_limit > self.max_page_limit
|| self.live_buffer_events == 0
{
return Err(ServerError::InvalidConfig(format!(
"page limits must be non-zero and no greater than {HARD_MAX_PAGE_LIMIT}; live limits must be non-zero and bounded"
)));
}
if !(MIN_PAGE_BYTES..=HARD_MAX_PAGE_BYTES).contains(&self.max_page_bytes) {
return Err(ServerError::InvalidConfig(format!(
"max_page_bytes must be in {MIN_PAGE_BYTES}..={HARD_MAX_PAGE_BYTES}"
)));
}
if !(MIN_PAGE_BYTES..=HARD_MAX_PAGE_BYTES).contains(&self.live_buffer_bytes) {
return Err(ServerError::InvalidConfig(format!(
"live_buffer_bytes must be in {MIN_PAGE_BYTES}..={HARD_MAX_PAGE_BYTES}"
)));
}
if self.live_poll_interval.is_zero() || self.keep_alive_interval.is_zero() {
return Err(ServerError::InvalidConfig(
"poll and keep-alive intervals must be non-zero".into(),
));
}
Ok(())
}
}
#[derive(Debug)]
pub enum ServerError {
InvalidConfig(String),
UiAsset { path: PathBuf, source: io::Error },
Io(io::Error),
Storage(StorageError),
Task(tokio::task::JoinError),
}
impl fmt::Display for ServerError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidConfig(message) => {
write!(formatter, "invalid server configuration: {message}")
}
Self::UiAsset { path, source } => {
write!(
formatter,
"cannot load required UI asset {}: {source}",
path.display()
)
}
Self::Io(error) => error.fmt(formatter),
Self::Storage(error) => error.fmt(formatter),
Self::Task(error) => write!(formatter, "server task failed: {error}"),
}
}
}
impl Error for ServerError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Io(error) => Some(error),
Self::UiAsset { source, .. } => Some(source),
Self::Storage(error) => Some(error),
Self::Task(error) => Some(error),
Self::InvalidConfig(_) => None,
}
}
}
impl From<io::Error> for ServerError {
fn from(error: io::Error) -> Self {
Self::Io(error)
}
}
impl From<StorageError> for ServerError {
fn from(error: StorageError) -> Self {
Self::Storage(error)
}
}
pub struct Server {
listener: TcpListener,
app: Router,
shutdown: watch::Sender<bool>,
local_addr: SocketAddr,
live_diagnostics: LiveDiagnostics,
}
impl Server {
pub async fn bind(
config: ServerConfig,
recording_path: impl AsRef<Path>,
) -> Result<Self, ServerError> {
config.validate()?;
RecordingReader::open(recording_path.as_ref())?;
let ui = UiAssets::for_config(&config)?;
let listener = TcpListener::bind(config.bind_addr).await?;
let local_addr = listener.local_addr()?;
let (app, shutdown, live_diagnostics) = build_router(
recording_path.as_ref().to_path_buf(),
config,
local_addr.port(),
ui,
);
Ok(Self {
listener,
app,
shutdown,
local_addr,
live_diagnostics,
})
}
pub fn local_addr(&self) -> SocketAddr {
self.local_addr
}
pub fn live_diagnostics(&self) -> LiveDiagnostics {
self.live_diagnostics.clone()
}
pub async fn serve(self) -> Result<(), ServerError> {
let mut shutdown = self.shutdown.subscribe();
axum::serve(self.listener, self.app)
.with_graceful_shutdown(async move {
while !*shutdown.borrow() {
if shutdown.changed().await.is_err() {
break;
}
}
})
.await?;
Ok(())
}
pub fn spawn(self) -> ServerHandle {
let local_addr = self.local_addr;
let shutdown = self.shutdown.clone();
let task = tokio::spawn(self.serve());
ServerHandle {
local_addr,
shutdown: Some(shutdown),
task: Some(task),
}
}
pub fn router(
config: ServerConfig,
recording_path: impl AsRef<Path>,
) -> Result<Router, ServerError> {
config.validate()?;
RecordingReader::open(recording_path.as_ref())?;
let ui = UiAssets::for_config(&config)?;
let allowed_port = config.bind_addr.port();
Ok(build_router(
recording_path.as_ref().to_path_buf(),
config,
allowed_port,
ui,
)
.0)
}
pub fn router_with_diagnostics(
config: ServerConfig,
recording_path: impl AsRef<Path>,
) -> Result<(Router, LiveDiagnostics), ServerError> {
config.validate()?;
RecordingReader::open(recording_path.as_ref())?;
let ui = UiAssets::for_config(&config)?;
let allowed_port = config.bind_addr.port();
let (router, _shutdown, diagnostics) = build_router(
recording_path.as_ref().to_path_buf(),
config,
allowed_port,
ui,
);
Ok((router, diagnostics))
}
}
pub struct ServerHandle {
local_addr: SocketAddr,
shutdown: Option<watch::Sender<bool>>,
task: Option<JoinHandle<Result<(), ServerError>>>,
}
impl ServerHandle {
pub fn local_addr(&self) -> SocketAddr {
self.local_addr
}
pub async fn shutdown(mut self) -> Result<(), ServerError> {
if let Some(shutdown) = self.shutdown.take() {
shutdown.send_replace(true);
}
self.task
.take()
.expect("a live server handle owns its task")
.await
.map_err(ServerError::Task)?
}
}
impl Drop for ServerHandle {
fn drop(&mut self) {
if let Some(shutdown) = self.shutdown.take() {
shutdown.send_replace(true);
}
if let Some(task) = self.task.take() {
task.abort();
}
}
}
#[derive(Clone)]
pub struct LiveDiagnostics {
metrics: Arc<LiveMetrics>,
}
impl LiveDiagnostics {
pub fn tailer_starts(&self) -> u64 {
self.metrics.starts.load(Ordering::Acquire)
}
pub fn storage_polls(&self) -> u64 {
self.metrics.polls.load(Ordering::Acquire)
}
pub fn tailer_active(&self) -> bool {
self.metrics.active.load(Ordering::Acquire)
}
pub fn subscribers(&self) -> u64 {
self.metrics.subscribers.load(Ordering::Acquire)
}
pub fn completed_catchups(&self) -> u64 {
self.metrics.catchups.load(Ordering::Acquire)
}
pub fn catchup_storage_reads(&self) -> u64 {
self.metrics.catchup_reads.load(Ordering::Acquire)
}
pub fn published_events(&self) -> u64 {
self.metrics.published.load(Ordering::Acquire)
}
pub fn tail_directory_scans(&self) -> u64 {
self.metrics.tail_directory_scans.load(Ordering::Acquire)
}
pub fn tail_reinitializations(&self) -> u64 {
self.metrics.tail_reinitializations.load(Ordering::Acquire)
}
pub fn tail_frames_decoded(&self) -> u64 {
self.metrics.tail_frames_decoded.load(Ordering::Acquire)
}
}
#[derive(Default)]
struct LiveMetrics {
starts: AtomicU64,
polls: AtomicU64,
active: AtomicBool,
subscribers: AtomicU64,
catchups: AtomicU64,
catchup_reads: AtomicU64,
published: AtomicU64,
tail_directory_scans: AtomicU64,
tail_reinitializations: AtomicU64,
tail_frames_decoded: AtomicU64,
}
struct LiveHub {
path: Arc<PathBuf>,
config: ServerConfig,
shutdown: watch::Sender<bool>,
notifications: watch::Sender<u64>,
state: Mutex<LiveFanoutState>,
started: AtomicBool,
metrics: Arc<LiveMetrics>,
}
#[derive(Default)]
struct LiveFanoutState {
events: VecDeque<Arc<EventEnvelope>>,
event_bytes: usize,
evicted_through: Option<u64>,
initialized: bool,
tail_cursor: Option<u64>,
complete: bool,
terminal_error: Option<String>,
}
struct LiveSubscription {
notifications: watch::Receiver<u64>,
metrics: Arc<LiveMetrics>,
baseline: LiveBaseline,
}
impl Drop for LiveSubscription {
fn drop(&mut self) {
self.metrics.subscribers.fetch_sub(1, Ordering::Release);
}
}
enum LiveRead {
Event(Arc<EventEnvelope>),
Pending,
Complete,
TerminalError(String),
Lagged,
}
#[derive(Clone, Copy)]
enum LiveBaseline {
Pending,
Ready(Option<u64>),
}
impl LiveHub {
fn new(path: Arc<PathBuf>, config: ServerConfig, shutdown: watch::Sender<bool>) -> Arc<Self> {
let (notifications, _) = watch::channel(0);
Arc::new(Self {
path,
config,
shutdown,
notifications,
state: Mutex::new(LiveFanoutState::default()),
started: AtomicBool::new(false),
metrics: Arc::new(LiveMetrics::default()),
})
}
fn subscribe(self: &Arc<Self>) -> LiveSubscription {
let notifications = self.notifications.subscribe();
let baseline = {
let state = self
.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if state.initialized {
LiveBaseline::Ready(state.tail_cursor)
} else {
LiveBaseline::Pending
}
};
self.metrics.subscribers.fetch_add(1, Ordering::AcqRel);
self.ensure_started();
LiveSubscription {
notifications,
metrics: Arc::clone(&self.metrics),
baseline,
}
}
fn ensure_started(self: &Arc<Self>) {
if self
.started
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
.is_err()
{
return;
}
self.metrics.starts.fetch_add(1, Ordering::Relaxed);
self.metrics.active.store(true, Ordering::Release);
let weak = Arc::downgrade(self);
let path = Arc::clone(&self.path);
let config = self.config.clone();
let shutdown = self.shutdown.subscribe();
let metrics = Arc::clone(&self.metrics);
tokio::spawn(shared_tailer(weak, path, config, shutdown, metrics));
}
fn publish(&self, events: Vec<EventEnvelope>) -> Result<(), String> {
if events.is_empty() {
return Ok(());
}
let event_count = events.len() as u64;
if let Some(event) = events
.iter()
.find(|event| live_event_weight(event) > self.config.live_buffer_bytes)
{
let weight = live_event_weight(event);
return Err(format!(
"live event {} requires {weight} bytes, exceeding live_buffer_bytes {}",
event.sequence.get(),
self.config.live_buffer_bytes
));
}
let mut state = self
.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
for event in events {
let weight = live_event_weight(&event);
while state.events.len() >= self.config.live_buffer_events
|| state.event_bytes.saturating_add(weight) > self.config.live_buffer_bytes
{
let Some(evicted) = state.events.pop_front() else {
break;
};
state.event_bytes = state
.event_bytes
.saturating_sub(live_event_weight(&evicted));
state.evicted_through = Some(evicted.sequence.get());
}
state.event_bytes = state.event_bytes.saturating_add(weight);
state.tail_cursor = Some(event.sequence.get());
state.events.push_back(Arc::new(event));
}
drop(state);
self.metrics
.published
.fetch_add(event_count, Ordering::Release);
self.notify();
Ok(())
}
fn initialize(&self, baseline_cursor: Option<u64>, complete: bool) {
let mut state = self
.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
state.tail_cursor = baseline_cursor;
state.initialized = true;
state.complete = complete;
drop(state);
self.notify();
}
fn baseline(&self) -> LiveBaseline {
let state = self
.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if state.initialized {
LiveBaseline::Ready(state.tail_cursor)
} else {
LiveBaseline::Pending
}
}
fn mark_complete(&self) {
self.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.complete = true;
self.notify();
}
fn fail(&self, message: String) {
let mut state = self
.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if state.terminal_error.is_none() {
state.terminal_error = Some(message);
}
if !state.initialized {
state.initialized = true;
state.tail_cursor = None;
}
drop(state);
self.notify();
}
fn read_after(&self, cursor: Option<u64>) -> LiveRead {
let state = self
.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if state
.evicted_through
.is_some_and(|evicted| cursor.is_none_or(|cursor| cursor < evicted))
{
return LiveRead::Lagged;
}
let event = state
.events
.iter()
.find(|event| cursor.is_none_or(|cursor| event.sequence.get() > cursor))
.cloned();
if let Some(event) = event {
LiveRead::Event(event)
} else if let Some(message) = &state.terminal_error {
LiveRead::TerminalError(message.clone())
} else if state.complete {
LiveRead::Complete
} else {
LiveRead::Pending
}
}
fn notify(&self) {
self.notifications.send_modify(|version| {
*version = version.wrapping_add(1);
});
}
}
struct ActiveTailer(Arc<LiveMetrics>);
impl Drop for ActiveTailer {
fn drop(&mut self) {
self.0.active.store(false, Ordering::Release);
}
}
async fn shared_tailer(
hub: Weak<LiveHub>,
path: Arc<PathBuf>,
config: ServerConfig,
mut shutdown: watch::Receiver<bool>,
metrics: Arc<LiveMetrics>,
) {
let _active = ActiveTailer(Arc::clone(&metrics));
metrics.polls.fetch_add(1, Ordering::Relaxed);
let initialize_path = Arc::clone(&path);
let initialization = blocking(move || {
let reader = RecordingReader::open(initialize_path.as_ref())?;
Ok((
reader.last_durable_sequence()?.map(Sequence::get),
reader.manifest().state == RecordingState::Complete,
))
})
.await;
let (cursor, initially_complete) = match initialization {
Ok(initialization) => initialization,
Err(error) => {
if let Some(hub) = hub.upgrade() {
hub.fail(error.message);
}
return;
}
};
let Some(initial_hub) = hub.upgrade() else {
return;
};
initial_hub.initialize(cursor, initially_complete);
drop(initial_hub);
if initially_complete {
return;
}
let tail_path = Arc::clone(&path);
let mut tail = match blocking(move || {
DurableTailReader::open(tail_path.as_ref(), cursor.map(Sequence::new))
})
.await
{
Ok(tail) => tail,
Err(error) => {
if let Some(hub) = hub.upgrade() {
hub.fail(error.message);
}
return;
}
};
loop {
if *shutdown.borrow() {
break;
}
let Some(current_hub) = hub.upgrade() else {
break;
};
drop(current_hub);
metrics.polls.fetch_add(1, Ordering::Relaxed);
let batch_limit = config.live_batch_limit;
let byte_limit = config.live_buffer_bytes;
let activity_path = Arc::clone(&path);
let result = blocking(move || {
let page = tail.poll_bounded(batch_limit, byte_limit, live_event_weight)?;
let activity = if tail.state() == RecordingState::Complete {
RecordingActivity::Complete
} else {
recording_activity(activity_path.as_ref())?
};
let stats = tail.stats();
Ok((tail, page, activity, stats))
})
.await;
let (returned_tail, page, activity, stats) = match result {
Ok(result) => result,
Err(error) => {
if let Some(hub) = hub.upgrade() {
hub.fail(error.message);
}
break;
}
};
tail = returned_tail;
metrics
.tail_directory_scans
.store(stats.directory_scans, Ordering::Release);
metrics
.tail_reinitializations
.store(stats.reinitializations, Ordering::Release);
metrics
.tail_frames_decoded
.store(stats.frames_decoded, Ordering::Release);
if page.events.is_empty() {
if let Some(weight) = page.boundary_event_weight {
if let Some(hub) = hub.upgrade() {
hub.fail(format!(
"live event requires {weight} bytes, exceeding live_buffer_bytes {byte_limit}"
));
}
break;
}
}
let has_more = page.has_more;
if let Some(hub) = hub.upgrade() {
if let Err(message) = hub.publish(page.events) {
hub.fail(message);
break;
}
if activity != RecordingActivity::Active && !has_more {
hub.mark_complete();
break;
}
} else {
break;
}
if has_more {
continue;
}
tokio::select! {
_ = tokio::time::sleep(config.live_poll_interval) => {},
changed = shutdown.changed() => {
if changed.is_err() || *shutdown.borrow() { break; }
}
}
}
}
fn live_event_weight(event: &EventEnvelope) -> usize {
let retained_memory = std::mem::size_of::<EventEnvelope>()
.saturating_add(2 * std::mem::size_of::<usize>())
.saturating_add(
event
.payload
.as_ref()
.map_or(0, |payload| payload.data.capacity()),
);
retained_memory.max(event_response_size_bound(event))
}
#[derive(Clone)]
struct AppState {
recording_path: Arc<PathBuf>,
config: ServerConfig,
shutdown: watch::Sender<bool>,
live: Arc<LiveHub>,
ui: Option<Arc<UiAssets>>,
}
#[derive(Debug)]
struct UiAssets {
index_html: Bytes,
app_js: Bytes,
app_css: Bytes,
globals_css: Bytes,
}
impl UiAssets {
fn for_config(config: &ServerConfig) -> Result<Option<Arc<Self>>, ServerError> {
if let Some(directory) = &config.ui_dir {
return Self::from_directory(directory).map(Arc::new).map(Some);
}
#[cfg(feature = "ui")]
{
Ok(Some(Arc::new(Self {
index_html: Bytes::from_static(dbgflow_ui::index_html().as_bytes()),
app_js: Bytes::from_static(dbgflow_ui::app_js().as_bytes()),
app_css: Bytes::from_static(dbgflow_ui::app_css().as_bytes()),
globals_css: Bytes::from_static(dbgflow_ui::globals_css().as_bytes()),
})))
}
#[cfg(not(feature = "ui"))]
{
Ok(None)
}
}
fn from_directory(directory: &Path) -> Result<Self, ServerError> {
let read = |name: &str| {
let path = directory.join(name);
let metadata =
std::fs::symlink_metadata(&path).map_err(|source| ServerError::UiAsset {
path: path.clone(),
source,
})?;
if !metadata.file_type().is_file() {
return Err(ServerError::UiAsset {
path,
source: io::Error::new(
io::ErrorKind::InvalidInput,
"expected a regular file (symlinks and directories are rejected)",
),
});
}
std::fs::read(&path)
.map(Bytes::from)
.map_err(|source| ServerError::UiAsset { path, source })
};
Ok(Self {
index_html: read("index.html")?,
app_js: read("app.js")?,
app_css: read("app.css")?,
globals_css: read("globals.css")?,
})
}
}
#[derive(Clone, Copy)]
struct HostPolicy {
port: u16,
}
fn build_router(
path: PathBuf,
config: ServerConfig,
allowed_port: u16,
ui: Option<Arc<UiAssets>>,
) -> (Router, watch::Sender<bool>, LiveDiagnostics) {
let (shutdown, _) = watch::channel(false);
let live_hub = LiveHub::new(Arc::new(path.clone()), config.clone(), shutdown.clone());
let live_diagnostics = LiveDiagnostics {
metrics: Arc::clone(&live_hub.metrics),
};
let state = AppState {
recording_path: Arc::new(path),
config,
shutdown: shutdown.clone(),
live: live_hub,
ui: ui.clone(),
};
let router = Router::new()
.route("/api/v1/capabilities", get(capabilities))
.route("/api/v1/recording", get(recording))
.route("/api/v1/events", get(events))
.route("/api/v1/replay", get(replay))
.route("/api/v1/live", get(live));
let router = if ui.is_some() {
router
.route("/", get(ui_index))
.route("/app.js", get(ui_javascript))
.route("/app.css", get(ui_css))
.route("/globals.css", get(ui_globals_css))
} else {
router
};
let router = router
.with_state(state)
.layer(middleware::from_fn(security_headers))
.layer(middleware::from_fn_with_state(
HostPolicy { port: allowed_port },
enforce_loopback_host,
));
(router, shutdown, live_diagnostics)
}
async fn enforce_loopback_host(
State(policy): State<HostPolicy>,
request: Request,
next: Next,
) -> Response {
let mut values = request.headers().get_all(HOST).iter();
let host = values.next().and_then(|value| value.to_str().ok());
let single_host = host.is_some() && values.next().is_none();
if !single_host || !host.is_some_and(|value| allowed_host(value, policy.port)) {
return secure_response(
(
StatusCode::MISDIRECTED_REQUEST,
Json(ErrorResponse {
error: "Host must name the loopback dbgflow listener".into(),
}),
)
.into_response(),
);
}
next.run(request).await
}
fn allowed_host(value: &str, port: u16) -> bool {
let value = value.to_ascii_lowercase();
value == format!("localhost:{port}")
|| value == format!("127.0.0.1:{port}")
|| value == format!("[::1]:{port}")
}
async fn security_headers(request: Request, next: Next) -> Response {
secure_response(next.run(request).await)
}
fn secure_response(mut response: Response) -> Response {
let headers = response.headers_mut();
headers.insert(
"content-security-policy",
HeaderValue::from_static(
"default-src 'none'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; font-src 'self'; worker-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'",
),
);
headers.insert(
"x-content-type-options",
HeaderValue::from_static("nosniff"),
);
headers.insert("x-frame-options", HeaderValue::from_static("DENY"));
headers.insert("referrer-policy", HeaderValue::from_static("no-referrer"));
headers.insert(
"permissions-policy",
HeaderValue::from_static("camera=(), microphone=(), geolocation=()"),
);
headers.insert(
"cross-origin-opener-policy",
HeaderValue::from_static("same-origin"),
);
headers.insert(
"cross-origin-resource-policy",
HeaderValue::from_static("same-origin"),
);
response
}
async fn ui_index(State(state): State<AppState>) -> Response {
ui_asset_response(
state
.ui
.expect("UI routes require loaded assets")
.index_html
.clone(),
"text/html; charset=utf-8",
)
}
async fn ui_javascript(State(state): State<AppState>) -> Response {
ui_asset_response(
state
.ui
.expect("UI routes require loaded assets")
.app_js
.clone(),
"application/javascript; charset=utf-8",
)
}
async fn ui_css(State(state): State<AppState>) -> Response {
ui_asset_response(
state
.ui
.expect("UI routes require loaded assets")
.app_css
.clone(),
"text/css; charset=utf-8",
)
}
async fn ui_globals_css(State(state): State<AppState>) -> Response {
ui_asset_response(
state
.ui
.expect("UI routes require loaded assets")
.globals_css
.clone(),
"text/css; charset=utf-8",
)
}
fn ui_asset_response(bytes: Bytes, content_type: &'static str) -> Response {
Response::builder()
.header("content-type", content_type)
.body(Body::from(bytes))
.expect("static UI response headers are valid")
}
#[derive(Serialize)]
struct CapabilitiesResponse {
api_version: u32,
schema_version: u32,
format_version: u32,
transports: [&'static str; 1],
cursor: CursorCapabilities,
pagination: PaginationCapabilities,
retention: RetentionCapabilities,
replay: ReplayCapabilities,
}
#[derive(Serialize)]
struct CursorCapabilities {
kind: &'static str,
semantics: &'static str,
durability: &'static str,
}
#[derive(Serialize)]
struct PaginationCapabilities {
default_limit: usize,
max_limit: usize,
max_response_bytes: usize,
directions: [&'static str; 2],
}
#[derive(Serialize)]
struct RetentionCapabilities {
rolling: bool,
notice_event: &'static str,
}
#[derive(Serialize)]
struct ReplayCapabilities {
checkpoint_projection_version: u32,
endpoint: &'static str,
semantics: &'static str,
}
async fn capabilities(State(state): State<AppState>) -> Json<CapabilitiesResponse> {
Json(CapabilitiesResponse {
api_version: API_VERSION,
schema_version: CURRENT_SCHEMA_VERSION,
format_version: CURRENT_FORMAT_VERSION,
transports: ["sse"],
cursor: CursorCapabilities {
kind: "sequence",
semantics: "exclusive",
durability: "synced_frames",
},
pagination: PaginationCapabilities {
default_limit: state.config.default_page_limit,
max_limit: state.config.max_page_limit,
max_response_bytes: state.config.max_page_bytes,
directions: ["forward", "backward"],
},
retention: RetentionCapabilities {
rolling: true,
notice_event: "dbgflow.retention",
},
replay: ReplayCapabilities {
checkpoint_projection_version: CURRENT_PROJECTION_VERSION,
endpoint: "/api/v1/replay",
semantics: "nearest_checkpoint_plus_deltas",
},
})
}
#[derive(Serialize)]
struct RecordingResponse {
format_version: u32,
schema_version: u32,
recording_id: String,
state: RecordingState,
activity: RecordingActivity,
created_unix_ms: Option<i64>,
metadata: BTreeMap<String, String>,
durable: DurableResponse,
committed: DurableResponse,
}
#[derive(Clone, Serialize)]
struct DurableResponse {
immutable_chunk_count: u64,
has_active_prefix: bool,
event_count: u64,
first_cursor: Option<String>,
last_cursor: Option<String>,
retained_prefix: RetainedPrefixResponse,
}
#[derive(Clone, Serialize)]
struct RetainedPrefixResponse {
history_truncated: bool,
generation: String,
first_chunk_number: String,
first_cursor: Option<String>,
removed_through_cursor: Option<String>,
}
async fn recording(State(state): State<AppState>) -> Result<Json<RecordingResponse>, ApiError> {
let path = state.recording_path.clone();
let (manifest, durable, activity) = blocking(move || {
let reader = RecordingReader::open(path.as_ref())?;
let durable = reader.durable_metadata()?;
let activity = recording_activity(path.as_ref())?;
Ok((reader.manifest().clone(), durable, activity))
})
.await?;
Ok(Json(recording_response(manifest, durable, activity)))
}
#[derive(Default, Deserialize)]
struct EventsQuery {
after: Option<u64>,
before: Option<u64>,
limit: Option<usize>,
}
#[derive(Serialize)]
struct EventsResponse {
events: Vec<EventResponse>,
next_cursor: Option<String>,
previous_cursor: Option<String>,
last_durable_cursor: Option<String>,
has_more: bool,
retained_prefix: RetainedPrefixResponse,
}
async fn events(
State(state): State<AppState>,
Query(query): Query<EventsQuery>,
) -> Result<Json<EventsResponse>, ApiError> {
let limit = query.limit.unwrap_or(state.config.default_page_limit);
validate_limit(limit, state.config.max_page_limit)?;
if query.after.is_some() && query.before.is_some() {
return Err(ApiError::bad_request(
"after and before cursors are mutually exclusive",
));
}
let direction = if let Some(before) = query.before {
PageDirection::Backward(Sequence::new(before))
} else {
PageDirection::Forward(query.after.map(Sequence::new))
};
let path = state.recording_path.clone();
let max_page_bytes = state.config.max_page_bytes;
let snapshot = blocking(move || {
let reader = RecordingReader::open(path.as_ref())?;
for _ in 0..8 {
let retained_before = reader.retained_prefix()?;
let page = match direction {
PageDirection::Forward(after) => reader.read_after_bounded(
after,
limit,
max_page_bytes,
event_response_size_bound,
)?,
PageDirection::Backward(before) => reader.read_before_bounded(
Some(before),
limit,
max_page_bytes,
event_response_size_bound,
)?,
};
let last_durable_sequence = reader.last_durable_sequence()?;
let retained_after = reader.retained_prefix()?;
if retained_before == retained_after {
return Ok(Some((page, last_durable_sequence, retained_after)));
}
}
Ok(None)
})
.await?;
let (page, last_durable_sequence, retained_prefix) = snapshot.ok_or_else(|| {
ApiError::internal(
"rolling retention changed repeatedly while capturing a coherent history page; retry",
)
})?;
if page.events.is_empty() && page.boundary_event_weight.is_some() {
return Err(ApiError::payload_too_large(format!(
"boundary event exceeds the configured page response budget of {max_page_bytes} bytes"
)));
}
let next_cursor = if query.before.is_none() {
page.events
.last()
.map(|event| event.sequence.get().to_string())
.or_else(|| query.after.map(|cursor| cursor.to_string()))
} else {
None
};
let previous_cursor = if query.before.is_some() {
page.events
.first()
.map(|event| event.sequence.get().to_string())
} else {
None
};
Ok(Json(EventsResponse {
events: page.events.iter().map(EventResponse::from).collect(),
next_cursor,
previous_cursor,
last_durable_cursor: last_durable_sequence.map(|value| value.get().to_string()),
has_more: page.has_more,
retained_prefix: retained_prefix_response(retained_prefix),
}))
}
#[derive(Clone, Copy)]
enum PageDirection {
Forward(Option<Sequence>),
Backward(Sequence),
}
#[derive(Deserialize)]
struct ReplayQuery {
cursor: u64,
boundary: Option<u64>,
after: Option<u64>,
limit: Option<usize>,
}
#[derive(Serialize)]
struct ReplayResponse {
requested_cursor: String,
resolved_cursor: String,
checkpoint: Option<CheckpointResponse>,
deltas: Vec<EventResponse>,
has_more: bool,
next_cursor: Option<String>,
exact: bool,
retained_prefix: RetainedPrefixResponse,
ignored_checkpoints: Vec<CheckpointIssueResponse>,
}
#[derive(Serialize)]
struct CheckpointResponse {
projection_version: u32,
recording_id: String,
durable_cursor: String,
complete: bool,
active_spans: Vec<ProjectedSpanResponse>,
known_spans: Vec<ProjectedSpanResponse>,
}
#[derive(Serialize)]
struct ProjectedSpanResponse {
context: ContextResponse,
opened_cursor: String,
enter_depth: u32,
is_open: bool,
metadata: Option<PayloadResponse>,
}
#[derive(Serialize)]
struct CheckpointIssueResponse {
file: String,
reason: String,
}
async fn replay(
State(state): State<AppState>,
Query(query): Query<ReplayQuery>,
) -> Result<Json<ReplayResponse>, ApiError> {
let limit = query.limit.unwrap_or(state.config.default_page_limit);
validate_limit(limit, state.config.max_page_limit)?;
if query.after.is_some_and(|after| after > query.cursor) {
return Err(ApiError::bad_request(
"replay after cursor must not exceed requested cursor",
));
}
if query
.boundary
.is_some_and(|boundary| boundary > query.cursor)
{
return Err(ApiError::bad_request(
"replay boundary cursor must not exceed requested cursor",
));
}
let requested = Sequence::new(query.cursor);
let path = state.recording_path.clone();
let max_page_bytes = state.config.max_page_bytes;
let captured = blocking(move || {
let reader = RecordingReader::open(path.as_ref())?;
for _ in 0..8 {
let retained_before = reader.retained_prefix()?;
let last = reader.last_durable_sequence()?.unwrap_or(Sequence::new(0));
let resolved = Sequence::new(requested.get().min(last.get()));
let lookup_cursor = Sequence::new(query.boundary.unwrap_or(resolved.get()));
let lookup = if query.boundary.is_some() {
reader.reconstruct_checkpoint_at(lookup_cursor, ProjectionLimits::default())?
} else {
reader.nearest_checkpoint_at_or_before(lookup_cursor)?
};
let checkpoint_after = lookup
.checkpoint
.as_ref()
.map(|checkpoint| checkpoint.durable_cursor);
let after = match (query.after, checkpoint_after) {
(Some(after), Some(checkpoint)) => Some(Sequence::new(after.max(checkpoint.get()))),
(Some(after), None) => Some(Sequence::new(after)),
(None, checkpoint) => checkpoint,
};
let page = reader.read_range_bounded(
after,
resolved,
limit,
max_page_bytes,
event_response_size_bound,
)?;
let retained_after = reader.retained_prefix()?;
if retained_before == retained_after {
return Ok(Some((resolved, lookup, page, retained_after)));
}
}
Ok(None)
})
.await?;
let (resolved, lookup, page, retained) = captured.ok_or_else(|| {
ApiError::internal(
"rolling retention changed repeatedly while capturing a coherent replay boundary; retry",
)
})?;
if page.events.is_empty() && page.boundary_event_weight.is_some() {
return Err(ApiError::payload_too_large(format!(
"boundary replay event exceeds the configured response budget of {max_page_bytes} bytes"
)));
}
let replayed_through = page
.events
.last()
.map(|event| event.sequence.get())
.or_else(|| {
lookup
.checkpoint
.as_ref()
.map(|checkpoint| checkpoint.durable_cursor.get())
});
let checkpoint_complete = lookup
.checkpoint
.as_ref()
.is_none_or(|checkpoint| checkpoint.complete);
let exact = checkpoint_complete
&& !page.has_more
&& replayed_through.is_some_and(|cursor| cursor == resolved.get())
&& !retention_notice_required(replayed_through, retained);
let next_cursor = page
.has_more
.then(|| {
page.events
.last()
.map(|event| event.sequence.get().to_string())
})
.flatten();
Ok(Json(ReplayResponse {
requested_cursor: requested.get().to_string(),
resolved_cursor: resolved.get().to_string(),
checkpoint: lookup.checkpoint.as_ref().map(CheckpointResponse::from),
deltas: page.events.iter().map(EventResponse::from).collect(),
has_more: page.has_more,
next_cursor,
exact,
retained_prefix: retained_prefix_response(retained),
ignored_checkpoints: lookup
.ignored
.into_iter()
.map(|issue| CheckpointIssueResponse {
file: issue
.path
.file_name()
.map_or_else(String::new, |name| name.to_string_lossy().into_owned()),
reason: issue.reason,
})
.collect(),
}))
}
#[derive(Default, Deserialize)]
struct LiveQuery {
after: Option<u64>,
}
async fn live(
State(state): State<AppState>,
Query(query): Query<LiveQuery>,
headers: HeaderMap,
) -> Result<Response, ApiError> {
let after = parse_last_event_id(&headers)?.or(query.after);
let subscription = state.live.subscribe();
let path = state.recording_path.clone();
let (last_durable_sequence, retained_prefix) = blocking({
let path = path.clone();
move || {
let reader = RecordingReader::open(path.as_ref())?;
Ok((reader.last_durable_sequence()?, reader.retained_prefix()?))
}
})
.await?;
let (sender, receiver) = mpsc::channel(1);
let mut shutdown = state.shutdown.subscribe();
let hub = Arc::clone(&state.live);
tokio::spawn(async move {
follow_shared(
hub,
after,
retained_prefix,
subscription,
&mut shutdown,
sender,
)
.await;
});
let stream = ReceiverStream::new(receiver);
let mut response = Sse::new(stream)
.keep_alive(
KeepAlive::new()
.interval(state.config.keep_alive_interval)
.text("keep-alive"),
)
.into_response();
response.headers_mut().insert(
LAST_DURABLE_CURSOR,
HeaderValue::from_str(
&last_durable_sequence.map_or_else(|| "".into(), |value| value.get().to_string()),
)
.expect("a decimal cursor is a valid header value"),
);
Ok(response)
}
async fn follow_shared(
hub: Arc<LiveHub>,
after: Option<u64>,
initial_retained_prefix: RetainedPrefix,
mut subscription: LiveSubscription,
shutdown: &mut watch::Receiver<bool>,
sender: mpsc::Sender<Result<SseEvent, Infallible>>,
) {
let mut cursor = after;
let mut last_notice_generation = None;
if retention_notice_required(cursor, initial_retained_prefix) {
if !send_retention_notice(
&sender,
shutdown,
&retained_prefix_response(initial_retained_prefix),
)
.await
{
return;
}
last_notice_generation = Some(initial_retained_prefix.generation);
advance_cursor_to_retained_boundary(&mut cursor, initial_retained_prefix);
}
let baseline = loop {
match subscription.baseline {
LiveBaseline::Ready(cursor) => break cursor,
LiveBaseline::Pending => {
tokio::select! {
changed = subscription.notifications.changed() => {
if changed.is_err() { return; }
},
changed = shutdown.changed() => {
if changed.is_err() || *shutdown.borrow() { return; }
},
_ = sender.closed() => return,
}
subscription.baseline = hub.baseline();
}
}
};
while baseline.is_some_and(|baseline| cursor.is_none_or(|cursor| cursor < baseline)) {
let read_path = Arc::clone(&hub.path);
let read_after = cursor.map(Sequence::new);
let batch_limit = hub.config.live_batch_limit.min(MAX_LIVE_CATCHUP_EVENTS);
let byte_limit = hub.config.live_buffer_bytes.min(MAX_LIVE_CATCHUP_BYTES);
hub.metrics.catchup_reads.fetch_add(1, Ordering::Relaxed);
let result = blocking(move || {
let reader = RecordingReader::open(read_path.as_ref())?;
let page = reader.read_after_bounded(
read_after,
batch_limit,
byte_limit,
live_event_weight,
)?;
let retained_prefix = reader.retained_prefix()?;
Ok((page, retained_prefix))
})
.await;
let (mut page, mut page_retained_prefix) = match result {
Ok(result) => result,
Err(error) => {
send_live_error(&sender, shutdown, error.message).await;
return;
}
};
if page.events.is_empty()
&& page
.boundary_event_weight
.is_some_and(|weight| weight <= hub.config.live_buffer_bytes)
&& byte_limit < hub.config.live_buffer_bytes
{
let read_path = Arc::clone(&hub.path);
let live_buffer_bytes = hub.config.live_buffer_bytes;
hub.metrics.catchup_reads.fetch_add(1, Ordering::Relaxed);
let result = blocking(move || {
let reader = RecordingReader::open(read_path.as_ref())?;
let page = reader.read_after_bounded(
read_after,
1,
live_buffer_bytes,
live_event_weight,
)?;
let retained_prefix = reader.retained_prefix()?;
Ok((page, retained_prefix))
})
.await;
(page, page_retained_prefix) = match result {
Ok(result) => result,
Err(error) => {
send_live_error(&sender, shutdown, error.message).await;
return;
}
};
}
if retention_notice_required(cursor, page_retained_prefix)
&& last_notice_generation != Some(page_retained_prefix.generation)
{
if !send_retention_notice(
&sender,
shutdown,
&retained_prefix_response(page_retained_prefix),
)
.await
{
return;
}
last_notice_generation = Some(page_retained_prefix.generation);
advance_cursor_to_retained_boundary(&mut cursor, page_retained_prefix);
}
for envelope in &page.events {
if baseline.is_some_and(|baseline| envelope.sequence.get() > baseline) {
break;
}
if cursor.is_some_and(|cursor| envelope.sequence.get() <= cursor) {
continue;
}
if !send_live_event(&sender, shutdown, envelope).await {
return;
}
cursor = Some(envelope.sequence.get());
}
if cursor.is_some_and(|cursor| baseline.is_some_and(|baseline| cursor >= baseline)) {
break;
}
if page.events.is_empty() {
if let Some(weight) = page.boundary_event_weight {
send_live_error(
&sender,
shutdown,
format!(
"live event requires {weight} bytes, exceeding live_buffer_bytes {}",
hub.config.live_buffer_bytes
),
)
.await;
return;
}
send_live_error(
&sender,
shutdown,
"durable history ended before the shared live baseline".into(),
)
.await;
return;
}
}
hub.metrics.catchups.fetch_add(1, Ordering::Relaxed);
loop {
match hub.read_after(cursor) {
LiveRead::Event(envelope) => {
if cursor.is_some_and(|cursor| envelope.sequence.get() <= cursor) {
continue;
}
if !send_live_event(&sender, shutdown, &envelope).await {
return;
}
cursor = Some(envelope.sequence.get());
}
LiveRead::Pending => {
tokio::select! {
changed = subscription.notifications.changed() => {
if changed.is_err() { return; }
},
changed = shutdown.changed() => {
if changed.is_err() || *shutdown.borrow() { return; }
},
_ = sender.closed() => return,
}
}
LiveRead::Complete | LiveRead::Lagged => return,
LiveRead::TerminalError(message) => {
send_live_error(&sender, shutdown, message).await;
return;
}
}
}
}
async fn send_live_event(
sender: &mpsc::Sender<Result<SseEvent, Infallible>>,
shutdown: &mut watch::Receiver<bool>,
envelope: &EventEnvelope,
) -> bool {
let permit = tokio::select! {
permit = sender.reserve() => match permit {
Ok(permit) => permit,
Err(_) => return false,
},
_ = shutdown.changed() => return false,
};
let name = if envelope.kind == EventKind::Gap {
"dbgflow.gap"
} else {
"dbgflow.event"
};
let event = SseEvent::default()
.event(name)
.id(envelope.sequence.get().to_string())
.json_data(EventResponse::from(envelope))
.expect("event response always serializes");
permit.send(Ok(event));
true
}
async fn send_retention_notice(
sender: &mpsc::Sender<Result<SseEvent, Infallible>>,
shutdown: &mut watch::Receiver<bool>,
notice: &RetainedPrefixResponse,
) -> bool {
let permit = tokio::select! {
permit = sender.reserve() => match permit {
Ok(permit) => permit,
Err(_) => return false,
},
_ = shutdown.changed() => return false,
};
let event = SseEvent::default()
.event("dbgflow.retention")
.json_data(notice)
.expect("retention notice always serializes");
permit.send(Ok(event));
true
}
async fn send_live_error(
sender: &mpsc::Sender<Result<SseEvent, Infallible>>,
shutdown: &mut watch::Receiver<bool>,
message: String,
) {
let permit = tokio::select! {
permit = sender.reserve() => match permit {
Ok(permit) => permit,
Err(_) => return,
},
_ = shutdown.changed() => return,
};
let data = serde_json::json!({"error": message});
let event = SseEvent::default()
.event("dbgflow.error")
.data(data.to_string());
permit.send(Ok(event));
}
fn parse_last_event_id(headers: &HeaderMap) -> Result<Option<u64>, ApiError> {
let Some(value) = headers.get(LAST_EVENT_ID) else {
return Ok(None);
};
let value = value
.to_str()
.map_err(|_| ApiError::bad_request("Last-Event-ID must be ASCII"))?;
value
.parse()
.map(Some)
.map_err(|_| ApiError::bad_request("Last-Event-ID must be an unsigned integer"))
}
fn validate_limit(limit: usize, maximum: usize) -> Result<(), ApiError> {
if limit == 0 || limit > maximum {
Err(ApiError::bad_request(format!(
"limit must be in 1..={maximum}"
)))
} else {
Ok(())
}
}
async fn blocking<T: Send + 'static>(
operation: impl FnOnce() -> Result<T, StorageError> + Send + 'static,
) -> Result<T, ApiError> {
tokio::task::spawn_blocking(operation)
.await
.map_err(|error| ApiError::internal(format!("storage task failed: {error}")))?
.map_err(ApiError::from)
}
fn recording_response(
manifest: Manifest,
durable: DurableMetadata,
activity: RecordingActivity,
) -> RecordingResponse {
let retained = durable.retained_prefix;
let durable = DurableResponse {
immutable_chunk_count: durable.chunk_count,
has_active_prefix: durable.has_active_prefix,
event_count: durable.event_count,
first_cursor: durable.first_sequence.map(|value| value.get().to_string()),
last_cursor: durable.last_sequence.map(|value| value.get().to_string()),
retained_prefix: retained_prefix_response(retained),
};
RecordingResponse {
format_version: manifest.format_version,
schema_version: manifest.schema_version,
recording_id: manifest.recording_id,
state: manifest.state,
activity,
created_unix_ms: manifest.created_unix_ms,
metadata: manifest.metadata,
durable: durable.clone(),
committed: durable,
}
}
fn retained_prefix_response(retained: RetainedPrefix) -> RetainedPrefixResponse {
RetainedPrefixResponse {
history_truncated: retained.history_truncated,
generation: retained.generation.to_string(),
first_chunk_number: retained.first_chunk_number.to_string(),
first_cursor: retained.first_sequence.map(|value| value.get().to_string()),
removed_through_cursor: retained
.removed_through_sequence
.map(|value| value.get().to_string()),
}
}
fn retention_notice_required(after: Option<u64>, retained: RetainedPrefix) -> bool {
if !retained.history_truncated {
return false;
}
if let Some(removed) = retained.removed_through_sequence {
return after.is_none_or(|after| after <= removed.get());
}
match retained.first_sequence {
Some(first) => after.is_none_or(|after| after < first.get()),
None => true,
}
}
fn advance_cursor_to_retained_boundary(cursor: &mut Option<u64>, retained: RetainedPrefix) {
let boundary = retained
.removed_through_sequence
.map(Sequence::get)
.or_else(|| {
retained
.first_sequence
.and_then(|first| first.get().checked_sub(1))
});
if let Some(boundary) = boundary {
*cursor = Some(cursor.map_or(boundary, |cursor| cursor.max(boundary)));
}
}
#[derive(Serialize)]
struct EventResponse {
schema_version: u32,
recording_id: String,
sequence: String,
timestamp: TimestampResponse,
context: ContextResponse,
kind: String,
payload: Option<PayloadResponse>,
}
#[derive(Serialize)]
struct TimestampResponse {
monotonic_ns: String,
wall_time_unix_ns: Option<String>,
}
#[derive(Serialize)]
struct ContextResponse {
process_id: u32,
thread_id: String,
flow_id: Option<String>,
span_id: Option<String>,
parent_span_id: Option<String>,
}
#[derive(Serialize)]
struct PayloadResponse {
encoding: String,
flags: u32,
data_base64: String,
}
impl From<&EventEnvelope> for EventResponse {
fn from(event: &EventEnvelope) -> Self {
Self {
schema_version: event.schema_version.get(),
recording_id: format!("{:032x}", event.recording_id.as_u128()),
sequence: event.sequence.get().to_string(),
timestamp: TimestampResponse {
monotonic_ns: event.timestamp.monotonic_ns.to_string(),
wall_time_unix_ns: event
.timestamp
.wall_time_unix_ns
.map(|value| value.to_string()),
},
context: context_response(event.context),
kind: event_kind(event.kind),
payload: event.payload.as_ref().map(payload_response),
}
}
}
impl From<&ProjectionCheckpoint> for CheckpointResponse {
fn from(checkpoint: &ProjectionCheckpoint) -> Self {
Self {
projection_version: checkpoint.projection_version,
recording_id: format!("{:032x}", checkpoint.recording_id.as_u128()),
durable_cursor: checkpoint.durable_cursor.get().to_string(),
complete: checkpoint.complete,
active_spans: checkpoint
.active_spans
.iter()
.map(ProjectedSpanResponse::from)
.collect(),
known_spans: checkpoint
.known_spans
.iter()
.map(ProjectedSpanResponse::from)
.collect(),
}
}
}
impl From<&ProjectedSpan> for ProjectedSpanResponse {
fn from(span: &ProjectedSpan) -> Self {
Self {
context: context_response(span.context),
opened_cursor: span.opened_sequence.get().to_string(),
enter_depth: span.enter_depth,
is_open: span.is_open,
metadata: span.metadata.as_ref().map(payload_response),
}
}
}
fn context_response(context: dbgflow_protocol::EventContext) -> ContextResponse {
ContextResponse {
process_id: context.process_id.get(),
thread_id: context.thread_id.get().to_string(),
flow_id: context.flow_id.map(|value| value.get().to_string()),
span_id: context.span_id.map(|value| value.get().to_string()),
parent_span_id: context.parent_span_id.map(|value| value.get().to_string()),
}
}
fn payload_response(payload: &Payload) -> PayloadResponse {
PayloadResponse {
encoding: payload_encoding(payload.encoding),
flags: payload.flags.bits(),
data_base64: BASE64.encode(&payload.data),
}
}
fn event_response_size_bound(event: &EventEnvelope) -> usize {
let encoded_payload = event.payload.as_ref().map_or(0, |payload| {
payload.data.len().div_ceil(3).saturating_mul(4)
});
EVENT_RESPONSE_OVERHEAD_BYTES.saturating_add(encoded_payload)
}
fn event_kind(kind: EventKind) -> String {
match kind {
EventKind::SpanOpen => "span_open".into(),
EventKind::SpanEnter => "span_enter".into(),
EventKind::SpanExit => "span_exit".into(),
EventKind::SpanClose => "span_close".into(),
EventKind::Snapshot => "snapshot".into(),
EventKind::Checkpoint => "checkpoint".into(),
EventKind::Log => "log".into(),
EventKind::Metadata => "metadata".into(),
EventKind::Gap => "gap".into(),
EventKind::Custom(value) => format!("custom:{value}"),
}
}
fn payload_encoding(encoding: PayloadEncoding) -> String {
match encoding {
PayloadEncoding::Opaque => "opaque".into(),
PayloadEncoding::Json => "json".into(),
PayloadEncoding::Utf8 => "utf8".into(),
PayloadEncoding::Protobuf => "protobuf".into(),
PayloadEncoding::Custom(value) => format!("custom:{value}"),
}
}
#[derive(Serialize)]
struct ErrorResponse {
error: String,
}
struct ApiError {
status: StatusCode,
message: String,
}
impl ApiError {
fn bad_request(message: impl Into<String>) -> Self {
Self {
status: StatusCode::BAD_REQUEST,
message: message.into(),
}
}
fn internal(message: impl Into<String>) -> Self {
Self {
status: StatusCode::INTERNAL_SERVER_ERROR,
message: message.into(),
}
}
fn payload_too_large(message: impl Into<String>) -> Self {
Self {
status: StatusCode::PAYLOAD_TOO_LARGE,
message: message.into(),
}
}
}
impl From<StorageError> for ApiError {
fn from(error: StorageError) -> Self {
Self::internal(error.to_string())
}
}
impl IntoResponse for ApiError {
fn into_response(self) -> Response {
(
self.status,
Json(ErrorResponse {
error: self.message,
}),
)
.into_response()
}
}
#[cfg(test)]
mod tests;