"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.wrapWithSpanAndContext = exports.tracer = exports.registerOpentelemetryTracing = void 0;
const tslib_1 = require("tslib");
const opentelemetry_instrumentation_sequelize_1 = require("opentelemetry-instrumentation-sequelize");
const api_1 = require("@opentelemetry/api");
const exporter_jaeger_1 = require("@opentelemetry/exporter-jaeger");
const instrumentation_1 = require("@opentelemetry/instrumentation");
const instrumentation_dns_1 = require("@opentelemetry/instrumentation-dns");
const instrumentation_express_1 = require("@opentelemetry/instrumentation-express");
const instrumentation_fs_1 = tslib_1.__importDefault(require("@opentelemetry/instrumentation-fs"));
const instrumentation_http_1 = require("@opentelemetry/instrumentation-http");
const instrumentation_ioredis_1 = require("@opentelemetry/instrumentation-ioredis");
const instrumentation_pg_1 = require("@opentelemetry/instrumentation-pg");
const resources_1 = require("@opentelemetry/resources");
const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
const logger_1 = require("../../helpers/logger");
const config_1 = require("../../initializers/config");
const tracer = api_1.trace.getTracer('peertube');
exports.tracer = tracer;
function registerOpentelemetryTracing() {
    if (config_1.CONFIG.OPEN_TELEMETRY.TRACING.ENABLED !== true)
        return;
    logger_1.logger.info('Registering Open Telemetry tracing');
    const customLogger = (level) => {
        return (message, ...args) => {
            let fullMessage = message;
            for (const arg of args) {
                if (typeof arg === 'string')
                    fullMessage += arg;
                else
                    break;
            }
            logger_1.logger[level](fullMessage);
        };
    };
    api_1.diag.setLogger({
        error: customLogger('error'),
        warn: customLogger('warn'),
        info: customLogger('info'),
        debug: customLogger('debug'),
        verbose: customLogger('verbose')
    }, api_1.DiagLogLevel.INFO);
    const tracerProvider = new sdk_trace_node_1.NodeTracerProvider({
        resource: new resources_1.Resource({
            [semantic_conventions_1.SemanticResourceAttributes.SERVICE_NAME]: 'peertube'
        })
    });
    (0, instrumentation_1.registerInstrumentations)({
        tracerProvider,
        instrumentations: [
            new instrumentation_pg_1.PgInstrumentation({
                enhancedDatabaseReporting: true
            }),
            new instrumentation_dns_1.DnsInstrumentation(),
            new instrumentation_http_1.HttpInstrumentation(),
            new instrumentation_express_1.ExpressInstrumentation(),
            new instrumentation_ioredis_1.IORedisInstrumentation({
                dbStatementSerializer: function (cmdName, cmdArgs) {
                    return [cmdName, ...cmdArgs].join(' ');
                }
            }),
            new instrumentation_fs_1.default(),
            new opentelemetry_instrumentation_sequelize_1.SequelizeInstrumentation()
        ]
    });
    tracerProvider.addSpanProcessor(new sdk_trace_base_1.BatchSpanProcessor(new exporter_jaeger_1.JaegerExporter({ endpoint: config_1.CONFIG.OPEN_TELEMETRY.TRACING.JAEGER_EXPORTER.ENDPOINT })));
    tracerProvider.register();
}
exports.registerOpentelemetryTracing = registerOpentelemetryTracing;
function wrapWithSpanAndContext(spanName, cb) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const span = tracer.startSpan(spanName);
        const activeContext = api_1.trace.setSpan(api_1.context.active(), span);
        const result = yield api_1.context.with(activeContext, () => cb());
        span.end();
        return result;
    });
}
exports.wrapWithSpanAndContext = wrapWithSpanAndContext;
//# sourceMappingURL=tracing.js.map