Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/local/src/App.integration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async function sendEnvelope(
headers: { "Content-Type": SENTRY_CONTENT_TYPE },
body: envelope(transaction, options),
});
expect(response.status).toBe(204);
expect(response.status).toBe(200);
}

async function sendLog(
Expand All @@ -124,7 +124,7 @@ async function sendLog(
"",
].join("\n"),
});
expect(response.status).toBe(204);
expect(response.status).toBe(200);
}

function renderViewer(port: number, path = "/") {
Expand Down Expand Up @@ -493,7 +493,7 @@ describe("local receiver to viewer integration", () => {
"\n",
),
});
expect(response.status).toBe(204);
expect(response.status).toBe(200);
const row = await screen.findByRole("button", {
name: /^View event:/,
});
Expand Down Expand Up @@ -845,7 +845,7 @@ describe("local receiver to viewer integration", () => {
headers: { "Content-Type": SENTRY_CONTENT_TYPE },
body: `{}\n${JSON.stringify(header)}\n${content}\n`,
});
expect(response.status).toBe(204);
expect(response.status).toBe(200);
const wire = JSON.parse(await received) as [
unknown,
[[unknown, { data: string }]],
Expand Down
2 changes: 1 addition & 1 deletion apps/local/src/App.sessions.integration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function startDaemon(now = Date.now, preferredPort = 0) {
body: `${JSON.stringify({ event_id: crypto.randomUUID() })}\n{"type":"transaction"}\n${JSON.stringify({ transaction })}\n`,
})
).status,
).toBe(204);
).toBe(200);
};
return {
port,
Expand Down
2 changes: 2 additions & 0 deletions docs/cli/local-control-plane.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ segments, like the tail's `[N spans]`: the transaction or segment span itself an
sampled function rows are not counted.

Receiver ingestion bounds encoded bodies to 10 MiB and decoded bodies to 32 MiB.
Accepted envelopes get 200 with `{"id":"<event_id>"}`, or `{}` when the header
has no event ID, as Relay answers; sentry-elixir retries anything else.
Invalid compression returns 400, oversized bodies 413, unsupported media or
encoding 415, and temporary admission overload 503. SDKs drop an envelope that
gets 413 without retrying, so `local run` and `local serve` print a warning in
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/commands/local/control-plane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Buffer } from "node:buffer";
import {
drainReceiver,
ingestReceiverEnvelope,
ingestResponse,
onOversizedEnvelope,
readReceiverBody,
receiverEnvelopeId,
Expand Down Expand Up @@ -509,7 +510,7 @@ export function createLocalControlPlane({
// Close can occur while the body is being read or decoded. Tracking the
// request lets close drain it, so a 413 reaches readers before the closing
// frame. Retention still stops at the close.
const status = await trackReceiverRequest(buffer, async () => {
const outcome = await trackReceiverRequest(buffer, async () => {
const body = await readReceiverBody(c.req.raw, MAX_BODY_BYTES);
const result =
typeof body === "number"
Expand All @@ -530,10 +531,9 @@ export function createLocalControlPlane({
reportIngestStatus(buffer, result);
return result;
});
if (status === 410) return closed();
if (status === 503) c.header("Retry-After", "1");
if (status === 204) sessionStore.recordActivity(reference);
return c.body(null, status);
if (outcome === 410) return closed();
if (typeof outcome === "object") sessionStore.recordActivity(reference);
return ingestResponse(c, outcome);
};
for (const path of SESSION_INGEST_PATHS) app.post(path, ingest);
app.post(`${CONTROL_PLANE_PREFIX}/sessions/:id/stream`, ingest);
Expand Down
40 changes: 30 additions & 10 deletions packages/cli/src/commands/local/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,33 @@ export function withoutOwnDeliveries(
* the read capability, so drop the key. Only the header line is rewritten.
*/
export function withoutDsnHeader(body: Buffer): Buffer {
// Only the first line is needed, so skip frame(): it would parse every item
// and reject envelopes that still have a usable header.
const parsed = readEnvelopeHeader(body);
if (!parsed || !("dsn" in parsed.header)) return body;
delete parsed.header["dsn"];
return Buffer.concat([
Buffer.from(JSON.stringify(parsed.header)),
body.subarray(parsed.end),
]);
}

/**
* The envelope header's `event_id` as Relay prints it: 32 lowercase hex digits
* without hyphens. Undefined when the header has no valid event id.
*/
export function envelopeEventId(body: Buffer): string | undefined {
const eventId = readEnvelopeHeader(body)?.header["event_id"];
if (typeof eventId !== "string") return;
const simple = eventId.replaceAll("-", "").toLowerCase();
return /^[0-9a-f]{32}$/.test(simple) ? simple : undefined;
}

/**
* Parse only the first line, without frame(): it would parse every item and
* reject envelopes that still have a usable header.
*/
function readEnvelopeHeader(
body: Buffer,
): { header: Record<string, unknown>; end: number } | undefined {
const newline = body.indexOf(10);
const end = newline < 0 ? body.length : newline;
let header: unknown;
Expand All @@ -433,15 +458,10 @@ export function withoutDsnHeader(body: Buffer): Buffer {
);
} catch {
// Malformed envelopes stay byte-exact for raw inspection.
logger.debug("Local receiver kept an undecodable envelope header.");
return body;
logger.debug("Local receiver could not parse an envelope header.");
return;
}
if (!isEnvelopeObject(header) || !("dsn" in header)) return body;
delete header["dsn"];
return Buffer.concat([
Buffer.from(JSON.stringify(header)),
body.subarray(end),
]);
return isEnvelopeObject(header) ? { header, end } : undefined;
}

/** Retain raw bytes synchronously; decode lazily without allowing parse errors out. */
Expand Down
31 changes: 25 additions & 6 deletions packages/cli/src/commands/local/receiver-ingest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/** Bounded HTTP decoding and raw delivery identity for the Local receiver. */
import { brotliDecompress, gunzip, inflate } from "node:zlib";
import type { Context } from "hono";
import { logger } from "../../lib/logger.js";
import {
createReceiverEnvelope,
envelopeEventId,
type ReceiverBuffer,
withoutDsnHeader,
withoutOwnDeliveries,
Expand All @@ -25,6 +27,11 @@ type ReceiverState = {
/** Requests still reading their body or reporting their status. */
requests: Set<Promise<unknown>>;
};
/**
* Relay's body for an accepted envelope. SDKs such as sentry-elixir accept a
* delivery only on HTTP 200 with this JSON, and retry anything else.
*/
type ReceiverAccepted = { id?: string };
const receivers = new WeakMap<ReceiverBuffer, ReceiverState>();
const oversizedListeners = new WeakMap<ReceiverBuffer, Set<() => void>>();

Expand All @@ -45,12 +52,22 @@ export function onOversizedEnvelope(
/** Tell this receiver's listeners when a delivery got HTTP 413. */
export function reportIngestStatus(
buffer: ReceiverBuffer,
status: number,
result: number | ReceiverAccepted,
): void {
if (status !== 413) return;
if (result !== 413) return;
for (const listener of oversizedListeners.get(buffer) ?? []) listener();
}

/** Answer an ingest request: 200 with the accepted JSON, otherwise the bare status. */
export function ingestResponse(
c: Context,
result: ReceiverAccepted | 400 | 410 | 413 | 415 | 503,
): Response {
if (typeof result === "object") return c.json(result);
if (result === 503) c.header("Retry-After", "1");
return c.body(null, result);
}

/** Stop consuming chunked input as soon as its encoded body exceeds the limit. */
export async function readReceiverBody(
request: Request,
Expand Down Expand Up @@ -158,7 +175,7 @@ export function ingestReceiverEnvelope(
*/
closed?: boolean;
},
): Promise<204 | 400 | 410 | 413 | 415 | 503> {
): Promise<ReceiverAccepted | 400 | 410 | 413 | 415 | 503> {
const contentType = input.contentType?.split(";")[0]?.trim().toLowerCase();
if (
contentType &&
Expand All @@ -173,7 +190,7 @@ export function ingestReceiverEnvelope(
if (state.pending.size >= MAX_PENDING) return Promise.resolve(503);
const generation = state.generation;
const operation = state.queue.then(
async (): Promise<204 | 400 | 410 | 413 | 503> => {
async (): Promise<ReceiverAccepted | 400 | 410 | 413 | 503> => {
if (generation !== state.generation) return 503;
let body = input.body;
if (encoding && encoding !== "identity") {
Expand All @@ -200,20 +217,22 @@ export function ingestReceiverEnvelope(
if (generation !== state.generation) return 503;
if (body.length > MAX_DECODED_BYTES) return 413;
if (input.closed) return 410;
const eventId = envelopeEventId(body);
const accepted = eventId ? { id: eventId } : {};
if (input.dropDsnHeader) body = withoutDsnHeader(body);
const delivery = input.receiverUrl
? withoutOwnDeliveries(body, input.receiverUrl)
: body;
// Envelopes with only Local's own delivery telemetry use no retention.
if (!delivery) return 204;
if (!delivery) return accepted;
buffer.put(
createReceiverEnvelope({
body: delivery,
contentType: CONTENT_TYPE,
userAgent: input.userAgent,
}),
);
return 204;
return accepted;
},
);
state.pending.add(operation);
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/commands/local/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { setTimeout as sleep } from "node:timers/promises";
import { serve } from "@hono/node-server";
import {
ingestReceiverEnvelope,
ingestResponse,
MAX_DECODED_BYTES,
onOversizedEnvelope,
readReceiverBody,
Expand Down Expand Up @@ -516,17 +517,16 @@ export function buildApp(
const contentEncoding = c.req.header("content-encoding");
const userAgent = c.req.header("user-agent");

const status = await ingestReceiverEnvelope(buffer, {
const result = await ingestReceiverEnvelope(buffer, {
body,
encoding: contentEncoding,
contentType,
receiverUrl: c.req.url,
userAgent,
});

reportIngestStatus(buffer, status);
if (status === 503) c.header("Retry-After", "1");
return c.body(null, status);
reportIngestStatus(buffer, result);
return ingestResponse(c, result);
};

app.post("/stream", ingest);
Expand Down
54 changes: 45 additions & 9 deletions packages/cli/test/commands/local/control-plane.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,42 @@ describe("Local control plane", () => {
);
});

test("session ingest answers like Relay on every ingest path", async () => {
const plane = createLocalControlPlane({ controlToken: CONTROL_TOKEN });
const created = await plane.app.request("/_local/v1/sessions", {
method: "POST",
headers: controlHeaders(),
body: JSON.stringify({ label: "phoenix", cwd: "/phoenix" }),
});
const { ingestUrl } = await created.json();
const id = "0b6e5f9cb8d44f4e9a6f3e1c2d7a8b90";
const envelope = `{"event_id":"${id}"}\n{"type":"event"}\n{"message":"boom"}`;
for (const url of [
ingestUrl,
`${ingestUrl}/stream`,
`${ingestUrl}/api/0/envelope/`,
`${ingestUrl}/api/0/envelope`,
]) {
const response = await plane.app.request(url, {
method: "POST",
headers: { "Content-Type": "application/x-sentry-envelope" },
body: envelope,
});
expect(response.status).toBe(200);
expect(await response.json()).toEqual({ id });
}
const gzipped = await plane.app.request(ingestUrl, {
method: "POST",
headers: {
"Content-Type": "application/x-sentry-envelope",
"Content-Encoding": "gzip",
},
body: gzipSync('{}\n{"type":"log"}\n{"items":[]}'),
});
expect(gzipped.status).toBe(200);
expect(await gzipped.json()).toEqual({});
});

test("replaces expired clientId sessions with usable fresh capabilities on creation", async () => {
let now = 1000;
const sessionStore = createLocalSessionStore({ now: () => now });
Expand Down Expand Up @@ -267,7 +303,7 @@ describe("Local control plane", () => {
headers: { "Content-Type": "application/x-sentry-envelope" },
body: '{}\n{"type":"event"}\n{"event_id":"fresh","message":"fresh capture"}',
});
expect(ingested.status).toBe(204);
expect(ingested.status).toBe(200);
expect(
sessionStore.getBuffer(replacement.id).read({ all: true }),
).toHaveLength(1);
Expand Down Expand Up @@ -546,7 +582,7 @@ describe("Local control plane", () => {
headers: { "Content-Type": "application/x-sentry-envelope" },
body: envelope,
});
expect(ingested.status).toBe(204);
expect(ingested.status).toBe(200);
expect(
controlPlane.sessionStore.getBuffer(session.id).read({ all: true }),
).toHaveLength(1);
Expand Down Expand Up @@ -579,7 +615,7 @@ describe("Local control plane", () => {
// `<dsn path>/api/<project>/envelope/` from a DSN.
for (const suffix of ["/stream", "/api/0/envelope/", "/api/0/envelope"]) {
expect((await post(`${ingest}/${caps.ingest}${suffix}`)).status).toBe(
204,
200,
);
expect((await post(`${ingest}/wrong${suffix}`)).status).toBe(401);
expect((await post(`${ingest}/${caps.stream}${suffix}`)).status).toBe(
Expand Down Expand Up @@ -640,7 +676,7 @@ describe("Local control plane", () => {
body,
},
);
expect(posted.status).toBe(204);
expect(posted.status).toBe(200);
}

const stored = plane.sessionStore
Expand Down Expand Up @@ -838,7 +874,7 @@ describe("Local control plane", () => {
.join("\n"),
},
);
expect(response.status).toBe(204);
expect(response.status).toBe(200);
}

const listed = await plane.app.request(
Expand Down Expand Up @@ -876,7 +912,7 @@ describe("Local control plane", () => {
body: lines.map((line) => JSON.stringify(line)).join("\n"),
},
);
expect(response.status).toBe(204);
expect(response.status).toBe(200);
};
const before = Date.now();

Expand Down Expand Up @@ -1088,7 +1124,7 @@ describe("Local control plane", () => {
.join("\n"),
},
);
expect(response.status).toBe(204);
expect(response.status).toBe(200);
const listed = await plane.app.request(
`/_local/v1/sessions/${session.id}/events`,
{ headers: controlHeaders() },
Expand Down Expand Up @@ -1264,7 +1300,7 @@ describe("Local daemon receiver compatibility", () => {
),
},
);
expect(response.status).toBe(204);
expect(response.status).toBe(200);
const events = await plane.app.request(
`/_local/v1/sessions/${session.id}/events/compressed`,
{ headers: controlHeaders() },
Expand Down Expand Up @@ -1292,7 +1328,7 @@ describe("Local daemon receiver compatibility", () => {
body: '{"event_id":"browser"}\n{"type":"event"}\n{"message":"browser SDK"}',
},
);
expect(ingested.status).toBe(204);
expect(ingested.status).toBe(200);
const response = await plane.app.request(
`/_local/v1/sessions/${session.id}/events/browser`,
{ headers: controlHeaders() },
Expand Down
Loading
Loading