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

Rename AbstractSpan to Span for consistent access to static methods #824

Merged
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
6 changes: 3 additions & 3 deletions docs/laravel-quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ the `use` keyword. This is what our list of open-telemetry imported classes shou
```php
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use OpenTelemetry\API\Trace\AbstractSpan;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\API\Trace\TracerInterface;
use OpenTelemetry\Contrib\Jaeger\Exporter as JaegerExporter;
Expand Down Expand Up @@ -255,7 +255,7 @@ child span of the rootSpan. We can do the same as follows:
- Import the required functions on top of the file:

```php
use OpenTelemetry\API\Trace\AbstractSpan;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\SDK\Trace\TracerProvider;
```
Expand All @@ -267,7 +267,7 @@ use OpenTelemetry\SDK\Trace\TracerProvider;
$tracer = TracerProvider::getDefaultTracer();
if ($tracer) {
/** @var Span $span */
$span = AbstractSpan::getCurrent();
$span = Span::getCurrent();

$span->setAttribute('foo', 'bar');
$span->setAttribute('Application', 'Laravel');
Expand Down
60 changes: 7 additions & 53 deletions src/API/Trace/AbstractSpan.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,13 @@

namespace OpenTelemetry\API\Trace;

use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextKeys;
use OpenTelemetry\Context\ScopeInterface;
use const E_USER_DEPRECATED;
use function sprintf;
use function trigger_error;

abstract class AbstractSpan implements SpanInterface
{
private static ?self $invalidSpan = null;

/** @inheritDoc */
final public static function fromContext(Context $context): SpanInterface
{
if ($span = $context->get(ContextKeys::span())) {
return $span;
}

return NonRecordingSpan::getInvalid();
}

/** @inheritDoc */
final public static function getCurrent(): SpanInterface
{
return self::fromContext(Context::getCurrent());
}

/** @inheritDoc */
final public static function getInvalid(): SpanInterface
{
if (null === self::$invalidSpan) {
self::$invalidSpan = new NonRecordingSpan(SpanContext::getInvalid());
}
@trigger_error(sprintf('Using %s is deprecated, use %s instead.', AbstractSpan::class, Span::class), E_USER_DEPRECATED);

return self::$invalidSpan;
}

/** @inheritDoc */
final public static function wrap(SpanContextInterface $spanContext): SpanInterface
{
if (!$spanContext->isValid()) {
return self::getInvalid();
}

return new NonRecordingSpan($spanContext);
}

/** @inheritDoc */
final public function activate(): ScopeInterface
{
return Context::getCurrent()->withContextValue($this)->activate();
}

/** @inheritDoc */
final public function storeInContext(Context $context): Context
{
return $context->with(ContextKeys::span(), $this);
}
/** @deprecated Use {@link \OpenTelemetry\API\Trace\Span} instead. */
abstract class AbstractSpan extends Span
{
}
2 changes: 1 addition & 1 deletion src/API/Trace/NonRecordingSpan.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @internal
* @psalm-internal OpenTelemetry
*/
final class NonRecordingSpan extends AbstractSpan
final class NonRecordingSpan extends Span
{
private SpanContextInterface $context;

Expand Down
4 changes: 2 additions & 2 deletions src/API/Trace/NoopSpanBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public function setSpanKind(int $spanKind): SpanBuilderInterface

public function startSpan(): SpanInterface
{
$span = AbstractSpan::fromContext($this->parent ?? $this->contextStorage->current());
$span = Span::fromContext($this->parent ?? $this->contextStorage->current());
if ($span->isRecording()) {
$span = AbstractSpan::wrap($span->getContext());
$span = Span::wrap($span->getContext());
}

return $span;
Expand Down
6 changes: 3 additions & 3 deletions src/API/Trace/Propagation/TraceContextPropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use function count;
use function explode;
use function hexdec;
use OpenTelemetry\API\Trace\AbstractSpan;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\SpanContext;
use OpenTelemetry\API\Trace\SpanContextInterface;
use OpenTelemetry\API\Trace\TraceState;
Expand Down Expand Up @@ -60,7 +60,7 @@ public function inject(&$carrier, PropagationSetterInterface $setter = null, Con
{
$setter ??= ArrayAccessGetterSetter::getInstance();
$context ??= Context::getCurrent();
$spanContext = AbstractSpan::fromContext($context)->getContext();
$spanContext = Span::fromContext($context)->getContext();

if (!$spanContext->isValid()) {
return;
Expand Down Expand Up @@ -88,7 +88,7 @@ public function extract($carrier, PropagationGetterInterface $getter = null, Con
return $context;
}

return $context->withContextValue(AbstractSpan::wrap($spanContext));
return $context->withContextValue(Span::wrap($spanContext));
}

private static function extractImpl($carrier, PropagationGetterInterface $getter): SpanContextInterface
Expand Down
62 changes: 62 additions & 0 deletions src/API/Trace/Span.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\API\Trace;

use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextKeys;
use OpenTelemetry\Context\ScopeInterface;

abstract class Span implements SpanInterface
{
private static ?self $invalidSpan = null;

/** @inheritDoc */
final public static function fromContext(Context $context): SpanInterface
{
if ($span = $context->get(ContextKeys::span())) {
return $span;
}

return NonRecordingSpan::getInvalid();
}

/** @inheritDoc */
final public static function getCurrent(): SpanInterface
{
return self::fromContext(Context::getCurrent());
}

/** @inheritDoc */
final public static function getInvalid(): SpanInterface
{
if (null === self::$invalidSpan) {
self::$invalidSpan = new NonRecordingSpan(SpanContext::getInvalid());
}

return self::$invalidSpan;
}

/** @inheritDoc */
final public static function wrap(SpanContextInterface $spanContext): SpanInterface
{
if (!$spanContext->isValid()) {
return self::getInvalid();
}

return new NonRecordingSpan($spanContext);
}

/** @inheritDoc */
final public function activate(): ScopeInterface
{
return Context::getCurrent()->withContextValue($this)->activate();
}

/** @inheritDoc */
final public function storeInContext(Context $context): Context
{
return $context->with(ContextKeys::span(), $this);
}
}
6 changes: 3 additions & 3 deletions src/Extension/Propagator/B3/B3MultiPropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OpenTelemetry\Extension\Propagator\B3;

use OpenTelemetry\API\Trace\AbstractSpan;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\SpanContext;
use OpenTelemetry\API\Trace\SpanContextInterface;
use OpenTelemetry\Context\Context;
Expand Down Expand Up @@ -104,7 +104,7 @@ public function inject(&$carrier, PropagationSetterInterface $setter = null, Con
{
$setter ??= ArrayAccessGetterSetter::getInstance();
$context ??= Context::getCurrent();
$spanContext = AbstractSpan::fromContext($context)->getContext();
$spanContext = Span::fromContext($context)->getContext();

if (!$spanContext->isValid()) {
return;
Expand Down Expand Up @@ -132,7 +132,7 @@ public function extract($carrier, PropagationGetterInterface $getter = null, Con
return $context;
}

return $context->withContextValue(AbstractSpan::wrap($spanContext));
return $context->withContextValue(Span::wrap($spanContext));
}

private static function getSampledValue($carrier, PropagationGetterInterface $getter): ?int
Expand Down
6 changes: 3 additions & 3 deletions src/Extension/Propagator/B3/B3SinglePropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use function count;
use function explode;
use OpenTelemetry\API\Trace\AbstractSpan;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\SpanContext;
use OpenTelemetry\API\Trace\SpanContextInterface;
use OpenTelemetry\Context\Context;
Expand Down Expand Up @@ -56,7 +56,7 @@ public function inject(&$carrier, PropagationSetterInterface $setter = null, Con
{
$setter ??= ArrayAccessGetterSetter::getInstance();
$context ??= Context::getCurrent();
$spanContext = AbstractSpan::fromContext($context)->getContext();
$spanContext = Span::fromContext($context)->getContext();

if (!$spanContext->isValid()) {
return;
Expand Down Expand Up @@ -85,7 +85,7 @@ public function extract($carrier, PropagationGetterInterface $getter = null, Con
return $context;
}

return $context->withContextValue(AbstractSpan::wrap($spanContext));
return $context->withContextValue(Span::wrap($spanContext));
}

private static function processSampledValue($value): ?int
Expand Down
4 changes: 2 additions & 2 deletions src/SDK/Metrics/Exemplar/BucketStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use function assert;
use function class_exists;
use function count;
use OpenTelemetry\API\Trace\AbstractSpan;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Common\Attribute\AttributesFactoryInterface;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
Expand Down Expand Up @@ -44,7 +44,7 @@ public function store(int $bucket, $index, $value, AttributesInterface $attribut
$exemplar->attributes = $attributes;
$exemplar->revision = $revision;

if (class_exists(AbstractSpan::class) && ($spanContext = AbstractSpan::fromContext($context)->getContext())->isValid()) {
if (class_exists(Span::class) && ($spanContext = Span::fromContext($context)->getContext())->isValid()) {
$exemplar->traceId = $spanContext->getTraceId();
$exemplar->spanId = $spanContext->getSpanId();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter;

use function class_exists;
use OpenTelemetry\API\Trace\AbstractSpan;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilterInterface;
Expand All @@ -14,7 +14,7 @@ final class WithSampledTraceExemplarFilter implements ExemplarFilterInterface
{
public function accepts($value, AttributesInterface $attributes, Context $context, int $timestamp): bool
{
return class_exists(AbstractSpan::class)
&& AbstractSpan::fromContext($context)->getContext()->isSampled();
return class_exists(Span::class)
&& Span::fromContext($context)->getContext()->isSampled();
}
}
2 changes: 1 addition & 1 deletion src/SDK/Trace/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use OpenTelemetry\SDK\Resource\ResourceInfo;
use Throwable;

final class Span extends API\AbstractSpan implements ReadWriteSpanInterface
final class Span extends API\Span implements ReadWriteSpanInterface
{

/** @readonly */
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/SDK/Metrics/Exemplar/BucketStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OpenTelemetry\Tests\Unit\SDK\Metrics\Exemplar;

use OpenTelemetry\API\Trace\AbstractSpan;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\SpanContext;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
Expand Down Expand Up @@ -47,7 +47,7 @@ public function test_storage_stores_trace_information(): void
{
$storage = new BucketStorage(Attributes::factory());

$context = AbstractSpan::wrap(SpanContext::create('12345678901234567890123456789012', '1234567890123456'))
$context = Span::wrap(SpanContext::create('12345678901234567890123456789012', '1234567890123456'))
->storeInContext(Context::getRoot());

$storage->store(0, 0, 5, Attributes::create([]), $context, 7, 0);
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/SDK/Metrics/Exemplar/FilteredReservoirTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OpenTelemetry\Tests\Unit\SDK\Metrics\Exemplar;

use OpenTelemetry\API\Trace\AbstractSpan;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\SpanContext;
use OpenTelemetry\API\Trace\SpanContextInterface;
use OpenTelemetry\Context\Context;
Expand Down Expand Up @@ -57,7 +57,7 @@ public function test_with_sampled_trace_reservoir_returns_sampled_exemplars(): v
{
$reservoir = new FilteredReservoir(new FixedSizeReservoir(Attributes::factory(), 4), new WithSampledTraceExemplarFilter());

$context = AbstractSpan::wrap(SpanContext::create('12345678901234567890123456789012', '1234567890123456', SpanContextInterface::TRACE_FLAG_SAMPLED))
$context = Span::wrap(SpanContext::create('12345678901234567890123456789012', '1234567890123456', SpanContextInterface::TRACE_FLAG_SAMPLED))
->storeInContext(Context::getRoot());

$reservoir->offer(0, 5, Attributes::create([]), $context, 7, 0);
Expand All @@ -76,7 +76,7 @@ public function test_with_sampled_trace_reservoir_doesnt_return_not_sampled_exem
{
$reservoir = new FilteredReservoir(new FixedSizeReservoir(Attributes::factory(), 4), new WithSampledTraceExemplarFilter());

$context = AbstractSpan::wrap(SpanContext::create('12345678901234567890123456789012', '1234567890123456'))
$context = Span::wrap(SpanContext::create('12345678901234567890123456789012', '1234567890123456'))
->storeInContext(Context::getRoot());

$reservoir->offer(0, 5, Attributes::create([]), $context, 7, 0);
Expand Down