From 149f64a42a623f6a12d4472f7ccb2de384f7d99a Mon Sep 17 00:00:00 2001 From: Weslen Teche Date: Sat, 24 Feb 2024 00:58:18 -0300 Subject: [PATCH] Added default config of noop driver for `hyperf/opentracing`. (#6550) --- publish/opentracing.php | 4 +++ src/Adapter/NoOpTracerFactory.php | 24 ++++++++++++++++++ tests/TracerFactoryTest.php | 42 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/Adapter/NoOpTracerFactory.php diff --git a/publish/opentracing.php b/publish/opentracing.php index 0e675fb..104de6f 100644 --- a/publish/opentracing.php +++ b/publish/opentracing.php @@ -14,6 +14,7 @@ use function Hyperf\Support\env; return [ + // To disable hyperf/opentracing temporarily, set default driver to noop. 'default' => env('TRACER_DRIVER', 'zipkin'), 'enable' => [ 'coroutine' => env('TRACER_ENABLE_COROUTINE', false), @@ -88,6 +89,9 @@ ], ], ], + 'noop' => [ + 'driver' => Hyperf\Tracer\Adapter\NoOpTracerFactory::class, + ], ], 'tags' => [ 'http_client' => [ diff --git a/src/Adapter/NoOpTracerFactory.php b/src/Adapter/NoOpTracerFactory.php new file mode 100644 index 0000000..840b395 --- /dev/null +++ b/src/Adapter/NoOpTracerFactory.php @@ -0,0 +1,24 @@ + [ ], ], + 'noop' => [ + 'driver' => \Hyperf\Tracer\Adapter\NoOpTracerFactory::class, + ], ], ], ]); @@ -123,6 +126,9 @@ public function testJaegerFactory() 'options' => [ ], ], + 'noop' => [ + 'driver' => \Hyperf\Tracer\Adapter\NoOpTracerFactory::class, + ], ], ], ]); @@ -132,6 +138,42 @@ public function testJaegerFactory() $this->assertInstanceOf(\Jaeger\Tracer::class, $factory($container)); } + public function testNoOpFactory() + { + $config = new Config([ + 'opentracing' => [ + 'default' => 'noop', + 'enable' => [ + ], + 'tracer' => [ + 'zipkin' => [ + 'driver' => \Hyperf\Tracer\Adapter\ZipkinTracerFactory::class, + 'app' => [ + 'name' => 'skeleton', + // Hyperf will detect the system info automatically as the value if ipv4, ipv6, port is null + 'ipv4' => '127.0.0.1', + 'ipv6' => null, + 'port' => 9501, + ], + 'options' => [ + ], + 'sampler' => BinarySampler::createAsAlwaysSample(), + ], + 'jaeger' => [ + 'driver' => \Hyperf\Tracer\Adapter\JaegerTracerFactory::class, + 'name' => 'skeleton', + 'options' => [ + ], + ], + ], + ], + ]); + $container = $this->getContainer($config); + $factory = new TracerFactory(); + + $this->assertInstanceOf(\OpenTracing\NoopTracer::class, $factory($container)); + } + protected function getContainer($config) { $container = Mockery::mock(Container::class);