summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/api/Trace/functions.php
blob: 79f7307179041fcfb398a90175608f9df46d0554 (plain)
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
<?php

declare(strict_types=1);

namespace OpenTelemetry\API\Trace;

use Closure;
use Throwable;

/**
 * Executes the given closure within the provided span.
 *
 * The span will be ended.
 *
 * @template R
 * @param SpanInterface $span span to enclose the closure with
 * @param Closure(...): R $closure closure to invoke
 * @param iterable<int|string, mixed> $args arguments to provide to the closure
 * @return R result of the closure invocation
 *
 * @phpstan-ignore-next-line
 */
function trace(SpanInterface $span, Closure $closure, iterable $args = [])
{
    $s = $span;
    $c = $closure;
    $a = $args;
    unset($span, $closure, $args);

    $scope = $s->activate();

    try {
        /** @psalm-suppress InvalidArgument */
        return $c(...$a, ...($a = []));
    } catch (Throwable $e) {
        $s->setStatus(StatusCode::STATUS_ERROR, $e->getMessage());
        $s->recordException($e, ['exception.escaped' => true]);

        throw $e;
    } finally {
        $scope->detach();
        $s->end();
    }
}