Skip to content

Commit d0a600f

Browse files
authored
Add support for concurrent exports (#790)
* Add support for concurrent exports * Remove cancellation support from `::await()`
1 parent 0013e47 commit d0a600f

18 files changed

+114
-58
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\SDK\Common\Future;
6+
7+
/**
8+
* @template T
9+
* @template-implements FutureInterface<T>
10+
*/
11+
final class CompletedFuture implements FutureInterface
12+
{
13+
/** @var T */
14+
private $value;
15+
16+
/**
17+
* @param T $value
18+
*/
19+
public function __construct($value)
20+
{
21+
$this->value = $value;
22+
}
23+
24+
public function await()
25+
{
26+
return $this->value;
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\SDK\Common\Future;
6+
7+
/**
8+
* @template T
9+
*/
10+
interface FutureInterface
11+
{
12+
/**
13+
* @psalm-return T
14+
*/
15+
public function await();
16+
}

src/SDK/Trace/Behavior/SpanExporterDecoratorTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace OpenTelemetry\SDK\Trace\Behavior;
66

77
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
8+
use OpenTelemetry\SDK\Common\Future\FutureInterface;
89
use OpenTelemetry\SDK\Trace\SpanDataInterface;
910
use OpenTelemetry\SDK\Trace\SpanExporterInterface;
1011

@@ -14,16 +15,15 @@ trait SpanExporterDecoratorTrait
1415

1516
/**
1617
* @param iterable<SpanDataInterface> $spans
17-
* @return int
18-
* @psalm-return SpanExporterInterface::STATUS_*
18+
* @return FutureInterface<int>
1919
*/
20-
public function export(iterable $spans, ?CancellationInterface $cancellation = null): int
20+
public function export(iterable $spans, ?CancellationInterface $cancellation = null): FutureInterface
2121
{
2222
$response = $this->decorated->export(
2323
$this->beforeExport($spans),
2424
$cancellation,
2525
);
26-
$this->afterExport($spans, $response);
26+
$this->afterExport($spans, $response->await());
2727

2828
return $response;
2929
}

src/SDK/Trace/Behavior/SpanExporterTrait.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace OpenTelemetry\SDK\Trace\Behavior;
66

77
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
8+
use OpenTelemetry\SDK\Common\Future\CompletedFuture;
9+
use OpenTelemetry\SDK\Common\Future\FutureInterface;
810
use OpenTelemetry\SDK\Trace\SpanDataInterface;
911
use OpenTelemetry\SDK\Trace\SpanExporterInterface;
1012

@@ -29,19 +31,16 @@ public function forceFlush(?CancellationInterface $cancellation = null): bool
2931
abstract public static function fromConnectionString(string $endpointUrl, string $name, string $args);
3032

3133
/**
32-
* @param iterable<SpanDataInterface> $spans Batch of spans to export
33-
*
34-
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/specification/trace/sdk.md#exportbatch
35-
*
36-
* @psalm-return SpanExporterInterface::STATUS_*
34+
* @param iterable<SpanDataInterface> $spans
35+
* @return FutureInterface<int>
3736
*/
38-
public function export(iterable $spans, ?CancellationInterface $cancellation = null): int
37+
public function export(iterable $spans, ?CancellationInterface $cancellation = null): FutureInterface
3938
{
4039
if (!$this->running) {
41-
return SpanExporterInterface::STATUS_FAILED_NOT_RETRYABLE;
40+
return new CompletedFuture(SpanExporterInterface::STATUS_FAILED_NOT_RETRYABLE);
4241
}
4342

44-
return $this->doExport($spans); /** @phpstan-ignore-line */
43+
return new CompletedFuture($this->doExport($spans)); /** @phpstan-ignore-line */
4544
}
4645

4746
/**

src/SDK/Trace/SpanExporterInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace OpenTelemetry\SDK\Trace;
66

77
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
8+
use OpenTelemetry\SDK\Common\Future\FutureInterface;
89

910
/**
1011
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/specification/trace/sdk.md#span-exporter
@@ -25,9 +26,9 @@ public static function fromConnectionString(string $endpointUrl, string $name, s
2526
*
2627
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/specification/trace/sdk.md#exportbatch
2728
*
28-
* @psalm-return SpanExporterInterface::STATUS_*
29+
* @psalm-return FutureInterface<int>
2930
*/
30-
public function export(iterable $spans, ?CancellationInterface $cancellation = null): int;
31+
public function export(iterable $spans, ?CancellationInterface $cancellation = null): FutureInterface;
3132

3233
/** @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/specification/trace/sdk.md#shutdown-2 */
3334
public function shutdown(?CancellationInterface $cancellation = null): bool;

src/SDK/Trace/SpanProcessor/BatchSpanProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function forceFlush(?CancellationInterface $cancellation = null): bool
105105
return true;
106106
}
107107

108-
$this->exporter->export($this->queue);
108+
$this->exporter->export($this->queue)->await();
109109
$this->queue = [];
110110
$this->stopwatch->reset();
111111
$this->exporter->forceFlush();

src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function onEnd(ReadableSpanInterface $span): void
3737
}
3838

3939
if (null !== $this->exporter) {
40-
$this->exporter->export([$span->toSpanData()]);
40+
$this->exporter->export([$span->toSpanData()])->await();
4141
}
4242
}
4343

tests/Unit/Contrib/AbstractHttpExporterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function test_exporter_response_status($responseStatus, $expected): void
5353
$expected,
5454
$this->createExporter()->export([
5555
$this->createMock(SpanData::class),
56-
])
56+
])->await(),
5757
);
5858
}
5959

@@ -107,7 +107,7 @@ public function test_client_exception_decides_return_code($exception, $expected)
107107
$expected,
108108
$this->createExporter()->export([
109109
$this->createMock(SpanData::class),
110-
])
110+
])->await(),
111111
);
112112
}
113113

tests/Unit/Contrib/AgentExporterTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenTelemetry\Tests\Contrib\Unit;
5+
namespace OpenTelemetry\Tests\Unit\Contrib;
66

77
use OpenTelemetry\Contrib\Jaeger\AgentExporter;
88
use OpenTelemetry\SDK\Trace\SpanExporterInterface;
99
use OpenTelemetry\Tests\Unit\SDK\Util\SpanData;
1010
use PHPUnit\Framework\TestCase;
1111

1212
/**
13-
* @covers OpenTelemetry\Contrib\Jaeger\AgentExporter
14-
* @covers OpenTelemetry\Contrib\Jaeger\JaegerTransport
15-
* @covers OpenTelemetry\Contrib\Jaeger\ThriftUdpTransport
16-
* @covers OpenTelemetry\Contrib\Jaeger\ParsedEndpointUrl
13+
* @covers \OpenTelemetry\Contrib\Jaeger\AgentExporter
14+
* @covers \OpenTelemetry\Contrib\Jaeger\JaegerTransport
15+
* @covers \OpenTelemetry\Contrib\Jaeger\ThriftUdpTransport
16+
* @covers \OpenTelemetry\Contrib\Jaeger\ParsedEndpointUrl
1717
*/
1818
class AgentExporterTest extends TestCase
1919
{
@@ -24,7 +24,7 @@ public function test_happy_path()
2424
'someServiceName',
2525
);
2626

27-
$status = $exporter->export([new SpanData()]);
27+
$status = $exporter->export([new SpanData()])->await();
2828

2929
$this->assertSame(SpanExporterInterface::STATUS_SUCCESS, $status);
3030

tests/Unit/Contrib/JaegerHttpCollectorExporterTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
use PHPUnit\Framework\TestCase;
1111

1212
/**
13-
* @covers OpenTelemetry\Contrib\Jaeger\HttpCollectorExporter
14-
* @covers OpenTelemetry\Contrib\Jaeger\HttpSender
15-
* @covers OpenTelemetry\Contrib\Jaeger\ThriftHttpTransport
16-
* @covers OpenTelemetry\Contrib\Jaeger\ParsedEndpointUrl
17-
* @covers OpenTelemetry\Contrib\Jaeger\BatchAdapter\BatchAdapter
18-
* @covers OpenTelemetry\Contrib\Jaeger\BatchAdapter\BatchAdapterFactory
13+
* @covers \OpenTelemetry\Contrib\Jaeger\HttpCollectorExporter
14+
* @covers \OpenTelemetry\Contrib\Jaeger\HttpSender
15+
* @covers \OpenTelemetry\Contrib\Jaeger\ThriftHttpTransport
16+
* @covers \OpenTelemetry\Contrib\Jaeger\ParsedEndpointUrl
17+
* @covers \OpenTelemetry\Contrib\Jaeger\BatchAdapter\BatchAdapter
18+
* @covers \OpenTelemetry\Contrib\Jaeger\BatchAdapter\BatchAdapterFactory
1919
*
2020
*/
2121
class JaegerHttpCollectorExporterTest extends TestCase
@@ -35,7 +35,7 @@ public function test_happy_path()
3535
$this->getStreamFactoryInterfaceMock()
3636
);
3737

38-
$status = $exporter->export([new SpanData()]);
38+
$status = $exporter->export([new SpanData()])->await();
3939

4040
$this->assertSame(SpanExporterInterface::STATUS_SUCCESS, $status);
4141
}

0 commit comments

Comments
 (0)