Skip to content

Commit

Permalink
Add ReadableSpan/ReadWriteSpan interfaces (#403)
Browse files Browse the repository at this point in the history
* Add ReadableSpan interface

* Add ReadWriteSpan interface

Remove reading methods from API\Span interface.

* Fix code style
  • Loading branch information
lalex authored Aug 27, 2021
1 parent e5e8b74 commit 04f84d7
Show file tree
Hide file tree
Showing 32 changed files with 230 additions and 104 deletions.
29 changes: 0 additions & 29 deletions api/Trace/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,6 @@

interface Span extends SpanStatus, SpanKind
{
public function getSpanName(): string;
public function getContext(): SpanContext;
public function getParent(): ?SpanContext;

/**
* Returns Epoch timestamp value (RealtimeClock) when the Span was created
* @return int
*/
public function getStartEpochTimestamp(): int;

/**
* Returns system time clock value (MonotonicClock) when the Span was created
* @return int
*/
public function getStart(): int;

/**
* Returns system time clock value (MonotonicClock) when the Span was stopped
* @return int|null
*/
public function getEnd(): ?int;

public function getAttributes(): Attributes;
public function getLinks(): Links;
public function getEvents(): Events;
public function getStatus(): SpanStatus;

/**
* Attributes SHOULD preserve the order in which they're set. Setting an attribute with the same key as an existing
* attribute SHOULD overwrite the existing attribute's value.
Expand Down Expand Up @@ -90,6 +63,4 @@ public function setSpanStatus(string $code, ?string $description = null): Span;
public function end(int $timestamp = null): Span;

public function isRecording(): bool;

public function isSampled(): bool;
}
5 changes: 3 additions & 2 deletions api/Trace/SpanOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace OpenTelemetry\Trace;

use OpenTelemetry\Context\Context;

/**
* The SpanOptions implementation is intended to be tightly coupled with the Span and Tracer implementations.
* Hopefully the Span object can implement SpanOptions so that the toSpan() call is almost a no-op, but prevents
Expand All @@ -14,8 +16,7 @@ interface SpanOptions
public function setSpanName(string $name): SpanOptions;
/** should default to INTERNAL if not called */
public function setSpanKind(int $spanKind): SpanOptions;
public function setParentContext(SpanContext $span): SpanOptions;
public function setParentSpan(Span $span): SpanOptions;
public function setParent(Context $parentContext): SpanOptions;
public function addAttributes(Attributes $attributes): SpanOptions;
public function addLinks(Links $links): SpanOptions;

Expand Down
5 changes: 2 additions & 3 deletions contrib/Newrelic/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use GuzzleHttp\Psr7\HttpFactory;
use InvalidArgumentException;
use OpenTelemetry\Sdk\Trace;
use OpenTelemetry\Trace as API;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Client\NetworkExceptionInterface;
Expand Down Expand Up @@ -64,7 +63,7 @@ class Exporter implements Trace\Exporter
* @var RequestFactoryInterface
*/
private $requestFactory;

/**
* @var StreamFactoryInterface
*/
Expand Down Expand Up @@ -108,7 +107,7 @@ public function __construct(
/**
* Exports the provided Span data via the Newrelic protocol
*
* @param iterable<API\Span> $spans Array of Spans
* @param iterable<Trace\ReadableSpan> $spans Array of Spans
* @return int return code, defined on the Exporter interface
*/
public function export(iterable $spans): int
Expand Down
4 changes: 2 additions & 2 deletions contrib/Newrelic/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OpenTelemetry\Contrib\Newrelic;

use OpenTelemetry\Trace\Span;
use OpenTelemetry\Sdk\Trace\ReadableSpan;

class SpanConverter
{
Expand All @@ -21,7 +21,7 @@ public function __construct(string $serviceName)
$this->serviceName = $serviceName;
}

public function convert(Span $span)
public function convert(ReadableSpan $span)
{
$spanParent = $span->getParent();
$row = [
Expand Down
5 changes: 2 additions & 3 deletions contrib/Otlp/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use OpenTelemetry\Contrib\OtlpGrpc\Exporter as OtlpGrpcExporter;
use OpenTelemetry\Contrib\OtlpHttp\Exporter as OtlpHttpExporter;
use OpenTelemetry\Sdk\Trace;
use OpenTelemetry\Trace as API;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Client\NetworkExceptionInterface;
Expand Down Expand Up @@ -72,7 +71,7 @@ class Exporter implements Trace\Exporter
* @var RequestFactoryInterface
*/
private $requestFactory;

/**
* @var StreamFactoryInterface
*/
Expand Down Expand Up @@ -108,7 +107,7 @@ public function __construct(
/**
* Exports the provided Span data via the OTLP protocol
*
* @param iterable<API\Span> $spans Array of Spans
* @param iterable<Trace\ReadableSpan> $spans Array of Spans
* @return int return code, defined on the Exporter interface
*/
public function export(iterable $spans): int
Expand Down
4 changes: 2 additions & 2 deletions contrib/Otlp/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OpenTelemetry\Contrib\Otlp;

use OpenTelemetry\Trace\Span;
use OpenTelemetry\Sdk\Trace\ReadableSpan;

class SpanConverter
{
Expand Down Expand Up @@ -40,7 +40,7 @@ private function sanitiseTagValue($value)
return (string) $value;
}

public function convert(Span $span)
public function convert(ReadableSpan $span)
{
$spanParent = $span->getParent();
$row = [
Expand Down
7 changes: 3 additions & 4 deletions contrib/OtlpGrpc/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Opentelemetry\Proto\Collector\Trace\V1\ExportTraceServiceRequest;
use Opentelemetry\Proto\Collector\Trace\V1\TraceServiceClient;
use OpenTelemetry\Sdk\Trace;
use OpenTelemetry\Trace as API;

class Exporter implements Trace\Exporter
{
Expand Down Expand Up @@ -53,7 +52,7 @@ class Exporter implements Trace\Exporter
private $spanConverter;

private $metadata;

/**
* @var bool
*/
Expand Down Expand Up @@ -126,15 +125,15 @@ public function getClientOptions(): array
/**
* Exports the provided Span data via the OTLP protocol
*
* @param iterable<API\Span> $spans Array of Spans
* @param iterable<Trace\ReadableSpan> $spans Array of Spans
* @return int return code, defined on the Exporter interface
*/
public function export(iterable $spans): int
{
if (!$this->running) {
return Exporter::FAILED_NOT_RETRYABLE;
}

if (empty($spans)) {
return Trace\Exporter::SUCCESS;
}
Expand Down
4 changes: 2 additions & 2 deletions contrib/OtlpGrpc/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Opentelemetry\Proto\Trace\V1\Span\SpanKind;
use Opentelemetry\Proto\Trace\V1\Status;
use Opentelemetry\Proto\Trace\V1\Status\StatusCode;
use OpenTelemetry\Sdk\Trace\Span;
use OpenTelemetry\Sdk\Trace\ReadableSpan;
use OpenTelemetry\Sdk\Trace\SpanStatus;

class SpanConverter
Expand Down Expand Up @@ -78,7 +78,7 @@ public function as_otlp_span_kind($kind): int
return SpanKind::SPAN_KIND_UNSPECIFIED;
}

public function as_otlp_span(Span $span): CollectorSpan
public function as_otlp_span(ReadableSpan $span): CollectorSpan
{
$end_timestamp = ($span->getStartEpochTimestamp() + $span->getDuration());

Expand Down
3 changes: 1 addition & 2 deletions contrib/OtlpHttp/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use InvalidArgumentException;
use Opentelemetry\Proto\Collector\Trace\V1\ExportTraceServiceRequest;
use OpenTelemetry\Sdk\Trace;
use OpenTelemetry\Trace as API;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Client\NetworkExceptionInterface;
Expand Down Expand Up @@ -124,7 +123,7 @@ public function __construct(
/**
* Exports the provided Span data via the OTLP protocol
*
* @param iterable<API\Span> $spans Array of Spans
* @param iterable<Trace\ReadableSpan> $spans Array of Spans
* @return int return code, defined on the Exporter interface
*/
public function export(iterable $spans): int
Expand Down
4 changes: 2 additions & 2 deletions contrib/OtlpHttp/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Opentelemetry\Proto\Trace\V1\Span\SpanKind;
use Opentelemetry\Proto\Trace\V1\Status;
use Opentelemetry\Proto\Trace\V1\Status\StatusCode;
use OpenTelemetry\Sdk\Trace\Span;
use OpenTelemetry\Sdk\Trace\ReadableSpan;
use OpenTelemetry\Sdk\Trace\SpanStatus;

class SpanConverter
Expand Down Expand Up @@ -76,7 +76,7 @@ public function as_otlp_span_kind($kind): int
return SpanKind::SPAN_KIND_UNSPECIFIED;
}

public function as_otlp_span(Span $span): CollectorSpan
public function as_otlp_span(ReadableSpan $span): CollectorSpan
{
$end_timestamp = ($span->getStartEpochTimestamp() + $span->getDuration());

Expand Down
9 changes: 4 additions & 5 deletions contrib/Zipkin/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use GuzzleHttp\Psr7\HttpFactory;
use InvalidArgumentException;
use OpenTelemetry\Sdk\Trace;
use OpenTelemetry\Trace as API;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Client\NetworkExceptionInterface;
Expand Down Expand Up @@ -41,9 +40,9 @@ class Exporter implements Trace\Exporter
* @var ClientInterface
*/
private $client;

private $requestFactory;

private $streamFactory;

public function __construct(
Expand Down Expand Up @@ -79,7 +78,7 @@ public function __construct(
/**
* Exports the provided Span data via the Zipkin protocol
*
* @param iterable<API\Span> $spans Array of Spans
* @param iterable<Trace\ReadableSpan> $spans Array of Spans
* @return int return code, defined on the Exporter interface
*/
public function export(iterable $spans): int
Expand All @@ -103,7 +102,7 @@ public function export(iterable $spans): int
->createRequest('POST', $this->endpointUrl)
->withBody($body)
->withHeader('content-type', 'application/json');

$response = $this->client->sendRequest($request);
} catch (RequestExceptionInterface $e) {
return Trace\Exporter::FAILED_NOT_RETRYABLE;
Expand Down
4 changes: 2 additions & 2 deletions contrib/Zipkin/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OpenTelemetry\Contrib\Zipkin;

use OpenTelemetry\Trace\Span;
use OpenTelemetry\Sdk\Trace\ReadableSpan;

class SpanConverter
{
Expand Down Expand Up @@ -42,7 +42,7 @@ private function sanitiseTagValue($value)
return (string) $value;
}

public function convert(Span $span)
public function convert(ReadableSpan $span)
{
$spanParent = $span->getParent();
$row = [
Expand Down
5 changes: 2 additions & 3 deletions contrib/ZipkinToNewrelic/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use GuzzleHttp\Psr7\HttpFactory;
use InvalidArgumentException;
use OpenTelemetry\Sdk\Trace;
use OpenTelemetry\Trace as API;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Client\NetworkExceptionInterface;
Expand Down Expand Up @@ -56,7 +55,7 @@ class Exporter implements Trace\Exporter
* @var RequestFactoryInterface
*/
private $requestFactory;

/**
* @var StreamFactoryInterface
*/
Expand Down Expand Up @@ -101,7 +100,7 @@ public function __construct(
/**
* Exports the provided Span data via the Zipkin protocol
*
* @param iterable<API\Span> $spans Array of Spans
* @param iterable<Trace\ReadableSpan> $spans Array of Spans
* @return int return code, defined on the Exporter interface
*/
public function export(iterable $spans): int
Expand Down
4 changes: 2 additions & 2 deletions contrib/ZipkinToNewrelic/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OpenTelemetry\Contrib\ZipkinToNewrelic;

use OpenTelemetry\Trace\Span;
use OpenTelemetry\Sdk\Trace\ReadableSpan;

class SpanConverter
{
Expand Down Expand Up @@ -42,7 +42,7 @@ private function sanitiseTagValue($value)
return (string) $value;
}

public function convert(Span $span)
public function convert(ReadableSpan $span)
{
$spanParent = $span->getParent();
$row = [
Expand Down
4 changes: 1 addition & 3 deletions sdk/Trace/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace OpenTelemetry\Sdk\Trace;

use OpenTelemetry\Trace as API;

/**
* A simple Exporter interface
*
Expand All @@ -22,7 +20,7 @@ interface Exporter

/**
* Export trace data (spans)
* @param iterable<API\Span> $spans Batch of spans to export
* @param iterable<ReadableSpan> $spans Batch of spans to export
* @return int
*/
public function export(iterable $spans): int;
Expand Down
24 changes: 23 additions & 1 deletion sdk/Trace/NoopSpan.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

use OpenTelemetry\Context\ContextKey;
use OpenTelemetry\Context\ContextValueTrait;
use OpenTelemetry\Sdk\InstrumentationLibrary;
use OpenTelemetry\Sdk\Resource\ResourceInfo;
use OpenTelemetry\Trace as API;
use Throwable;

class NoopSpan implements API\Span
class NoopSpan implements API\Span, ReadableSpan
{
use ContextValueTrait;

Expand Down Expand Up @@ -192,4 +194,24 @@ protected static function getContextKey(): ContextKey
{
return SpanContextKey::instance();
}

public function getSpanContext(): API\SpanContext
{
return $this->context;
}

public function getEndEpochTimestamp(): ?int
{
return null;
}

public function getResource(): ResourceInfo
{
return ResourceInfo::emptyResource();
}

public function getInstrumentationLibrary(): InstrumentationLibrary
{
return new InstrumentationLibrary('');
}
}
Loading

0 comments on commit 04f84d7

Please sign in to comment.