mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-25 00:07:47 +01:00
chore: register integration events in Datadog integration (#7635)
https://linear.app/unleash/issue/2-2461/register-integration-events-datadog Registers integration events in the **Datadog** integration. Similar to: - #7634 - #7631 - #7626 - #7621
This commit is contained in:
parent
e07ded9455
commit
47ff73afb2
@ -1,18 +1,18 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`Should call datadog webhook for archived toggle 1`] = `"{"text":"%%% \\n *some@user.com* archived *some-toggle* in project ** \\n %%% ","title":"Unleash notification update"}"`;
|
||||
exports[`Datadog integration Should call datadog webhook for archived toggle 1`] = `"{"text":"%%% \\n *some@user.com* archived *some-toggle* in project ** \\n %%% ","title":"Unleash notification update"}"`;
|
||||
|
||||
exports[`Should call datadog webhook for archived toggle with project info 1`] = `"{"text":"%%% \\n *some@user.com* archived *some-toggle* in project *[some-project](http://some-url.com/projects/some-project)* \\n %%% ","title":"Unleash notification update"}"`;
|
||||
exports[`Datadog integration Should call datadog webhook for archived toggle with project info 1`] = `"{"text":"%%% \\n *some@user.com* archived *some-toggle* in project *[some-project](http://some-url.com/projects/some-project)* \\n %%% ","title":"Unleash notification update"}"`;
|
||||
|
||||
exports[`Should call datadog webhook 1`] = `"{"text":"%%% \\n *some@user.com* created *[some-toggle](http://some-url.com/projects//features/some-toggle)* in project ** \\n %%% ","title":"Unleash notification update"}"`;
|
||||
exports[`Datadog integration Should call datadog webhook 1`] = `"{"text":"%%% \\n *some@user.com* created *[some-toggle](http://some-url.com/projects//features/some-toggle)* in project ** \\n %%% ","title":"Unleash notification update"}"`;
|
||||
|
||||
exports[`Should call datadog webhook for toggled environment 1`] = `"{"text":"%%% \\n *some@user.com* disabled *[some-toggle](http://some-url.com/projects/default/features/some-toggle)* for the *development* environment in project *[default](http://some-url.com/projects/default)* \\n %%% ","title":"Unleash notification update"}"`;
|
||||
exports[`Datadog integration Should call datadog webhook for toggled environment 1`] = `"{"text":"%%% \\n *some@user.com* disabled *[some-toggle](http://some-url.com/projects/default/features/some-toggle)* for the *development* environment in project *[default](http://some-url.com/projects/default)* \\n %%% ","title":"Unleash notification update"}"`;
|
||||
|
||||
exports[`Should call datadog webhook with JSON when template set 1`] = `"{"text":"{\\n \\"event\\": \\"feature-created\\",\\n \\"createdBy\\": \\"some@user.com\\"\\n}","title":"Unleash notification update"}"`;
|
||||
exports[`Datadog integration Should call datadog webhook with JSON when template set 1`] = `"{"text":"{\\n \\"event\\": \\"feature-created\\",\\n \\"createdBy\\": \\"some@user.com\\"\\n}","title":"Unleash notification update"}"`;
|
||||
|
||||
exports[`Should include customHeaders in headers when calling service 1`] = `"{"text":"%%% \\n *some@user.com* disabled *[some-toggle](http://some-url.com/projects/default/features/some-toggle)* for the *development* environment in project *[default](http://some-url.com/projects/default)* \\n %%% ","title":"Unleash notification update"}"`;
|
||||
exports[`Datadog integration Should include customHeaders in headers when calling service 1`] = `"{"text":"%%% \\n *some@user.com* disabled *[some-toggle](http://some-url.com/projects/default/features/some-toggle)* for the *development* environment in project *[default](http://some-url.com/projects/default)* \\n %%% ","title":"Unleash notification update"}"`;
|
||||
|
||||
exports[`Should include customHeaders in headers when calling service 2`] = `
|
||||
exports[`Datadog integration Should include customHeaders in headers when calling service 2`] = `
|
||||
{
|
||||
"Content-Type": "application/json",
|
||||
"DD-API-KEY": "fakeKey",
|
||||
@ -20,9 +20,9 @@ exports[`Should include customHeaders in headers when calling service 2`] = `
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`Should not include source_type_name when included in the config 1`] = `"{"text":"%%% \\n *some@user.com* disabled *[some-toggle](http://some-url.com/projects/default/features/some-toggle)* for the *development* environment in project *[default](http://some-url.com/projects/default)* \\n %%% ","title":"Unleash notification update","source_type_name":"my-custom-source-type"}"`;
|
||||
exports[`Datadog integration Should not include source_type_name when included in the config 1`] = `"{"text":"%%% \\n *some@user.com* disabled *[some-toggle](http://some-url.com/projects/default/features/some-toggle)* for the *development* environment in project *[default](http://some-url.com/projects/default)* \\n %%% ","title":"Unleash notification update","source_type_name":"my-custom-source-type"}"`;
|
||||
|
||||
exports[`Should not include source_type_name when included in the config 2`] = `
|
||||
exports[`Datadog integration Should not include source_type_name when included in the config 2`] = `
|
||||
{
|
||||
"Content-Type": "application/json",
|
||||
"DD-API-KEY": "fakeKey",
|
||||
|
@ -9,10 +9,15 @@ import type { Logger } from '../logger';
|
||||
import DatadogAddon from './datadog';
|
||||
|
||||
import noLogger from '../../test/fixtures/no-logger';
|
||||
import type { IAddonConfig, IFlagResolver } from '../types';
|
||||
import {
|
||||
serializeDates,
|
||||
type IAddonConfig,
|
||||
type IFlagResolver,
|
||||
} from '../types';
|
||||
import type { IntegrationEventsService } from '../services';
|
||||
|
||||
let fetchRetryCalls: any[] = [];
|
||||
const registerEventMock = jest.fn();
|
||||
|
||||
const INTEGRATION_ID = 1337;
|
||||
const ARGS: IAddonConfig = {
|
||||
@ -40,16 +45,21 @@ jest.mock(
|
||||
retries,
|
||||
backoff,
|
||||
});
|
||||
return Promise.resolve({ status: 200 });
|
||||
return Promise.resolve({ ok: true, status: 200 });
|
||||
}
|
||||
|
||||
async registerEvent(_) {
|
||||
return Promise.resolve();
|
||||
async registerEvent(event) {
|
||||
return registerEventMock(event);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
test('Should call datadog webhook', async () => {
|
||||
describe('Datadog integration', () => {
|
||||
beforeEach(() => {
|
||||
registerEventMock.mockClear();
|
||||
});
|
||||
|
||||
test('Should call datadog webhook', async () => {
|
||||
const addon = new DatadogAddon(ARGS);
|
||||
const event: IEvent = {
|
||||
id: 1,
|
||||
@ -74,9 +84,9 @@ test('Should call datadog webhook', async () => {
|
||||
expect(fetchRetryCalls.length).toBe(1);
|
||||
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
test('Should call datadog webhook for archived toggle', async () => {
|
||||
test('Should call datadog webhook for archived toggle', async () => {
|
||||
const addon = new DatadogAddon(ARGS);
|
||||
const event: IEvent = {
|
||||
id: 2,
|
||||
@ -99,9 +109,9 @@ test('Should call datadog webhook for archived toggle', async () => {
|
||||
expect(fetchRetryCalls.length).toBe(1);
|
||||
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
test('Should call datadog webhook for archived toggle with project info', async () => {
|
||||
test('Should call datadog webhook for archived toggle with project info', async () => {
|
||||
const addon = new DatadogAddon(ARGS);
|
||||
const event: IEvent = {
|
||||
id: 2,
|
||||
@ -125,9 +135,9 @@ test('Should call datadog webhook for archived toggle with project info', async
|
||||
expect(fetchRetryCalls.length).toBe(1);
|
||||
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
test('Should call datadog webhook for toggled environment', async () => {
|
||||
test('Should call datadog webhook for toggled environment', async () => {
|
||||
const addon = new DatadogAddon(ARGS);
|
||||
const event: IEvent = {
|
||||
id: 2,
|
||||
@ -153,9 +163,9 @@ test('Should call datadog webhook for toggled environment', async () => {
|
||||
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
||||
expect(fetchRetryCalls[0].options.body).toMatch(/disabled/);
|
||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
test('Should include customHeaders in headers when calling service', async () => {
|
||||
test('Should include customHeaders in headers when calling service', async () => {
|
||||
const addon = new DatadogAddon(ARGS);
|
||||
const event: IEvent = {
|
||||
id: 2,
|
||||
@ -182,9 +192,9 @@ test('Should include customHeaders in headers when calling service', async () =>
|
||||
expect(fetchRetryCalls[0].options.body).toMatch(/disabled/);
|
||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||
expect(fetchRetryCalls[0].options.headers).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
test('Should not include source_type_name when included in the config', async () => {
|
||||
test('Should not include source_type_name when included in the config', async () => {
|
||||
const addon = new DatadogAddon(ARGS);
|
||||
const event: IEvent = {
|
||||
id: 2,
|
||||
@ -214,9 +224,9 @@ test('Should not include source_type_name when included in the config', async ()
|
||||
);
|
||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||
expect(fetchRetryCalls[0].options.headers).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
test('Should call datadog webhook with JSON when template set', async () => {
|
||||
test('Should call datadog webhook with JSON when template set', async () => {
|
||||
const addon = new DatadogAddon(ARGS);
|
||||
const event: IEvent = {
|
||||
id: 1,
|
||||
@ -243,4 +253,58 @@ test('Should call datadog webhook with JSON when template set', async () => {
|
||||
expect(fetchRetryCalls.length).toBe(1);
|
||||
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('Should call registerEvent', async () => {
|
||||
const addon = new DatadogAddon(ARGS);
|
||||
const event: IEvent = {
|
||||
id: 1,
|
||||
createdAt: new Date(),
|
||||
type: FEATURE_CREATED,
|
||||
createdBy: 'some@user.com',
|
||||
createdByUserId: -1337,
|
||||
featureName: 'some-toggle',
|
||||
data: {
|
||||
name: 'some-toggle',
|
||||
enabled: false,
|
||||
strategies: [{ name: 'default' }],
|
||||
},
|
||||
tags: [
|
||||
{
|
||||
type: 'test',
|
||||
value: '1',
|
||||
},
|
||||
{
|
||||
type: 'test',
|
||||
value: '2',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const parameters = {
|
||||
url: 'http://api.datadoghq.com/api/v1/events',
|
||||
apiKey: 'fakeKey',
|
||||
bodyTemplate:
|
||||
'{\n "event": "{{event.type}}",\n "createdBy": "{{event.createdBy}}"\n}',
|
||||
};
|
||||
|
||||
await addon.handleEvent(event, parameters, INTEGRATION_ID);
|
||||
|
||||
expect(registerEventMock).toHaveBeenCalledTimes(1);
|
||||
expect(registerEventMock).toHaveBeenCalledWith({
|
||||
integrationId: INTEGRATION_ID,
|
||||
state: 'success',
|
||||
stateDetails:
|
||||
'Datadog Events API request was successful with status code: 200.',
|
||||
event: serializeDates(event),
|
||||
details: {
|
||||
url: parameters.url,
|
||||
body: {
|
||||
text: `{\n "event": "${event.type}",\n "createdBy": "${event.createdBy}"\n}`,
|
||||
title: 'Unleash notification update',
|
||||
tags: ['test:1', 'test:2'],
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -2,13 +2,14 @@ import Addon from './addon';
|
||||
|
||||
import definition from './datadog-definition';
|
||||
import Mustache from 'mustache';
|
||||
import type { IAddonConfig } from '../types/model';
|
||||
import { type IAddonConfig, serializeDates } from '../types';
|
||||
import {
|
||||
type FeatureEventFormatter,
|
||||
FeatureEventFormatterMd,
|
||||
LinkStyle,
|
||||
} from './feature-event-formatter-md';
|
||||
import type { IEvent } from '../types/events';
|
||||
import type { IntegrationEventState } from '../features/integration-events/integration-events-store';
|
||||
|
||||
interface IDatadogParameters {
|
||||
url: string;
|
||||
@ -41,6 +42,9 @@ export default class DatadogAddon extends Addon {
|
||||
parameters: IDatadogParameters,
|
||||
integrationId: number,
|
||||
): Promise<void> {
|
||||
let state: IntegrationEventState = 'success';
|
||||
const stateDetails: string[] = [];
|
||||
|
||||
const {
|
||||
url = 'https://api.datadoghq.com/api/v1/events',
|
||||
apiKey,
|
||||
@ -75,9 +79,11 @@ export default class DatadogAddon extends Addon {
|
||||
try {
|
||||
extraHeaders = JSON.parse(customHeaders);
|
||||
} catch (e) {
|
||||
this.logger.warn(
|
||||
`Could not parse the json in the customHeaders parameter. [${customHeaders}]`,
|
||||
);
|
||||
state = 'successWithErrors';
|
||||
const badHeadersMessage =
|
||||
'Could not parse the JSON in the customHeaders parameter.';
|
||||
stateDetails.push(badHeadersMessage);
|
||||
this.logger.warn(badHeadersMessage);
|
||||
}
|
||||
}
|
||||
const requestOpts = {
|
||||
@ -90,8 +96,29 @@ export default class DatadogAddon extends Addon {
|
||||
body: JSON.stringify(body),
|
||||
};
|
||||
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 = `Datadog Events API request was successful with status code: ${res.status}.`;
|
||||
stateDetails.push(successMessage);
|
||||
this.logger.info(successMessage);
|
||||
} else {
|
||||
state = 'failed';
|
||||
const failedMessage = `Datadog 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,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user