<?php
declare(strict_types=1);
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\ClassLoader;
use Hyperf\Metric\Adapter\Prometheus\Constants;
use Hyperf\Metric\Adapter\Prometheus\MetricFactory;
use Hyperf\Nano\Factory\AppFactory;
use Hyperf\Tracer\Adapter\JaegerTracerFactory;
use Hyperf\Utils\Composer;
use Jaeger\Config;
use Psr\Log\LogLevel;
use const Jaeger\SAMPLER_TYPE_CONST;
define('BASE_PATH', __DIR__);
require_once BASE_PATH . '/vendor/autoload.php';
class ConfigProvider
{
public function __invoke()
{
return [
'app_name' => 'tracing-nano',
'app_env' => 'dev',
'scan_cacheable' => false,
'annotations' => [
'scan' => [
'paths' => [
// BASE_PATH . '/',
],
],
],
StdoutLoggerInterface::class => [
'log_level' => [
LogLevel::ALERT,
LogLevel::CRITICAL,
LogLevel::DEBUG,
LogLevel::EMERGENCY,
LogLevel::ERROR,
LogLevel::INFO,
LogLevel::NOTICE,
LogLevel::WARNING,
],
],
'aspects' => [
// ...
],
'metric' => [
'default' => 'prometheus',
'metric' => [
'prometheus' => [
'driver' => MetricFactory::class,
'mode' => Constants::SCRAPE_MODE,
'namespace' => 'tracing_nano',
'scrape_host' => '0.0.0.0',
'scrape_port' => '9502',
'scrape_path' => '/metrics',
],
],
],
'middlewares' => [
'http' => [
MetricMiddleware::class,
TraceMiddleware::class,
],
],
'opentracing' => [
'default' => 'jaeger',
'enable' => [
'guzzle' => true,
],
'tracer' => [
'jaeger' => [
'driver' => JaegerTracerFactory::class,
'name' => 'tracing-nano',
'options' => [
'sampler' => [
'type' => SAMPLER_TYPE_CONST,
'param' => true,
],
'logging' => true,
'dispatch_mode' => Config::JAEGER_OVER_BINARY_HTTP,
],
],
],
],
];
}
}
// Inject ConfigProvider into Hyperf\Utils\Composer::$extra
Closure::bind(function () {
self::getLockContent();
self::$extra[uniqid()] = [
'hyperf' => [
'config' => ConfigProvider::class,
],
];
}, null, Composer::class)();
ClassLoader::init();
$app = AppFactory::createBase('0.0.0.0', 9501);
$app->addCommand('foo', function () {
var_dump(config('annotations.scan'));
});
$app->run();
enhancement