"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetricsController = void 0;
const tslib_1 = require("tslib");
const http_1 = require("@deepkit/http");
/*@ts-ignore*/
var { __ΩAnyResponse } = require('../../http');
const helpers_1 = require("./helpers");
function isPrivateLanIp(ip) {
    // Handle IPv6-mapped IPv4 addresses (::ffff:x.x.x.x)
    if (ip.startsWith('::ffff:')) {
        ip = ip.slice(7);
    }
    // IPv4 private ranges
    if (ip.startsWith('10.'))
        return true; // 10.0.0.0/8
    if (/^172\.(1[6-9]|2[0-9]|3[0-1])\./.test(ip))
        return true; // 172.16.0.0/12
    if (ip.startsWith('192.168.'))
        return true; // 192.168.0.0/16
    if (ip.startsWith('127.'))
        return true; // 127.0.0.0/8 (localhost)
    // IPv6 private ranges
    if (ip === '::1')
        return true; // localhost
    if (/^f[cd][0-9a-f]{2}:/i.test(ip))
        return true; // fc00::/7 (unique local)
    if (/^fe80:/i.test(ip))
        return true; // fe80::/10 (link-local)
    return false;
}
isPrivateLanIp.__type = ['ip', 'isPrivateLanIp', 'P&2!)/"'];
let MetricsController = class MetricsController {
    async getMetrics(request) {
        const ip = request.getRemoteAddress();
        if (!isPrivateLanIp(ip)) {
            return new http_1.Response('Forbidden', 'text/plain', 403);
        }
        if (!helpers_1.OtelState.prometheusExporter) {
            return new http_1.Response('Metrics not available', 'text/plain', 503);
        }
        const exporter = helpers_1.OtelState.prometheusExporter;
        const { resourceMetrics, errors } = await exporter.collect();
        if (errors.length) {
            console.error('PrometheusExporter: metrics collection errors', ...errors);
        }
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const body = exporter._serializer.serialize(resourceMetrics);
        return new http_1.Response(body, 'text/plain', 200);
    }
};
exports.MetricsController = MetricsController;
MetricsController.__type = [() => http_1.HttpRequest, 'request', () => __ΩAnyResponse, 'getMetrics', 'MetricsController', 'PP7!2"n#0$5w%'];
tslib_1.__decorate([
    http_1.http.GET(),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [http_1.HttpRequest]),
    tslib_1.__metadata("design:returntype", Object)
], MetricsController.prototype, "getMetrics", null);
exports.MetricsController = MetricsController = tslib_1.__decorate([
    http_1.http.controller('/metrics')
], MetricsController);
//# sourceMappingURL=metrics.controller.js.map