-
Notifications
You must be signed in to change notification settings - Fork 6
/
DDEvent.cpp
242 lines (203 loc) · 5.67 KB
/
DDEvent.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "datadogstatsd/DDEvent.h"
using namespace std;
DDEvent::DDEvent(string title, string text)
{
this->title = title;
this->text = text;
//Set up the defaults
priority = Priority::NOT_SET;
alertType = AlertType::NOT_SET;
}
/// <summary>Update the title</summary>
/// <param name="title">The title text that should be sent to datadog (limited to 100 characters</param>
void DDEvent::setTitle(string title)
{
this->title = title;
}
/// <summary>Update the event text</summary>
/// <param name="text">The event text that should be sent to datadog (limited to 4000 characters)</param>
void DDEvent::setText(string text)
{
this->text = text;
}
/// <summary>Update the epoch time for the event (if this is not used then the current time is used at the point of the constructor instantiation</summary>
/// <param name="dateHappend">size_t representation of the epoch time</param>
void DDEvent::setDateHappened(size_t dateHappened)
{
stringstream dateStream;
dateStream << dateHappened;
this->dateHappened = dateStream.str();
}
/// <summary>The priority of the event</summary>
/// <param name="priority">An enum of priority Low or Normal</param>
void DDEvent::setPriority(DDEvent::Priority priority)
{
this->priority = priority;
}
/// <summary>Set the host string</summary>
/// <param name="host">The host name to send to datadog</param>
void DDEvent::setHost(string host)
{
this->host = host;
}
/// <summary>Set the alert type, if not used defaults to Info</summary>
/// <param name="alertType">An AlertType enum value</param>
void DDEvent::setAlertType(DDEvent::AlertType alertType)
{
this->alertType = alertType;
}
/// <summary>Set the aggregation key</summary>
/// <param name="aggegationKey">The aggregation key value to send to datadog</param>
void DDEvent::setAggregationKey(string aggregationKey)
{
this->aggregationKey = aggregationKey;
}
void DDEvent::setTags(string tags)
{
this->tags = tags;
}
void DDEvent::setTags(std::map<string, string> tags)
{
for (auto const &iter : tags)
{
stringstream tag_string;
tag_string << iter.first << ":" << iter.second;
this->tagsList.push_back(tag_string.str());
}
}
void DDEvent::setTags(std::vector<string> tags)
{
this->tagsList = tags;
}
/// <summary>Return the DDEvent object in the encoded format required for DataDog UDP Event Submissions</summary>
string DDEvent::returnDDEventUDPString()
{
stringstream fields;
fields << this->title;
if (!this->text.empty())
{
string tempString = this->text;
this->stringReplace(tempString, "\n", "\\n");
fields << "|" << tempString;
}
else
{
fields << "|";
}
if (!this->dateHappened.empty())
{
fields << "|d:" << this->dateHappened;
}
if (!this->host.empty())
{
fields << "|h:" << this->host;
}
if (!this->aggregationKey.empty())
{
fields << "|k:" << this->aggregationKey;
}
if (this->priority != DDEvent::Priority::NOT_SET)
{
fields << "|p:" << this->getPriorityStringFromEnum(this->priority);
}
if (this->alertType != DDEvent::AlertType::NOT_SET)
{
fields << "|t:" << this->getAlertTypeStringFromEnum(this->alertType);
}
if (!this->tags.empty())
{
fields << this->tags;
}
int titleLength = this->title.length();
int textLength = this->text.length();
stringstream outputString;
outputString << "_e{" << titleLength << "," << textLength << "}:" << fields.str();
return outputString.str();
}
void DDEvent::getDDEventAsJSONString(std::string *jsonString)
{
rapidjson::Document document;
document.SetObject();
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
document.AddMember("title", this->title, allocator);
if (!this->text.empty())
{
document.AddMember("text", this->text, allocator);
}
if (!this->dateHappened.empty())
{
document.AddMember("date_happened", this->dateHappened, allocator);
}
if (this->priority != DDEvent::Priority::NOT_SET)
{
document.AddMember("priority", this->getPriorityStringFromEnum(this->priority), allocator);
}
if (this->alertType != DDEvent::AlertType::NOT_SET)
{
document.AddMember("alert_type", this->getAlertTypeStringFromEnum(this->alertType), allocator);
}
if (!this->host.empty())
{
document.AddMember("host", this->host, allocator);
}
if (!this->aggregationKey.empty())
{
document.AddMember("aggregation_key", this->aggregationKey, allocator);
}
//If a basic string has been set as a tag, and the tags list hasn't been set, then add the string to the vector
if (!this->tags.empty() && this->tagsList.size() == 0)
{
this->tagsList.push_back(this->tags);
}
if (this->tagsList.size() > 0)
{
rapidjson::Value tagsJSONArray(rapidjson::kArrayType);
for (auto const &iter : this->tagsList)
{
rapidjson::Value jsonString;
jsonString.SetString(iter.c_str(), iter.length(), allocator);
tagsJSONArray.PushBack(jsonString, allocator);
}
document.AddMember("tags", tagsJSONArray, allocator);
}
rapidjson::StringBuffer buffer;
buffer.Clear();
rapidjson::Writer<rapidjson::StringBuffer>writer(buffer);
document.Accept(writer);
*jsonString = buffer.GetString();
}
std::string DDEvent::getPriorityStringFromEnum(DDEvent::Priority priority)
{
switch (priority)
{
case Priority::LOW:
return "low";
case Priority::NORMAL:
return "normal";
default:
return "";
}
}
std::string DDEvent::getAlertTypeStringFromEnum(DDEvent::AlertType alertType)
{
switch (alertType)
{
case AlertType::INFO:
return "info";
case AlertType::DD_ERROR:
return "error";
case AlertType::SUCCESS:
return "success";
case AlertType::WARNING:
return "warning";
default:
return "";
}
}
void DDEvent::stringReplace(string& source, string needle, string replace)
{
while (source.find(needle) != std::string::npos)
{
source.replace(source.find(needle), needle.length(), replace);
}
}