Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding auto-instrumentation attribute to API #1371

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading