UNPKG

4.4 kBPlain TextView Raw
1import { expect } from 'chai';
2import 'mocha';
3
4import { metrics } from '../src';
5
6describe('MetricsGatherer', () => {
7 beforeEach(() => {
8 metrics.clear();
9 });
10
11 const expectToErr = (f: () => void) => {
12 const errsBefore = metrics.internalErrorCount;
13 f();
14 expect(metrics.internalErrorCount - errsBefore).to.equal(1);
15 };
16
17 describe('Client Passthrough', () => {
18 it('method on client should be accessible (check exponentialBuckets)`', () => {
19 expect(metrics.client.exponentialBuckets(1, 2, 3)).to.eql([1, 2, 4]);
20 });
21 });
22
23 describe('Error', () => {
24 it('should fail if same name used for different kinds', () => {
25 expectToErr(() => {
26 metrics.counter('a_name', 1);
27 metrics.histogram('a_name', 1);
28 });
29 });
30 });
31
32 describe('Gauge', () => {
33 it('should create a gauge on inc() for undescribed metric', () => {
34 metrics.inc('undescribed_gauge', 10);
35 const output = metrics.output();
36 expect(/TYPE undescribed_gauge gauge/.test(output)).to.be.true;
37 });
38
39 it('should not create a gauge, but a counter, if _total suffix found', () => {
40 metrics.inc('build_error_total', 1);
41 const output = metrics.output();
42 expect(/TYPE build_error_total counter/.test(output)).to.be.true;
43 });
44
45 it('should inc and dec by 1 by default', () => {
46 let output: string;
47
48 metrics.inc('undescribed_gauge');
49 output = metrics.output();
50 expect(/undescribed_gauge 1/.test(output)).to.be.true;
51
52 metrics.inc('undescribed_gauge');
53 output = metrics.output();
54 expect(/undescribed_gauge 2/.test(output)).to.be.true;
55
56 metrics.dec('undescribed_gauge');
57 output = metrics.output();
58 expect(/undescribed_gauge 1/.test(output)).to.be.true;
59 });
60 });
61
62 describe('Counter', () => {
63 it('should create a counter', () => {
64 metrics.describe.counter(
65 'existent_counter',
66 'a counter that should exist',
67 );
68 metrics.counter('existent_counter');
69 const output = metrics.output();
70 expect(/TYPE existent_counter counter/.test(output)).to.be.true;
71 });
72
73 it('should reset a counter', () => {
74 metrics.describe.counter(
75 'resetting_counter',
76 'a counter that should be reset',
77 );
78 metrics.counter('resetting_counter');
79 metrics.reset('resetting_counter');
80 const output = metrics.output();
81 expect(/resetting_counter 0/.test(output)).to.be.true;
82 });
83
84 it('should increment by 1 if not specified', () => {
85 metrics.describe.counter('simple_counter', 'a simple counter');
86 metrics.counter('simple_counter');
87 const output = metrics.output();
88 expect(/simple_counter 1/.test(output)).to.be.true;
89 });
90
91 it('should increment by a variable amount', () => {
92 metrics.describe.counter(
93 'variable_counter',
94 'a counter that should increment by variable amounts',
95 );
96 metrics.counter('variable_counter', 1);
97 metrics.inc('variable_counter', 3);
98 const output = metrics.output();
99 expect(/variable_counter 4/.test(output)).to.be.true;
100 });
101
102 it('should throw an error on dec() to counter', () => {
103 expectToErr(() => {
104 metrics.counter('counter_metric', 1);
105 metrics.dec('counter_metric');
106 });
107 });
108 });
109
110 describe('Summary', () => {
111 it('should throw an error on inc() to summary', () => {
112 expectToErr(() => {
113 metrics.summary('summary_metric', 1);
114 metrics.inc('summary_metric');
115 });
116 });
117
118 it('should throw an error on dec() to summary', () => {
119 expectToErr(() => {
120 metrics.summary('summary_metric', 1);
121 metrics.dec('summary_metric');
122 });
123 });
124
125 it('should allow single-line CustomParams', () => {
126 const percentiles = [0.1, 0.5, 0.9];
127 metrics.summary('summary_metric', 1, {}, { percentiles });
128 expect(metrics.meta['summary_metric'].customParams.percentiles).to.eql(
129 percentiles,
130 );
131 });
132 });
133
134 describe('Histogram', () => {
135 it('should throw an error on inc() to histogram', () => {
136 expectToErr(() => {
137 metrics.histogram('histogram_metric', 1);
138 metrics.inc('histogram_metric');
139 });
140 });
141
142 it('should throw an error on dec() to histogram', () => {
143 expectToErr(() => {
144 metrics.histogram('histogram_metric', 1);
145 metrics.dec('histogram_metric');
146 });
147 });
148
149 it('should allow single-line CustomParams', () => {
150 const buckets = [1, 9, 99, 999];
151 metrics.histogram('histogram_metric', 1, {}, { buckets });
152 expect(metrics.meta['histogram_metric'].customParams.buckets).to.eql(
153 buckets,
154 );
155 });
156 });
157});