generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from keepsuit/refactoring-tracer
Refactoring tracer api
- Loading branch information
Showing
17 changed files
with
225 additions
and
198 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
parameters: | ||
level: 7 | ||
level: 8 | ||
paths: | ||
- src | ||
- config | ||
|
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
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,113 @@ | ||
<?php | ||
|
||
namespace Keepsuit\LaravelOpenTelemetry\Support; | ||
|
||
use Carbon\CarbonInterface; | ||
use Closure; | ||
use Illuminate\Foundation\Bus\PendingDispatch; | ||
use OpenTelemetry\API\Trace\SpanBuilderInterface; | ||
use OpenTelemetry\API\Trace\SpanContextInterface; | ||
use OpenTelemetry\API\Trace\SpanInterface; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\Context\ContextInterface; | ||
use Throwable; | ||
|
||
class SpanBuilder | ||
{ | ||
public function __construct( | ||
protected SpanBuilderInterface $spanBuilder | ||
) { | ||
} | ||
|
||
public function setParent(?ContextInterface $context): SpanBuilder | ||
{ | ||
$this->spanBuilder->setParent($context); | ||
|
||
return $this; | ||
} | ||
|
||
public function addLink(SpanContextInterface $context, iterable $attributes = []): SpanBuilder | ||
{ | ||
$this->spanBuilder->addLink($context, $attributes); | ||
|
||
return $this; | ||
} | ||
|
||
public function setAttribute(string $key, mixed $value): SpanBuilder | ||
{ | ||
$this->spanBuilder->setAttribute($key, $value); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param iterable<string,mixed> $attributes | ||
*/ | ||
public function setAttributes(iterable $attributes): SpanBuilder | ||
{ | ||
$this->spanBuilder->setAttributes($attributes); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param CarbonInterface|int $timestamp A carbon instance or a timestamp in nanoseconds | ||
*/ | ||
public function setStartTimestamp(CarbonInterface|int $timestamp): SpanBuilder | ||
{ | ||
if ($timestamp instanceof CarbonInterface) { | ||
$timestamp = CarbonClock::carbonToNanos($timestamp); | ||
} | ||
|
||
$this->spanBuilder->setStartTimestamp($timestamp); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @phpstan-param SpanKind::KIND_* $spanKind | ||
*/ | ||
public function setSpanKind(int $spanKind): SpanBuilder | ||
{ | ||
$this->spanBuilder->setSpanKind($spanKind); | ||
|
||
return $this; | ||
} | ||
|
||
public function start(): SpanInterface | ||
{ | ||
return $this->spanBuilder->startSpan(); | ||
} | ||
|
||
/** | ||
* @template U | ||
* | ||
* @param Closure(SpanInterface $span): U $callback | ||
* @return (U is PendingDispatch ? null : U) | ||
* | ||
* @throws Throwable | ||
*/ | ||
public function measure(Closure $callback): mixed | ||
{ | ||
$span = $this->start(); | ||
$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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.