-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathdatadog.ts
129 lines (115 loc) · 3.8 KB
/
datadog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import Addon from './addon';
import definition from './datadog-definition';
import Mustache from 'mustache';
import {
type IAddonConfig,
type IFlagResolver,
serializeDates,
} from '../types';
import {
type FeatureEventFormatter,
FeatureEventFormatterMd,
} 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;
apiKey: string;
sourceTypeName?: string;
customHeaders?: string;
bodyTemplate?: string;
}
interface DDRequestBody {
text: string;
title: string;
tags?: string[];
source_type_name?: string;
}
export default class DatadogAddon extends Addon {
private msgFormatter: FeatureEventFormatter;
declare flagResolver: IFlagResolver;
constructor(config: IAddonConfig) {
super(definition, config);
this.msgFormatter = new FeatureEventFormatterMd({
unleashUrl: config.unleashUrl,
});
this.flagResolver = config.flagResolver;
}
async handleEvent(
event: IEvent,
parameters: IDatadogParameters,
integrationId: number,
): Promise<void> {
let state: IntegrationEventState = 'success';
const stateDetails: string[] = [];
const {
url = 'https://api.datadoghq.com/api/v1/events',
apiKey,
sourceTypeName,
customHeaders,
bodyTemplate,
} = parameters;
const context = {
event,
};
let text: string;
if (typeof bodyTemplate === 'string' && bodyTemplate.length > 1) {
text = Mustache.render(bodyTemplate, context);
} else {
text = `%%% \n ${this.msgFormatter.format(event).text} \n %%% `;
}
const { tags: eventTags } = event;
const tags = eventTags?.map((tag) => `${tag.type}:${tag.value}`);
const body: DDRequestBody = {
text: text,
title: 'Unleash notification update',
tags,
};
if (sourceTypeName) {
body.source_type_name = sourceTypeName;
}
let extraHeaders = {};
if (typeof customHeaders === 'string' && customHeaders.length > 1) {
try {
extraHeaders = JSON.parse(customHeaders);
} catch (e) {
state = 'successWithErrors';
const badHeadersMessage =
'Could not parse the JSON in the customHeaders parameter.';
stateDetails.push(badHeadersMessage);
this.logger.warn(badHeadersMessage);
}
}
const requestOpts = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'DD-API-KEY': apiKey,
...extraHeaders,
},
body: JSON.stringify(body),
};
const res = await this.fetchRetry(url, requestOpts);
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,
},
});
}
}