接入配置(如有需要, 点击这里查看详情)
接入
安装扩展 (自动上报需要PHP8)
opentelemetry扩展
composer扩展
配置
TEL_PHP_AUTOLOAD_ENABLED=true
TEL_SERVICE_NAME=test
TEL_TRACES_EXPORTER=otlp
TEL_METRICS_EXPORTER=none
TEL_LOGS_EXPORTER=none
TEL_EXPORTER_OTLP_PROTOCOL=grpc
TEL_EXPORTER_OTLP_ENDPOINT=http://
TEL_EXPORTER_OTLP_HEADERS=Authentication=xxx
TEL_EXPORTER_OTLP_TIMEOUT=1000
TEL_EXPORTER_OTLP_TRACES_TIMEOUT=1000
运行
{
"files": [
"_register.php"
]
}
会根据以下解析出一个BatchProcessSpan
public static function autoload(): bool
{
if (!self::isEnabled() || self::isExcludedUrl()) {
return false;
}
Globals::registerInitializer(function (Configurator $configurator) {
$propagator = (new PropagatorFactory())->create();
if (Sdk::isDisabled()) {
//@see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#general-sdk-configuration
return $configurator->withPropagator($propagator);
}
$emitMetrics = Configuration::getBoolean(Variables::OTEL_PHP_INTERNAL_METRICS_ENABLED);
$resource = ResourceInfoFactory::defaultResource();
$exporter = (new ExporterFactory())->create();
$meterProvider = (new MeterProviderFactory())->create($resource);
// 主要关注这一行, 这里我们会创建出一个 BatchSpanProcessor
$spanProcessor = (new SpanProcessorFactory())->create($exporter, $emitMetrics ? $meterProvider : null);
$tracerProvider = (new TracerProviderBuilder())
->addSpanProcessor($spanProcessor)
->setResource($resource)
->setSampler((new SamplerFactory())->create())
->build();
$loggerProvider = (new LoggerProviderFactory())->create($emitMetrics ? $meterProvider : null, $resource);
ShutdownHandler::register($tracerProvider->shutdown(...));
ShutdownHandler::register($meterProvider->shutdown(...));
ShutdownHandler::register($loggerProvider->shutdown(...));
return $configurator
->withTracerProvider($tracerProvider)
->withMeterProvider($meterProvider)
->withLoggerProvider($loggerProvider)
->withPropagator($propagator)
;
});
return true;
}
案例代码
tracer = Globals::tracerProvider()
->getTracer('io.opentelemetry.contrib.php.laravel');
}
/**
* 请查看 AppServiceProvider 注册为 scoped, 适用于 Octane
* @return Tracer
*/
public static function getInstance(): Tracer
{
return app(Tracer::class);
}
public function startRootSpan($name): void
{
$span = $this->startSpan($name);
$this->rootSpan = $span;
}
public function startAndEndLastSpan($name): SpanInterface
{
$this->endLastSpan();
return $this->startSpan($name);
}
public function startSpan($name): SpanInterface
{
$span = $this->tracer->spanBuilder($name)->startSpan();
$this->spanMap[$name] = $span;
$this->lastSpan = $span;
return $span;
}
public function endRootSpan(): void
{
$this->endSpan($this->rootSpan);
}
/**
* @return void 方便的 end 上一个 span
*/
public function endLastSpan(): void
{
$this->endSpan($this->lastSpan);
}
public function endSpan(?SpanInterface $span): void
{
if (is_null($span)) {
return;
}
$span->end();
}
}
把Tracer类注册到服务提供者app/Providers/AppServiceProvider.php
app->scoped(Tracer::class, function () {
return new Tracer();
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}
startRootSpan('xxxx');
// 步骤 1
$tracer->startSpan('s1');
// 业务代码 xxx
// 结束步骤 1, 并开启步骤 2
$tracer->startAndEndLastSpan('s2');
// 业务代码 xxx
// 结束步骤 2
$tracer->endLastSpan();
// 结束 root
$tracer->endRootSpan();
}
}
问题
查看end()的实现
class BatchSpanProcessor {
public function onEnd(ReadableSpanInterface $span): void
{
if ($this->closed) {
return;
}
if (!$span->getContext()->isSampled()) {
return;
}
if ($this->queueSize === $this->maxQueueSize) {
$this->dropped++;
return;
}
$this->queueSize++;
$this->batch[] = $span->toSpanData();
$this->nextScheduledRun ??= $this->clock->now() + $this->scheduledDelayNanos;
if (count($this->batch) === $this->maxExportBatchSize) {
$this->enqueueBatch();
}
if ($this->autoFlush) {
// flush
$this->flush();
}
}
}
解决办法
[ol]
[/ol]