Instrumenting Laravel artisan commands with OpenTelemetry

I’m using OpenTelemetry to instrument a Laravel application, and struggling with how to create root spans for console commands (though Laravel artisan). I have noticed that the framework dispatches CommandStarting and CommandFinished events when it’s starting and finishing a command, which I’m attempting to listen for to construct my root span:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
namespace AppOpenTelemetryInstrumentation;
use IlluminateConsoleEventsCommandFinished;
use IlluminateConsoleEventsCommandStarting;
use OpenTelemetryAPITraceSpan;
use OpenTelemetryAPITraceSpanKind;
use OpenTelemetryAPITraceTracerInterface;
use OpenTelemetryContextContext;
class ArtisanInstrumentation implements Instrumentation
{
public function register(): void
{
app('events')->listen(CommandStarting::class, [$this, 'recordStartOfCommand']);
app('events')->listen(CommandFinished::class, [$this, 'recordEndOfCommand']);
}
public function recordStartOfCommand(CommandStarting $event): void
{
/** @var TracerInterface $tracer */
$tracer = app(TracerInterface::class);
$span = $tracer
->spanBuilder($event->command)
->setSpanKind(SpanKind::KIND_SERVER)
->startSpan();
$span->activate();
}
public function recordEndOfCommand(CommandFinished $event): void
{
$scope = Context::storage()->scope();
$span = Span::fromContext($scope->context());
$scope->detach();
$span->end();
}
}
</code>
<code><?php namespace AppOpenTelemetryInstrumentation; use IlluminateConsoleEventsCommandFinished; use IlluminateConsoleEventsCommandStarting; use OpenTelemetryAPITraceSpan; use OpenTelemetryAPITraceSpanKind; use OpenTelemetryAPITraceTracerInterface; use OpenTelemetryContextContext; class ArtisanInstrumentation implements Instrumentation { public function register(): void { app('events')->listen(CommandStarting::class, [$this, 'recordStartOfCommand']); app('events')->listen(CommandFinished::class, [$this, 'recordEndOfCommand']); } public function recordStartOfCommand(CommandStarting $event): void { /** @var TracerInterface $tracer */ $tracer = app(TracerInterface::class); $span = $tracer ->spanBuilder($event->command) ->setSpanKind(SpanKind::KIND_SERVER) ->startSpan(); $span->activate(); } public function recordEndOfCommand(CommandFinished $event): void { $scope = Context::storage()->scope(); $span = Span::fromContext($scope->context()); $scope->detach(); $span->end(); } } </code>
<?php

namespace AppOpenTelemetryInstrumentation;

use IlluminateConsoleEventsCommandFinished;
use IlluminateConsoleEventsCommandStarting;
use OpenTelemetryAPITraceSpan;
use OpenTelemetryAPITraceSpanKind;
use OpenTelemetryAPITraceTracerInterface;
use OpenTelemetryContextContext;

class ArtisanInstrumentation implements Instrumentation
{
    public function register(): void
    {
        app('events')->listen(CommandStarting::class, [$this, 'recordStartOfCommand']);
        app('events')->listen(CommandFinished::class, [$this, 'recordEndOfCommand']);
    }

    public function recordStartOfCommand(CommandStarting $event): void
    {
        /** @var TracerInterface $tracer */
        $tracer = app(TracerInterface::class);

        $span = $tracer
            ->spanBuilder($event->command)
            ->setSpanKind(SpanKind::KIND_SERVER)
            ->startSpan();
        $span->activate();
    }

    public function recordEndOfCommand(CommandFinished $event): void
    {
        $scope = Context::storage()->scope();
        $span = Span::fromContext($scope->context());
        $scope->detach();
        $span->end();
    }
}

As you can see I’m attempting to start a span when the command starts running, and end it later when the command finishes. From the above code though I get the following error about a missing call to detach a scope:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> Scope: missing call to Scope::detach() for scope #3776, created
at OpenTelemetry.Context.Context.activate(Context.php:87)
at OpenTelemetry.API.Trace.Span.activate(Span.php:51)
at App.OpenTelemetry.Instrumentation.ArtisanInstrumentation.recordStartOfCommand(ArtisanInstrumentation.php:32)
</code>
<code> Scope: missing call to Scope::detach() for scope #3776, created at OpenTelemetry.Context.Context.activate(Context.php:87) at OpenTelemetry.API.Trace.Span.activate(Span.php:51) at App.OpenTelemetry.Instrumentation.ArtisanInstrumentation.recordStartOfCommand(ArtisanInstrumentation.php:32) </code>
  Scope: missing call to Scope::detach() for scope #3776, created 
        at OpenTelemetry.Context.Context.activate(Context.php:87)
        at OpenTelemetry.API.Trace.Span.activate(Span.php:51)
        at App.OpenTelemetry.Instrumentation.ArtisanInstrumentation.recordStartOfCommand(ArtisanInstrumentation.php:32)

The span that I’m retrieving with Span::fromContext($scope->context()) is the correct span, which I have confirmed by dumping it out and inspecting its name. But calling detach() on the scope is obviously not detaching it properly.

I think I’m just a bit lost in generate as to how to scopes work in general and how to retrieve a span’s scope somewhere else in the codebase. All of the examples in OpenTelemetry’s documentation show using a scope in the context that you created it in, not dynamically retrieving it from unrelated code.

Through trial-and-error I have made it work by storing the result of $span->activate() in a property on the class, and then deactivating it in the recordEndOfCommand method like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$scope = Context::storage()->scope();
$span = Span::fromContext($scope->context());
$this->scope->detach();
$span->end();
</code>
<code>$scope = Context::storage()->scope(); $span = Span::fromContext($scope->context()); $this->scope->detach(); $span->end(); </code>
$scope = Context::storage()->scope();
$span = Span::fromContext($scope->context());
$this->scope->detach();
$span->end();

but I’m not sure if this is preferred over retrieving the scope from context?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật