-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- move content type from `::send()` to transport property - move otlp exporter to `Otlp\` namespace
- Loading branch information
Showing
31 changed files
with
429 additions
and
385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Otlp; | ||
|
||
use Opentelemetry\Proto\Collector\Metrics\V1\ExportMetricsServiceResponse; | ||
use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; | ||
use OpenTelemetry\SDK\Common\Export\TransportInterface; | ||
use OpenTelemetry\SDK\Metrics\Data\Temporality; | ||
use OpenTelemetry\SDK\Metrics\MetricExporterInterface; | ||
use OpenTelemetry\SDK\Metrics\MetricMetadataInterface; | ||
use Throwable; | ||
|
||
/** | ||
* @psalm-import-type SUPPORTED_CONTENT_TYPES from ProtobufSerializer | ||
*/ | ||
final class MetricExporter implements MetricExporterInterface | ||
{ | ||
use LogsMessagesTrait; | ||
|
||
private TransportInterface $transport; | ||
private ProtobufSerializer $serializer; | ||
private $temporality; | ||
|
||
/** | ||
* @param string|Temporality|null $temporality | ||
* | ||
* @psalm-param TransportInterface<SUPPORTED_CONTENT_TYPES> $transport | ||
*/ | ||
public function __construct(TransportInterface $transport, $temporality = null) | ||
{ | ||
$this->transport = $transport; | ||
$this->serializer = ProtobufSerializer::forTransport($transport); | ||
$this->temporality = $temporality; | ||
} | ||
|
||
public function temporality(MetricMetadataInterface $metric) | ||
{ | ||
return $this->temporality ?? $metric->temporality(); | ||
} | ||
|
||
public function export(iterable $batch): bool | ||
{ | ||
return $this->transport | ||
->send($this->serializer->serialize((new MetricConverter())->convert($batch))) | ||
->map(function (?string $payload): bool { | ||
if ($payload === null) { | ||
return true; | ||
} | ||
|
||
$serviceResponse = new ExportMetricsServiceResponse(); | ||
$this->serializer->hydrate($serviceResponse, $payload); | ||
|
||
$partialSuccess = $serviceResponse->getPartialSuccess(); | ||
if ($partialSuccess !== null && $partialSuccess->getRejectedDataPoints()) { | ||
self::logError('Export partial success', [ | ||
'rejected_data_points' => $partialSuccess->getRejectedDataPoints(), | ||
'error_message' => $partialSuccess->getErrorMessage(), | ||
]); | ||
|
||
return false; | ||
} | ||
if ($partialSuccess !== null && $partialSuccess->getErrorMessage()) { | ||
self::logWarning('Export success with warnings/suggestions', ['error_message' => $partialSuccess->getErrorMessage()]); | ||
} | ||
|
||
return true; | ||
}) | ||
->catch(static function (Throwable $throwable): bool { | ||
self::logError('Export failure', ['exception' => $throwable]); | ||
|
||
return false; | ||
}) | ||
->await(); | ||
} | ||
|
||
public function shutdown(): bool | ||
{ | ||
return $this->transport->shutdown(); | ||
} | ||
|
||
public function forceFlush(): bool | ||
{ | ||
return $this->transport->forceFlush(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Otlp; | ||
|
||
use AssertionError; | ||
use Google\Protobuf\Internal\Message; | ||
use InvalidArgumentException; | ||
use OpenTelemetry\SDK\Common\Export\TransportInterface; | ||
use function sprintf; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @psalm-type SUPPORTED_CONTENT_TYPES = self::PROTOBUF|self::JSON|self::NDJSON | ||
*/ | ||
final class ProtobufSerializer | ||
{ | ||
private const PROTOBUF = 'application/x-protobuf'; | ||
private const JSON = 'application/json'; | ||
private const NDJSON = 'application/x-ndjson'; | ||
|
||
private string $contentType; | ||
|
||
private function __construct(string $contentType) | ||
{ | ||
$this->contentType = $contentType; | ||
} | ||
|
||
/** | ||
* @param TransportInterface<SUPPORTED_CONTENT_TYPES> $transport | ||
*/ | ||
public static function forTransport(TransportInterface $transport): ProtobufSerializer | ||
{ | ||
switch ($contentType = $transport->contentType()) { | ||
case self::PROTOBUF: | ||
case self::JSON: | ||
case self::NDJSON: | ||
return new self($contentType); | ||
default: | ||
throw new InvalidArgumentException(sprintf('Not supported content type "%s"', $contentType)); | ||
} | ||
} | ||
|
||
public function serialize(Message $message): string | ||
{ | ||
switch ($this->contentType) { | ||
case self::PROTOBUF: | ||
return $message->serializeToString(); | ||
case self::JSON: | ||
return $message->serializeToJsonString(); | ||
case self::NDJSON: | ||
return $message->serializeToJsonString() . "\n"; | ||
default: | ||
throw new AssertionError(); | ||
} | ||
} | ||
|
||
public function hydrate(Message $message, string $payload): void | ||
{ | ||
switch ($this->contentType) { | ||
case self::PROTOBUF: | ||
$message->mergeFromString($payload); | ||
|
||
break; | ||
case self::JSON: | ||
case self::NDJSON: | ||
$message->mergeFromJsonString($payload); | ||
|
||
break; | ||
default: | ||
throw new AssertionError(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Otlp; | ||
|
||
use Opentelemetry\Proto\Collector\Trace\V1\ExportTraceServiceResponse; | ||
use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; | ||
use OpenTelemetry\SDK\Common\Export\TransportInterface; | ||
use OpenTelemetry\SDK\Common\Future\CancellationInterface; | ||
use OpenTelemetry\SDK\Common\Future\FutureInterface; | ||
use OpenTelemetry\SDK\Trace\SpanExporterInterface; | ||
use Throwable; | ||
|
||
/** | ||
* @psalm-import-type SUPPORTED_CONTENT_TYPES from ProtobufSerializer | ||
*/ | ||
final class SpanExporter implements SpanExporterInterface | ||
{ | ||
use LogsMessagesTrait; | ||
|
||
private TransportInterface $transport; | ||
private ProtobufSerializer $serializer; | ||
|
||
/** | ||
* @psalm-param TransportInterface<SUPPORTED_CONTENT_TYPES> $transport | ||
*/ | ||
public function __construct(TransportInterface $transport) | ||
{ | ||
$this->transport = $transport; | ||
$this->serializer = ProtobufSerializer::forTransport($transport); | ||
} | ||
|
||
public function export(iterable $batch, ?CancellationInterface $cancellation = null): FutureInterface | ||
{ | ||
return $this->transport | ||
->send($this->serializer->serialize((new SpanConverter())->convert($batch)), $cancellation) | ||
->map(function (?string $payload): bool { | ||
if ($payload === null) { | ||
return true; | ||
} | ||
|
||
$serviceResponse = new ExportTraceServiceResponse(); | ||
$this->serializer->hydrate($serviceResponse, $payload); | ||
|
||
$partialSuccess = $serviceResponse->getPartialSuccess(); | ||
if ($partialSuccess !== null && $partialSuccess->getRejectedSpans()) { | ||
self::logError('Export partial success', [ | ||
'rejected_spans' => $partialSuccess->getRejectedSpans(), | ||
'error_message' => $partialSuccess->getErrorMessage(), | ||
]); | ||
|
||
return false; | ||
} | ||
if ($partialSuccess !== null && $partialSuccess->getErrorMessage()) { | ||
self::logWarning('Export success with warnings/suggestions', ['error_message' => $partialSuccess->getErrorMessage()]); | ||
} | ||
|
||
return true; | ||
}) | ||
->catch(static function (Throwable $throwable): bool { | ||
self::logError('Export failure', ['exception' => $throwable]); | ||
|
||
return false; | ||
}); | ||
} | ||
|
||
public function shutdown(?CancellationInterface $cancellation = null): bool | ||
{ | ||
return $this->transport->shutdown($cancellation); | ||
} | ||
|
||
public function forceFlush(?CancellationInterface $cancellation = null): bool | ||
{ | ||
return $this->transport->forceFlush($cancellation); | ||
} | ||
} |
Oops, something went wrong.