Skip to content

Commit 31d902b

Browse files
authored
fix gRPC timeout (#1498) (#1499)
1 parent 96ce422 commit 31d902b

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

src/API/Common/Time/ClockInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface ClockInterface
99
public const NANOS_PER_SECOND = 1_000_000_000;
1010
public const NANOS_PER_MILLISECOND = 1_000_000;
1111
public const NANOS_PER_MICROSECOND = 1_000;
12+
public const MICROS_PER_MILLISECOND = 1_000;
1213
public const MILLIS_PER_SECOND = 1_000;
1314

1415
/**

src/Contrib/Grpc/GrpcTransport.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use const Grpc\OP_SEND_MESSAGE;
1919
use const Grpc\STATUS_OK;
2020
use Grpc\Timeval;
21+
use OpenTelemetry\API\Common\Time\ClockInterface;
2122
use OpenTelemetry\Contrib\Otlp\ContentTypes;
2223
use OpenTelemetry\SDK\Common\Export\TransportInterface;
2324
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
@@ -38,15 +39,18 @@ final class GrpcTransport implements TransportInterface
3839
private readonly array $metadata;
3940
private readonly Channel $channel;
4041
private bool $closed = false;
42+
private Timeval $exportTimeout;
4143

4244
public function __construct(
4345
string $endpoint,
4446
array $opts,
4547
private readonly string $method,
4648
array $headers = [],
49+
int $timeoutMillis = 500,
4750
) {
4851
$this->channel = new Channel($endpoint, $opts);
4952
$this->metadata = $this->formatMetadata(array_change_key_case($headers));
53+
$this->exportTimeout = new Timeval($timeoutMillis * ClockInterface::MICROS_PER_MILLISECOND);
5054
}
5155

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

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

6569
$cancellation ??= new NullCancellation();
6670
$cancellationId = $cancellation->subscribe(static fn (Throwable $e) => $call->cancel());

src/Contrib/Grpc/GrpcTransportFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function create(
8181
$opts,
8282
$method,
8383
$headers,
84+
(int) ($timeout * 1000),
8485
);
8586
}
8687

@@ -116,7 +117,6 @@ private static function createOpts(
116117
'method' => null,
117118
],
118119
],
119-
'timeout' => sprintf('%0.6fs', $timeout),
120120
'retryPolicy' => [
121121
'maxAttempts' => $maxRetries,
122122
'initialBackoff' => sprintf('%0.3fs', $retryDelay / 1000),

tests/Unit/Contrib/Grpc/GrpcTransportTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class GrpcTransportTest extends TestCase
1818

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

2424
public function test_grpc_transport_supports_only_protobuf(): void

0 commit comments

Comments
 (0)