Skip to content

Commit

Permalink
Add auto instrumentation for open telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
LauJosefsen committed Jan 26, 2024
1 parent 96b3204 commit 359405d
Showing 1 changed file with 0 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,6 @@ public static function register(): void
}
}

/**
* @param CachedInstrumentation $instrumentation
*
* @return void
*/
public static function registerHandleNextMessageHook(CachedInstrumentation $instrumentation): void
{
hook(Consumer::class, 'handleNextMessage',
static function (Consumer $topicHandler, array $params) use ($instrumentation) {
// Detach the current span, if any
$scope = Context::storage()->scope();

if ($scope) {
$scope->detach();
}

if ( ! ($kafkaMessage = $params[0]) instanceof KafkaMessage || ! $kafkaMessage->isRealMessage()) {
return;
}
$span = $instrumentation->tracer()->spanBuilder(sprintf('%s | %s', $topicHandler->getGroupId(), $kafkaMessage->getTopic()))
->setSpanKind(SpanKind::KIND_CONSUMER)
->setAttribute(TraceAttributes::MESSAGING_SYSTEM, 'kafka')
->startSpan();
Context::storage()->attach($span->storeInContext(Context::getCurrent()));
},
static function (Consumer $topicHandler, array $params, bool $result, ?Throwable $exception) {
if ( ! ($kafkaMessage = $params[0]) instanceof KafkaMessage || ! $kafkaMessage->isRealMessage()) {
return;
}

self::endCurrentSpan($exception);
});
}

/**
* @param Throwable|null $exception
*
Expand Down

0 comments on commit 359405d

Please sign in to comment.