diff --git a/src/lib/addons/__snapshots__/datadog.test.ts.snap b/src/lib/addons/__snapshots__/datadog.test.ts.snap index 57c3116b66..c2031adf15 100644 --- a/src/lib/addons/__snapshots__/datadog.test.ts.snap +++ b/src/lib/addons/__snapshots__/datadog.test.ts.snap @@ -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", +} +`; diff --git a/src/lib/addons/datadog-definition.ts b/src/lib/addons/datadog-definition.ts index 8c00af3872..b6db754d9a 100644 --- a/src/lib/addons/datadog-definition.ts +++ b/src/lib/addons/datadog-definition.ts @@ -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', diff --git a/src/lib/addons/datadog.test.ts b/src/lib/addons/datadog.test.ts index 5f52a215cb..43857afa33 100644 --- a/src/lib/addons/datadog.test.ts +++ b/src/lib/addons/datadog.test.ts @@ -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(); +}); diff --git a/src/lib/addons/datadog.ts b/src/lib/addons/datadog.ts index 4393321565..ee03c93d34 100644 --- a/src/lib/addons/datadog.ts +++ b/src/lib/addons/datadog.ts @@ -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 {