File size: 3,161 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import config from '@automattic/calypso-config';
import superagent from 'superagent';
import { createStatsdURL, logServerEvent } from '../statsd-utils';

jest.mock( '@automattic/calypso-config' );

describe( 'StatsD Analytics Utils', () => {
	describe( 'createStatsdURL', () => {
		beforeAll( () => {
			config.mockReturnValue( 'development' ); // boom_analytics_key
		} );

		test( 'returns a URL for sending events to statsd', () => {
			const events = [
				{
					name: 'response_time',
					value: 100,
					type: 'timing',
				},
				{
					name: 'page-load',
					type: 'counting',
				},
			];
			const sdUrl = new URL( createStatsdURL( 'my-section-name', events ) );

			expect( sdUrl.searchParams.get( 'v' ) ).toEqual( 'calypso' );
			expect( sdUrl.searchParams.get( 'u' ) ).toEqual( 'my_section_name' );
			expect( sdUrl.searchParams.get( 'json' ) ).toEqual(
				JSON.stringify( {
					beacons: [
						'calypso.development.server.my_section_name.response_time:100|ms',
						'calypso.development.server.my_section_name.page_load:1|c',
					],
				} )
			);
		} );

		test( 'does not include server hierarchy in legacy events', () => {
			const sdUrl = new URL(
				createStatsdURL( 'my-section-name', {
					name: 'test',
					type: 'counting',
					isLegacy: true,
				} )
			);
			expect( sdUrl.searchParams.get( 'json' ) ).toEqual(
				JSON.stringify( {
					beacons: [ 'calypso.development.my_section_name.test:1|c' ],
				} )
			);
		} );

		test( 'processes a single event without an array container', () => {
			const event = {
				name: 'response_time',
				value: 100,
				type: 'timing',
			};
			const sdUrl = new URL( createStatsdURL( 'my-section-name', event ) );

			expect( sdUrl.searchParams.get( 'v' ) ).toEqual( 'calypso' );
			expect( sdUrl.searchParams.get( 'u' ) ).toEqual( 'my_section_name' );
			expect( sdUrl.searchParams.get( 'json' ) ).toEqual(
				JSON.stringify( {
					beacons: [ 'calypso.development.server.my_section_name.response_time:100|ms' ],
				} )
			);
		} );
	} );

	describe( 'logServerEvent', () => {
		beforeAll( () => {
			jest.spyOn( superagent, 'get' ).mockReturnValue( { end: () => {} } );
		} );

		afterAll( () => {
			config.mockRestore();
		} );

		afterEach( () => {
			superagent.get.mockClear();
		} );

		test( 'sends an HTTP request to the statsd URL', () => {
			config.mockReturnValue( true ); // server_side_boom_analytics_enabled
			logServerEvent( 'test-section', { name: 'foo', type: 'counting' } );

			expect( superagent.get ).toHaveBeenCalledWith( expect.stringContaining( 'u=test_section' ) );
		} );

		test( 'does not record event if statsd analytics is not allowed', () => {
			config.mockReturnValue( false ); // server_side_boom_analytics_enabled
			logServerEvent( { name: 'foo', type: 'counting' } );

			expect( superagent.get ).not.toHaveBeenCalled();
		} );

		test( 'sets calypso section to unknown if undefined', () => {
			config.mockReturnValue( true ); // server_side_boom_analytics_enabled
			logServerEvent( undefined, { name: 'foo', type: 'counting' } );
			expect( superagent.get ).toHaveBeenCalledWith( expect.stringContaining( 'u=unknown' ) );
		} );
	} );
} );