Skip to content

Commit

Permalink
dont implement span builder interface
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Feb 9, 2024
1 parent 320178b commit ff7acdf
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 82 deletions.
3 changes: 0 additions & 3 deletions src/Facades/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Keepsuit\LaravelOpenTelemetry\Facades;

use Closure;
use Illuminate\Support\Facades\Facade;
use Keepsuit\LaravelOpenTelemetry\Support\SpanBuilder;
use OpenTelemetry\API\Trace\SpanInterface;
Expand All @@ -19,8 +18,6 @@
* @method static array propagationHeaders(?ContextInterface $context = null)
* @method static Context|null extractContextFromPropagationHeaders(array $headers)
* @method static SpanBuilder newSpan(string $name)
* @method static SpanInterface start(string $name)
* @method static mixed measure(string $name, Closure $callback)
* @method static void updateLogContext()
*/
class Tracer extends Facade
Expand Down
2 changes: 1 addition & 1 deletion src/Instrumentation/QueryInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function recordQuery(QueryExecuted $event): void
->setAttribute(TraceAttributes::DB_USER, $event->connection->getConfig('username'))
->setAttribute(TraceAttributes::SERVER_ADDRESS, $event->connection->getConfig('host'))
->setAttribute(TraceAttributes::SERVER_PORT, $event->connection->getConfig('port'))
->startSpan();
->start();

$span->end();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Instrumentation/QueueInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function recordJobStart(): void
->setAttribute('messaging.destination.kind', 'queue')
->setAttribute('messaging.destination.name', $event->job->getQueue())
->setAttribute('messaging.destination.template', $event->job->resolveName())
->startSpan();
->start();

$span->activate();

Expand Down
2 changes: 1 addition & 1 deletion src/Instrumentation/RedisInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function recordCommand(CommandExecuted $event): void
$span = Tracer::newSpan($traceName)
->setSpanKind(SpanKind::KIND_CLIENT)
->setStartTimestamp($this->getEventStartTimestampNs($event->time))
->startSpan();
->start();

if ($span->isRecording()) {
$span->setAttribute(TraceAttributes::DB_SYSTEM, 'redis')
Expand Down
7 changes: 4 additions & 3 deletions src/Support/CarbonClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Common\Time\SystemClock;

Expand All @@ -19,14 +20,14 @@ public function __construct()
public function now(): int
{
if (Carbon::hasTestNow()) {
return (int) CarbonImmutable::now()->getPreciseTimestamp(6) * 1000;
return static::carbonToNanos(CarbonImmutable::now());
}

return $this->systemClock->now();
}

public function nanoTime(): int
public static function carbonToNanos(CarbonInterface $carbon): int
{
return $this->now();
return (int) $carbon->getPreciseTimestamp(6) * 1000;
}
}
2 changes: 1 addition & 1 deletion src/Support/HttpClient/GuzzleTraceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function make(): Closure
->setAttribute(TraceAttributes::URL_SCHEME, $request->getUri()->getScheme())
->setAttribute(TraceAttributes::SERVER_ADDRESS, $request->getUri()->getHost())
->setAttribute(TraceAttributes::SERVER_PORT, $request->getUri()->getPort())
->startSpan();
->start();

$context = $span->storeInContext(Tracer::currentContext());

Expand Down
2 changes: 1 addition & 1 deletion src/Support/HttpServer/TraceRequestMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function startTracing(Request $request): SpanInterface
->setAttribute(TraceAttributes::USER_AGENT_ORIGINAL, $request->userAgent())
->setAttribute(TraceAttributes::NETWORK_PROTOCOL_VERSION, $request->getProtocolVersion())
->setAttribute(TraceAttributes::NETWORK_PEER_ADDRESS, $request->ip())
->startSpan();
->start();
}

protected function recordHttpResponseToSpan(SpanInterface $span, Response $response): void
Expand Down
33 changes: 23 additions & 10 deletions src/Support/SpanBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

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 implements SpanBuilderInterface
class SpanBuilder
{
public function __construct(
protected SpanBuilderInterface $spanBuilder
) {
}

public function setParent($context): SpanBuilder
public function setParent(?ContextInterface $context): SpanBuilder
{
$this->spanBuilder->setParent($context);

Expand All @@ -30,38 +33,48 @@ public function addLink(SpanContextInterface $context, iterable $attributes = []
return $this;
}

/**
* @param mixed $value
*/
public function setAttribute(string $key, $value): SpanBuilder
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;
}

public function setStartTimestamp(int $timestampNanos): SpanBuilder
/**
* @param CarbonInterface|int $timestamp A carbon instance or a timestamp in nanoseconds
*/
public function setStartTimestamp(CarbonInterface|int $timestamp): SpanBuilder
{
$this->spanBuilder->setStartTimestamp($timestampNanos);
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 startSpan(): SpanInterface
public function start(): SpanInterface
{
return $this->spanBuilder->startSpan();
}
Expand All @@ -76,7 +89,7 @@ public function startSpan(): SpanInterface
*/
public function measure(Closure $callback): mixed
{
$span = $this->startSpan();
$span = $this->start();
$scope = $span->activate();

try {
Expand Down
68 changes: 22 additions & 46 deletions src/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Keepsuit\LaravelOpenTelemetry;

use Closure;
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Support\Facades\Log;
use Keepsuit\LaravelOpenTelemetry\Support\SpanBuilder;
use OpenTelemetry\API\Trace\SpanInterface;
Expand All @@ -22,36 +20,6 @@ public function __construct(
) {
}

/**
* @phpstan-param non-empty-string $name
*/
public function newSpan(string $name): SpanBuilder
{
return new SpanBuilder($this->tracer->spanBuilder($name));
}

/**
* @phpstan-param non-empty-string $name
*/
public function start(string $name): SpanInterface
{
return $this->newSpan($name)->startSpan();
}

/**
* @template U
*
* @param non-empty-string $name
* @param Closure(SpanInterface $span): U $callback
* @return (U is PendingDispatch ? null : U)
*
* @throws \Throwable
*/
public function measure(string $name, Closure $callback)
{
return $this->newSpan($name)->measure($callback);
}

public function currentContext(): ContextInterface
{
return Context::getCurrent();
Expand All @@ -72,20 +40,6 @@ public function traceId(): string
return $this->activeSpan()->getContext()->getTraceId();
}

public function propagationHeaders(?ContextInterface $context = null): array
{
$headers = [];

$this->propagator->inject($headers, context: $context);

return $headers;
}

public function extractContextFromPropagationHeaders(array $headers): ContextInterface
{
return $this->propagator->extract($headers);
}

public function isRecording(): bool
{
$enabled = config('opentelemetry.enabled', true);
Expand All @@ -101,6 +55,28 @@ public function isRecording(): bool
return false;
}

/**
* @phpstan-param non-empty-string $name
*/
public function newSpan(string $name): SpanBuilder
{
return new SpanBuilder($this->tracer->spanBuilder($name));
}

public function propagationHeaders(?ContextInterface $context = null): array
{
$headers = [];

$this->propagator->inject($headers, context: $context);

return $headers;
}

public function extractContextFromPropagationHeaders(array $headers): ContextInterface
{
return $this->propagator->extract($headers);
}

public function updateLogContext(): void
{
if (config('opentelemetry.logs.inject_trace_id', true)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Instrumentation/HttpClientInstrumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
new Response(200, ['Content-Length' => 0]),
]);

$root = Tracer::start('root');
$root = Tracer::newSpan('root')->start();
$scope = $root->activate();

Http::withTrace()->get(Server::$url);
Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function getRecordedSpans(): array

function withRootSpan(Closure $callback): mixed
{
$rootSpan = \Keepsuit\LaravelOpenTelemetry\Facades\Tracer::newSpan('root')->startSpan();
$rootSpan = \Keepsuit\LaravelOpenTelemetry\Facades\Tracer::newSpan('root')->start();
$rootScope = $rootSpan->activate();

$result = $callback();
Expand Down
Loading

0 comments on commit ff7acdf

Please sign in to comment.