generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
166 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace Keepsuit\LaravelOpenTelemetry\Support; | ||
|
||
use Closure; | ||
use Illuminate\Foundation\Bus\PendingDispatch; | ||
use OpenTelemetry\API\Trace\SpanBuilderInterface; | ||
use OpenTelemetry\API\Trace\SpanContextInterface; | ||
use OpenTelemetry\API\Trace\SpanInterface; | ||
use Throwable; | ||
|
||
class SpanBuilder implements SpanBuilderInterface | ||
{ | ||
public function __construct( | ||
protected SpanBuilderInterface $spanBuilder | ||
) { | ||
} | ||
|
||
public function setParent($context): SpanBuilder | ||
{ | ||
$this->spanBuilder->setParent($context); | ||
|
||
return $this; | ||
} | ||
|
||
public function addLink(SpanContextInterface $context, iterable $attributes = []): SpanBuilder | ||
{ | ||
$this->spanBuilder->addLink($context, $attributes); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public function setAttribute(string $key, $value): SpanBuilder | ||
{ | ||
$this->spanBuilder->setAttribute($key, $value); | ||
|
||
return $this; | ||
} | ||
|
||
public function setAttributes(iterable $attributes): SpanBuilder | ||
{ | ||
$this->spanBuilder->setAttributes($attributes); | ||
|
||
return $this; | ||
} | ||
|
||
public function setStartTimestamp(int $timestampNanos): SpanBuilder | ||
{ | ||
$this->spanBuilder->setStartTimestamp($timestampNanos); | ||
|
||
return $this; | ||
} | ||
|
||
public function setSpanKind(int $spanKind): SpanBuilder | ||
{ | ||
$this->spanBuilder->setSpanKind($spanKind); | ||
|
||
return $this; | ||
} | ||
|
||
public function startSpan(): SpanInterface | ||
{ | ||
return $this->spanBuilder->startSpan(); | ||
} | ||
|
||
/** | ||
* @template U | ||
* | ||
* @param Closure(SpanInterface $span): U $callback | ||
* @return U | ||
* | ||
* @throws Throwable | ||
*/ | ||
public function measure(Closure $callback): mixed | ||
{ | ||
$span = $this->startSpan(); | ||
$scope = $span->activate(); | ||
|
||
try { | ||
$result = $callback($span); | ||
|
||
// Fix: Dispatch is effective only on destruct | ||
if ($result instanceof PendingDispatch) { | ||
$result = null; | ||
} | ||
|
||
return $result; | ||
} catch (Throwable $exception) { | ||
$span->recordException($exception); | ||
|
||
throw $exception; | ||
} finally { | ||
$span->end(); | ||
$scope->detach(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.