1 import Counter from '../lib/Counter';
2 import Histogram from '../lib/Histogram';
3 import MetricsApi from '../lib/MetricsApi';
4 import MetricsBase from '../lib/MetricsBase';
5 import MetricsRequestService from '../lib/MetricsRequestService';
6 import type IMetricsRequestService from '../lib/types/IMetricsRequestService';
7 import type MetricSchema from '../lib/types/MetricSchema';
9 export const counterName = 'counter_name';
10 export const counterVersion = 1;
12 export const histogramName = 'histogram_name';
13 export const histogramVersion = 1;
15 class MetricsTest extends MetricsBase {
16 public counter: Counter<MetricSchema>;
18 public histogram: Histogram<MetricSchema>;
20 constructor(requestService: IMetricsRequestService) {
21 super(requestService);
23 this.counter = new Counter<MetricSchema>({ name: counterName, version: counterVersion }, this.requestService);
24 this.histogram = new Histogram<MetricSchema>(
25 { name: histogramName, version: histogramVersion },
31 const metricsApi = new MetricsApi();
32 const metricsRequestService = new MetricsRequestService(metricsApi, { reportMetrics: true });
33 const metrics = new MetricsTest(metricsRequestService);
36 const clientID = 'clientID';
37 const appVersion = 'appVersion';
45 const time = new Date('2020-01-01');
47 describe('metrics', () => {
53 fetchMock.resetMocks();
54 jest.clearAllTimers();
55 jest.setSystemTime(time);
62 describe('counter requests', () => {
63 it('uses the metrics v1 url', () => {
64 metrics.counter.increment({
68 const url = fetchMock.mock.lastCall?.[0];
70 expect(fetchMock).toHaveBeenCalledTimes(1);
71 expect(url).toBe('/api/data/v1/metrics');
74 it('makes a post request', async () => {
75 metrics.counter.increment({
79 const content = fetchMock.mock.lastCall?.[1];
81 expect(fetchMock).toHaveBeenCalledTimes(1);
82 expect(content?.method).toBe('post');
85 it('contains the correct headers', async () => {
86 metrics.counter.increment({
90 const content = fetchMock.mock.lastCall?.[1];
92 expect(fetchMock).toHaveBeenCalledTimes(1);
93 expect(content?.headers).toEqual({
94 'content-type': 'application/json',
96 'x-pm-appversion': `${clientID}@${appVersion}-dev`,
101 it('sends the metric name', async () => {
102 metrics.counter.increment({
106 const content = fetchMock.mock.lastCall?.[1];
108 const body = JSON.parse(content?.body);
110 expect(fetchMock).toHaveBeenCalledTimes(1);
111 expect(body.Metrics[0].Name).toEqual(counterName);
114 it('sends the metric version', async () => {
115 metrics.counter.increment({
119 const content = fetchMock.mock.lastCall?.[1];
121 const body = JSON.parse(content?.body);
123 expect(fetchMock).toHaveBeenCalledTimes(1);
124 expect(body.Metrics[0].Version).toEqual(counterVersion);
127 it('uses the current time for the timestamp', async () => {
128 metrics.counter.increment({
132 const content = fetchMock.mock.lastCall?.[1];
134 const body = JSON.parse(content?.body);
136 expect(fetchMock).toHaveBeenCalledTimes(1);
137 expect(body.Metrics[0].Timestamp).toEqual(time.getTime() / 1000);
140 it('sends Value as 1', async () => {
141 metrics.counter.increment({
145 const content = fetchMock.mock.lastCall?.[1];
147 const body = JSON.parse(content?.body);
149 expect(fetchMock).toHaveBeenCalledTimes(1);
150 expect(body.Metrics[0].Data.Value).toBe(1);
153 it('sends labels', async () => {
158 metrics.counter.increment(labels);
160 const content = fetchMock.mock.lastCall?.[1];
162 const body = JSON.parse(content?.body);
164 expect(fetchMock).toHaveBeenCalledTimes(1);
165 expect(body.Metrics[0].Data.Labels).toEqual(labels);
169 describe('histogram requests', () => {
170 it('uses the metrics v1 url', async () => {
171 metrics.histogram.observe({
178 const url = fetchMock.mock.lastCall?.[0];
180 expect(fetchMock).toHaveBeenCalledTimes(1);
181 expect(url).toBe('/api/data/v1/metrics');
184 it('makes a post request', async () => {
185 metrics.histogram.observe({
192 const content = fetchMock.mock.lastCall?.[1];
194 expect(fetchMock).toHaveBeenCalledTimes(1);
195 expect(content?.method).toBe('post');
198 it('contains the correct headers', async () => {
199 metrics.histogram.observe({
206 const content = fetchMock.mock.lastCall?.[1];
208 expect(fetchMock).toHaveBeenCalledTimes(1);
209 expect(content?.headers).toEqual({
210 'content-type': 'application/json',
212 'x-pm-appversion': `${clientID}@${appVersion}-dev`,
217 it('sends the metric name', async () => {
218 metrics.histogram.observe({
225 const content = fetchMock.mock.lastCall?.[1];
227 const body = JSON.parse(content?.body);
229 expect(fetchMock).toHaveBeenCalledTimes(1);
230 expect(body.Metrics[0].Name).toEqual(histogramName);
233 it('sends the metric version', async () => {
234 metrics.histogram.observe({
241 const content = fetchMock.mock.lastCall?.[1];
243 const body = JSON.parse(content?.body);
245 expect(fetchMock).toHaveBeenCalledTimes(1);
246 expect(body.Metrics[0].Version).toEqual(histogramVersion);
249 it('uses the current time for the timestamp', async () => {
250 metrics.histogram.observe({
257 const content = fetchMock.mock.lastCall?.[1];
259 const body = JSON.parse(content?.body);
261 expect(fetchMock).toHaveBeenCalledTimes(1);
262 expect(body.Metrics[0].Timestamp).toEqual(time.getTime() / 1000);
265 it('sends float Values', async () => {
267 metrics.histogram.observe({
274 const content = fetchMock.mock.lastCall?.[1];
276 const body = JSON.parse(content?.body);
278 expect(fetchMock).toHaveBeenCalledTimes(1);
279 expect(body.Metrics[0].Data.Value).toBe(Value);
282 it('sends Labels', async () => {
287 metrics.histogram.observe({
292 const content = fetchMock.mock.lastCall?.[1];
294 const body = JSON.parse(content?.body);
296 expect(fetchMock).toHaveBeenCalledTimes(1);
297 expect(body.Metrics[0].Data.Labels).toEqual(Labels);