diff --git a/src/Support/JaegerDecoder.php b/src/Support/JaegerDecoder.php index 08cf9d5..2d19df7 100644 --- a/src/Support/JaegerDecoder.php +++ b/src/Support/JaegerDecoder.php @@ -21,8 +21,9 @@ public static function traceIdDecoder(string $traceId): string if (strlen($traceId) == 32 || !is_numeric($traceId)) { return $traceId; } - - return substr($traceId, 0, 16) . substr($traceId, -16, 16); + $hiLen = strlen($traceId) - 16; + $newTraceId = substr($traceId, 0, $hiLen) . substr($traceId, $hiLen); + return str_pad($newTraceId, 32, '0', STR_PAD_LEFT); } public static function spanIdDecoder(string $spanId): string diff --git a/src/TracerContext.php b/src/TracerContext.php index d86f713..6857d5c 100644 --- a/src/TracerContext.php +++ b/src/TracerContext.php @@ -35,6 +35,11 @@ public static function setTracer(Tracer $tracer): Tracer return Context::set(self::TRACER, $tracer); } + public static function hasTracer(): bool + { + return Context::has(self::TRACER); + } + public static function getTracer(): Tracer { return Context::getOrSet(self::TRACER, fn () => make(Tracer::class)); diff --git a/src/TracerFactory.php b/src/TracerFactory.php index e9562c7..b450778 100644 --- a/src/TracerFactory.php +++ b/src/TracerFactory.php @@ -27,6 +27,10 @@ class TracerFactory */ public function __invoke(ContainerInterface $container): Tracer { + if(TracerContext::hasTracer()) { + return TracerContext::getTracer(); + } + $config = $container->get(ConfigInterface::class); $name = $config->get('opentracing.default'); @@ -51,6 +55,8 @@ public function __invoke(ContainerInterface $container): Tracer ); } - return $factory->make($name); + $tracer = $factory->make($name); + TracerContext::setTracer($tracer); + return TracerContext::getTracer(); } }