:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(logs): add Amazon Data Firehose intake for CloudWatch logs (#101964)
Jimmy Zhu2e9d333411ee
diff --git a/rust/capture-logs/README.md b/rust/capture-logs/README.mdindex d792e8afe925..9ddf4f6a8e69 100644--- a/rust/capture-logs/README.md+++ b/rust/capture-logs/README.md@@ -23,6 +23,7 @@ The service is configured using environment variables:| MANAGEMENT_BIND_HOST | :: | Host to bind the health check and metrics server || MANAGEMENT_BIND_PORT | 8080 | Port for the health check and metrics server || MAX_REQUEST_BODY_SIZE_BYTES | 2097152 | Rejects larger request bodies, before and after gzip decompression |+| FIREHOSE_MAX_REQUEST_BODY_SIZE_BYTES | 8388608 | Body cap for the Amazon Data Firehose route only || DROP_EVENTS_BY_TOKEN | (none) | Comma-separated tokens to drop |## Authentication@@ -118,12 +119,42 @@ Requirements:2. Include your PostHog project token in the Authorization header or as a query parameter3. Use standard OTLP log format (JSON, JSONL, or Protobuf)+## Amazon Data Firehose (CloudWatch Logs)++`POST /i/v1/logs/aws/firehose` implements the [Firehose HTTP endpoint destination contract](https://docs.aws.amazon.com/firehose/latest/dev/httpdeliveryrequestresponse.html).+A customer creates a Firehose stream with this URL as its HTTP endpoint and the project API key as the stream's access key, then subscribes CloudWatch log groups to the stream.++- Auth: the project API key in `X-Amz-Firehose-Access-Key`, or a Bearer `Authorization` header for manual testing.+ A pasted `Bearer` prefix in the access key field is tolerated.+ The same shape check as every other route applies.+- Each Firehose record is one CloudWatch subscription batch (base64, usually gzip).+ Every `logEvents` entry becomes a log row.+ `CONTROL_MESSAGE` records are acknowledged and skipped.+ A record that is not a CloudWatch envelope (VPC flow logs, WAF) is split on newlines.+ A record that cannot be decoded at all is skipped and counted, so one bad record does not make Firehose retry the good ones.+- Row mapping: `body` = message; `timestamp` = the event timestamp; `service.name` is inferred from the log group (`/aws/lambda/<fn>` → `<fn>`, `/aws/rds/instance/<id>/...` → `<id>`, else the group name) unless a Firehose common attribute `service.name` is set.+ Resource attributes are `cloud.provider=aws`, `cloud.account.id`, `cloud.region` (from `X-Amz-Firehose-Source-Arn`), `aws.log.group.name`, `aws.log.stream.name`, plus any common attributes; the AWS facts win over a common attribute of the same name.+ Severity comes from a JSON `level`/`severity` key or a leading `ERROR`/`WARN`/`INFO`/`DEBUG` token, else `info`.+- Rows are chunked into Kafka messages sized at half the producer's `message.max.bytes`, because a decoded Firehose request can be far larger than one message.+ Batches are produced concurrently.+ Delivery is at-least-once: a Kafka failure returns 500, Firehose redelivers the whole request with the same request id, and the batches already produced are duplicated.+- Size limits: the request body is capped by `FIREHOSE_MAX_REQUEST_BODY_SIZE_BYTES` (default 8 MiB, above the 2 MiB used by the other routes because Firehose buffers whole MiB and base64 adds a third), and the decoded records of one request are capped at eight times that.+ Either limit answers 413, which Firehose treats as permanent, so the failed batch lands in the customer's S3 backup bucket.+- Every response carries the contract body: `{"requestId", "timestamp"}` on 200, plus `"errorMessage"` on 400 (unparseable body, no decodable record), 401 (missing or invalid access key), 413 and 500 (Kafka).+ Rejections raised by the body-size and decompression layers are re-shaped into the same body.+- Recommended stream settings: buffer 1 MB / 60 s, GZIP content encoding, retry 300 s, S3 backup for failed data only.++Metrics: `capture_logs_firehose_records_total{kind}` (`data`, `control`, `raw`, `invalid`) and `capture_logs_firehose_events_total`.+Request outcomes are on the shared `http_requests_total{path,status}` and rejected tokens on `capture_logs_requests_rejected_total`.+## Endpoints### Log Ingestion- `POST /v1/logs` - Accept OTLP logs (JSON, JSONL, or Protobuf)- `POST /i/v1/logs` - Alternative endpoint for OTLP logs+- `POST /i/v1/logs/datadog[/<token>]` - Datadog agent intake+- `POST /i/v1/logs/aws/firehose` - Amazon Data Firehose HTTP endpoint destination- `OPTIONS /v1/logs` - CORS preflight support- `OPTIONS /i/v1/logs` - CORS preflight supportdiff --git a/rust/capture-logs/src/authorizer.rs b/rust/capture-logs/src/authorizer.rsindex 2ebce97d92ab..37b91417fd94 100644--- a/rust/capture-logs/src/authorizer.rs+++ b/rust/capture-logs/src/authorizer.rs@@ -62,6 +62,30 @@ impl Authorizer {Ok(token)}+ /// Resolve the token from a vendor-specific header that carries it verbatim, falling back to+ /// `authorize`. For intakes such as Amazon Data Firehose, whose access key field is copied into+ /// its own header. A pasted `Bearer ` prefix is tolerated, because the shape check would let it+ /// through and the consumer would then drop every row for an unknown token.+ pub fn authorize_from_header_or_bearer<'a>(+ &self,+ headers: &'a HeaderMap,+ header_name: &str,+ signal: Signal,+ ) -> Result<&'a str, Rejection> {+ let verbatim = headers+ .get(header_name)+ .and_then(|value| value.to_str().ok())+ .map(strip_bearer)+ .filter(|token| !token.is_empty());+ match verbatim {+ Some(token) => {+ self.authorize_token(token, signal)?;+ Ok(token)+ }+ None => self.authorize(headers, None, signal),+ }+ }+/// Authorize a token that the caller resolved itself. The Datadog endpoint accepts the token/// in a path segment and in a bare `Authorization` value, so it cannot use `authorize`.pub fn authorize_token(&self, token: &str, signal: Signal) -> Result<(), Rejection> {@@ -100,6 +124,11 @@ fn unauthorized(message: impl Into<String>) -> Rejection {)}+/// Splits before it trims, so a value of exactly `Bearer ` still yields the empty token callers reject.+fn strip_bearer(value: &str) -> &str {+ value.split("Bearer ").last().unwrap_or("").trim()+}+/// Bearer header first, `?token=` second, matching what the handlers did individually.fn resolve_token<'a>(headers: &'a HeaderMap,@@ -111,11 +140,7 @@ fn resolve_token<'a>(};if let Some(value) = headers.get("Authorization") {- return value- .to_str()- .unwrap_or("")- .split("Bearer ")- .last()+ return Some(strip_bearer(value.to_str().unwrap_or(""))).filter(|token| !token.is_empty()).ok_or_else(missing);}diff --git a/rust/capture-logs/src/config.rs b/rust/capture-logs/src/config.rsindex a02db1e147ba..e4c42e284cfc 100644--- a/rust/capture-logs/src/config.rs+++ b/rust/capture-logs/src/config.rs@@ -28,6 +28,11 @@ pub struct Config {#[envconfig(from = "MAX_REQUEST_BODY_SIZE_BYTES", default = "2097152")] // 2MB (Axum default)pub max_request_body_size_bytes: usize,++ // Firehose buffers 1-64 MiB per delivery and base64 adds a third, so the Firehose route gets its+ // own, larger body cap. Decoded size is bounded separately by the endpoint.+ #[envconfig(from = "FIREHOSE_MAX_REQUEST_BODY_SIZE_BYTES", default = "8388608")] // 8 MiB+ pub firehose_max_request_body_size_bytes: usize,}impl Config {diff --git a/rust/capture-logs/src/endpoints/aws_firehose.rs b/rust/capture-logs/src/endpoints/aws_firehose.rsnew file mode 100644index 000000000000..f679b62e910b--- /dev/null+++ b/rust/capture-logs/src/endpoints/aws_firehose.rs@@ -0,0 +1,952 @@+//! Amazon Data Firehose HTTP endpoint destination for CloudWatch Logs:+//! https://docs.aws.amazon.com/firehose/latest/dev/httpdeliveryrequestresponse.html+//!+//! Every response, errors included, must be JSON with the request id, or Firehose treats it as a+//! 500 and retries with the same request id (only 413 is permanent), so delivery is at-least-once.+//! Records are decoded one at a time under a request-wide decoded-size budget, and rows are chunked+//! into Kafka messages sized against the producer's message cap, because one Firehose request can+//! decode to many megabytes.++use crate::authorizer::Signal;+use crate::log_record::{+ apply_timestamp_override, convert_severity_text_to_number, datetime_from_millis,+ severity_alias, try_extract_severity, KafkaLogRow,+};+use crate::service::{gunzip_if_magic, Service};+use axum::{+ extract::{Request, State},+ http::{header::CONTENT_TYPE, HeaderMap, StatusCode},+ middleware::Next,+ response::{IntoResponse, Json, Response},+};+use bytes::Bytes;+use chrono::{DateTime, Utc};+use common_compression::decode_base64;+use metrics::counter;+use serde::{Deserialize, Serialize};+use serde_json::{json, Value};+use std::collections::HashMap;+use tokio::task::JoinSet;+use tracing::{debug, error, instrument, warn};+use uuid::Uuid;++const ACCESS_KEY_HEADER: &str = "x-amz-firehose-access-key";+const REQUEST_ID_HEADER: &str = "x-amz-firehose-request-id";+const SOURCE_ARN_HEADER: &str = "x-amz-firehose-source-arn";+const COMMON_ATTRIBUTES_HEADER: &str = "x-amz-firehose-common-attributes";++const RECORDS_COUNTER: &str = "capture_logs_firehose_records_total";+const EVENTS_COUNTER: &str = "capture_logs_firehose_events_total";++/// Decoded bytes one request may produce across all its records, as a multiple of the body cap.+/// CloudWatch envelopes gzip at roughly 5-15x, so 8x admits real traffic and bounds a deflate bomb.+const DECODED_BUDGET_FACTOR: usize = 8;++/// Fraction of the producer's message cap a batch of rows may fill before zstd; the Avro framing+/// plus attribute strings compress well enough that half is a safe budget.+const KAFKA_BATCH_BUDGET_DIVISOR: usize = 2;++type Rejection = (StatusCode, Json<Value>);++#[derive(Deserialize, Debug)]+#[serde(rename_all = "camelCase")]+pub struct FirehoseRequest {+ pub request_id: String,+ #[serde(default)]+ pub records: Vec<FirehoseRecord>,+}++#[derive(Deserialize, Debug)]+pub struct FirehoseRecord {+ pub data: String,+}++/// The payload CloudWatch Logs writes for a subscription filter, after gunzip.+#[derive(Deserialize, Debug)]+#[serde(rename_all = "camelCase")]+pub struct CloudWatchEnvelope {+ #[serde(default)]+ pub message_type: String,+ #[serde(default)]+ pub owner: String,+ #[serde(default)]+ pub log_group: String,+ #[serde(default)]+ pub log_stream: String,+ #[serde(default)]+ pub subscription_filters: Vec<String>,+ #[serde(default)]+ pub log_events: Vec<CloudWatchLogEvent>,+}++#[derive(Deserialize, Debug)]+pub struct CloudWatchLogEvent {+ #[serde(default)]+ pub id: String,+ #[serde(default)]+ pub timestamp: Option<i64>,+ #[serde(default)]+ pub message: String,+}++#[derive(Debug, Default)]+pub struct FirehoseContext {+ pub region: Option<String>,+ pub common_attributes: HashMap<String, Value>,+}++#[derive(Debug)]+pub enum DecodedRecord {+ CloudWatch(CloudWatchEnvelope),+ /// Not a CloudWatch envelope: VPC flow logs, WAF, or anything else put on the stream+ /// directly. Treated as newline-separated lines.+ Raw(String),+}++/// Rows from one record, each paired with whether its timestamp was clamped.+pub type DecodedRows = Vec<(KafkaLogRow, bool)>;++fn contract_body(request_id: &str, error_message: Option<&str>) -> Json<Value> {+ let mut body = json!({+ "requestId": request_id,+ "timestamp": Utc::now().timestamp_millis(),+ });+ if let Some(message) = error_message {+ body["errorMessage"] = json!(message);+ }+ Json(body)+}++fn rejection(status: StatusCode, request_id: &str, message: &str) -> Rejection {+ (status, contract_body(request_id, Some(message)))+}++/// Re-shape a `{"error": ...}` rejection from the shared helpers into the Firehose contract.+fn into_firehose_rejection((status, Json(body)): Rejection, request_id: &str) -> Rejection {+ let message = body+ .get("error")+ .and_then(Value::as_str)+ .unwrap_or("request rejected");+ rejection(status, request_id, message)+}++fn header_str<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> {+ headers+ .get(name)+ .and_then(|v| v.to_str().ok())+ .map(str::trim)+ .filter(|v| !v.is_empty())+}++/// Outermost layer on the Firehose router. The body-size limit and the decompression layer answer+/// with plain-text errors before the handler runs; Firehose needs the contract body on every+/// status, and 413 in particular is the one it treats as permanent.+pub async fn shape_layer_rejections(request: Request, next: Next) -> Response {+ let request_id = header_str(request.headers(), REQUEST_ID_HEADER)+ .unwrap_or("")+ .to_string();+ let response = next.run(request).await;+ let status = response.status();+ let is_json = response+ .headers()+ .get(CONTENT_TYPE)+ .and_then(|v| v.to_str().ok())+ .is_some_and(|v| v.starts_with("application/json"));+ if status.is_success() || is_json {+ return response;+ }+ let message = match status {+ StatusCode::PAYLOAD_TOO_LARGE => {+ "request body exceeds the size limit; lower the Firehose buffer size"+ }+ StatusCode::UNSUPPORTED_MEDIA_TYPE => "unsupported Content-Encoding; use GZIP or none",+ _ => "request rejected",+ };+ (status, contract_body(&request_id, Some(message))).into_response()+}++/// `arn:aws:firehose:us-east-1:123456789012:deliverystream/name` → `us-east-1`.+pub fn region_from_source_arn(arn: &str) -> Option<String> {+ arn.split(':')+ .nth(3)+ .filter(|region| !region.is_empty())+ .map(str::to_string)+}++/// `X-Amz-Firehose-Common-Attributes` is `{"commonAttributes": {"k": "v"}}`; a flat object is+/// also accepted.+pub fn parse_common_attributes(raw: Option<&str>) -> HashMap<String, Value> {+ let Some(raw) = raw else {+ return HashMap::new();+ };+ let Ok(mut value) = serde_json::from_str::<Value>(raw) else {+ warn!("Ignoring unparseable {COMMON_ATTRIBUTES_HEADER} header");+ return HashMap::new();+ };+ let object = match value.get_mut("commonAttributes") {+ Some(inner) => inner.take(),+ None => value,+ };+ match object {+ Value::Object(map) => map.into_iter().collect(),+ _ => HashMap::new(),+ }+}++/// The log group name is the best signal CloudWatch gives for which service wrote a line.+/// AWS-managed groups follow `/aws/<service>/<name>`; anything else is used as-is.+pub fn infer_service_name(log_group: &str) -> String {+ let parts: Vec<&str> = log_group.trim_matches('/').split('/').collect();+ let named = |idx: usize| {+ parts+ .get(idx)+ .filter(|s| !s.is_empty())+ .map(|s| s.to_string())+ };++ if parts.first() != Some(&"aws") {+ return log_group.to_string();+ }+ match parts.get(1).copied() {+ Some("lambda" | "eks" | "codebuild" | "apigateway" | "api-gateway" | "http-api") => {+ named(2)+ }+ Some("rds") => named(3),+ Some("ecs") if parts.get(2) == Some(&"containerinsights") => named(3),+ Some("ecs") => named(2),+ Some("vpc") => Some("vpc-flow-logs".to_string()),+ _ => None,+ }+ .unwrap_or_else(|| log_group.to_string())+}++/// A JSON body is checked for the usual level keys first; otherwise a leading `ERROR`, `[WARN]`+/// or `<timestamp> INFO` token is used, because CloudWatch lines carry no severity of their own.+pub fn infer_severity(message: &str) -> (String, i32) {+ let text = try_extract_severity(message).or_else(|| {+ // Look at the first three whitespace-separated tokens so a timestamp or request-id prefix+ // does not hide the level.+ message+ .split_whitespace()+ .take(3)+ .map(|token| token.trim_matches(|c: char| !c.is_ascii_alphabetic()))+ .find_map(severity_alias)+ });+ let text = text.unwrap_or("info");+ (text.to_string(), convert_severity_text_to_number(text))+}++/// Attribute values travel as JSON-encoded strings; this is the one place that encodes them.+fn put(map: &mut HashMap<String, String>, key: &str, value: impl Serialize) {+ if let Ok(encoded) = serde_json::to_string(&value) {+ map.insert(key.to_string(), encoded);+ }+}++/// Attributes shared by every row in one record, computed once instead of per event.+struct RowTemplate {+ service_name: String,+ resource_attributes: HashMap<String, String>,+ attributes: HashMap<String, String>,+}++impl FirehoseContext {+ fn template(&self, envelope: Option<&CloudWatchEnvelope>) -> RowTemplate {+ let mut resource_attributes = HashMap::new();+ for (key, value) in &self.common_attributes {+ put(&mut resource_attributes, key, value);+ }+ // A customer-set service.name wins over inference; the AWS facts below win over everything.+ let service_name = match self.common_attributes.get("service.name") {+ Some(Value::String(name)) if !name.is_empty() => name.clone(),+ _ => envelope+ .map(|env| infer_service_name(&env.log_group))+ .unwrap_or_else(|| "aws-firehose".to_string()),+ };+ put(&mut resource_attributes, "service.name", &service_name);+ put(&mut resource_attributes, "cloud.provider", "aws");+ if let Some(region) = &self.region {+ put(&mut resource_attributes, "cloud.region", region);+ }++ let mut attributes = HashMap::new();+ if let Some(env) = envelope {+ if !env.owner.is_empty() {+ put(&mut resource_attributes, "cloud.account.id", &env.owner);+ }+ put(+ &mut resource_attributes,+ "aws.log.group.name",+ &env.log_group,+ );+ put(+ &mut resource_attributes,+ "aws.log.stream.name",+ &env.log_stream,+ );+ if !env.subscription_filters.is_empty() {+ put(+ &mut attributes,+ "aws.subscription_filters",+ env.subscription_filters.join(","),+ );+ }+ }++ RowTemplate {+ service_name,+ resource_attributes,+ attributes,+ }+ }+}++fn build_row(+ template: &RowTemplate,+ body: String,+ raw_timestamp: DateTime<Utc>,+ event_id: Option<&str>,+ now: DateTime<Utc>,+) -> (KafkaLogRow, bool) {+ let mut attributes = template.attributes.clone();+ if let Some(id) = event_id {+ put(&mut attributes, "aws.log.event.id", id);+ }+ let (timestamp, was_overridden) = apply_timestamp_override(raw_timestamp, &mut attributes);+ let (severity_text, severity_number) = infer_severity(&body);++ let row = KafkaLogRow {+ uuid: Uuid::now_v7().to_string(),+ trace_id: String::new(),+ span_id: String::new(),+ trace_flags: 0,+ timestamp,+ observed_timestamp: now,+ body,+ severity_text,+ severity_number,+ service_name: template.service_name.clone(),+ resource_attributes: template.resource_attributes.clone(),+ instrumentation_scope: String::new(),+ event_name: String::new(),+ attributes,+ bytes_uncompressed: None,+ retention_days: None,+ pattern: None,+ pattern_version: None,+ }+ .with_computed_bytes();+ (row, was_overridden)+}++pub fn cloudwatch_envelope_to_rows(+ envelope: CloudWatchEnvelope,+ ctx: &FirehoseContext,+) -> DecodedRows {+ let template = ctx.template(Some(&envelope));+ let now = Utc::now();+ envelope+ .log_events+ .into_iter()+ .map(+ |CloudWatchLogEvent {+ id,+ timestamp,+ message,+ }| {+ let event_id = (!id.is_empty()).then_some(id.as_str());+ build_row(+ &template,+ message,+ datetime_from_millis(timestamp),+ event_id,+ now,+ )+ },+ )+ .collect()+}++/// Lines from a record that is not a CloudWatch envelope. There is no per-line timestamp, so+/// every row gets the request's observed time.+pub fn raw_record_to_rows(text: &str, ctx: &FirehoseContext) -> DecodedRows {+ let template = ctx.template(None);+ let now = Utc::now();+ text.lines()+ .filter(|line| !line.trim().is_empty())+ .map(|line| build_row(&template, line.to_string(), now, None, now))+ .collect()+}++/// Base64-decode a Firehose record, gunzip it when CloudWatch compressed it, and classify it.+/// Returns the decoded byte count alongside, for the request-wide budget. The error carries the+/// status Firehose should see: 413 for an oversized record, else 400.+pub fn decode_record(+ data: &str,+ max_bytes: usize,+) -> Result<(DecodedRecord, usize), (StatusCode, String)> {+ let bytes = decode_base64(data.trim()).map_err(|e| {+ (+ StatusCode::BAD_REQUEST,+ format!("record is not valid base64: {e}"),+ )+ })?;+ let bytes = gunzip_if_magic(&bytes, max_bytes)?.unwrap_or(bytes);+ let decoded_len = bytes.len();++ if let Ok(envelope) = serde_json::from_slice::<CloudWatchEnvelope>(&bytes) {+ if !envelope.log_group.is_empty() || !envelope.log_events.is_empty() {+ return Ok((DecodedRecord::CloudWatch(envelope), decoded_len));+ }+ }++ String::from_utf8(bytes)+ .map(|text| (DecodedRecord::Raw(text), decoded_len))+ .map_err(|_| {+ (+ StatusCode::BAD_REQUEST,+ "record is neither a CloudWatch envelope nor UTF-8 text".to_string(),+ )+ })+}++fn decoded_record_to_rows(decoded: DecodedRecord, ctx: &FirehoseContext) -> Option<DecodedRows> {+ match decoded {+ DecodedRecord::CloudWatch(envelope) if envelope.message_type == "CONTROL_MESSAGE" => {+ counter!(RECORDS_COUNTER, "kind" => "control").increment(1);+ None+ }+ DecodedRecord::CloudWatch(envelope) => {+ counter!(RECORDS_COUNTER, "kind" => "data").increment(1);+ Some(cloudwatch_envelope_to_rows(envelope, ctx))+ }+ DecodedRecord::Raw(text) => {+ counter!(RECORDS_COUNTER, "kind" => "raw").increment(1);+ Some(raw_record_to_rows(&text, ctx))+ }+ }+}++/// Accumulates rows into Kafka batches of at most `budget` bytes (by each row's own byte count),+/// so no batch exceeds the producer cap and a single oversized record is split rather than failed.+pub struct BatchBuilder {+ budget: u64,+ rows: Vec<KafkaLogRow>,+ bytes: u64,+ timestamps_overridden: u64,+}++pub struct Batch {+ pub rows: Vec<KafkaLogRow>,+ pub bytes: u64,+ pub timestamps_overridden: u64,+}++impl BatchBuilder {+ pub fn new(budget: u64) -> Self {+ Self {+ budget,+ rows: Vec::new(),+ bytes: 0,+ timestamps_overridden: 0,+ }+ }++ /// Add a row; returns a full batch to produce when this row did not fit alongside the pending+ /// ones. A single row larger than the budget still goes out on its own.+ pub fn push(&mut self, row: KafkaLogRow, overridden: bool) -> Option<Batch> {+ let row_bytes = row.byte_count();+ let full = if !self.rows.is_empty() && self.bytes + row_bytes > self.budget {+ self.take()+ } else {+ None+ };+ self.rows.push(row);+ self.bytes += row_bytes;+ self.timestamps_overridden += u64::from(overridden);+ full+ }++ pub fn finish(mut self) -> Option<Batch> {+ self.take()+ }++ fn take(&mut self) -> Option<Batch> {+ if self.rows.is_empty() {+ return None;+ }+ Some(Batch {+ rows: std::mem::take(&mut self.rows),+ bytes: std::mem::take(&mut self.bytes),+ timestamps_overridden: std::mem::take(&mut self.timestamps_overridden),+ })+ }+}++fn spawn_write(+ writes: &mut JoinSet<anyhow::Result<()>>,+ service: &Service,+ token: &str,+ batch: Batch,+) {+ let sink = service.sink.clone();+ let token = token.to_string();+ writes.spawn(async move {+ sink.write(&token, batch.rows, batch.bytes, batch.timestamps_overridden)+ .await+ });+}++#[instrument(skip_all, fields(+ token = tracing::field::Empty,+ firehose_request_id = %header_str(&headers, REQUEST_ID_HEADER).unwrap_or(""),+ content_length = %header_str(&headers, "content-length").unwrap_or(""),+ content_encoding = %header_str(&headers, "content-encoding").unwrap_or("")))+]+pub async fn export_aws_firehose_logs_http(+ State(service): State<Service>,+ headers: HeaderMap,+ body: Bytes,+) -> Result<Json<Value>, Rejection> {+ // The header is the only request id available before the body is parsed, and Firehose keeps+ // it stable across retries, so it is what every early rejection echoes.+ let header_request_id = header_str(&headers, REQUEST_ID_HEADER)+ .unwrap_or("")+ .to_string();++ let token = service+ .authorizer+ .authorize_from_header_or_bearer(&headers, ACCESS_KEY_HEADER, Signal::Logs)+ .map_err(|rej| into_firehose_rejection(rej, &header_request_id))?+ .to_string();+ tracing::Span::current().record("token", token.as_str());++ let body_cap = service.firehose_max_request_body_size_bytes;+ let body = match gunzip_if_magic(&body, body_cap) {+ Ok(None) => body,+ Ok(Some(decompressed)) => Bytes::from(decompressed),+ Err((status, message)) => return Err(rejection(status, &header_request_id, &message)),+ };++ let request: FirehoseRequest = serde_json::from_slice(&body).map_err(|e| {+ error!("Failed to parse Firehose request: {e}");+ rejection(+ StatusCode::BAD_REQUEST,+ &header_request_id,+ &format!("body is not a Firehose HTTP endpoint request: {e}"),+ )+ })?;+ let request_id = if request.request_id.is_empty() {+ header_request_id+ } else {+ request.request_id.clone()+ };+ drop(body);++ let ctx = FirehoseContext {+ region: header_str(&headers, SOURCE_ARN_HEADER).and_then(region_from_source_arn),+ common_attributes: parse_common_attributes(header_str(&headers, COMMON_ATTRIBUTES_HEADER)),+ };++ let decoded_budget = body_cap.saturating_mul(DECODED_BUDGET_FACTOR);+ let mut decoded_total: usize = 0;+ let mut invalid_records: usize = 0;+ let mut total_events: u64 = 0;+ let mut batches = BatchBuilder::new(+ (service.sink.logs_message_max_bytes() / KAFKA_BATCH_BUDGET_DIVISOR) as u64,+ );+ let mut writes: JoinSet<anyhow::Result<()>> = JoinSet::new();+ let record_count = request.records.len();++ for (index, record) in request.records.into_iter().enumerate() {+ let (decoded, decoded_len) = match decode_record(&record.data, body_cap) {+ Ok(decoded) => decoded,+ Err((StatusCode::PAYLOAD_TOO_LARGE, message)) => {+ writes.abort_all();+ return Err(rejection(+ StatusCode::PAYLOAD_TOO_LARGE,+ &request_id,+ &format!("record {index}: {message}"),+ ));+ }+ Err((_, message)) => {+ // A record that can never decode would otherwise make Firehose retry the whole+ // request for its retry window, taking the good records down with it.+ counter!(RECORDS_COUNTER, "kind" => "invalid").increment(1);+ warn!("Skipping undecodable Firehose record {index}: {message}");+ invalid_records += 1;+ continue;+ }+ };+ decoded_total += decoded_len;+ if decoded_total > decoded_budget {+ writes.abort_all();+ return Err(rejection(+ StatusCode::PAYLOAD_TOO_LARGE,+ &request_id,+ &format!(+ "decoded records exceed {decoded_budget} bytes; lower the Firehose buffer size"+ ),+ ));+ }++ let Some(decoded_rows) = decoded_record_to_rows(decoded, &ctx) else {+ continue;+ };+ total_events += decoded_rows.len() as u64;+ for (row, overridden) in decoded_rows {+ if let Some(batch) = batches.push(row, overridden) {+ spawn_write(&mut writes, &service, &token, batch);+ }+ }+ }+ if let Some(batch) = batches.finish() {+ spawn_write(&mut writes, &service, &token, batch);+ }++ if invalid_records > 0 && invalid_records == record_count {+ return Err(rejection(+ StatusCode::BAD_REQUEST,+ &request_id,+ "no record could be decoded as a CloudWatch envelope or text",+ ));+ }++ while let Some(joined) = writes.join_next().await {+ let failed = match joined {+ Ok(Ok(())) => None,+ Ok(Err(e)) => Some(e.to_string()),+ Err(e) => Some(e.to_string()),+ };+ if let Some(e) = failed {+ writes.abort_all();+ error!("Failed to send Firehose batch to Kafka: {e}");+ return Err(rejection(+ StatusCode::INTERNAL_SERVER_ERROR,+ &request_id,+ "Internal server error",+ ));+ }+ }++ counter!(EVENTS_COUNTER).increment(total_events);+ debug!(+ "Accepted Firehose request with {} records ({} invalid) and {} log events",+ record_count, invalid_records, total_events+ );+ Ok(contract_body(&request_id, None))+}++#[cfg(test)]+mod tests {+ use super::*;+ use axum::body::Body;+ use common_compression::{compress_gzip, encode_base64};++ fn gzip_b64(data: &[u8]) -> String {+ encode_base64(&compress_gzip(data).unwrap())+ }++ fn envelope_json(message_type: &str) -> String {+ json!({+ "messageType": message_type,+ "owner": "123456789012",+ "logGroup": "/aws/lambda/checkout-api",+ "logStream": "2026/09/16/[$LATEST]abc",+ "subscriptionFilters": ["posthog"],+ "logEvents": [+ {"id": "1", "timestamp": Utc::now().timestamp_millis(), "message": "ERROR payment declined"},+ {"id": "2", "timestamp": Utc::now().timestamp_millis(), "message": "{\"level\":\"warning\",\"msg\":\"slow\"}"}+ ]+ })+ .to_string()+ }++ fn envelope() -> CloudWatchEnvelope {+ serde_json::from_str(&envelope_json("DATA_MESSAGE")).unwrap()+ }++ fn context() -> FirehoseContext {+ FirehoseContext {+ region: Some("us-east-1".to_string()),+ common_attributes: HashMap::from([("env".to_string(), json!("prod"))]),+ }+ }++ #[test]+ fn decode_record_reads_cloudwatch_envelopes_gzipped_or_plain() {+ let json = envelope_json("DATA_MESSAGE");+ for data in [gzip_b64(json.as_bytes()), encode_base64(json.as_bytes())] {+ let (decoded, len) = decode_record(&data, 1 << 20).unwrap();+ assert_eq!(len, json.len());+ match decoded {+ DecodedRecord::CloudWatch(env) => {+ assert_eq!(env.log_group, "/aws/lambda/checkout-api");+ assert_eq!(env.log_events.len(), 2);+ }+ DecodedRecord::Raw(_) => panic!("expected a CloudWatch envelope"),+ }+ }+ }++ #[test]+ fn decode_record_falls_back_to_raw_lines() {+ let data = encode_base64(b"2 123 eni-1 10.0.0.1 10.0.0.2 ACCEPT OK\nline two\n");+ match decode_record(&data, 1 << 20).unwrap().0 {+ DecodedRecord::Raw(text) => assert!(text.starts_with("2 123")),+ DecodedRecord::CloudWatch(_) => panic!("expected raw"),+ }+ }++ #[test]+ fn decode_record_maps_failures_to_firehose_statuses() {+ let big = vec![b'a'; 4096];+ assert_eq!(+ decode_record(&gzip_b64(&big), 1024).unwrap_err().0,+ StatusCode::PAYLOAD_TOO_LARGE+ );+ assert_eq!(+ decode_record("not base64!!", 1024).unwrap_err().0,+ StatusCode::BAD_REQUEST+ );+ }++ #[test]+ fn service_name_inference_table() {+ let cases = [+ ("/aws/lambda/checkout-api", "checkout-api"),+ ("/aws/rds/instance/prod-db/postgresql", "prod-db"),+ ("/aws/rds/cluster/prod-cluster/error", "prod-cluster"),+ (+ "/aws/ecs/containerinsights/web-cluster/performance",+ "web-cluster",+ ),+ ("/aws/ecs/web-cluster", "web-cluster"),+ ("/aws/eks/main/cluster", "main"),+ ("/aws/apigateway/abc123", "abc123"),+ ("/aws/codebuild/frontend", "frontend"),+ ("/aws/vpc/flowlogs", "vpc-flow-logs"),+ ("/aws/unknown", "/aws/unknown"),+ ("/aws/lambda/", "/aws/lambda/"),+ ("my-app-logs", "my-app-logs"),+ ];+ for (group, expected) in cases {+ assert_eq!(infer_service_name(group), expected, "{group}");+ }+ }++ #[test]+ fn severity_inference_table() {+ let cases = [+ ("ERROR payment declined", "error", 17),+ ("[WARN] slow query", "warn", 13),+ ("2026-09-16T10:00:00Z INFO started", "info", 9),+ ("abc-123 DEBUG cache miss", "debug", 5),+ ("FATAL: out of memory", "fatal", 21),+ ("CRIT disk full", "fatal", 21),+ ("{\"level\":\"warn\",\"msg\":\"x\"}", "warn", 13),+ ("{\"level\":\"warning\",\"msg\":\"x\"}", "warn", 13),+ ("{\"severity\":\"CRITICAL\"}", "fatal", 21),+ ("plain line with error later in it", "info", 9),+ ("Errors: 0", "info", 9),+ ("", "info", 9),+ ];+ for (message, text, number) in cases {+ assert_eq!(+ infer_severity(message),+ (text.to_string(), number),+ "{message}"+ );+ }+ }++ #[test]+ fn region_is_parsed_from_source_arn() {+ assert_eq!(+ region_from_source_arn("arn:aws:firehose:eu-central-1:123456789012:deliverystream/x"),+ Some("eu-central-1".to_string())+ );+ assert_eq!(region_from_source_arn("garbage"), None);+ }++ #[test]+ fn common_attributes_keep_json_types_and_accept_both_shapes() {+ let wrapped = parse_common_attributes(Some(+ r#"{"commonAttributes":{"env":"prod","team":"payments"}}"#,+ ));+ assert_eq!(wrapped["env"], json!("prod"));+ let flat = parse_common_attributes(Some(r#"{"env":"staging","count":3}"#));+ assert_eq!(flat["count"], json!(3));+ assert!(parse_common_attributes(Some("not json")).is_empty());+ assert!(parse_common_attributes(None).is_empty());++ let ctx = FirehoseContext {+ common_attributes: flat,+ ..FirehoseContext::default()+ };+ let template = ctx.template(None);+ assert_eq!(template.resource_attributes["count"], "3");+ assert_eq!(template.resource_attributes["env"], "\"staging\"");+ }++ #[test]+ fn cloudwatch_envelope_maps_to_rows_with_aws_attributes() {+ let mut env = envelope();+ env.log_events.push(CloudWatchLogEvent {+ id: String::new(),+ timestamp: Some(Utc::now().timestamp_millis() - 72 * 3600 * 1000),+ message: "old".to_string(),+ });++ let decoded = cloudwatch_envelope_to_rows(env, &context());++ assert_eq!(decoded.len(), 3);+ assert_eq!(decoded.iter().filter(|(_, clamped)| *clamped).count(), 1);+ let row = &decoded[0].0;+ assert_eq!(row.body, "ERROR payment declined");+ assert_eq!(row.service_name, "checkout-api");+ assert_eq!(row.severity_text, "error");+ assert_eq!(row.severity_number, 17);+ assert_eq!(row.resource_attributes["cloud.provider"], "\"aws\"");+ assert_eq!(+ row.resource_attributes["cloud.account.id"],+ "\"123456789012\""+ );+ assert_eq!(row.resource_attributes["cloud.region"], "\"us-east-1\"");+ assert_eq!(+ row.resource_attributes["aws.log.group.name"],+ "\"/aws/lambda/checkout-api\""+ );+ assert_eq!(row.resource_attributes["service.name"], "\"checkout-api\"");+ assert_eq!(row.resource_attributes["env"], "\"prod\"");+ assert_eq!(row.attributes["aws.log.event.id"], "\"1\"");+ assert_eq!(row.attributes["aws.subscription_filters"], "\"posthog\"");+ assert!(row.bytes_uncompressed.is_some());+ assert_eq!(decoded[1].0.severity_text, "warn");+ let old = &decoded[2].0;+ assert!(old.attributes.contains_key("$originalTimestamp"));+ assert!(!old.attributes.contains_key("aws.log.event.id"));+ }++ #[test]+ fn a_common_attribute_service_name_wins_for_column_and_attribute() {+ let ctx = FirehoseContext {+ common_attributes: HashMap::from([("service.name".to_string(), json!("payments"))]),+ ..FirehoseContext::default()+ };+ let decoded = cloudwatch_envelope_to_rows(envelope(), &ctx);+ assert_eq!(decoded[0].0.service_name, "payments");+ assert_eq!(+ decoded[0].0.resource_attributes["service.name"],+ "\"payments\""+ );+ // AWS facts are never overridden by common attributes.+ let ctx = FirehoseContext {+ common_attributes: HashMap::from([(+ "aws.log.group.name".to_string(),+ json!("spoofed"),+ )]),+ ..FirehoseContext::default()+ };+ let decoded = cloudwatch_envelope_to_rows(envelope(), &ctx);+ assert_eq!(+ decoded[0].0.resource_attributes["aws.log.group.name"],+ "\"/aws/lambda/checkout-api\""+ );+ }++ #[test]+ fn raw_record_yields_one_row_per_non_empty_line() {+ let decoded = raw_record_to_rows("a\n\nb\n", &FirehoseContext::default());+ assert_eq!(decoded.len(), 2);+ assert_eq!(decoded[0].0.service_name, "aws-firehose");+ }++ #[test]+ fn batch_builder_splits_by_row_bytes_and_never_drops_rows() {+ let one_row = || {+ raw_record_to_rows(&"x".repeat(50), &FirehoseContext::default())+ .remove(0)+ .0+ };+ let row_bytes = one_row().bytes_uncompressed.unwrap() as u64;+ let mut builder = BatchBuilder::new(row_bytes * 3);+ let mut batches = Vec::new();+ for _ in 0..7 {+ if let Some(batch) = builder.push(one_row(), false) {+ batches.push(batch);+ }+ }+ batches.extend(builder.finish());+ let sizes: Vec<usize> = batches.iter().map(|b| b.rows.len()).collect();+ assert_eq!(sizes, vec![3, 3, 1]);+ assert!(batches.iter().all(|b| b.bytes <= row_bytes * 3));++ // One row above the budget still goes out alone rather than being dropped.+ let mut small = BatchBuilder::new(1);+ assert!(small.push(one_row(), false).is_none());+ assert_eq!(small.finish().unwrap().rows.len(), 1);+ }++ #[test]+ fn contract_bodies_carry_request_id_and_timestamp() {+ let (status, Json(body)) = rejection(StatusCode::UNAUTHORIZED, "req-1", "nope");+ assert_eq!(status, StatusCode::UNAUTHORIZED);+ assert_eq!(body["requestId"], "req-1");+ assert!(body["timestamp"].is_i64());+ assert_eq!(body["errorMessage"], "nope");++ let converted = into_firehose_rejection(+ (+ StatusCode::PAYLOAD_TOO_LARGE,+ Json(json!({"error": "too big"})),+ ),+ "req-2",+ );+ assert_eq!(converted.0, StatusCode::PAYLOAD_TOO_LARGE);+ assert_eq!(converted.1 .0["errorMessage"], "too big");++ let Json(ok) = contract_body("req-3", None);+ assert_eq!(ok["requestId"], "req-3");+ assert!(ok.get("errorMessage").is_none());+ }++ #[tokio::test]+ async fn layer_rejections_are_reshaped_into_the_contract() {+ use axum::{routing::post, Router};+ use tower::ServiceExt;++ async fn too_large() -> Response {+ (StatusCode::PAYLOAD_TOO_LARGE, "length limit exceeded").into_response()+ }+ let app = Router::new()+ .route("/", post(too_large))+ .layer(axum::middleware::from_fn(shape_layer_rejections));+ let request = Request::builder()+ .method("POST")+ .uri("/")+ .header(REQUEST_ID_HEADER, "req-9")+ .body(Body::empty())+ .unwrap();++ let response = app.oneshot(request).await.unwrap();++ assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);+ let bytes = axum::body::to_bytes(response.into_body(), 4096)+ .await+ .unwrap();+ let body: Value = serde_json::from_slice(&bytes).unwrap();+ assert_eq!(body["requestId"], "req-9");+ assert!(body["errorMessage"]+ .as_str()+ .unwrap()+ .contains("buffer size"));+ }++ #[test]+ fn firehose_request_defaults_records_to_empty() {+ let request: FirehoseRequest = serde_json::from_str(r#"{"requestId":"abc"}"#).unwrap();+ assert_eq!(request.request_id, "abc");+ assert!(request.records.is_empty());+ }+}diff --git a/rust/capture-logs/src/endpoints/datadog.rs b/rust/capture-logs/src/endpoints/datadog.rsindex ba108fa761ae..8c4973eb29e2 100644--- a/rust/capture-logs/src/endpoints/datadog.rs+++ b/rust/capture-logs/src/endpoints/datadog.rs@@ -1,5 +1,5 @@use crate::authorizer::Signal;-use crate::log_record::{override_timestamp, KafkaLogRow};+use crate::log_record::{apply_timestamp_override, datetime_from_millis, KafkaLogRow};use crate::service::{decode_body_if_gzip_magic, Service};use axum::{extract::State,@@ -9,7 +9,7 @@ use axum::{};use base64::{engine::general_purpose::STANDARD as base64_standard, Engine};use bytes::Bytes;-use chrono::{TimeZone, Utc};+use chrono::Utc;use serde::{Deserialize, Deserializer};use serde_json::json;use std::collections::HashMap;@@ -172,16 +172,8 @@ pub fn datadog_log_to_kafka_row(let status = log.status.as_deref().or(query_params.status.as_deref());let (severity_text, severity_number) = normalize_datadog_severity(status);- let raw_timestamp = log- .timestamp- .and_then(|ts| {- // Datadog uses milliseconds since epoch- Utc.timestamp_millis_opt(ts).single()- })- .unwrap_or_else(Utc::now);-- let (timestamp, original_timestamp) = override_timestamp(raw_timestamp);- let was_overridden = original_timestamp.is_some();+ // Datadog uses milliseconds since epoch+ let raw_timestamp = datetime_from_millis(log.timestamp);let service = log.service.or_else(|| query_params.service.clone());let hostname = log.hostname.or_else(|| query_params.hostname.clone());@@ -237,9 +229,7 @@ pub fn datadog_log_to_kafka_row(attributes.insert(key.clone(), value.to_string());}- if let Some(original) = original_timestamp {- attributes.insert("$originalTimestamp".to_string(), original.to_rfc3339());- }+ let (timestamp, was_overridden) = apply_timestamp_override(raw_timestamp, &mut attributes);let row = KafkaLogRow {uuid: Uuid::now_v7().to_string(),diff --git a/rust/capture-logs/src/endpoints/mod.rs b/rust/capture-logs/src/endpoints/mod.rsindex 7c5ecac44a03..bd3a2f946dd1 100644--- a/rust/capture-logs/src/endpoints/mod.rs+++ b/rust/capture-logs/src/endpoints/mod.rs@@ -1,2 +1,3 @@+pub mod aws_firehose;pub mod datadog;pub mod prometheus;diff --git a/rust/capture-logs/src/kafka.rs b/rust/capture-logs/src/kafka.rsindex 4e9d61992f48..1b387f422846 100644--- a/rust/capture-logs/src/kafka.rs+++ b/rust/capture-logs/src/kafka.rs@@ -16,6 +16,7 @@ use rdkafka::producer::{FutureProducer, FutureRecord, Producer};use rdkafka::util::Timeout;use rdkafka::ClientConfig;use std::result::Result::Ok;+use std::sync::Arc;use std::time::Duration;use tracing::log::{debug, info};@@ -117,6 +118,12 @@ pub struct KafkaSink {logs_topic: String,traces_topic: String,metrics_topic: String,+ // Shared, because `KafkaSink` is cloned into every request's state and a `Schema` clone is+ // a few hundred allocations.+ logs_schema: Arc<Schema>,+ traces_schema: Arc<Schema>,+ metrics_schema: Arc<Schema>,+ logs_message_max_bytes: usize,}#[allow(clippy::too_many_arguments)]@@ -323,9 +330,19 @@ impl KafkaSink {logs_topic: config.kafka_topic,traces_topic: config.kafka_traces_topic,metrics_topic: config.kafka_metrics_topic,+ logs_schema: Arc::new(Schema::parse_str(AVRO_SCHEMA)?),+ traces_schema: Arc::new(Schema::parse_str(TRACES_AVRO_SCHEMA)?),+ metrics_schema: Arc::new(Schema::parse_str(METRICS_AVRO_SCHEMA)?),+ logs_message_max_bytes: config.kafka_producer_message_max_bytes as usize,})}+ /// The producer's `message.max.bytes` for the logs topic, so an intake that chunks a large+ /// delivery can size its batches against the real cap instead of a guess.+ pub fn logs_message_max_bytes(&self) -> usize {+ self.logs_message_max_bytes+ }+pub fn flush(&self) -> Result<(), KafkaError> {// TODO: hook it up on shutdownself.logs_producer.flush(Duration::new(30, 0))?;@@ -338,16 +355,15 @@ impl KafkaSink {&self,producer: &FutureProducer<KafkaContext>,topic: &str,- avro_schema_str: &str,+ schema: &Schema,token: &str,rows: &[T],uncompressed_bytes: u64,records_uncompressed_bytes: Option<u64>,timestamps_overridden: u64,) -> Result<(), anyhow::Error> {- let schema = Schema::parse_str(avro_schema_str)?;let mut writer = Writer::with_codec(- &schema,+ schema,Vec::new(),Codec::Zstandard(ZstandardSettings::new(1)),);@@ -442,7 +458,7 @@ impl KafkaSink {self.write_avro_batch(&self.logs_producer,&self.logs_topic,- AVRO_SCHEMA,+ &self.logs_schema,token,&rows,uncompressed_bytes,@@ -472,7 +488,7 @@ impl KafkaSink {self.write_avro_batch(&self.traces_producer,&self.traces_topic,- TRACES_AVRO_SCHEMA,+ &self.traces_schema,token,&rows,uncompressed_bytes,@@ -502,7 +518,7 @@ impl KafkaSink {self.write_avro_batch(&self.metrics_producer,&self.metrics_topic,- METRICS_AVRO_SCHEMA,+ &self.metrics_schema,token,&rows,uncompressed_bytes,diff --git a/rust/capture-logs/src/log_record.rs b/rust/capture-logs/src/log_record.rsindex 2ddda7174409..c5bad4622bae 100644--- a/rust/capture-logs/src/log_record.rs+++ b/rust/capture-logs/src/log_record.rs@@ -81,14 +81,16 @@ pub fn compute_kafka_log_row_bytes(row: &KafkaLogRow) -> i64 {/// payload-sized `bytes_uncompressed` header — so the two can be compared before billing/// switches to the records-based value.pub fn sum_kafka_log_row_bytes(rows: &[KafkaLogRow]) -> u64 {- rows.iter()- .map(|row| row.bytes_uncompressed.unwrap_or(0).max(0) as u64)- .sum()+ rows.iter().map(KafkaLogRow::byte_count).sum()}impl KafkaLogRow {/// Set `bytes_uncompressed` from the row's variable-length content. Consuming/// builder; the `mut self` is encapsulated and never escapes.+ pub(crate) fn byte_count(&self) -> u64 {+ self.bytes_uncompressed.unwrap_or(0).max(0) as u64+ }+pub(crate) fn with_computed_bytes(mut self) -> Self {self.bytes_uncompressed = Some(compute_kafka_log_row_bytes(&self));self@@ -113,7 +115,7 @@ impl KafkaLogRow {let mut severity_number = record.severity_number;if let Some(parsed_severity) = try_extract_severity(&body) {- severity_text = parsed_severity;+ severity_text = parsed_severity.to_string();severity_number = convert_severity_text_to_number(&severity_text);}@@ -159,11 +161,7 @@ impl KafkaLogRow {_ => DateTime::<Utc>::from_timestamp_nanos(record.time_unix_nano.try_into()?),};- let (timestamp, original_timestamp) = override_timestamp(raw_timestamp);- let was_overridden = original_timestamp.is_some();- if let Some(original) = original_timestamp {- attributes.insert("$originalTimestamp".to_string(), original.to_rfc3339());- }+ let (timestamp, was_overridden) = apply_timestamp_override(raw_timestamp, &mut attributes);let observed_timestamp = Utc::now();@@ -209,6 +207,25 @@ pub fn override_timestamp(timestamp: DateTime<Utc>) -> (DateTime<Utc>, Option<Da}}+/// `override_timestamp` plus the `$originalTimestamp` attribute the consumer reads back; every intake+/// goes through here so the attribute name and format stay one contract.+pub(crate) fn apply_timestamp_override(+ raw: DateTime<Utc>,+ attributes: &mut HashMap<String, String>,+) -> (DateTime<Utc>, bool) {+ let (timestamp, original) = override_timestamp(raw);+ if let Some(original) = original {+ attributes.insert("$originalTimestamp".to_string(), original.to_rfc3339());+ }+ (timestamp, original.is_some())+}++pub(crate) fn datetime_from_millis(millis: Option<i64>) -> DateTime<Utc> {+ millis+ .and_then(DateTime::<Utc>::from_timestamp_millis)+ .unwrap_or_else(Utc::now)+}+// extract a JSON value as a string. If it's a string, strip the surrounding "quotes"fn extract_string_from_map(attributes: &HashMap<String, String>, key: &str) -> String {if let Some(value) = attributes.get(key) {@@ -242,20 +259,26 @@ pub fn extract_span_id(input: &[u8]) -> [u8; 8] {}}-fn normalize_severity_text(severity_text: String) -> String {- match severity_text.to_lowercase().as_str() {- "critical" | "fatal" | "crit" | "alert" | "emerg" => "fatal".to_string(),- "error" | "err" | "eror" => "error".to_string(),- "warn" | "warning" => "warn".to_string(),- "info" | "information" | "informational" => "info".to_string(),- "debug" | "dbug" => "debug".to_string(),- "trace" => "trace".to_string(),- // don't allow arbitrary values in severity text. normalize unknown to info- _ => "info".to_string(),- }+fn severity_for_lowercase(lowered: &str) -> Option<&'static str> {+ Some(match lowered {+ "critical" | "fatal" | "crit" | "alert" | "emerg" => "fatal",+ "error" | "err" | "eror" => "error",+ "warn" | "warning" => "warn",+ "info" | "information" | "informational" => "info",+ "debug" | "dbug" => "debug",+ "trace" => "trace",+ _ => return None,+ })+}++pub(crate) fn normalize_severity_text(severity_text: String) -> String {+ // don't allow arbitrary values in severity text. normalize unknown to info+ severity_for_lowercase(&severity_text.to_lowercase())+ .unwrap_or("info")+ .to_string()}-fn convert_severity_text_to_number(severity_text: &str) -> i32 {+pub(crate) fn convert_severity_text_to_number(severity_text: &str) -> i32 {match severity_text {"trace" => 1,"debug" => 5,@@ -319,25 +342,30 @@ pub fn extract_resource_attributes(resource: Option<Resource>) -> HashMap<String// TODO - pull this from PGconst SEVERITY_KEYS: [&str; 4] = ["level", "severity", "log.level", "config.log_level"];-fn try_extract_severity(body: &str) -> Option<String> {+pub(crate) fn try_extract_severity(body: &str) -> Option<&'static str> {+ if !body.trim_start().starts_with('{') {+ return None;+ }let Ok(val) = serde_json::from_str::<JsonValue>(body) else {return None;};for key in SEVERITY_KEYS {- if let Some(severity) = val.get(key) {- let Some(found) = severity.as_str() else {- continue;- };- let found = found.to_lowercase();- if convert_severity_text_to_number(&found) != 0 {- return Some(found);+ if let Some(found) = val.get(key).and_then(|severity| severity.as_str()) {+ if let Some(text) = severity_alias(found) {+ return Some(text);}}}None}+/// Canonical severity for a level word, or `None` when the word is not a known level. Unlike+/// `normalize_severity_text`, an unknown word is not folded to `info`, so callers can keep looking.+pub(crate) fn severity_alias(word: &str) -> Option<&'static str> {+ severity_for_lowercase(&word.trim().to_lowercase())+}+pub fn any_value_to_json(value: AnyValue) -> JsonValue {match value.value {Some(value_enum) => match value_enum {diff --git a/rust/capture-logs/src/main.rs b/rust/capture-logs/src/main.rsindex 8d85bc36ec85..67a528e501c4 100644--- a/rust/capture-logs/src/main.rs+++ b/rust/capture-logs/src/main.rs@@ -5,6 +5,7 @@ use axum::{extract::DefaultBodyLimit, http::Method, routing::get, routing::post,use capture::metrics_middleware::track_metrics;use capture_logs::authorizer::Authorizer;use capture_logs::config::Config;+use capture_logs::endpoints::aws_firehose;use capture_logs::endpoints::datadog;use capture_logs::endpoints::prometheus;use capture_logs::kafka::KafkaSink;@@ -120,14 +121,20 @@ async fn main() {let token_dropper = TokenDropper::new(&config.drop_events_by_token.unwrap_or_default());let authorizer = Authorizer::new(Arc::new(token_dropper));- let logs_service =- match Service::new(kafka_sink, authorizer, config.max_request_body_size_bytes).await {- Ok(service) => service,- Err(e) => {- error!("Failed to initialize log service: {}", e);- panic!("Could not start log capture service: {e}");- }- };+ let logs_service = match Service::new(+ kafka_sink,+ authorizer,+ config.max_request_body_size_bytes,+ config.firehose_max_request_body_size_bytes,+ )+ .await+ {+ Ok(service) => service,+ Err(e) => {+ error!("Failed to initialize log service: {}", e);+ panic!("Could not start log capture service: {e}");+ }+ };let http_bind = format!("{}:{}", config.host, config.port);info!("Listening on {}", http_bind);let http_listener = tokio::net::TcpListener::bind(http_bind)@@ -201,11 +208,32 @@ async fn main() {"/i/v1/prometheus/write/:token",post(prometheus::export_prometheus_remote_write_http).options(options_handler),)- .with_state(logs_service)+ .with_state(logs_service.clone()).layer(DefaultBodyLimit::max(config.max_request_body_size_bytes)).layer(axum::middleware::from_fn(track_metrics));- let http_router = http_router.merge(prometheus_router).layer(cors);+ // Amazon Data Firehose HTTP endpoint destination. Its own router because Firehose buffers up to+ // 64 MiB per delivery, so the body cap is separate, and because Firehose only understands a+ // JSON error body: the outermost layer re-shapes rejections raised by the layers below it.+ let firehose_router = Router::new()+ .route(+ "/i/v1/logs/aws/firehose",+ post(aws_firehose::export_aws_firehose_logs_http).options(options_handler),+ )+ .with_state(logs_service)+ .layer(DefaultBodyLimit::max(+ config.firehose_max_request_body_size_bytes,+ ))+ .layer(axum::middleware::from_fn(track_metrics))+ .layer(RequestDecompressionLayer::new())+ .layer(axum::middleware::from_fn(+ aws_firehose::shape_layer_rejections,+ ));++ let http_router = http_router+ .merge(prometheus_router)+ .merge(firehose_router)+ .layer(cors);let http_server = tokio::spawn(async move {if let Err(e) = axum::serve(diff --git a/rust/capture-logs/src/service.rs b/rust/capture-logs/src/service.rsindex 4a3072167144..31e300fbaf26 100644--- a/rust/capture-logs/src/service.rs+++ b/rust/capture-logs/src/service.rs@@ -304,6 +304,7 @@ pub struct Service {pub(crate) sink: KafkaSink,pub(crate) authorizer: Authorizer,pub(crate) max_request_body_size_bytes: usize,+ pub(crate) firehose_max_request_body_size_bytes: usize,}#[derive(Deserialize)]@@ -316,41 +317,53 @@ impl Service {kafka_sink: KafkaSink,authorizer: Authorizer,max_request_body_size_bytes: usize,+ firehose_max_request_body_size_bytes: usize,) -> Result<Self, anyhow::Error> {Ok(Self {sink: kafka_sink,authorizer,max_request_body_size_bytes,+ firehose_max_request_body_size_bytes,})}}-pub(crate) fn decode_body_if_gzip_magic(- body: Bytes,- max_request_body_size_bytes: usize,-) -> Result<Bytes, (StatusCode, Json<serde_json::Value>)> {- if !has_gzip_magic_header(&body) {- return Ok(body);+/// Gunzip `bytes` when they start with the gzip magic header, capped at `limit` decoded bytes.+/// `Ok(None)` means the input was not gzip and can be used as is.+pub(crate) fn gunzip_if_magic(+ bytes: &[u8],+ limit: usize,+) -> Result<Option<Vec<u8>>, (StatusCode, String)> {+ if !has_gzip_magic_header(bytes) {+ return Ok(None);}-- match decompress_gzip_capped(&body, max_request_body_size_bytes) {- Ok(decompressed) => Ok(Bytes::from(decompressed)),+ match decompress_gzip_capped(bytes, limit) {+ Ok(decompressed) => Ok(Some(decompressed)),Err(CompressionError::OutputTooLarge {decompressed,limit,}) => Err((StatusCode::PAYLOAD_TOO_LARGE,- Json(json!({- "error": format!("Decompressed request body exceeds limit ({decompressed} > {limit} bytes)")- })),+ format!("Decompressed request body exceeds limit ({decompressed} > {limit} bytes)"),)),Err(e) => Err((StatusCode::BAD_REQUEST,- Json(json!({"error": format!("Failed to decompress gzip request body: {e}")})),+ format!("Failed to decompress gzip request body: {e}"),)),}}+pub(crate) fn decode_body_if_gzip_magic(+ body: Bytes,+ max_request_body_size_bytes: usize,+) -> Result<Bytes, (StatusCode, Json<serde_json::Value>)> {+ match gunzip_if_magic(&body, max_request_body_size_bytes) {+ Ok(None) => Ok(body),+ Ok(Some(decompressed)) => Ok(Bytes::from(decompressed)),+ Err((status, message)) => Err((status, Json(json!({"error": message})))),+ }+}+#[instrument(skip_all, fields(token = tracing::field::Empty,content_type = %headers.get("content-type")