-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathLogsAggregator.php
More file actions
206 lines (165 loc) · 6.11 KB
/
LogsAggregator.php
File metadata and controls
206 lines (165 loc) · 6.11 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
declare(strict_types=1);
namespace Sentry\Logs;
use Sentry\Attributes\Attribute;
use Sentry\Client;
use Sentry\Event;
use Sentry\EventId;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Sentry\State\ScopeManager;
use Sentry\Util\Arr;
use Sentry\Util\Str;
/**
* @internal
*/
final class LogsAggregator
{
/**
* @var Log[]
*/
private $logs = [];
/**
* @param string $message see sprintf for a description of format
* @param array<int, string|int|float> $values see sprintf for a description of values
* @param array<string, mixed> $attributes additional attributes to add to the log
*/
public function add(
LogLevel $level,
string $message,
array $values = [],
array $attributes = []
): void {
$timestamp = microtime(true);
$client = SentrySdk::getClient();
$options = $client->getOptions();
$sdkLogger = $options->getLogger();
if (!$options->getEnableLogs()) {
if ($sdkLogger !== null) {
$sdkLogger->info(
'Log will be discarded because "enable_logs" is "false".'
);
}
return;
}
$formattedMessage = Str::vsprintfOrNull($message, $values);
if ($formattedMessage === null) {
// If formatting fails we don't format the message and log the error
if ($sdkLogger !== null) {
$sdkLogger->warning('Failed to format log message with values.', [
'message' => $message,
'values' => $values,
]);
}
$formattedMessage = $message;
}
$span = SentrySdk::getCurrentScope()->getSpan();
$log = (new Log($timestamp, $this->getTraceId(), $level, $formattedMessage))
->setAttribute('sentry.release', $options->getRelease())
->setAttribute('sentry.environment', $options->getEnvironment() ?? Event::DEFAULT_ENVIRONMENT)
->setAttribute('sentry.server.address', $options->getServerName())
->setAttribute('sentry.trace.parent_span_id', $span !== null ? $span->getSpanId() : null);
if ($client instanceof Client) {
$log->setAttribute('sentry.sdk.name', $client->getSdkIdentifier());
$log->setAttribute('sentry.sdk.version', $client->getSdkVersion());
}
$scope = SentrySdk::getMergedScope();
$this->applyScopeAttributes($log, $scope);
$user = $scope->getUser();
if ($user !== null) {
if ($user->getId() !== null) {
$log->setAttribute('user.id', $user->getId());
}
if ($user->getEmail() !== null) {
$log->setAttribute('user.email', $user->getEmail());
}
if ($user->getUsername() !== null) {
$log->setAttribute('user.name', $user->getUsername());
}
}
if (\count($values)) {
$log->setAttribute('sentry.message.template', $message);
foreach ($values as $key => $value) {
$log->setAttribute("sentry.message.parameter.{$key}", $value);
}
}
$attributes = Arr::simpleDot($attributes);
foreach ($attributes as $key => $value) {
if (!\is_string($key)) {
if ($sdkLogger !== null) {
$sdkLogger->info(
\sprintf("Dropping log attribute with non-string key '%s' and value of type '%s'.", $key, \gettype($value))
);
}
continue;
}
$attribute = Attribute::tryFromValue($value);
if ($attribute === null) {
if ($sdkLogger !== null) {
$sdkLogger->info(
\sprintf("Dropping log attribute {$key} with value of type '%s' because it is not serializable or an unsupported type.", \gettype($value))
);
}
continue;
}
$log->setAttribute($key, $attribute);
}
$log = ($options->getBeforeSendLogCallback())($log);
if ($log === null) {
if ($sdkLogger !== null) {
$sdkLogger->info(
'Log will be discarded because the "before_send_log" callback returned "null".',
['log' => $log]
);
}
return;
}
if ($sdkLogger !== null) {
$sdkLogger->log($log->getPsrLevel(), "Logs item: {$log->getBody()}", $log->attributes()->toSimpleArray());
}
$this->logs[] = $log;
}
public function flush(?ScopeManager $scopeManager = null): ?EventId
{
if (empty($this->logs)) {
return null;
}
$scopeManager = $scopeManager ?? SentrySdk::getCurrentRuntimeContext()->getScopeManager();
$event = Event::createLogs()->setLogs($this->logs);
$this->logs = [];
$mergedScope = Scope::mergeScopes(
$scopeManager->getGlobalScope(),
$scopeManager->getIsolationScope(),
$scopeManager->getCurrentScope()
);
$client = Scope::getClientFromScopes(
$scopeManager->getGlobalScope(),
$scopeManager->getIsolationScope(),
$scopeManager->getCurrentScope()
);
return $client->captureEvent($event, null, $mergedScope);
}
/**
* @return Log[]
*/
public function all(): array
{
return $this->logs;
}
private function getTraceId(): string
{
$span = SentrySdk::getCurrentScope()->getSpan();
if ($span !== null) {
return (string) $span->getTraceId();
}
return (string) SentrySdk::getIsolationScope()->getPropagationContext()->getTraceId();
}
private function applyScopeAttributes(Log $log, Scope $scope): void
{
foreach ($scope->getAttributes()->all() as $key => $attribute) {
if ($log->attributes()->get($key) === null) {
$log->setAttribute($key, $attribute);
}
}
}
}