Skip to content

Commit

Permalink
Merge pull request #9 from DieZeeL/add.start_stop_span_with_duration
Browse files Browse the repository at this point in the history
startStop method for spans (with duration)
  • Loading branch information
AidynMakhataev authored Jan 4, 2022
2 parents d9c5bb5 + d31ebf5 commit f788c6b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
32 changes: 30 additions & 2 deletions src/Jaeger.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ public function stop(string $operationName, array $tags = []): void
}
}

public function startStop(string $operationName, array $tags = [], ?float $duration = 0): void
{
$currentTime = microtime(true);

$startTime = $currentTime - $duration;

if ($this->spans->isEmpty()) {
$span = $this->startSpan($operationName, $this->serverContext, $startTime);
} else {
/** @var Span $parentSpan */
$parentSpan = $this->spans->top();
$span = $this->startSpan($operationName, $parentSpan->getContext(), $startTime);
}

if ($tags) {
foreach ($tags as $key => $value) {
$span->setTag($key, $value);
}
}

$span->finish(intval($currentTime * 1000000));
}

public function inject(array &$carrier): void
{
if ($this->getCurrentSpan() === null) {
Expand Down Expand Up @@ -137,19 +160,24 @@ private function finishSpans(): void
}

/**
* @param string $operationName
* @param string $operationName
* @param SpanContext|null $context
* @param float|null $startTime
*
* @return Span
*/
private function startSpan(string $operationName, SpanContext $context = null): Span
private function startSpan(string $operationName, SpanContext $context = null, float $startTime = null): Span
{
$options = [];

if ($context !== null) {
$options['child_of'] = $context;
}

if ($startTime !== null) {
$options['start_time'] = $startTime;
}

return $this->tracer->startSpan($operationName, $options);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Listeners/QueryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public function __construct(Jaeger $jaeger)

public function handle(QueryExecuted $event): void
{
$this->jaeger->start("DB Query: {$event->sql}", [
$this->jaeger->startStop("DB Query: {$event->sql}", [
'query.sql' => $event->sql,
'query.bindings' => $event->bindings,
'query.connection_name' => $event->connectionName,
'query.time' => $event->time
]);
], $event->time/1000);
}
}
}

0 comments on commit f788c6b

Please sign in to comment.