Skip to content
betterstack-alchemyUnofficial

Telemetry.IngestLogs

Kind BindingType id BetterStack.Telemetry.IngestLogsSource src/Telemetry/IngestLogs.ts

Ship log events from running code into a Source.

When to use it: for application telemetry prefer Effect’s own logs and spans exported with Telemetry.Export (logs travel with their trace). IngestLogs is for events that are not OpenTelemetry — an audit record with your own fields, a batch forwarded from elsewhere — or a host where the export layer is not available.

Right after the source is created. A new source’s ingesting host refuses its token for a few seconds (401); Telemetry.Source only reports a create done once the token is accepted, and every host that binds the source is deployed after it, so a host in the same stack never sees that window.

Credential. The host receives the source’s own ingest token (Redacted, a secret on every host) and its ingestingHost, as BETTERSTACK_SOURCE_<SOURCE>_TOKEN / …_INGESTING_HOST. That is the least Better Stack offers: a source token can only write into that one source. It cannot read logs, touch another source or manage anything. No API token reaches the host.

Cost. Every event counts against the team’s log ingestion volume and retention; a 402 means the quota is used up. Batch events (pass a list) rather than one request per line, or use ShipLogs to batch Effect’s own logs.

Errors: Forbidden when the token is wrong (the source was replaced, or deleted); the SDK’s typed errors for 402/406/413.

Provide IngestLogsHttp on the host’s implementation.

Bind a source and send structured events

export const AppLogs = BetterStack.Telemetry.Source("AppLogs", { platform: "http" });
export default class Api extends BetterStack.Cloud.Service<Api>()(
"Api",
{ server: Box, main: import.meta.url, port: 3000 },
Effect.gen(function* () {
const logs = yield* BetterStack.Telemetry.IngestLogs(AppLogs);
return {
fetch: Effect.gen(function* () {
yield* logs([{ message: "order placed", orderId: "o-1" }]);
return HttpServerResponse.text("ok");
}).pipe(Effect.orDie),
};
}).pipe(Effect.provide(BetterStack.Telemetry.IngestLogsHttp)),
) {}

The binding writes its two values through Alchemy’s RuntimeContext, so the same lines work in a Cloudflare Worker, a Lambda Function or a container: the token becomes that host’s secret (secret_text on a Worker, an encrypted variable on Lambda).

In a Cloudflare Worker

const logs = yield* BetterStack.Telemetry.IngestLogs(AppLogs);
// ...in the fetch handler:
yield* logs({ message: "request", path: url.pathname });