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

fix gRPC timeout (#1498) #1499

Merged
merged 2 commits into from
Feb 3, 2025
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
1 change: 1 addition & 0 deletions src/API/Common/Time/ClockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface ClockInterface
public const NANOS_PER_SECOND = 1_000_000_000;
public const NANOS_PER_MILLISECOND = 1_000_000;
public const NANOS_PER_MICROSECOND = 1_000;
public const MICROS_PER_MILLISECOND = 1_000;
public const MILLIS_PER_SECOND = 1_000;

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Contrib/Grpc/GrpcTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use const Grpc\OP_SEND_MESSAGE;
use const Grpc\STATUS_OK;
use Grpc\Timeval;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\Contrib\Otlp\ContentTypes;
use OpenTelemetry\SDK\Common\Export\TransportInterface;
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
Expand All @@ -38,15 +39,18 @@ final class GrpcTransport implements TransportInterface
private readonly array $metadata;
private readonly Channel $channel;
private bool $closed = false;
private Timeval $exportTimeout;

public function __construct(
string $endpoint,
array $opts,
private readonly string $method,
array $headers = [],
int $timeoutMillis = 500,
) {
$this->channel = new Channel($endpoint, $opts);
$this->metadata = $this->formatMetadata(array_change_key_case($headers));
$this->exportTimeout = new Timeval($timeoutMillis * ClockInterface::MICROS_PER_MILLISECOND);
}

public function contentType(): string
Expand All @@ -60,7 +64,7 @@ public function send(string $payload, ?CancellationInterface $cancellation = nul
return new ErrorFuture(new BadMethodCallException('Transport closed'));
}

$call = new Call($this->channel, $this->method, Timeval::infFuture());
$call = new Call($this->channel, $this->method, $this->exportTimeout);

$cancellation ??= new NullCancellation();
$cancellationId = $cancellation->subscribe(static fn (Throwable $e) => $call->cancel());
Expand Down
2 changes: 1 addition & 1 deletion src/Contrib/Grpc/GrpcTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function create(
$opts,
$method,
$headers,
(int) ($timeout * 1000),
);
}

Expand Down Expand Up @@ -116,7 +117,6 @@ private static function createOpts(
'method' => null,
],
],
'timeout' => sprintf('%0.6fs', $timeout),
'retryPolicy' => [
'maxAttempts' => $maxRetries,
'initialBackoff' => sprintf('%0.3fs', $retryDelay / 1000),
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Contrib/Grpc/GrpcTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class GrpcTransportTest extends TestCase

public function setUp(): void
{
$this->transport = new GrpcTransport('http://localhost:4317', [], '/method', []);
$this->transport = new GrpcTransport('http://localhost:4317', [], '/method', [], 123);
}

public function test_grpc_transport_supports_only_protobuf(): void
Expand Down
Loading