Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 9a36584243c2fac1c09dfc5e596b77d1aac8c8e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
 * Metrics collector test
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import {
    PrometheusFactory,
    PrometheusConfig,
} from '../../src/system/PrometheusFactory';
import { Histogram, Pushgateway, Counter, Gauge } from 'prom-client';
import { EventEmitter } from 'events';
import { expect } from 'chai';
import {
    MetricsCollector as Sut,
    MetricTimer,
} from '../../src/system/MetricsCollector';

const sinon = require( 'sinon' );

describe( 'system.MetricsCollector captures events and pushes metrics', () =>
{
    it( 'process-complete event is hooked', () =>
    {
        let histogram_called = false;
        let counter_called   = false;

        const emitter = new EventEmitter();
        const conf    = createMockConfig();
        const timer   = createMockTimer();
        const factory = createMockFactory( {
            histogram_cb: () => { histogram_called = true },
            counter_cb:   () => { counter_called   = true },
        } );

        const sut = new Sut( factory, conf, emitter, timer );

        emitter.emit( 'delta-process-end' );

        expect( histogram_called ).to.be.true;
        expect( counter_called ).to.be.true;

        sut.stop();
    } );


    it( 'process-error event is hooked', () =>
    {
        let counter_called = false;

        const emitter = new EventEmitter();
        const conf    = createMockConfig();
        const timer   = createMockTimer();
        const factory = createMockFactory( {
            counter_cb: () => { counter_called = true },
        } );

        const sut = new Sut( factory, conf, emitter, timer );

        emitter.emit( 'error' );

        expect( counter_called ).to.be.true;

        sut.stop();
    } );


    it( 'process-complete is timed properly', () =>
    {
        let   actual_ms     = 0;
        const uid           = 'foo';
        const start_time_ns = 1234;
        const end_time_ns   = 5678;
        const expected_ms   = ( end_time_ns - start_time_ns ) / 1000000;
        const emitter       = new EventEmitter();
        const conf          = createMockConfig();
        const timer         = createMockTimer( start_time_ns, end_time_ns );
        const factory       = createMockFactory( {
            histogram_cb: ( n: number ) => { actual_ms = n },
        } );

        const sut = new Sut( factory, conf, emitter, timer );

        emitter.emit( 'delta-process-start', uid );
        emitter.emit( 'delta-process-end', uid );

        expect( actual_ms ).to.be.equal( expected_ms );

        sut.stop();
    } );
} );


function createMockFactory(
    {
        gateway_cb   = () => {},
        counter_cb   = () => {},
        histogram_cb = ( _n: number = 0 ) => {},
        gauge_cb     = ( _n: number = 0 ) => {},
    }:
    {
        gateway_cb   ?: () => void;
        counter_cb   ?: () => void;
        histogram_cb ?: ( _n: number ) => void;
        gauge_cb     ?: ( _n: number ) => void;
    }
): PrometheusFactory
{
    const gateway   = sinon.mock( Pushgateway );
    const counter   = sinon.mock( Counter );
    const histogram = sinon.mock( Histogram );
    const gauge     = sinon.mock( Gauge );

    gateway.pushAdd   = gateway_cb;
    counter.inc       = counter_cb;
    histogram.observe = histogram_cb;
    gauge.set         = gauge_cb;

    return <PrometheusFactory>{
        createGateway()  { return gateway },
        createCounter()  { return counter },
        createHistogram(){ return histogram },
        createGauge()    { return gauge },
    };
}


function createMockConfig(): PrometheusConfig
{
    return <PrometheusConfig>{
        hostname:         'foo.com',
        port:             123,
        env:              'test',
        push_interval_ms: 1000,
    }
}


function createMockTimer( _start: number = 0, _end: number = 0 ): MetricTimer
{
    return ( _start_time?: [ number, number ] ) =>
    {
        if ( !_start_time )
        {
            return [ 0, _start ];
        }

        return [ 0, _end - _start_time[ 1 ] ];
    };
}