Skip to content

Commit

Permalink
Move span and baggage context key to context package
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevay committed Sep 11, 2022
1 parent fe53dc1 commit 489d44d
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 74 deletions.
3 changes: 0 additions & 3 deletions deptrac.baseline.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
deptrac:
skip_violations:
OpenTelemetry\Context\Context:
- OpenTelemetry\API\Trace\SpanContextKey
- OpenTelemetry\API\Trace\SpanInterface
OpenTelemetry\SDK\Trace\Span:
- OpenTelemetry\API\Trace\NonRecordingSpan
5 changes: 3 additions & 2 deletions src/API/Baggage/Baggage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OpenTelemetry\API\Baggage;

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

final class Baggage implements BaggageInterface
Expand All @@ -14,7 +15,7 @@ final class Baggage implements BaggageInterface
/** @inheritDoc */
public static function fromContext(Context $context): BaggageInterface
{
if ($baggage = $context->get(BaggageContextKey::instance())) {
if ($baggage = $context->get(ContextKeys::baggage())) {
return $baggage;
}

Expand Down Expand Up @@ -97,6 +98,6 @@ public function toBuilder(): BaggageBuilderInterface
/** @inheritDoc */
public function storeInContext(Context $context): Context
{
return $context->with(BaggageContextKey::instance(), $this);
return $context->with(ContextKeys::baggage(), $this);
}
}
27 changes: 0 additions & 27 deletions src/API/Baggage/BaggageContextKey.php

This file was deleted.

5 changes: 3 additions & 2 deletions src/API/Trace/AbstractSpan.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OpenTelemetry\API\Trace;

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

abstract class AbstractSpan implements SpanInterface
Expand All @@ -14,7 +15,7 @@ abstract class AbstractSpan implements SpanInterface
/** @inheritDoc */
final public static function fromContext(Context $context): SpanInterface
{
if ($span = $context->get(SpanContextKey::instance())) {
if ($span = $context->get(ContextKeys::span())) {
return $span;
}

Expand Down Expand Up @@ -56,6 +57,6 @@ final public function activate(): ScopeInterface
/** @inheritDoc */
final public function storeInContext(Context $context): Context
{
return $context->with(SpanContextKey::instance(), $this);
return $context->with(ContextKeys::span(), $this);
}
}
27 changes: 0 additions & 27 deletions src/API/Trace/SpanContextKey.php

This file was deleted.

10 changes: 3 additions & 7 deletions src/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace OpenTelemetry\Context;

use function assert;
use OpenTelemetry\API\Trace\SpanContextKey;
use OpenTelemetry\API\Trace\SpanInterface;
use function spl_object_id;

/**
Expand All @@ -19,15 +16,15 @@ final class Context

// Optimization for spans to avoid copying the context array.
private static ContextKey $spanContextKey;
private ?SpanInterface $span = null;
private ?object $span = null;
/** @var array<int, mixed> */
private array $context = [];
/** @var array<int, ContextKey> */
private array $contextKeys = [];

private function __construct()
{
self::$spanContextKey = SpanContextKey::instance();
self::$spanContextKey = ContextKeys::span();
}

public static function createKey(string $key): ContextKey
Expand Down Expand Up @@ -88,8 +85,7 @@ public function with(ContextKey $key, $value): self
$self = clone $this;

if ($key === self::$spanContextKey) {
assert($value instanceof SpanInterface || $value === null);
$self->span = $value;
$self->span = $value; // @phan-suppress-current-line PhanTypeMismatchPropertyReal

return $self;
}
Expand Down
25 changes: 25 additions & 0 deletions src/Context/ContextKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Context;

/**
* @psalm-internal OpenTelemetry
*/
final class ContextKeys
{
public static function span(): ContextKey
{
static $instance;

return $instance ??= Context::createKey('opentelemetry-trace-span-key');
}

public static function baggage(): ContextKey
{
static $instance;

return $instance ??= Context::createKey('opentelemetry-trace-baggage-key');
}
}
12 changes: 6 additions & 6 deletions tests/Unit/API/Trace/NoopSpanBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

declare(strict_types=1);

namespace OpenTelemetry\Tests\API\Unit\Trace;
namespace OpenTelemetry\Tests\Unit\API\Trace;

use OpenTelemetry\API\Trace\NonRecordingSpan;
use OpenTelemetry\API\Trace\NoopSpanBuilder;
use OpenTelemetry\API\Trace\SpanContextInterface;
use OpenTelemetry\API\Trace\SpanContextKey;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextKeys;
use OpenTelemetry\Context\ContextStorageInterface;
use OpenTelemetry\Tests\Unit\SDK\Util\TestClock;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -39,7 +39,7 @@ public function test_noop_created_span_uses_provided_context(): void
$span = $this->createMock(SpanInterface::class);
$span->method('getContext')->willReturn($spanContext);

$context = Context::getRoot()->with(SpanContextKey::instance(), $span);
$context = Context::getRoot()->with(ContextKeys::span(), $span);

$contextStorage = $this->createMock(ContextStorageInterface::class);

Expand All @@ -58,7 +58,7 @@ public function test_noop_created_span_uses_current_context(): void
$span = $this->createMock(SpanInterface::class);
$span->method('getContext')->willReturn($spanContext);

$context = Context::getRoot()->with(SpanContextKey::instance(), $span);
$context = Context::getRoot()->with(ContextKeys::span(), $span);

$contextStorage = $this->createMock(ContextStorageInterface::class);
$contextStorage->method('current')->willReturn($context);
Expand All @@ -77,7 +77,7 @@ public function test_noop_created_span_doesnt_use_current_context_if_no_parent()
$span = $this->createMock(SpanInterface::class);
$span->method('getContext')->willReturn($spanContext);

$context = Context::getRoot()->with(SpanContextKey::instance(), $span);
$context = Context::getRoot()->with(ContextKeys::span(), $span);

$contextStorage = $this->createMock(ContextStorageInterface::class);
$contextStorage->method('current')->willReturn($context);
Expand All @@ -96,7 +96,7 @@ public function test_noop_created_span_removes_is_recording_flag(): void
$span = $this->createMock(SpanInterface::class);
$span->method('isRecording')->willReturn(true);

$context = Context::getRoot()->with(SpanContextKey::instance(), $span);
$context = Context::getRoot()->with(ContextKeys::span(), $span);

$contextStorage = $this->createMock(ContextStorageInterface::class);

Expand Down

0 comments on commit 489d44d

Please sign in to comment.