mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-20 00:08:02 +01:00
chore: Datadog addon, ability to include source type name (#4196)
## About the changes Include a new configuration parameter to be able to specify source_type_name. This is an opt-in feature which provides backward compatibility to our existing users. ![image](https://github.com/Unleash/unleash/assets/455064/0e65584f-f601-4f17-b7a5-e73dae55772e) Closes #4109 ## Discussion points Maybe this should be hardcoded to `Unleash` but this gives additional flexibility
This commit is contained in:
parent
d5ef1dda0a
commit
b37851acea
@ -17,3 +17,12 @@ exports[`Should include customHeaders in headers when calling service 2`] = `
|
||||
"MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE",
|
||||
}
|
||||
`;
|
||||
|
||||
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) in *development* environment in project *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`] = `
|
||||
{
|
||||
"Content-Type": "application/json",
|
||||
"DD-API-KEY": "fakeKey",
|
||||
}
|
||||
`;
|
||||
|
@ -40,6 +40,15 @@ const dataDogDefinition: IAddonDefinition = {
|
||||
required: true,
|
||||
sensitive: true,
|
||||
},
|
||||
{
|
||||
name: 'sourceTypeName',
|
||||
displayName: 'Datadog Source Type Name',
|
||||
description:
|
||||
'(Optional) source_type_name parameter to be included in Datadog events.',
|
||||
type: 'text',
|
||||
required: false,
|
||||
sensitive: false,
|
||||
},
|
||||
{
|
||||
name: 'customHeaders',
|
||||
displayName: 'Extra HTTP Headers',
|
||||
|
@ -179,3 +179,37 @@ test(`Should include customHeaders in headers when calling service`, async () =>
|
||||
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 () => {
|
||||
const addon = new DatadogAddon({
|
||||
getLogger: noLogger,
|
||||
unleashUrl: 'http://some-url.com',
|
||||
});
|
||||
const event: IEvent = {
|
||||
id: 2,
|
||||
createdAt: new Date(),
|
||||
type: FEATURE_ENVIRONMENT_DISABLED,
|
||||
createdBy: 'some@user.com',
|
||||
environment: 'development',
|
||||
project: 'default',
|
||||
featureName: 'some-toggle',
|
||||
data: {
|
||||
name: 'some-toggle',
|
||||
},
|
||||
};
|
||||
|
||||
const parameters = {
|
||||
url: 'http://hooks.slack.com',
|
||||
apiKey: 'fakeKey',
|
||||
sourceTypeName: 'my-custom-source-type',
|
||||
};
|
||||
|
||||
await addon.handleEvent(event, parameters);
|
||||
expect(fetchRetryCalls).toHaveLength(1);
|
||||
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
||||
expect(fetchRetryCalls[0].options.body).toMatch(
|
||||
/"source_type_name":"my-custom-source-type"/,
|
||||
);
|
||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||
expect(fetchRetryCalls[0].options.headers).toMatchSnapshot();
|
||||
});
|
||||
|
@ -12,9 +12,17 @@ import { IEvent } from '../types/events';
|
||||
interface IDatadogParameters {
|
||||
url: string;
|
||||
apiKey: string;
|
||||
sourceTypeName?: string;
|
||||
customHeaders?: string;
|
||||
}
|
||||
|
||||
interface DDRequestBody {
|
||||
text: string;
|
||||
title: string;
|
||||
tags?: string[];
|
||||
source_type_name?: string;
|
||||
}
|
||||
|
||||
export default class DatadogAddon extends Addon {
|
||||
private msgFormatter: FeatureEventFormatter;
|
||||
|
||||
@ -26,7 +34,6 @@ export default class DatadogAddon extends Addon {
|
||||
);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
async handleEvent(
|
||||
event: IEvent,
|
||||
parameters: IDatadogParameters,
|
||||
@ -34,6 +41,7 @@ export default class DatadogAddon extends Addon {
|
||||
const {
|
||||
url = 'https://api.datadoghq.com/api/v1/events',
|
||||
apiKey,
|
||||
sourceTypeName,
|
||||
customHeaders,
|
||||
} = parameters;
|
||||
|
||||
@ -42,11 +50,14 @@ export default class DatadogAddon extends Addon {
|
||||
const { tags: eventTags } = event;
|
||||
const tags =
|
||||
eventTags && eventTags.map((tag) => `${tag.type}:${tag.value}`);
|
||||
const body = {
|
||||
const body: DDRequestBody = {
|
||||
text: `%%% \n ${text} \n %%% `,
|
||||
title: 'Unleash notification update',
|
||||
tags,
|
||||
};
|
||||
if (sourceTypeName) {
|
||||
body.source_type_name = sourceTypeName;
|
||||
}
|
||||
let extraHeaders = {};
|
||||
if (typeof customHeaders === 'string' && customHeaders.length > 1) {
|
||||
try {
|
||||
|
Loading…
Reference in New Issue
Block a user