Skip to content

Commit b803003

Browse files
maria-hambardzumianmaria-hambardzumian
andauthored
EPMRPP-113713 || Introduce the instanceId property (#256)
* EPMRPP-113713 || Introduce the instanceId property * EPMRPP-113713 || crf - 1 --------- Co-authored-by: maria-hambardzumian <maria_hambardzumyan@gmail.com>
1 parent 34007e1 commit b803003

5 files changed

Lines changed: 159 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
### Added
2+
- Google Analytics improvements.
13

24
## [5.5.10] - 2026-02-05
35
### Fixed

__tests__/report-portal-client.spec.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ describe('ReportPortal javascript client', () => {
158158
endpoint: 'https://rp.us/api/v1',
159159
project: 'tst',
160160
});
161+
jest.spyOn(client, 'fetchServerInfo').mockResolvedValue({});
161162
jest.spyOn(client.statistics, 'trackEvent').mockImplementation();
162163

163164
await client.triggerStatisticsEvent();
@@ -218,6 +219,101 @@ describe('ReportPortal javascript client', () => {
218219
}),
219220
);
220221
});
222+
223+
it('should fetch server info and set instanceID before tracking event', async () => {
224+
const client = new RPClient({
225+
apiKey: 'startLaunchTest',
226+
endpoint: 'https://rp.us/api/v1',
227+
project: 'tst',
228+
});
229+
const serverInfoResponse = {
230+
extensions: {
231+
result: {
232+
'server.details.instance': 'test-instance-123',
233+
},
234+
},
235+
};
236+
jest.spyOn(client, 'fetchServerInfo').mockResolvedValue(serverInfoResponse);
237+
jest.spyOn(client.statistics, 'trackEvent').mockImplementation();
238+
jest.spyOn(client.statistics, 'setInstanceID');
239+
240+
await client.triggerStatisticsEvent();
241+
242+
expect(client.fetchServerInfo).toHaveBeenCalled();
243+
expect(client.statistics.setInstanceID).toHaveBeenCalledWith('test-instance-123');
244+
expect(client.statistics.trackEvent).toHaveBeenCalled();
245+
});
246+
247+
it('should still track event with not_set instanceID if fetchServerInfo fails', async () => {
248+
const client = new RPClient({
249+
apiKey: 'startLaunchTest',
250+
endpoint: 'https://rp.us/api/v1',
251+
project: 'tst',
252+
});
253+
jest.spyOn(client, 'fetchServerInfo').mockRejectedValue(new Error('Network error'));
254+
jest.spyOn(client.statistics, 'trackEvent').mockImplementation();
255+
jest.spyOn(client.statistics, 'setInstanceID');
256+
257+
await client.triggerStatisticsEvent();
258+
259+
expect(client.statistics.setInstanceID).toHaveBeenCalledWith('not_set');
260+
expect(client.statistics.trackEvent).toHaveBeenCalled();
261+
});
262+
263+
it('should not set instanceID if server info does not contain it', async () => {
264+
const client = new RPClient({
265+
apiKey: 'startLaunchTest',
266+
endpoint: 'https://rp.us/api/v1',
267+
project: 'tst',
268+
});
269+
jest.spyOn(client, 'fetchServerInfo').mockResolvedValue({});
270+
jest.spyOn(client.statistics, 'trackEvent').mockImplementation();
271+
jest.spyOn(client.statistics, 'setInstanceID');
272+
273+
await client.triggerStatisticsEvent();
274+
275+
expect(client.statistics.setInstanceID).not.toHaveBeenCalled();
276+
expect(client.statistics.trackEvent).toHaveBeenCalled();
277+
});
278+
});
279+
280+
describe('getServerInfoUrl', () => {
281+
it('should return correct info URL for v1 endpoint', () => {
282+
const client = new RPClient({
283+
apiKey: 'test',
284+
project: 'test',
285+
endpoint: 'https://rp.us/api/v1',
286+
});
287+
288+
expect(client.getServerInfoUrl()).toBe('https://rp.us/api/info');
289+
});
290+
291+
it('should return correct info URL for v2 endpoint', () => {
292+
const client = new RPClient({
293+
apiKey: 'test',
294+
project: 'test',
295+
endpoint: 'https://rp.us/api/v2',
296+
});
297+
298+
expect(client.getServerInfoUrl()).toBe('https://rp.us/api/info');
299+
});
300+
});
301+
302+
describe('fetchServerInfo', () => {
303+
it('should call restClient.request with correct URL', async () => {
304+
const client = new RPClient({
305+
apiKey: 'test',
306+
project: 'test',
307+
endpoint: 'https://rp.us/api/v1',
308+
});
309+
const serverInfo = { extensions: { result: {} } };
310+
jest.spyOn(client.restClient, 'request').mockResolvedValue(serverInfo);
311+
312+
const result = await client.fetchServerInfo();
313+
314+
expect(client.restClient.request).toHaveBeenCalledWith('GET', 'https://rp.us/api/info', {});
315+
expect(result).toEqual(serverInfo);
316+
});
221317
});
222318

223319
describe('startLaunch', () => {

__tests__/statistics.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,43 @@ describe('Statistics', () => {
9191
expect(console.error).toHaveBeenCalledWith(errorMessage);
9292
});
9393
});
94+
95+
describe('setInstanceID', () => {
96+
it('should set instanceID in event params', async () => {
97+
jest.spyOn(axios, 'post').mockReturnValue({
98+
send: () => {}, // eslint-disable-line
99+
});
100+
101+
const statistics = new Statistics(eventName, agentParams);
102+
statistics.setInstanceID('test-instance-id');
103+
await statistics.trackEvent();
104+
105+
expect(axios.post).toHaveBeenCalledTimes(1);
106+
expect(axios.post).toHaveBeenCalledWith(
107+
url,
108+
expect.objectContaining({
109+
events: expect.arrayContaining([
110+
expect.objectContaining({
111+
params: expect.objectContaining({
112+
instanceID: 'test-instance-id',
113+
}),
114+
}),
115+
]),
116+
}),
117+
);
118+
});
119+
120+
it('should not include instanceID if setInstanceID was not called', async () => {
121+
jest.spyOn(axios, 'post').mockReturnValue({
122+
send: () => {}, // eslint-disable-line
123+
});
124+
125+
const statistics = new Statistics(eventName, agentParams);
126+
await statistics.trackEvent();
127+
128+
expect(axios.post).toHaveBeenCalledTimes(1);
129+
const callArgs = axios.post.mock.calls[0][1];
130+
expect(callArgs.events[0].params).not.toHaveProperty('instanceID');
131+
});
132+
});
94133
});

lib/report-portal-client.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,28 @@ class RPClient {
146146
return this.restClient.request('GET', url, {});
147147
}
148148

149+
getServerInfoUrl() {
150+
return this.config.endpoint.replace('/v1', '/info').replace('/v2', '/info');
151+
}
152+
153+
async fetchServerInfo() {
154+
const url = this.getServerInfoUrl();
155+
return this.restClient.request('GET', url, {});
156+
}
157+
149158
async triggerStatisticsEvent() {
150159
if (process.env.REPORTPORTAL_CLIENT_JS_NO_ANALYTICS) {
151160
return;
152161
}
162+
try {
163+
const serverInfo = await this.fetchServerInfo();
164+
const instanceID = serverInfo?.extensions?.result?.['server.details.instance'];
165+
if (instanceID) {
166+
this.statistics.setInstanceID(instanceID);
167+
}
168+
} catch (e) {
169+
this.statistics.setInstanceID('not_set');
170+
}
153171
await this.statistics.trackEvent();
154172
}
155173

statistics/statistics.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class Statistics {
2727
return params;
2828
}
2929

30+
setInstanceID(instanceID) {
31+
this.eventParams.instanceID = instanceID;
32+
}
33+
3034
async trackEvent() {
3135
try {
3236
const requestBody = {

0 commit comments

Comments
 (0)