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