forked from open-telemetry/opentelemetry-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding auto-instrumentation attribute to API (open-telemetry#1371)
adding WithSpan and SpanAttribute attributes to the API. These attributes are used by an upcoming feature in auto-instrumentation which allows users to add attributes to their code to enable auto-instrumentation.
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\API\Instrumentation; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* For function and methods that have the {@link WithSpan} | ||
* attribute, adding this attribute to an argument will | ||
* add the argument as a span attribute. | ||
*/ | ||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
final class SpanAttribute | ||
{ | ||
/** | ||
* @param string|null $name Optional name to use for the attribute. Default: argument name. | ||
*/ | ||
public function __construct( | ||
public readonly ?string $name = null, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\API\Instrumentation; | ||
|
||
use Attribute; | ||
use OpenTelemetry\API\Trace\SpanKind; //@phan-suppress-current-line PhanUnreferencedUseNormal | ||
|
||
/** | ||
* Functions and methods with this attribute will be auto-instrumented | ||
* by the OpenTelemetry extension. | ||
*/ | ||
#[Attribute(Attribute::TARGET_FUNCTION|Attribute::TARGET_METHOD)] | ||
final class WithSpan | ||
{ | ||
/** | ||
* @param string|null $span_name Optional span name. Default: function name or class::method | ||
* @param int|null $span_kind Optional {@link SpanKind}. Default: {@link SpanKind::KIND_INTERNAL} | ||
* @param array $attributes Optional attributes to be added to the span. | ||
*/ | ||
public function __construct( | ||
public readonly ?string $span_name = null, | ||
public readonly ?int $span_kind = null, | ||
public readonly array $attributes = [], | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Tests\Unit\API\Instrumentation; | ||
|
||
use OpenTelemetry\API\Instrumentation\SpanAttribute; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(SpanAttribute::class)] | ||
class SpanAttributeTest extends TestCase | ||
{ | ||
public function test_with_span(): void | ||
{ | ||
$attr = new SpanAttribute('foo'); | ||
$this->assertSame('foo', $attr->name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Tests\Unit\API\Instrumentation; | ||
|
||
use OpenTelemetry\API\Instrumentation\WithSpan; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(WithSpan::class)] | ||
class WithSpanTest extends TestCase | ||
{ | ||
public function test_with_span(): void | ||
{ | ||
$attr = new WithSpan('foo', SpanKind::KIND_PRODUCER, ['foo' => 'bar']); | ||
$this->assertSame('foo', $attr->span_name); | ||
$this->assertSame(SpanKind::KIND_PRODUCER, $attr->span_kind); | ||
$this->assertSame(['foo' => 'bar'], $attr->attributes); | ||
} | ||
} |