import { newContext } from "../context";
import { getLogger, LOGGER_LEVEL_ENV, newLogger, withLogger } from "../logger";
import { withServiceTrn, initializeServiceTrnProcessId } from "../message";
import { getTracing, withTracing } from "../tracing";
export class LambdaHandler {
    constructor(region, serviceName) {
        this.serviceNamePlural = () => {
            return this.serviceName + "s";
        };
        this.setTimeout = (ctx, milli) => {
            this.clearTimeout();
            this.timeout = setTimeout(() => {
                getLogger(ctx).error(`timed out after ${milli} ms`);
            }, milli - 100);
        };
        this.clearTimeout = () => {
            clearTimeout(this.timeout);
        };
        this.serviceName = serviceName;
        this.region = region;
        let ctx = newContext();
        // tracing
        // https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime
        ctx = withTracing(newContext(), process.env["_X_AMZN_TRACE_ID"]);
        // logger
        const log = newLogger({
            service: this.serviceName,
            level: process.env[LOGGER_LEVEL_ENV],
            tracingId: getTracing(ctx),
        });
        ctx = withLogger(ctx, log);
        ctx = withServiceTrn(ctx, initializeServiceTrnProcessId(this.serviceNamePlural()));
        this.mainCtx = ctx;
    }
}
//# sourceMappingURL=runtime.js.map