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

client.js « observability « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c55600f3db239494580ffed061ee3133784cb973 (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
import axios from '~/lib/utils/axios_utils';
// import mockData from './mock_traces.json';

function enableTraces() {
  // TODO remove mocks https://gitlab.com/gitlab-org/opstrace/opstrace/-/issues/2271
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, 1000);
  });
}

function isTracingEnabled() {
  // TODO remove mocks https://gitlab.com/gitlab-org/opstrace/opstrace/-/issues/2271
  return new Promise((resolve) => {
    setTimeout(() => {
      // Currently relying on manual provisioning, hence assuming tracing is enabled
      resolve(true);
    }, 1000);
  });
}

function traceWithDuration(trace) {
  // aggregating duration on the client for now, but expecting to be coming from the backend
  // https://gitlab.com/gitlab-org/opstrace/opstrace/-/issues/2274
  const duration = trace.spans[0].duration_nano;
  return {
    ...trace,
    duration: duration / 1000,
  };
}

async function fetchTrace(tracingUrl, traceId) {
  if (!traceId) {
    throw new Error('traceId is required.');
  }

  const { data } = await axios.get(tracingUrl, {
    withCredentials: true,
    params: {
      trace_id: traceId,
    },
  });

  // TODO: Improve local GDK dev experience with tracing https://gitlab.com/gitlab-org/opstrace/opstrace/-/issues/2308
  // const data = mockData;
  // const trace = data.traces.find((t) => t.trace_id === traceId);

  if (!Array.isArray(data.traces) || data.traces.length === 0) {
    throw new Error('traces are missing/invalid in the response.'); // eslint-disable-line @gitlab/require-i18n-strings
  }

  const trace = data.traces[0];
  return traceWithDuration(trace);
}

/**
 * Filters (and operators) allowed by tracing query API
 */
const SUPPORTED_FILTERS = {
  durationMs: ['>', '<'],
  operation: ['=', '!='],
  serviceName: ['=', '!='],
  period: ['='],
  traceId: ['=', '!='],
  // free-text 'search' temporarily ignored https://gitlab.com/gitlab-org/opstrace/opstrace/-/issues/2309
};

/**
 * Mapping of filter name to query param
 */
const FILTER_TO_QUERY_PARAM = {
  durationMs: 'duration_nano',
  operation: 'operation',
  serviceName: 'service_name',
  period: 'period',
  traceId: 'trace_id',
};

const FILTER_OPERATORS_PREFIX = {
  '!=': 'not',
  '>': 'gt',
  '<': 'lt',
};

/**
 * Builds the query param name for the given filter and operator
 *
 * @param {String} filterName - The filter name
 * @param {String} operator - The operator
 * @returns String | undefined - Query param name
 */
function getFilterParamName(filterName, operator) {
  const paramKey = FILTER_TO_QUERY_PARAM[filterName];
  if (!paramKey) return undefined;

  if (operator === '=') {
    return paramKey;
  }

  const prefix = FILTER_OPERATORS_PREFIX[operator];
  if (prefix) {
    return `${prefix}[${paramKey}]`;
  }

  return undefined;
}

/**
 * Builds URLSearchParams from a filter object of type { [filterName]: undefined | null | Array<{operator: String, value: any} }
 *  e.g:
 *
 *  filterObj =  {
 *      durationMs: [{operator: '>', value: '100'}, {operator: '<', value: '1000' }],
 *      operation: [{operator: '=', value: 'someOp' }],
 *      serviceName: [{operator: '!=', value: 'foo' }]
 *    }
 *
 * It handles converting the filter to the proper supported query params
 *
 * @param {Object} filterObj : An Object representing filters
 * @returns URLSearchParams
 */
function filterObjToQueryParams(filterObj) {
  const filterParams = new URLSearchParams();

  Object.keys(SUPPORTED_FILTERS).forEach((filterName) => {
    const filterValues = filterObj[filterName] || [];
    const supportedFilters = filterValues.filter((f) =>
      SUPPORTED_FILTERS[filterName].includes(f.operator),
    );
    supportedFilters.forEach(({ operator, value: rawValue }) => {
      const paramName = getFilterParamName(filterName, operator);

      let value = rawValue;
      if (filterName === 'durationMs') {
        // converting durationMs to duration_nano
        value *= 1000;
      }

      if (paramName && value) {
        filterParams.append(paramName, value);
      }
    });
  });
  return filterParams;
}

/**
 * Fetches traces with given tracing API URL and filters
 *
 * @param {String} tracingUrl : The API base URL
 * @param {Object} filters : A filter object of type: { [filterName]: undefined | null | Array<{operator: String, value: String} }
 *  e.g:
 *
 *    {
 *      durationMs: [ {operator: '>', value: '100'}, {operator: '<', value: '1000'}],
 *      operation: [ {operator: '=', value: 'someOp}],
 *      serviceName: [ {operator: '!=', value: 'foo}]
 *    }
 *
 * @returns Array<Trace> : A list of traces
 */
async function fetchTraces(tracingUrl, filters = {}) {
  const filterParams = filterObjToQueryParams(filters);

  const { data } = await axios.get(tracingUrl, {
    withCredentials: true,
    params: filterParams,
  });
  // TODO: Improve local GDK dev experience with tracing https://gitlab.com/gitlab-org/opstrace/opstrace/-/issues/2308
  // Uncomment the line below to test this locally
  // const data = mockData;

  if (!Array.isArray(data.traces)) {
    throw new Error('traces are missing/invalid in the response.'); // eslint-disable-line @gitlab/require-i18n-strings
  }
  return data.traces.map(traceWithDuration);
}

export function buildClient({ provisioningUrl, tracingUrl }) {
  return {
    enableTraces: () => enableTraces(provisioningUrl),
    isTracingEnabled: () => isTracingEnabled(provisioningUrl),
    fetchTraces: (filters) => fetchTraces(tracingUrl, filters),
    fetchTrace: (traceId) => fetchTrace(tracingUrl, traceId),
  };
}