PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.11
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.11
1.10.25 1.10.24 1.10.23 1.10.22 1.10.21 1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 All 169 releases
woocommerce-pos / vendor_prefixed / sentry / sentry / src / functions.php

functions.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.11, at vendor_prefixed/sentry/sentry/src/functions.php

454 lines 16.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare (strict_types=1);
4 namespace WCPOS\Vendor\Sentry;
5
6 use WCPOS\Vendor\Psr\Log\LoggerInterface;
7 use WCPOS\Vendor\Sentry\HttpClient\HttpClientInterface;
8 use WCPOS\Vendor\Sentry\Integration\IntegrationInterface;
9 use WCPOS\Vendor\Sentry\Integration\OTLPIntegration;
10 use WCPOS\Vendor\Sentry\Logs\Logs;
11 use WCPOS\Vendor\Sentry\Metrics\Metrics;
12 use WCPOS\Vendor\Sentry\Metrics\TraceMetrics;
13 use WCPOS\Vendor\Sentry\State\Scope;
14 use WCPOS\Vendor\Sentry\Tracing\PropagationContext;
15 use WCPOS\Vendor\Sentry\Tracing\SpanContext;
16 use WCPOS\Vendor\Sentry\Tracing\Transaction;
17 use WCPOS\Vendor\Sentry\Tracing\TransactionContext;
18 use WCPOS\Vendor\Sentry\Transport\TransportInterface;
19 /**
20 * Creates a new Client and Hub which will be set as current.
21 *
22 * @param array{
23 * attach_metric_code_locations?: bool,
24 * attach_stacktrace?: bool,
25 * before_breadcrumb?: callable,
26 * before_send?: callable,
27 * before_send_check_in?: callable,
28 * before_send_log?: callable,
29 * before_send_transaction?: callable,
30 * capture_silenced_errors?: bool,
31 * context_lines?: int|null,
32 * default_integrations?: bool,
33 * dsn?: string|bool|Dsn|null,
34 * enable_logs?: bool,
35 * enable_metrics?: bool,
36 * environment?: string|null,
37 * error_types?: int|null,
38 * http_client?: HttpClientInterface|null,
39 * http_compression?: bool,
40 * http_connect_timeout?: int|float,
41 * http_proxy?: string|null,
42 * http_proxy_authentication?: string|null,
43 * http_ssl_verify_peer?: bool,
44 * http_timeout?: int|float,
45 * http_enable_curl_share_handle?: bool,
46 * ignore_exceptions?: array<class-string>,
47 * ignore_transactions?: array<string>,
48 * in_app_exclude?: array<string>,
49 * in_app_include?: array<string>,
50 * integrations?: IntegrationInterface[]|callable(IntegrationInterface[]): IntegrationInterface[],
51 * logger?: LoggerInterface|null,
52 * log_flush_threshold?: int|null,
53 * metric_flush_threshold?: int|null,
54 * max_breadcrumbs?: int,
55 * max_request_body_size?: "none"|"never"|"small"|"medium"|"always",
56 * max_value_length?: int,
57 * org_id?: int|null,
58 * prefixes?: array<string>,
59 * profiles_sample_rate?: int|float|null,
60 * profiles_sampler?: callable|null,
61 * release?: string|null,
62 * sample_rate?: float|int,
63 * send_attempts?: int,
64 * send_default_pii?: bool,
65 * server_name?: string,
66 * spotlight?: bool,
67 * spotlight_url?: string,
68 * strict_trace_continuation?: bool,
69 * tags?: array<string>,
70 * trace_propagation_targets?: array<string>|null,
71 * traces_sample_rate?: float|int|null,
72 * traces_sampler?: callable|null,
73 * transport?: TransportInterface|null,
74 * } $options The client options
75 */
76 function init(array $options = []) : void
77 {
78 $client = ClientBuilder::create($options)->getClient();
79 SentrySdk::init()->bindClient($client);
80 }
81 /**
82 * Captures a message event and sends it to Sentry.
83 *
84 * @param string $message The message
85 * @param Severity|null $level The severity level of the message
86 * @param EventHint|null $hint Object that can contain additional information about the event
87 */
88 function captureMessage(string $message, ?Severity $level = null, ?EventHint $hint = null) : ?EventId
89 {
90 return SentrySdk::getCurrentHub()->captureMessage($message, $level, $hint);
91 }
92 /**
93 * Captures an exception event and sends it to Sentry.
94 *
95 * @param \Throwable $exception The exception
96 * @param EventHint|null $hint Object that can contain additional information about the event
97 */
98 function captureException(\Throwable $exception, ?EventHint $hint = null) : ?EventId
99 {
100 return SentrySdk::getCurrentHub()->captureException($exception, $hint);
101 }
102 /**
103 * Captures a new event using the provided data.
104 *
105 * @param Event $event The event being captured
106 * @param EventHint|null $hint May contain additional information about the event
107 */
108 function captureEvent(Event $event, ?EventHint $hint = null) : ?EventId
109 {
110 return SentrySdk::getCurrentHub()->captureEvent($event, $hint);
111 }
112 /**
113 * Logs the most recent error (obtained with {@see error_get_last()}).
114 *
115 * @param EventHint|null $hint Object that can contain additional information about the event
116 */
117 function captureLastError(?EventHint $hint = null) : ?EventId
118 {
119 return SentrySdk::getCurrentHub()->captureLastError($hint);
120 }
121 /**
122 * Captures a check-in and sends it to Sentry.
123 *
124 * @param string $slug Identifier of the Monitor
125 * @param CheckInStatus $status The status of the check-in
126 * @param int|float|null $duration The duration of the check-in
127 * @param MonitorConfig|null $monitorConfig Configuration of the Monitor
128 * @param string|null $checkInId A check-in ID from the previous check-in
129 */
130 function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null) : ?string
131 {
132 return SentrySdk::getCurrentHub()->captureCheckIn($slug, $status, $duration, $monitorConfig, $checkInId);
133 }
134 /**
135 * Execute the given callable while wrapping it in a monitor check-in.
136 *
137 * @param string $slug Identifier of the Monitor
138 * @param callable $callback The callable that is going to be monitored
139 * @param MonitorConfig|null $monitorConfig Configuration of the Monitor
140 *
141 * @return mixed
142 */
143 function withMonitor(string $slug, callable $callback, ?MonitorConfig $monitorConfig = null)
144 {
145 $checkInId = SentrySdk::getCurrentHub()->captureCheckIn($slug, CheckInStatus::inProgress(), null, $monitorConfig);
146 $status = CheckInStatus::ok();
147 $duration = 0;
148 try {
149 $start = \microtime(\true);
150 $result = $callback();
151 $duration = \microtime(\true) - $start;
152 return $result;
153 } catch (\Throwable $e) {
154 $status = CheckInStatus::error();
155 throw $e;
156 } finally {
157 SentrySdk::getCurrentHub()->captureCheckIn($slug, $status, $duration, $monitorConfig, $checkInId);
158 }
159 }
160 /**
161 * Records a new breadcrumb which will be attached to future events. They
162 * will be added to subsequent events to provide more context on user's
163 * actions prior to an error or crash.
164 *
165 * @param Breadcrumb|string $category The category of the breadcrumb, can be a Breadcrumb instance as well (in which case the other parameters are ignored)
166 * @param string|null $message Breadcrumb message
167 * @param array<string, mixed> $metadata Additional information about the breadcrumb
168 * @param string $level The error level of the breadcrumb
169 * @param string $type The type of the breadcrumb
170 * @param float|null $timestamp Optional timestamp of the breadcrumb
171 */
172 function addBreadcrumb($category, ?string $message = null, array $metadata = [], string $level = Breadcrumb::LEVEL_INFO, string $type = Breadcrumb::TYPE_DEFAULT, ?float $timestamp = null) : void
173 {
174 SentrySdk::getCurrentHub()->addBreadcrumb($category instanceof Breadcrumb ? $category : new Breadcrumb($level, $type, $category, $message, $metadata, $timestamp));
175 }
176 /**
177 * Calls the given callback passing to it the current scope so that any
178 * operation can be run within its context.
179 *
180 * @param callable $callback The callback to be executed
181 */
182 function configureScope(callable $callback) : void
183 {
184 SentrySdk::getCurrentHub()->configureScope($callback);
185 }
186 /**
187 * Creates a new scope with and executes the given operation within. The scope
188 * is automatically removed once the operation finishes or throws.
189 *
190 * @param callable $callback The callback to be executed
191 *
192 * @phpstan-template T
193 *
194 * @phpstan-param callable(Scope): T $callback
195 *
196 * @return mixed|void The callback's return value, upon successful execution
197 *
198 * @phpstan-return T
199 */
200 function withScope(callable $callback)
201 {
202 return SentrySdk::getCurrentHub()->withScope($callback);
203 }
204 function startContext() : void
205 {
206 SentrySdk::startContext();
207 }
208 function endContext(?int $timeout = null) : void
209 {
210 SentrySdk::endContext($timeout);
211 }
212 /**
213 * Executes the given callback within an isolated context.
214 *
215 * If a context is already active for the current execution key, it is reused.
216 *
217 * @param callable $callback The callback to execute
218 * @param int|null $timeout The maximum number of seconds to wait while flushing the client transport
219 *
220 * @phpstan-template T
221 *
222 * @phpstan-param callable(): T $callback
223 *
224 * @return mixed
225 *
226 * @phpstan-return T
227 */
228 function withContext(callable $callback, ?int $timeout = null)
229 {
230 return SentrySdk::withContext($callback, $timeout);
231 }
232 /**
233 * Starts a new `Transaction` and returns it. This is the entry point to manual
234 * tracing instrumentation.
235 *
236 * A tree structure can be built by adding child spans to the transaction, and
237 * child spans to other spans. To start a new child span within the transaction
238 * or any span, call the respective `startChild()` method.
239 *
240 * Every child span must be finished before the transaction is finished,
241 * otherwise the unfinished spans are discarded.
242 *
243 * The transaction must be finished with a call to its `finish()` method, at
244 * which point the transaction with all its finished child spans will be sent to
245 * Sentry.
246 *
247 * @param TransactionContext $context Properties of the new transaction
248 * @param array<string, mixed> $customSamplingContext Additional context that will be passed to the {@see Tracing\SamplingContext}
249 */
250 function startTransaction(TransactionContext $context, array $customSamplingContext = []) : Transaction
251 {
252 return SentrySdk::getCurrentHub()->startTransaction($context, $customSamplingContext);
253 }
254 /**
255 * Execute the given callable while wrapping it in a span added as a child to the current transaction and active span.
256 * If there is no transaction active this is a no-op and the scope passed to the trace callable will be unused.
257 *
258 * @template T
259 *
260 * @param callable(Scope): T $trace The callable that is going to be traced
261 * @param SpanContext $context The context of the span to be created
262 *
263 * @return T
264 */
265 function trace(callable $trace, SpanContext $context)
266 {
267 return SentrySdk::getCurrentHub()->withScope(static function (Scope $scope) use($context, $trace) {
268 $parentSpan = $scope->getSpan();
269 $span = null;
270 // If there is a span set on the scope and it's sampled there is an active transaction.
271 // If that is the case we create the child span and set it on the scope.
272 // Otherwise we only execute the callable without creating a span.
273 if ($parentSpan !== null && $parentSpan->getSampled()) {
274 $span = $parentSpan->startChild($context);
275 $scope->setSpan($span);
276 }
277 try {
278 return $trace($scope);
279 } finally {
280 if ($span !== null) {
281 $span->finish();
282 $scope->setSpan($parentSpan);
283 }
284 }
285 });
286 }
287 /**
288 * Returns the OTLP traces endpoint configured for the current client.
289 */
290 function getOtlpTracesEndpointUrl() : ?string
291 {
292 $hub = SentrySdk::getCurrentHub();
293 $client = $hub->getClient();
294 if ($client === null) {
295 return null;
296 }
297 $integration = $hub->getIntegration(OTLPIntegration::class);
298 if ($integration instanceof OTLPIntegration && $integration->getCollectorUrl() !== null) {
299 return $integration->getCollectorUrl();
300 }
301 $dsn = $client->getOptions()->getDsn();
302 if ($dsn === null) {
303 return null;
304 }
305 return $dsn->getOtlpTracesEndpointUrl();
306 }
307 /**
308 * Creates the current Sentry traceparent string, to be used as a HTTP header value
309 * or HTML meta tag value.
310 * This function is context aware, as in it either returns the traceparent based
311 * on the current span, or the scope's propagation context.
312 */
313 function getTraceparent() : string
314 {
315 $hub = SentrySdk::getCurrentHub();
316 $client = $hub->getClient();
317 if ($client !== null) {
318 $options = $client->getOptions();
319 if ($options->isTracingEnabled()) {
320 $span = SentrySdk::getCurrentHub()->getSpan();
321 if ($span !== null) {
322 return $span->toTraceparent();
323 }
324 }
325 }
326 $traceParent = '';
327 $hub->configureScope(static function (Scope $scope) use(&$traceParent) {
328 if ($scope->hasExternalPropagationContext()) {
329 return;
330 }
331 $traceParent = $scope->getPropagationContext()->toTraceparent();
332 });
333 return $traceParent;
334 }
335 /**
336 * Creates the current W3C traceparent string, to be used as a HTTP header value
337 * or HTML meta tag value.
338 * This function is context aware, as in it either returns the traceparent based
339 * on the current span, or the scope's propagation context.
340 *
341 * @deprecated since version 4.12. To be removed in version 5.0.
342 */
343 function getW3CTraceparent() : string
344 {
345 return '';
346 }
347 /**
348 * Creates the baggage content string, to be used as a HTTP header value
349 * or HTML meta tag value.
350 * This function is context aware, as in it either returns the baggage based
351 * on the current span or the scope's propagation context.
352 */
353 function getBaggage() : string
354 {
355 $hub = SentrySdk::getCurrentHub();
356 $client = $hub->getClient();
357 if ($client !== null) {
358 $options = $client->getOptions();
359 if ($options->isTracingEnabled()) {
360 $span = SentrySdk::getCurrentHub()->getSpan();
361 if ($span !== null) {
362 return $span->toBaggage();
363 }
364 }
365 }
366 $baggage = '';
367 $hub->configureScope(static function (Scope $scope) use(&$baggage) {
368 if ($scope->hasExternalPropagationContext()) {
369 return;
370 }
371 $baggage = $scope->getPropagationContext()->toBaggage();
372 });
373 return $baggage;
374 }
375 /**
376 * Continue a trace based on HTTP header values.
377 * If the SDK is configured with enabled tracing,
378 * this function returns a populated TransactionContext.
379 * In any other cases, it populates the propagation context on the scope.
380 */
381 function continueTrace(string $sentryTrace, string $baggage) : TransactionContext
382 {
383 // With the new `strict_trace_continuation`, it's possible that we start two new
384 // traces if we parse the TransactionContext and PropagationContext from the same
385 // headers. To make sure the trace is the same, we will create one transaction
386 // context from headers and copy relevant information over.
387 $transactionContext = TransactionContext::fromHeaders($sentryTrace, $baggage);
388 $propagationContext = PropagationContext::fromDefaults();
389 $metadata = $transactionContext->getMetadata();
390 $traceId = $transactionContext->getTraceId() ?? $propagationContext->getTraceId();
391 $transactionContext->setTraceId($traceId);
392 $propagationContext->setTraceId($traceId);
393 $propagationContext->setParentSpanId($transactionContext->getParentSpanId());
394 $propagationContext->setSampleRand($metadata->getSampleRand());
395 $dynamicSamplingContext = $metadata->getDynamicSamplingContext();
396 if ($dynamicSamplingContext !== null) {
397 $propagationContext->setDynamicSamplingContext($dynamicSamplingContext);
398 }
399 $hub = SentrySdk::getCurrentHub();
400 $hub->configureScope(static function (Scope $scope) use($propagationContext) : void {
401 $scope->setPropagationContext($propagationContext);
402 });
403 return $transactionContext;
404 }
405 /**
406 * Get the Sentry Logs client.
407 */
408 function logger() : Logs
409 {
410 return Logs::getInstance();
411 }
412 /**
413 * @deprecated use `traceMetrics` instead
414 */
415 function metrics() : Metrics
416 {
417 return Metrics::getInstance();
418 }
419 function traceMetrics() : TraceMetrics
420 {
421 return TraceMetrics::getInstance();
422 }
423 /**
424 * @deprecated use `traceMetrics` instead
425 */
426 function trace_metrics() : TraceMetrics
427 {
428 return TraceMetrics::getInstance();
429 }
430 /**
431 * Adds a feature flag evaluation to the current scope.
432 * When invoked repeatedly for the same name, the most recent value is used.
433 */
434 function addFeatureFlag(string $name, bool $result) : void
435 {
436 SentrySdk::getCurrentHub()->configureScope(static function (Scope $scope) use($name, $result) {
437 $scope->addFeatureFlag($name, $result);
438 });
439 }
440 /**
441 * Flushes all buffered telemetry data.
442 *
443 * This is a convenience facade that forwards the flush operation to all
444 * internally managed components.
445 *
446 * Calling this method is equivalent to invoking `flush()` on each component
447 * individually. It does not change flushing behavior, improve performance,
448 * or reduce the number of network requests.
449 */
450 function flush() : void
451 {
452 SentrySdk::flush();
453 }
454