Skip to content

Commit

Permalink
adding auto-instrumentation attribute to API (open-telemetry#1371)
Browse files Browse the repository at this point in the history
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
brettmc authored Aug 30, 2024
1 parent 4fc1700 commit bee9c64
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/API/Instrumentation/SpanAttribute.php
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,
) {
}
}
28 changes: 28 additions & 0 deletions src/API/Instrumentation/WithSpan.php
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 = [],
) {
}
}
19 changes: 19 additions & 0 deletions tests/Unit/API/Instrumentation/SpanAttributeTest.php
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);
}
}
22 changes: 22 additions & 0 deletions tests/Unit/API/Instrumentation/WithSpanTest.php
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);
}
}

0 comments on commit bee9c64

Please sign in to comment.