Skip to content

Commit 47fcb66

Browse files
Refactor InMemory exporters to use shared storage manager (#1559)
Introduced `InMemoryStorageManager` to centralize storage management for spans, metrics, and logs. Updated InMemory exporters and their factory classes to utilize this shared storage. * Add integration test for InMemoryExporter - add integration test that shows that storage contains metrics - because of src/SDK/Metrics/MetricReader/ExportingReader.php:148 I had to add `PushMetricExporterInterface` to InMemoryExporter class * Add integration test for InMemoryExporter
1 parent 0e7804c commit 47fcb66

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\SDK\Common\Export;
6+
7+
use ArrayObject;
8+
9+
class InMemoryStorageManager
10+
{
11+
private static ArrayObject $spans;
12+
private static ArrayObject $metrics;
13+
private static ArrayObject $logs;
14+
15+
public static function metrics(): ArrayObject
16+
{
17+
/** @psalm-suppress RedundantPropertyInitializationCheck */
18+
return self::$metrics ??= new ArrayObject();
19+
}
20+
21+
public static function logs(): ArrayObject
22+
{
23+
/** @psalm-suppress RedundantPropertyInitializationCheck */
24+
return self::$logs ??= new ArrayObject();
25+
}
26+
27+
public static function spans(): ArrayObject
28+
{
29+
/** @psalm-suppress RedundantPropertyInitializationCheck */
30+
return self::$spans ??= new ArrayObject();
31+
}
32+
}

Logs/Exporter/InMemoryExporterFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace OpenTelemetry\SDK\Logs\Exporter;
66

7+
use OpenTelemetry\SDK\Common\Export\InMemoryStorageManager;
78
use OpenTelemetry\SDK\Logs\LogRecordExporterFactoryInterface;
89
use OpenTelemetry\SDK\Logs\LogRecordExporterInterface;
910

1011
class InMemoryExporterFactory implements LogRecordExporterFactoryInterface
1112
{
1213
public function create(): LogRecordExporterInterface
1314
{
14-
return new InMemoryExporter();
15+
return new InMemoryExporter(InMemoryStorageManager::logs());
1516
}
1617
}

Metrics/MetricExporter/InMemoryExporter.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,30 @@
44

55
namespace OpenTelemetry\SDK\Metrics\MetricExporter;
66

7-
use function array_push;
7+
use ArrayObject;
88
use OpenTelemetry\SDK\Metrics\AggregationTemporalitySelectorInterface;
99
use OpenTelemetry\SDK\Metrics\Data\Metric;
1010
use OpenTelemetry\SDK\Metrics\Data\Temporality;
1111
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
1212
use OpenTelemetry\SDK\Metrics\MetricMetadataInterface;
13+
use OpenTelemetry\SDK\Metrics\PushMetricExporterInterface;
1314

1415
/**
1516
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk_exporters/in-memory.md
1617
*/
17-
final class InMemoryExporter implements MetricExporterInterface, AggregationTemporalitySelectorInterface
18+
final class InMemoryExporter implements MetricExporterInterface, AggregationTemporalitySelectorInterface, PushMetricExporterInterface
1819
{
19-
/**
20-
* @var list<Metric>
21-
*/
22-
private array $metrics = [];
23-
2420
private bool $closed = false;
2521

26-
public function __construct(private readonly string|Temporality|null $temporality = null)
27-
{
22+
/**
23+
* @template-implements ArrayObject<Metric> $storage
24+
* @param ArrayObject $storage
25+
* @param string|Temporality|null $temporality
26+
*/
27+
public function __construct(
28+
private ArrayObject $storage = new ArrayObject(),
29+
private readonly string|Temporality|null $temporality = null,
30+
) {
2831
}
2932

3033
public function temporality(MetricMetadataInterface $metric): string|Temporality|null
@@ -33,13 +36,13 @@ public function temporality(MetricMetadataInterface $metric): string|Temporality
3336
}
3437

3538
/**
36-
* @return list<Metric>
39+
* @return Metric[]
3740
*/
3841
public function collect(bool $reset = false): array
3942
{
40-
$metrics = $this->metrics;
43+
$metrics = $this->storage->getArrayCopy();
4144
if ($reset) {
42-
$this->metrics = [];
45+
$this->storage = new ArrayObject();
4346
}
4447

4548
return $metrics;
@@ -51,8 +54,9 @@ public function export(iterable $batch): bool
5154
return false;
5255
}
5356

54-
/** @psalm-suppress InvalidPropertyAssignmentValue */
55-
array_push($this->metrics, ...$batch);
57+
foreach ($batch as $metric) {
58+
$this->storage->append($metric);
59+
}
5660

5761
return true;
5862
}
@@ -67,4 +71,9 @@ public function shutdown(): bool
6771

6872
return true;
6973
}
74+
75+
public function forceFlush(): bool
76+
{
77+
return true;
78+
}
7079
}

Metrics/MetricExporter/InMemoryExporterFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace OpenTelemetry\SDK\Metrics\MetricExporter;
66

7+
use OpenTelemetry\SDK\Common\Export\InMemoryStorageManager;
78
use OpenTelemetry\SDK\Metrics\MetricExporterFactoryInterface;
89
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
910

1011
class InMemoryExporterFactory implements MetricExporterFactoryInterface
1112
{
1213
public function create(): MetricExporterInterface
1314
{
14-
return new InMemoryExporter();
15+
return new InMemoryExporter(InMemoryStorageManager::metrics());
1516
}
1617
}

Trace/SpanExporter/InMemorySpanExporterFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
namespace OpenTelemetry\SDK\Trace\SpanExporter;
66

7+
use OpenTelemetry\SDK\Common\Export\InMemoryStorageManager;
78
use OpenTelemetry\SDK\Trace\SpanExporterInterface;
89

910
class InMemorySpanExporterFactory implements SpanExporterFactoryInterface
1011
{
1112
public function create(): SpanExporterInterface
1213
{
13-
return new InMemoryExporter();
14+
return new InMemoryExporter(InMemoryStorageManager::spans());
1415
}
1516
}

0 commit comments

Comments
 (0)