-
Notifications
You must be signed in to change notification settings - Fork 19
/
Sentry.php
189 lines (165 loc) · 5.57 KB
/
Sentry.php
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
<?php
namespace Utopia\Logger\Adapter;
use Exception;
use Utopia\Logger\Adapter;
use Utopia\Logger\Log;
use Utopia\Logger\Logger;
// Reference Material
// https://develop.sentry.dev/sdk/event-payloads/
class Sentry extends Adapter
{
/**
* @var string (required, this part of Sentry DSN: 'https://{{THIS_PART}}@blabla.ingest.sentry.io/blabla')
*/
protected string $sentryKey;
/**
* @var string (required, this part of Sentry DSN: 'https://blabla@blabla.ingest.sentry.io/{{THIS_PART}}')
*/
protected string $projectId;
/**
* @var string (optional, the host where Sentry is reachable, in case of self-hosted Sentry could
* look like 'https://sentry.mycompany.com'. defaults to 'https://sentry.io')
*/
protected string $sentryHost;
/**
* Sentry constructor.
*
* @param string $projectId
* @param string $key
* @param string $host
*/
public function __construct(string $projectId, string $key, string $host = 'https://sentry.io')
{
$this->sentryHost = $host;
$this->sentryKey = $key;
$this->projectId = $projectId;
}
/**
* Return unique adapter name
*
* @return string
*/
public static function getName(): string
{
return 'sentry';
}
/**
* Push log to external provider
*
* @param Log $log
* @return int
*
* @throws Exception
*/
public function push(Log $log): int
{
$breadcrumbsObject = $log->getBreadcrumbs();
$breadcrumbsArray = [];
foreach ($breadcrumbsObject as $breadcrumb) {
\array_push($breadcrumbsArray, [
'type' => 'default',
'level' => $breadcrumb->getType(),
'category' => $breadcrumb->getCategory(),
'message' => $breadcrumb->getMessage(),
'timestamp' => $breadcrumb->getTimestamp(),
]);
}
$stackFrames = [];
if (isset($log->getExtra()['detailedTrace'])) {
$detailedTrace = $log->getExtra()['detailedTrace'];
if (! is_array($detailedTrace)) {
throw new Exception('detailedTrace must be an array');
}
foreach ($detailedTrace as $trace) {
if (! is_array($trace)) {
throw new Exception('detailedTrace must be an array of arrays');
}
\array_push($stackFrames, [
'filename' => $trace['file'] ?? '',
'lineno' => $trace['line'] ?? 0,
'function' => $trace['function'] ?? '',
]);
}
}
// Reverse array (because Sentry expects the list to go from the oldest to the newest calls)
$stackFrames = \array_reverse($stackFrames);
// prepare log (request body)
$requestBody = [
'timestamp' => $log->getTimestamp(),
'platform' => 'php',
'level' => 'error',
'logger' => $log->getNamespace(),
'transaction' => $log->getAction(),
'server_name' => $log->getServer(),
'release' => $log->getVersion(),
'environment' => $log->getEnvironment(),
'message' => [
'message' => $log->getMessage(),
],
'exception' => [
'values' => [
[
'type' => $log->getMessage(),
'stacktrace' => [
'frames' => $stackFrames,
],
],
],
],
'tags' => $log->getTags(),
'extra' => $log->getExtra(),
'breadcrumbs' => $breadcrumbsArray,
'user' => empty($log->getUser()) ? null : [
'id' => $log->getUser()->getId(),
'email' => $log->getUser()->getEmail(),
'username' => $log->getUser()->getUsername(),
],
];
// init curl object
$ch = \curl_init();
// define options
$optArray = [
CURLOPT_URL => $this->sentryHost.'/api/'.$this->projectId.'/store/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => \json_encode($requestBody),
CURLOPT_HEADEROPT => \CURLHEADER_UNIFIED,
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'X-Sentry-Auth: Sentry sentry_version=7, sentry_key='.$this->sentryKey.', sentry_client=utopia-logger/'.Logger::LIBRARY_VERSION],
];
// 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;
}
public function getSupportedTypes(): array
{
return [
Log::TYPE_INFO,
Log::TYPE_DEBUG,
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_WARNING,
Log::TYPE_ERROR,
];
}
}