OpenTelemetry
CloudWatch’s built-in metrics describe your resources from the outside. To see inside your code, its spans, structured logs and custom metrics, export OpenTelemetry (OTel). Effect records all three as your code runs, and AWS.CloudWatch.Telemetry sends them to CloudWatch’s OTel endpoints, one per signal, in the stack’s region.
Each export is signed with the runtime’s own AWS identity, the same way an AWS SDK call is. There’s no ingest token to create, store or rotate.
- signs as
- its IAM role
Export from a Function
Section titled “Export from a Function”Provide the layer on the Function, and say which signals to send:
import * as AWS from "@alchemy.run/aws";import * as Effect from "effect/Effect";import * as HttpServerResponse from "effect/http/HttpServerResponse";import * as Metric from "effect/Metric";import { ApiLogs } from "./logs.ts";
const checkouts = Metric.counter("checkouts_total");
export default class Api extends AWS.Lambda.Function<Api>()( "Api", { main: import.meta.url }, Effect.gen(function* () { return { fetch: Effect.gen(function* () { yield* Metric.update(checkouts, 1); yield* Effect.logInfo("checkout complete"); return HttpServerResponse.text("ok"); }).pipe(Effect.withSpan("checkout")), }; }).pipe( Effect.provide(AWS.CloudWatch.Telemetry({ traces: true, metrics: true, logs: ApiLogs })), ),) {}The layer gives Api’s role permission to send each signal it exports, and nothing more: xray:PutTraceSegments for traces, cloudwatch:PutMetricData for metrics, and logs:PutLogEvents on the one log stream. Everything recorded during an invocation is sent before it ends.
Logs go to a stream you declare
Section titled “Logs go to a stream you declare”CloudWatch doesn’t create streams for OTel logs, so declare the log group and a stream in it:
import * as AWS from "@alchemy.run/aws";import * as Effect from "effect/Effect";
export const AppLogs = AWS.Logs.LogGroup("AppLogs", { retention: "2 weeks" });
export const ApiLogs = Effect.gen(function* () { const group = yield* AppLogs; return yield* AWS.Logs.LogStream("ApiLogs", { logGroupName: group.logGroupName });});These are your code’s structured logs, separate from what Lambda writes to /aws/lambda/<name>. See Logs.
Metrics are read with PromQL
Section titled “Metrics are read with PromQL”OTel metrics are stored apart from CloudWatch’s classic metrics. GetMetricData and CloudWatch’s metrics browser don’t see them. Query them with PromQL, in CloudWatch’s Query Studio or its Prometheus-compatible API. A metric’s attributes become labels:
sum(rate(checkouts_total[5m]))PromQL is also how a CloudWatch Omni SLO reads them.
Traces need Transaction Search
Section titled “Traces need Transaction Search”CloudWatch accepts OTel traces only while the account sends X-Ray spans to CloudWatch Logs, a setting called Transaction Search. With traces: true, the layer turns it on for the account if it’s off, and leaves it on when the stack is destroyed, since other stacks may send traces too.
// the account manages Transaction Search elsewhereEffect.provide(AWS.CloudWatch.Telemetry({ traces: true, transactionSearch: false }));The first switch takes about seven minutes, and spans sent before it finishes are refused. See Traces.
Export from a runtime outside AWS
Section titled “Export from a runtime outside AWS”A Cloudflare Worker or a Fly App has no AWS identity of its own. The same layer creates an IAM Role for it with the permissions above, and the runtime signs as that Role. A Fly App assumes it with Fly’s own signed token; a Cloudflare Worker gets an access key that can only assume that Role:
Effect.provide(AWS.CloudWatch.Telemetry({ metrics: true }));Send to more than one place
Section titled “Send to more than one place”Telemetry layers combine. Merge them, and each destination receives the signals it’s configured for, with the same trace and span IDs:
Effect.provide( Layer.mergeAll( AWS.CloudWatch.Telemetry({ metrics: true }), Axiom.Telemetry({ token: Ingest, traces: Traces, logs: Logs }), ),);See Telemetry for how exporters run inside every runtime.