-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathDynatrace.php
More file actions
182 lines (155 loc) · 5.08 KB
/
Dynatrace.php
File metadata and controls
182 lines (155 loc) · 5.08 KB
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
<?php
namespace Utopia\Logger\Adapter;
use Exception;
use Utopia\Logger\Adapter;
use Utopia\Logger\Log;
use Utopia\Logger\Logger;
// Reference Material
// https://www.dynatrace.com/support/help/dynatrace-api/environment-api/events-v2
class Dynatrace extends Adapter
{
/**
* @var string (required, can be found in Dynatrace -> Access tokens)
*/
protected string $apiKey;
/**
* @var string (required, this is part of Dynatrace endpoint: 'https://{{THIS_PART}}.live.dynatrace.com/api/v2/events/ingest')
*/
protected string $environmentId;
/**
* Return unique adapter name
*
* @return string
*/
public static function getName(): string
{
return "dynatrace";
}
/**
* Push log to external provider
*
* @param Log $log
* @return int
* @throws Exception
*/
public function push(Log $log): int
{
$id = empty($log->getUser()) ? null : $log->getUser()->getId();
$email = empty($log->getUser()) ? null : $log->getUser()->getEmail();
$username = empty($log->getUser()) ? null : $log->getUser()->getUsername();
$breadcrumbsObject = $log->getBreadcrumbs();
$breadcrumbsArray = [];
foreach ($breadcrumbsObject as $breadcrumb) {
\array_push($breadcrumbsArray, [
'type' => $breadcrumb->getType(),
'category' => $breadcrumb->getCategory(),
'message' => $breadcrumb->getMessage(),
'timestamp' => \intval($breadcrumb->getTimestamp())
]);
}
$stackFrames = [];
if (isset($log->getExtra()['detailedTrace'])) {
foreach ($log->getExtra()['detailedTrace'] as $trace) {
\array_push($stackFrames, [
'filename' => $trace['file'],
'lineno' => $trace['line'],
'function' => $trace['function'],
]);
}
}
$tags = array();
foreach ($log->getTags() as $tagKey => $tagValue) {
$tags[$tagKey] = $tagValue;
}
if (!empty($log->getType())) {
$tags['type'] = $log->getType();
}
if (!empty($log->getVersion())) {
$tags['version'] = $log->getVersion();
}
if (!empty($log->getEnvironment())) {
$tags['environment'] = $log->getEnvironment();
}
if (!empty($log->getAction())) {
$tags['action'] = $log->getAction();
}
if (!empty($log->getNamespace())) {
$tags['namespace'] = $log->getNamespace();
}
if (!empty($log->getServer())) {
$tags['server'] = $log->getServer();
}
$tags['userId'] = $id;
$tags['userEmail'] = $email;
$tags['userName'] = $username;
$tags['stacktrace'] = $stackFrames;
$tags['breadcrumbs'] = $breadcrumbsArray;
// prepare log (request body)
$requestBody = [
'eventType' => 'ERROR_EVENT',
'title' => $log->getMessage(),
'startTime' => \intval($log->getTimestamp()),
'endTime' => \intval($log->getTimestamp()),
'properties' => $tags
];
// init curl object
$ch = \curl_init();
// define options
$optArray = array(
CURLOPT_URL => 'https://' . $this->environmentId . '.live.dynatrace.com/api/v2/events/ingest',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => \json_encode($requestBody),
CURLOPT_HEADEROPT => \CURLHEADER_UNIFIED,
CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'Authorization: Api-Token ' . $this->apiKey)
);
// apply those options
\curl_setopt_array($ch, $optArray);
// execute request and get response
$result = curl_exec($ch);
$response = curl_getinfo($ch, \CURLINFO_HTTP_CODE);
if(!$result && $response >= 400) {
throw new Exception("Log could not be pushed with status code " . $response . ": " . \curl_error($ch));
}
\curl_close($ch);
return $response;
}
/**
* Dynatrace constructor.
*
* @param string $configKey
*/
public function __construct(string $configKey)
{
$configChunks = \explode(";", $configKey);
$this->apiKey = $configChunks[0];
$this->environmentId = $configChunks[1];
}
public function getSupportedTypes(): array
{
return [
Log::TYPE_INFO,
Log::TYPE_DEBUG,
Log::TYPE_VERBOSE,
Log::TYPE_WARNING,
Log::TYPE_ERROR
];
}
public function getSupportedEnvironments(): array
{
return [
Log::ENVIRONMENT_STAGING,
Log::ENVIRONMENT_PRODUCTION
];
}
public function getSupportedBreadcrumbTypes(): array
{
return [
Log::TYPE_INFO,
Log::TYPE_DEBUG,
Log::TYPE_VERBOSE,
Log::TYPE_WARNING,
Log::TYPE_ERROR
];
}
}