JaegerTracerFactory.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Tracer\Adapter;
  12. use Hyperf\Contract\ConfigInterface;
  13. use Hyperf\Tracer\Contract\NamedFactoryInterface;
  14. use Jaeger\Config;
  15. use OpenTracing\Tracer;
  16. use Psr\Cache\CacheItemPoolInterface;
  17. use Psr\Log\LoggerInterface;
  18. use const Jaeger\SAMPLER_TYPE_CONST;
  19. class JaegerTracerFactory implements NamedFactoryInterface
  20. {
  21. /**
  22. * @var ConfigInterface
  23. */
  24. private $config;
  25. /**
  26. * @var null|LoggerInterface
  27. */
  28. private $logger;
  29. /**
  30. * @var null|CacheItemPoolInterface
  31. */
  32. private $cache;
  33. /**
  34. * @var string
  35. */
  36. private $prefix;
  37. public function __construct(ConfigInterface $config, ?LoggerInterface $logger = null, ?CacheItemPoolInterface $cache = null)
  38. {
  39. $this->config = $config;
  40. $this->logger = $logger;
  41. $this->cache = $cache;
  42. }
  43. public function make(string $name): Tracer
  44. {
  45. $this->prefix = "opentracing.tracer.{$name}.";
  46. [$name, $options] = $this->parseConfig();
  47. $jaegerConfig = new Config(
  48. $options,
  49. $name,
  50. $this->logger,
  51. $this->cache
  52. );
  53. return $jaegerConfig->initializeTracer();
  54. }
  55. private function parseConfig(): array
  56. {
  57. return [
  58. $this->getConfig('name', 'skeleton'),
  59. $this->getConfig('options', [
  60. 'sampler' => [
  61. 'type' => SAMPLER_TYPE_CONST,
  62. 'param' => true,
  63. ],
  64. 'logging' => false,
  65. ]),
  66. ];
  67. }
  68. private function getConfig(string $key, $default)
  69. {
  70. return $this->config->get($this->prefix . $key, $default);
  71. }
  72. }