1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-24 01:18:01 +02:00

chore: register integration events in New Relic integration (#7636)

https://linear.app/unleash/issue/2-2462/register-integration-events-new-relic

Registers integration events in the **New Relic** integration.

Similar to:
- #7635
- #7634
- #7631
- #7626
- #7621
This commit is contained in:
Nuno Góis 2024-07-23 10:07:31 +01:00 committed by GitHub
parent fe6a758f80
commit 9ff393b3d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 174 additions and 106 deletions

View File

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Should call New Relic Event API for $type toggle 1`] = ` exports[`New Relic integration Should call New Relic Event API for $type toggle 1`] = `
{ {
"Api-Key": "fakeLicenseKey", "Api-Key": "fakeLicenseKey",
"Content-Encoding": "gzip", "Content-Encoding": "gzip",
@ -8,7 +8,7 @@ exports[`Should call New Relic Event API for $type toggle 1`] = `
} }
`; `;
exports[`Should call New Relic Event API for FEATURE_ARCHIVED toggle with project info 1`] = ` exports[`New Relic integration Should call New Relic Event API for FEATURE_ARCHIVED toggle with project info 1`] = `
{ {
"Api-Key": "fakeLicenseKey", "Api-Key": "fakeLicenseKey",
"Content-Encoding": "gzip", "Content-Encoding": "gzip",
@ -16,7 +16,7 @@ exports[`Should call New Relic Event API for FEATURE_ARCHIVED toggle with projec
} }
`; `;
exports[`Should call New Relic Event API for FEATURE_ARCHIVED with project info 1`] = ` exports[`New Relic integration Should call New Relic Event API for FEATURE_ARCHIVED with project info 1`] = `
{ {
"Api-Key": "fakeLicenseKey", "Api-Key": "fakeLicenseKey",
"Content-Encoding": "gzip", "Content-Encoding": "gzip",
@ -24,7 +24,7 @@ exports[`Should call New Relic Event API for FEATURE_ARCHIVED with project info
} }
`; `;
exports[`Should call New Relic Event API for custom body template 1`] = ` exports[`New Relic integration Should call New Relic Event API for custom body template 1`] = `
{ {
"Api-Key": "fakeLicenseKey", "Api-Key": "fakeLicenseKey",
"Content-Encoding": "gzip", "Content-Encoding": "gzip",
@ -32,7 +32,7 @@ exports[`Should call New Relic Event API for custom body template 1`] = `
} }
`; `;
exports[`Should call New Relic Event API for customHeaders in headers when calling service 1`] = ` exports[`New Relic integration Should call New Relic Event API for customHeaders in headers when calling service 1`] = `
{ {
"Api-Key": "fakeLicenseKey", "Api-Key": "fakeLicenseKey",
"Content-Encoding": "gzip", "Content-Encoding": "gzip",
@ -41,7 +41,7 @@ exports[`Should call New Relic Event API for customHeaders in headers when calli
} }
`; `;
exports[`Should call New Relic Event API for toggled environment 1`] = ` exports[`New Relic integration Should call New Relic Event API for toggled environment 1`] = `
{ {
"Api-Key": "fakeLicenseKey", "Api-Key": "fakeLicenseKey",
"Content-Encoding": "gzip", "Content-Encoding": "gzip",

View File

@ -5,6 +5,7 @@ import {
type IFlagResolver, type IFlagResolver,
type IAddonConfig, type IAddonConfig,
type IEvent, type IEvent,
serializeDates,
} from '../types'; } from '../types';
import type { Logger } from '../logger'; import type { Logger } from '../logger';
@ -18,6 +19,7 @@ import type { IntegrationEventsService } from '../services';
const asyncGunzip = promisify(gunzip); const asyncGunzip = promisify(gunzip);
let fetchRetryCalls: any[] = []; let fetchRetryCalls: any[] = [];
const registerEventMock = jest.fn();
const INTEGRATION_ID = 1337; const INTEGRATION_ID = 1337;
const ARGS: IAddonConfig = { const ARGS: IAddonConfig = {
@ -45,11 +47,11 @@ jest.mock(
retries, retries,
backoff, backoff,
}); });
return Promise.resolve({ status: 200 }); return Promise.resolve({ ok: true, status: 200 });
} }
async registerEvent(_) { async registerEvent(event) {
return Promise.resolve(); return registerEventMock(event);
} }
}, },
); );
@ -79,6 +81,11 @@ const makeAddHandleEvent = (event: IEvent, parameters: INewRelicParameters) => {
return () => addon.handleEvent(event, parameters, INTEGRATION_ID); return () => addon.handleEvent(event, parameters, INTEGRATION_ID);
}; };
describe('New Relic integration', () => {
beforeEach(() => {
registerEventMock.mockClear();
});
test.each([ test.each([
{ {
partialEvent: { type: FEATURE_CREATED }, partialEvent: { type: FEATURE_CREATED },
@ -174,3 +181,32 @@ test.each([
expect(jsonBody.createdAt).toBe(event.createdAt.getTime()); expect(jsonBody.createdAt).toBe(event.createdAt.getTime());
}, },
); );
test('Should call registerEvent', async () => {
const handleEvent = makeAddHandleEvent(defaultEvent, defaultParameters);
await handleEvent();
expect(registerEventMock).toHaveBeenCalledTimes(1);
expect(registerEventMock).toHaveBeenCalledWith({
integrationId: INTEGRATION_ID,
state: 'success',
stateDetails:
'New Relic Events API request was successful with status code: 200.',
event: serializeDates(defaultEvent),
details: {
url: defaultParameters.url,
body: {
eventType: 'UnleashServiceEvent',
unleashEventType: defaultEvent.type,
featureName: defaultEvent.featureName,
environment: defaultEvent.environment,
createdBy: defaultEvent.createdBy,
createdByUserId: defaultEvent.createdByUserId,
createdAt: defaultEvent.createdAt.getTime(),
...defaultEvent.data,
},
},
});
});
});

View File

@ -2,7 +2,12 @@ import Addon from './addon';
import definition from './new-relic-definition'; import definition from './new-relic-definition';
import Mustache from 'mustache'; import Mustache from 'mustache';
import type { IAddonConfig, IEvent, IEventType } from '../types'; import {
type IAddonConfig,
type IEvent,
type IEventType,
serializeDates,
} from '../types';
import { import {
type FeatureEventFormatter, type FeatureEventFormatter,
FeatureEventFormatterMd, FeatureEventFormatterMd,
@ -10,6 +15,7 @@ import {
} from './feature-event-formatter-md'; } from './feature-event-formatter-md';
import { gzip } from 'node:zlib'; import { gzip } from 'node:zlib';
import { promisify } from 'util'; import { promisify } from 'util';
import type { IntegrationEventState } from '../features/integration-events/integration-events-store';
const asyncGzip = promisify(gzip); const asyncGzip = promisify(gzip);
@ -46,6 +52,9 @@ export default class NewRelicAddon extends Addon {
parameters: INewRelicParameters, parameters: INewRelicParameters,
integrationId: number, integrationId: number,
): Promise<void> { ): Promise<void> {
let state: IntegrationEventState = 'success';
const stateDetails: string[] = [];
const { url, licenseKey, customHeaders, bodyTemplate } = parameters; const { url, licenseKey, customHeaders, bodyTemplate } = parameters;
const context = { const context = {
event, event,
@ -74,9 +83,11 @@ export default class NewRelicAddon extends Addon {
try { try {
extraHeaders = JSON.parse(customHeaders); extraHeaders = JSON.parse(customHeaders);
} catch (e) { } catch (e) {
this.logger.warn( state = 'successWithErrors';
`Could not parse the json in the customHeaders parameter. [${customHeaders}]`, const badHeadersMessage =
); 'Could not parse the JSON in the customHeaders parameter.';
stateDetails.push(badHeadersMessage);
this.logger.warn(badHeadersMessage);
} }
} }
@ -92,8 +103,29 @@ export default class NewRelicAddon extends Addon {
}; };
const res = await this.fetchRetry(url, requestOpts); const res = await this.fetchRetry(url, requestOpts);
this.logger.info(
`Handled event ${event.type}. Status codes=${res.status}`, this.logger.info(`Handled event "${event.type}".`);
);
if (res.ok) {
const successMessage = `New Relic Events API request was successful with status code: ${res.status}.`;
stateDetails.push(successMessage);
this.logger.info(successMessage);
} else {
state = 'failed';
const failedMessage = `New Relic Events API request failed with status code: ${res.status}.`;
stateDetails.push(failedMessage);
this.logger.warn(failedMessage);
}
this.registerEvent({
integrationId,
state,
stateDetails: stateDetails.join('\n'),
event: serializeDates(event),
details: {
url,
body,
},
});
} }
} }