"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const request = require("supertest");
const global_1 = require("../global");
const settings_1 = require("../settings");
let base = require('../index');
let assert = require('assert');
let options = {
    morgan: {
        useStdout: false
    }
};
describe('metrics', function () {
    beforeEach(function () {
        global_1.default.Instance.Metrics.reset();
        global_1.default.Instance.personalIsFunctional = function () { return true; };
    });
    it("get metrics without register anything", function (done) {
        let register = global_1.default.Instance.getMergedMetrics();
        assert.notEqual(register, null);
        let metrics = register.metrics();
        assert.equal(metrics, "");
        done();
    });
    it("create registers", function (done) {
        settings_1.default.Instance.setMetrics("register1", { name: "register1" });
        global_1.default.Instance.Metrics.newInstance("register1");
        let register = global_1.default.Instance.Metrics.get("register1");
        let register_default = global_1.default.Instance.Metrics.get();
        if (register == null || register_default == null) {
            assert.notEqual(register, null);
            assert.notEqual(register_default, null);
            done("ERROR");
            return;
        }
        register.newCounter("counter", "test");
        register.newGauge("gauge", "test", [], (base.AGGREGATORS.MIN | base.AGGREGATORS.AVERAGE | base.AGGREGATORS.MAX));
        register_default.newHistogram("histogram", "test");
        let counter = register.counter("counter");
        let gauge = register.gauge("gauge");
        let histo = register_default.histogram("histogram");
        assert.equal(register.histogram("histogram"), null);
        assert.equal(register_default.counter("counter"), null);
        if (counter == null || gauge == null || histo == null) {
            assert.notEqual(counter, null);
            assert.notEqual(gauge, null);
            assert.notEqual(histo, null);
            done("ERROR");
            return;
        }
        counter.inc(20);
        counter.inc(10);
        gauge.set(800);
        gauge.set(850);
        gauge.inc(20);
        gauge.dec(30);
        histo.observe(300);
        histo.observe(250);
        histo.observe(360);
        let metrics = register_default.metrics();
        assert.notEqual(metrics.indexOf("# HELP counter test"), -1);
        assert.notEqual(metrics.indexOf("# TYPE counter counter"), -1);
        assert.notEqual(metrics.indexOf("counter 30"), -1);
        assert.notEqual(metrics.indexOf("# HELP gauge test"), -1);
        assert.notEqual(metrics.indexOf("# TYPE gauge gauge"), -1);
        assert.notEqual(metrics.indexOf('gauge 840'), -1);
        assert.notEqual(metrics.indexOf('gauge{aggregator="max"} 870'), -1);
        assert.notEqual(metrics.indexOf('gauge{aggregator="avg"} 840'), -1);
        assert.notEqual(metrics.indexOf('gauge{aggregator="min"} 800'), -1);
        assert.notEqual(metrics.indexOf("# HELP histogram test"), -1);
        assert.notEqual(metrics.indexOf("# TYPE histogram histogram"), -1);
        assert.notEqual(metrics.indexOf('histogram_sum 910'), -1);
        assert.notEqual(metrics.indexOf('histogram_count 3'), -1);
        done();
    });
    it("test counters", function (done) {
        let register = global_1.default.Instance.Metrics.get();
        if (register == null) {
            assert.notEqual(register, null);
            done("ERROR");
            return;
        }
        register.newCounter("test", "test help");
        let counter = register.counter("test");
        if (counter == null) {
            assert.notEqual(counter, null);
            done("ERROR");
            return;
        }
        counter.inc(40);
        assert.notEqual(register.metrics().indexOf("test 40"), -1);
        counter.inc(8);
        assert.notEqual(register.metrics().indexOf("test 48"), -1);
        counter.inc(0);
        assert.notEqual(register.metrics().indexOf("test 48"), -1);
        counter.inc();
        assert.notEqual(register.metrics().indexOf("test 49"), -1);
        counter.inc(1);
        assert.notEqual(register.metrics().indexOf("test 50"), -1);
        counter.inc(-0);
        assert.notEqual(register.metrics().indexOf("test 50"), -1);
        try {
            counter.inc(-1);
        }
        catch (err) {
            assert.notEqual(err, null);
            assert.equal(err.message, "It is not possible to decrease a counter");
        }
        done();
    });
    it("test gauge", function (done) {
        let register = global_1.default.Instance.Metrics.get();
        if (register == null) {
            assert.notEqual(register, null);
            done("ERROR");
            return;
        }
        register.newGauge("test", "test help", [], (base.AGGREGATORS.MIN | base.AGGREGATORS.AVERAGE | base.AGGREGATORS.MAX));
        let gauge = register.gauge("test");
        if (gauge == null) {
            assert.notEqual(gauge, null);
            done("ERROR");
            return;
        }
        gauge.set(40);
        assert.notEqual(register.metrics().indexOf("test 40"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 40'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 40'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 40'), -1);
        gauge.inc(8);
        assert.notEqual(register.metrics().indexOf("test 48"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 48'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 44'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 40'), -1);
        gauge.inc(0); //Bug in prom-client/lib/gauge:210 treats 0 like undefine and replase with 1
        assert.notEqual(register.metrics().indexOf("test 49"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 49'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 45.666'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 40'), -1);
        gauge.inc();
        assert.notEqual(register.metrics().indexOf("test 50"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 50'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 46.75'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 40'), -1);
        gauge.inc(1);
        assert.notEqual(register.metrics().indexOf("test 51"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 51'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 47.6'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 40'), -1);
        gauge.dec(8);
        assert.notEqual(register.metrics().indexOf("test 43"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 51'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 46.833'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 40'), -1);
        gauge.dec(0); //Bug in prom-client/lib/gauge:210 treats 0 like undefine and replase with 1
        assert.notEqual(register.metrics().indexOf("test 42"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 51'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 46.142'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 40'), -1);
        gauge.set(0);
        assert.notEqual(register.metrics().indexOf("test 0"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 51'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 40.375'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 0'), -1);
        gauge.set(120);
        assert.notEqual(register.metrics().indexOf("test 120"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 120'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 49.222'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 0'), -1);
        gauge.dec(-0);
        assert.notEqual(register.metrics().indexOf("test 119"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 120'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 56.2'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 0'), -1);
        gauge.inc(-1); //Do not trow error
        assert.notEqual(register.metrics().indexOf("test 118"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 120'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 61.818'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 0'), -1);
        gauge.dec(-10); //Do not trow error
        assert.notEqual(register.metrics().indexOf("test 128"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 128'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 67.333'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 0'), -1);
        gauge.dec(-10); //Do not trow error
        assert.notEqual(register.metrics().indexOf("test 138"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 138'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 72.769'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 0'), -1);
        gauge.reset();
        assert.equal(register.metrics().indexOf('test{aggregator="max"}'), -1);
        assert.equal(register.metrics().indexOf('test{aggregator="avg"}'), -1);
        assert.equal(register.metrics().indexOf('test{aggregator="min"}'), -1);
        gauge.set(40);
        assert.notEqual(register.metrics().indexOf("test 40"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 40'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 40'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 40'), -1);
        gauge.inc(8);
        assert.notEqual(register.metrics().indexOf("test 48"), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="max"} 48'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="avg"} 44'), -1);
        assert.notEqual(register.metrics().indexOf('test{aggregator="min"} 40'), -1);
        done();
    });
    it("test histogram", function (done) {
        let register = global_1.default.Instance.Metrics.get();
        if (register == null) {
            assert.notEqual(register, null);
            done("ERROR");
            return;
        }
        register.newHistogram("test", "test help");
        let histogram = register.histogram("test");
        if (histogram == null) {
            assert.notEqual(histogram, null);
            done("ERROR");
            return;
        }
        histogram.observe(40);
        assert.notEqual(register.metrics().indexOf("test_sum 40"), -1);
        assert.notEqual(register.metrics().indexOf("test_count 1"), -1);
        histogram.observe(8);
        assert.notEqual(register.metrics().indexOf("test_sum 48"), -1);
        assert.notEqual(register.metrics().indexOf("test_count 2"), -1);
        histogram.observe(0);
        assert.notEqual(register.metrics().indexOf("test_sum 48"), -1);
        assert.notEqual(register.metrics().indexOf("test_count 3"), -1);
        histogram.observe(1);
        assert.notEqual(register.metrics().indexOf("test_sum 49"), -1);
        assert.notEqual(register.metrics().indexOf("test_count 4"), -1);
        histogram.observe(-1);
        assert.notEqual(register.metrics().indexOf("test_sum 48"), -1);
        assert.notEqual(register.metrics().indexOf("test_count 5"), -1);
        histogram.observe(-10);
        assert.notEqual(register.metrics().indexOf("test_sum 38"), -1);
        assert.notEqual(register.metrics().indexOf("test_count 6"), -1);
        done();
    });
});
describe('metrics with express', function () {
    it("check default data", function (done) {
        let web = base.express(options);
        let app = web.initialize();
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        request(app).get('/metrics')
            .expect(200, function (err, res) {
            let text = res.text;
            assert.notEqual(text, null);
            assert.notEqual(text.indexOf("process_cpu_user_seconds_total"), -1);
            assert.notEqual(text.indexOf("process_cpu_system_seconds_total"), -1);
            assert.notEqual(text.indexOf("process_cpu_seconds_total"), -1);
            assert.notEqual(text.indexOf("process_start_time_seconds"), -1);
            assert.notEqual(text.indexOf("process_resident_memory_bytes"), -1);
            assert.notEqual(text.indexOf("nodejs_eventloop_lag_seconds"), -1);
            assert.notEqual(text.indexOf("nodejs_active_handles_total"), -1);
            assert.notEqual(text.indexOf("nodejs_active_requests_total"), -1);
            assert.notEqual(text.indexOf("nodejs_heap_size_total_bytes"), -1);
            assert.notEqual(text.indexOf("nodejs_heap_size_used_bytes"), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_total_bytes{space="new"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_total_bytes{space="old"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_total_bytes{space="code"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_total_bytes{space="map"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_total_bytes{space="large_object"'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_used_bytes{space="new"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_used_bytes{space="old"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_used_bytes{space="code"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_used_bytes{space="map"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_used_bytes{space="large_object"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_available_bytes{space="new"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_available_bytes{space="old"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_available_bytes{space="code"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_available_bytes{space="map"}'), -1);
            assert.notEqual(text.indexOf('nodejs_heap_space_size_available_bytes{space="large_object"}'), -1);
            assert.notEqual(text.indexOf('nodejs_version_info{version='), -1);
            assert.notEqual(text.indexOf('up'), -1);
            done();
        });
    });
    it("ensure check all routes", function (done) {
        let web = base.express(options);
        let app = web.initialize();
        app.get('/test', function (req, res) {
            res.status(200);
            res.send(JSON.stringify({ result: "Our test response" }));
        });
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        new Promise(function (resolve, reject) {
            request(app).get('/health')
                .expect(200, function (err, res) {
                resolve();
            });
        }).then(function (result) {
            return new Promise(function (resolve, reject) {
                request(app).get('/swagger.json')
                    .expect(200, resolve);
            });
        }).then(function (result) {
            return new Promise(function (resolve, reject) {
                request(app).get('/metrics')
                    .expect(200, resolve);
            });
        }).then(function (result) {
            return new Promise(function (resolve, reject) {
                request(app).get('/test')
                    .expect(200, resolve);
            });
        }).then(function (result) {
            return new Promise(function (resolve, reject) {
                request(app).get('/metrics')
                    .expect(200, function (err, res) {
                    let text = res.text;
                    assert.notEqual(text, null);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="0.5",status_code="200",method="GET",path="/health"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="5",status_code="200",method="GET",path="/health"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="10",status_code="200",method="GET",path="/health"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="25",status_code="200",method="GET",path="/health"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="50",status_code="200",method="GET",path="/health"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="+Inf",status_code="200",method="GET",path="/health"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_sum{status_code="200",method="GET",path="/health"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_count{status_code="200",method="GET",path="/health"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="0.5",status_code="200",method="GET",path="/metrics"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="5",status_code="200",method="GET",path="/metrics"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="10",status_code="200",method="GET",path="/metrics"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="25",status_code="200",method="GET",path="/metrics"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="50",status_code="200",method="GET",path="/metrics"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="+Inf",status_code="200",method="GET",path="/metrics"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_sum{status_code="200",method="GET",path="/metrics"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_count{status_code="200",method="GET",path="/metrics"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="0.5",status_code="200",method="GET",path="/test"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="5",status_code="200",method="GET",path="/test"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="10",status_code="200",method="GET",path="/test"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="25",status_code="200",method="GET",path="/test"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="50",status_code="200",method="GET",path="/test"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_bucket{le="+Inf",status_code="200",method="GET",path="/test"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_sum{status_code="200",method="GET",path="/test"}'), -1);
                    assert.notEqual(text.indexOf('http_request_duration_seconds_count{status_code="200",method="GET",path="/test"}'), -1);
                    resolve();
                });
            });
        }).then(function (result) {
            done();
        }).catch(function (error) {
            done(error);
        });
    });
    it("insert custom data", function (done) {
        let web = base.express(options);
        let app = web.initialize();
        let i = 0;
        let values = [10, 3, 8];
        base.metrics.newInstance("test");
        base.metrics("test").newGauge("test_val", "valuare_test", [], (base.AGGREGATORS.MIN | base.AGGREGATORS.AVERAGE | base.AGGREGATORS.MAX));
        app.get('/test', function (req, res) {
            base.metrics("test").gauge("test_val").set(values[i]);
            i++;
            res.status(200);
            res.send(JSON.stringify({ result: "Our test response" }));
        });
        app.listen(function (err) {
            if (err) {
                return done(err);
            }
        });
        new Promise(function (resolve, reject) {
            request(app).get('/test')
                .expect(200, resolve);
        }).then(function (result) {
            return new Promise(function (resolve, reject) {
                request(app).get('/test')
                    .expect(200, resolve);
            });
        }).then(function (result) {
            return new Promise(function (resolve, reject) {
                request(app).get('/test')
                    .expect(200, resolve);
            });
        }).then(function (result) {
            return new Promise(function (resolve, reject) {
                request(app).get('/metrics')
                    .expect(200, function (err, res) {
                    let text = res.text;
                    assert.notEqual(text, null);
                    assert.notEqual(text.indexOf('test_val 8'), -1);
                    assert.notEqual(text.indexOf('test_val{aggregator="max"} 10'), -1);
                    assert.notEqual(text.indexOf('test_val{aggregator="avg"} 7'), -1);
                    assert.notEqual(text.indexOf('test_val{aggregator="min"} 3'), -1);
                    base.metrics("test").gauge("test_val").reset();
                    resolve();
                });
            });
        }).then(function (result) {
            return new Promise(function (resolve, reject) {
                request(app).get('/metrics')
                    .expect(200, function (err, res) {
                    let text = res.text;
                    assert.notEqual(text, null);
                    assert.equal(text.indexOf('test_val{aggregator="max"}'), -1);
                    assert.equal(text.indexOf('test_val{aggregator="avg"}'), -1);
                    assert.equal(text.indexOf('test_val{aggregator="min"}'), -1);
                    resolve();
                });
            });
        }).then(function (result) {
            done();
        }).catch(function (error) {
            done(error);
        });
    });
});
//# sourceMappingURL=test-metrics.js.map