"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const shared_1 = require("../../shared");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Open Telemetry', function () {
    let server;
    describe('Metrics', function () {
        const metricsUrl = 'http://127.0.0.1:9092/metrics';
        it('Should not enable open telemetry metrics', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                server = yield (0, server_commands_1.createSingleServer)(1);
                let hasError = false;
                try {
                    yield (0, server_commands_1.makeRawRequest)({ url: metricsUrl, expectedStatus: 404 });
                }
                catch (err) {
                    hasError = err.message.includes('ECONNREFUSED');
                }
                (0, chai_1.expect)(hasError).to.be.true;
                yield server.kill();
            });
        });
        it('Should enable open telemetry metrics', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                server = yield (0, server_commands_1.createSingleServer)(1, {
                    open_telemetry: {
                        metrics: {
                            enabled: true
                        }
                    }
                });
                const res = yield (0, server_commands_1.makeRawRequest)({ url: metricsUrl, expectedStatus: 200 });
                (0, chai_1.expect)(res.text).to.contain('peertube_job_queue_total{');
            });
        });
        it('Should have playback metrics', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.setAccessTokensToServers)([server]);
                const video = yield server.videos.quickUpload({ name: 'video' });
                yield server.metrics.addPlaybackMetric({
                    metrics: {
                        playerMode: 'p2p-media-loader',
                        resolution: 1080,
                        fps: 30,
                        resolutionChanges: 1,
                        errors: 2,
                        downloadedBytesP2P: 0,
                        downloadedBytesHTTP: 0,
                        uploadedBytesP2P: 5,
                        videoId: video.uuid
                    }
                });
                const res = yield (0, server_commands_1.makeRawRequest)({ url: metricsUrl, expectedStatus: 200 });
                (0, chai_1.expect)(res.text).to.contain('peertube_playback_http_downloaded_bytes_total{');
            });
        });
        after(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.kill();
            });
        });
    });
    describe('Tracing', function () {
        let mockHTTP;
        let mockPort;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                mockHTTP = new shared_1.MockHTTP();
                mockPort = yield mockHTTP.initialize();
            });
        });
        it('Should enable open telemetry tracing', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                server = yield (0, server_commands_1.createSingleServer)(1);
                yield (0, shared_1.expectLogDoesNotContain)(server, 'Registering Open Telemetry tracing');
                yield server.kill();
            });
        });
        it('Should enable open telemetry metrics', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                server = yield (0, server_commands_1.createSingleServer)(1, {
                    open_telemetry: {
                        tracing: {
                            enabled: true,
                            jaeger_exporter: {
                                endpoint: 'http://127.0.0.1:' + mockPort
                            }
                        }
                    }
                });
                yield (0, shared_1.expectLogContain)(server, 'Registering Open Telemetry tracing');
            });
        });
        it('Should upload a video and correctly works', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.setAccessTokensToServers)([server]);
                const { uuid } = yield server.videos.quickUpload({ name: 'video', privacy: 1 });
                const video = yield server.videos.get({ id: uuid });
                (0, chai_1.expect)(video.name).to.equal('video');
            });
        });
        after(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield mockHTTP.terminate();
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=open-telemetry.js.map