diff --git a/docs/laravel-quickstart.md b/docs/laravel-quickstart.md index cca05746d..689003969 100644 --- a/docs/laravel-quickstart.md +++ b/docs/laravel-quickstart.md @@ -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; @@ -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; ``` @@ -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'); diff --git a/src/API/Trace/AbstractSpan.php b/src/API/Trace/AbstractSpan.php index d1534722b..4cc72bdd2 100644 --- a/src/API/Trace/AbstractSpan.php +++ b/src/API/Trace/AbstractSpan.php @@ -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 +{ } diff --git a/src/API/Trace/NonRecordingSpan.php b/src/API/Trace/NonRecordingSpan.php index aed4f1d1f..ce2481899 100644 --- a/src/API/Trace/NonRecordingSpan.php +++ b/src/API/Trace/NonRecordingSpan.php @@ -12,7 +12,7 @@ * @internal * @psalm-internal OpenTelemetry */ -final class NonRecordingSpan extends AbstractSpan +final class NonRecordingSpan extends Span { private SpanContextInterface $context; diff --git a/src/API/Trace/NoopSpanBuilder.php b/src/API/Trace/NoopSpanBuilder.php index 8c79ce991..b98a552fe 100644 --- a/src/API/Trace/NoopSpanBuilder.php +++ b/src/API/Trace/NoopSpanBuilder.php @@ -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; diff --git a/src/API/Trace/Propagation/TraceContextPropagator.php b/src/API/Trace/Propagation/TraceContextPropagator.php index 8b7819bc7..928747993 100644 --- a/src/API/Trace/Propagation/TraceContextPropagator.php +++ b/src/API/Trace/Propagation/TraceContextPropagator.php @@ -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; @@ -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; @@ -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 diff --git a/src/API/Trace/Span.php b/src/API/Trace/Span.php new file mode 100644 index 000000000..17a53884d --- /dev/null +++ b/src/API/Trace/Span.php @@ -0,0 +1,62 @@ +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); + } +} diff --git a/src/Extension/Propagator/B3/B3MultiPropagator.php b/src/Extension/Propagator/B3/B3MultiPropagator.php index 2d56fbe0e..5f52d3c66 100644 --- a/src/Extension/Propagator/B3/B3MultiPropagator.php +++ b/src/Extension/Propagator/B3/B3MultiPropagator.php @@ -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; @@ -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; @@ -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 diff --git a/src/Extension/Propagator/B3/B3SinglePropagator.php b/src/Extension/Propagator/B3/B3SinglePropagator.php index 35643abbc..3b1d4e546 100644 --- a/src/Extension/Propagator/B3/B3SinglePropagator.php +++ b/src/Extension/Propagator/B3/B3SinglePropagator.php @@ -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; @@ -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; @@ -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 diff --git a/src/SDK/Metrics/Exemplar/BucketStorage.php b/src/SDK/Metrics/Exemplar/BucketStorage.php index 7d757389b..aed98249c 100644 --- a/src/SDK/Metrics/Exemplar/BucketStorage.php +++ b/src/SDK/Metrics/Exemplar/BucketStorage.php @@ -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; @@ -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 { diff --git a/src/SDK/Metrics/Exemplar/ExemplarFilter/WithSampledTraceExemplarFilter.php b/src/SDK/Metrics/Exemplar/ExemplarFilter/WithSampledTraceExemplarFilter.php index 48583a4aa..15796263e 100644 --- a/src/SDK/Metrics/Exemplar/ExemplarFilter/WithSampledTraceExemplarFilter.php +++ b/src/SDK/Metrics/Exemplar/ExemplarFilter/WithSampledTraceExemplarFilter.php @@ -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; @@ -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(); } } diff --git a/src/SDK/Trace/Span.php b/src/SDK/Trace/Span.php index 8d9ca09eb..c174f8bde 100644 --- a/src/SDK/Trace/Span.php +++ b/src/SDK/Trace/Span.php @@ -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 */ diff --git a/tests/Unit/SDK/Metrics/Exemplar/BucketStorageTest.php b/tests/Unit/SDK/Metrics/Exemplar/BucketStorageTest.php index 98737cfe2..b7a704cc9 100644 --- a/tests/Unit/SDK/Metrics/Exemplar/BucketStorageTest.php +++ b/tests/Unit/SDK/Metrics/Exemplar/BucketStorageTest.php @@ -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; @@ -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); diff --git a/tests/Unit/SDK/Metrics/Exemplar/FilteredReservoirTest.php b/tests/Unit/SDK/Metrics/Exemplar/FilteredReservoirTest.php index 4c13dc037..5fe6b6fa7 100644 --- a/tests/Unit/SDK/Metrics/Exemplar/FilteredReservoirTest.php +++ b/tests/Unit/SDK/Metrics/Exemplar/FilteredReservoirTest.php @@ -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; @@ -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); @@ -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);