Welcome to mirror list, hosted at ThFree Co, Russian Federation.

client_spec.js « observability « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 056175eac078d6df4b8c8a406e6132c8cfae333f (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import MockAdapter from 'axios-mock-adapter';
import * as Sentry from '@sentry/browser';
import { buildClient } from '~/observability/client';
import axios from '~/lib/utils/axios_utils';

jest.mock('~/lib/utils/axios_utils');
jest.mock('@sentry/browser');

describe('buildClient', () => {
  let client;
  let axiosMock;

  const tracingUrl = 'https://example.com/tracing';
  const provisioningUrl = 'https://example.com/provisioning';

  const FETCHING_TRACES_ERROR = 'traces are missing/invalid in the response';

  beforeEach(() => {
    axiosMock = new MockAdapter(axios);
    jest.spyOn(axios, 'get');

    client = buildClient({
      tracingUrl,
      provisioningUrl,
    });
  });

  afterEach(() => {
    axiosMock.restore();
  });

  describe('isTracingEnabled', () => {
    it('returns true if requests succeedes', async () => {
      axiosMock.onGet(provisioningUrl).reply(200, {
        status: 'ready',
      });

      const enabled = await client.isTracingEnabled();

      expect(enabled).toBe(true);
    });

    it('returns false if response is 404', async () => {
      axiosMock.onGet(provisioningUrl).reply(404);

      const enabled = await client.isTracingEnabled();

      expect(enabled).toBe(false);
    });

    // we currently ignore the 'status' payload and just check if the request was successful
    // We might improve this as part of https://gitlab.com/gitlab-org/opstrace/opstrace/-/issues/2315
    it('returns true for any status', async () => {
      axiosMock.onGet(provisioningUrl).reply(200, {
        status: 'not ready',
      });

      const enabled = await client.isTracingEnabled();

      expect(enabled).toBe(true);
    });

    it('throws in case of any non-404 error', async () => {
      axiosMock.onGet(provisioningUrl).reply(500);

      const e = 'Request failed with status code 500';
      await expect(client.isTracingEnabled()).rejects.toThrow(e);
      expect(Sentry.captureException).toHaveBeenCalledWith(new Error(e));
    });

    it('throws in case of unexpected response', async () => {
      axiosMock.onGet(provisioningUrl).reply(200, {});

      const e = 'Failed to check provisioning';
      await expect(client.isTracingEnabled()).rejects.toThrow(e);
      expect(Sentry.captureException).toHaveBeenCalledWith(new Error(e));
    });
  });

  describe('enableTraces', () => {
    it('makes a PUT request to the provisioning URL', async () => {
      let putConfig;
      axiosMock.onPut(provisioningUrl).reply((config) => {
        putConfig = config;
        return [200];
      });

      await client.enableTraces();

      expect(putConfig.withCredentials).toBe(true);
    });

    it('reports an error if the req fails', async () => {
      axiosMock.onPut(provisioningUrl).reply(401);

      const e = 'Request failed with status code 401';

      await expect(client.enableTraces()).rejects.toThrow(e);
      expect(Sentry.captureException).toHaveBeenCalledWith(new Error(e));
    });
  });

  describe('fetchTrace', () => {
    it('fetches the trace from the tracing URL', async () => {
      const mockTraces = [
        {
          trace_id: 'trace-1',
          duration_nano: 3000,
          spans: [{ duration_nano: 1000 }, { duration_nano: 2000 }],
        },
      ];

      axiosMock.onGet(tracingUrl).reply(200, {
        traces: mockTraces,
      });

      const result = await client.fetchTrace('trace-1');

      expect(axios.get).toHaveBeenCalledTimes(1);
      expect(axios.get).toHaveBeenCalledWith(tracingUrl, {
        withCredentials: true,
        params: { trace_id: 'trace-1' },
      });
      expect(result).toEqual(mockTraces[0]);
    });

    it('rejects if trace id is missing', () => {
      return expect(client.fetchTrace()).rejects.toThrow('traceId is required.');
    });

    it('rejects if traces are empty', async () => {
      axiosMock.onGet(tracingUrl).reply(200, { traces: [] });

      await expect(client.fetchTrace('trace-1')).rejects.toThrow(FETCHING_TRACES_ERROR);
      expect(Sentry.captureException).toHaveBeenCalledWith(new Error(FETCHING_TRACES_ERROR));
    });

    it('rejects if traces are invalid', async () => {
      axiosMock.onGet(tracingUrl).reply(200, { traces: 'invalid' });

      await expect(client.fetchTraces()).rejects.toThrow(FETCHING_TRACES_ERROR);
      expect(Sentry.captureException).toHaveBeenCalledWith(new Error(FETCHING_TRACES_ERROR));
    });
  });

  describe('fetchTraces', () => {
    it('fetches traces from the tracing URL', async () => {
      const mockTraces = [
        {
          trace_id: 'trace-1',
          duration_nano: 3000,
          spans: [{ duration_nano: 1000 }, { duration_nano: 2000 }],
        },
        { trace_id: 'trace-2', duration_nano: 3000, spans: [{ duration_nano: 2000 }] },
      ];

      axiosMock.onGet(tracingUrl).reply(200, {
        traces: mockTraces,
      });

      const result = await client.fetchTraces();

      expect(axios.get).toHaveBeenCalledTimes(1);
      expect(axios.get).toHaveBeenCalledWith(tracingUrl, {
        withCredentials: true,
        params: new URLSearchParams(),
      });
      expect(result).toEqual(mockTraces);
    });

    it('rejects if traces are missing', async () => {
      axiosMock.onGet(tracingUrl).reply(200, {});

      await expect(client.fetchTraces()).rejects.toThrow(FETCHING_TRACES_ERROR);
      expect(Sentry.captureException).toHaveBeenCalledWith(new Error(FETCHING_TRACES_ERROR));
    });

    it('rejects if traces are invalid', async () => {
      axiosMock.onGet(tracingUrl).reply(200, { traces: 'invalid' });

      await expect(client.fetchTraces()).rejects.toThrow(FETCHING_TRACES_ERROR);
      expect(Sentry.captureException).toHaveBeenCalledWith(new Error(FETCHING_TRACES_ERROR));
    });

    describe('query filter', () => {
      beforeEach(() => {
        axiosMock.onGet(tracingUrl).reply(200, {
          traces: [],
        });
      });

      const getQueryParam = () => decodeURIComponent(axios.get.mock.calls[0][1].params.toString());

      it('does not set any query param without filters', async () => {
        await client.fetchTraces();

        expect(getQueryParam()).toBe('');
      });

      it('converts filter to proper query params', async () => {
        await client.fetchTraces({
          durationMs: [
            { operator: '>', value: '100' },
            { operator: '<', value: '1000' },
          ],
          operation: [
            { operator: '=', value: 'op' },
            { operator: '!=', value: 'not-op' },
          ],
          serviceName: [
            { operator: '=', value: 'service' },
            { operator: '!=', value: 'not-service' },
          ],
          period: [{ operator: '=', value: '5m' }],
          traceId: [
            { operator: '=', value: 'trace-id' },
            { operator: '!=', value: 'not-trace-id' },
          ],
        });
        expect(getQueryParam()).toBe(
          'gt[duration_nano]=100000&lt[duration_nano]=1000000' +
            '&operation=op&not[operation]=not-op' +
            '&service_name=service&not[service_name]=not-service' +
            '&period=5m' +
            '&trace_id=trace-id&not[trace_id]=not-trace-id',
        );
      });

      it('handles repeated params', async () => {
        await client.fetchTraces({
          operation: [
            { operator: '=', value: 'op' },
            { operator: '=', value: 'op2' },
          ],
        });
        expect(getQueryParam()).toBe('operation=op&operation=op2');
      });

      it('ignores unsupported filters', async () => {
        await client.fetchTraces({
          unsupportedFilter: [{ operator: '=', value: 'foo' }],
        });

        expect(getQueryParam()).toBe('');
      });

      it('ignores empty filters', async () => {
        await client.fetchTraces({
          durationMs: null,
          traceId: undefined,
        });

        expect(getQueryParam()).toBe('');
      });

      it('ignores unsupported operators', async () => {
        await client.fetchTraces({
          durationMs: [
            { operator: '*', value: 'foo' },
            { operator: '=', value: 'foo' },
            { operator: '!=', value: 'foo' },
          ],
          operation: [
            { operator: '>', value: 'foo' },
            { operator: '<', value: 'foo' },
          ],
          serviceName: [
            { operator: '>', value: 'foo' },
            { operator: '<', value: 'foo' },
          ],
          period: [{ operator: '!=', value: 'foo' }],
          traceId: [
            { operator: '>', value: 'foo' },
            { operator: '<', value: 'foo' },
          ],
        });

        expect(getQueryParam()).toBe('');
      });
    });
  });
});