-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathComponentProviderBasedLogRecordProcessorFactory.php
More file actions
98 lines (82 loc) · 3.63 KB
/
ComponentProviderBasedLogRecordProcessorFactory.php
File metadata and controls
98 lines (82 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
declare(strict_types=1);
namespace OpenTelemetry\SDK\Logs;
use InvalidArgumentException;
use OpenTelemetry\API\Configuration\Context;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\Config\SDK\ComponentProvider\Logs\LogRecordProcessorBatch;
use OpenTelemetry\Config\SDK\ComponentProvider\Logs\LogRecordProcessorSimple;
use OpenTelemetry\SDK\Common\Configuration\Configuration;
use OpenTelemetry\SDK\Common\Configuration\KnownValues;
use OpenTelemetry\SDK\Common\Configuration\KnownValues as Values;
use OpenTelemetry\SDK\Common\Configuration\Variables;
use OpenTelemetry\SDK\Logs\Processor\MultiLogRecordProcessor;
use OpenTelemetry\SDK\Logs\Processor\NoopLogRecordProcessor;
/**
* ComponentProvider-based LogRecordProcessor factory that reads configuration from environment variables
* and uses the modern ComponentProvider system for component creation.
*/
class ComponentProviderBasedLogRecordProcessorFactory
{
private Context $context;
public function __construct(?MeterProviderInterface $meterProvider = null)
{
$this->context = new Context(
meterProvider: $meterProvider,
);
}
public function create(LogRecordExporterInterface $exporter): LogRecordProcessorInterface
{
$processors = [];
$list = Configuration::getList(Variables::OTEL_PHP_LOGS_PROCESSOR);
foreach ($list as $name) {
$processors[] = $this->createProcessor($name, $exporter);
}
return match (count($processors)) {
0 => NoopLogRecordProcessor::getInstance(),
1 => $processors[0],
default => new MultiLogRecordProcessor($processors),
};
}
private function createProcessor(string $name, LogRecordExporterInterface $exporter): LogRecordProcessorInterface
{
return match ($name) {
KnownValues::VALUE_BATCH => $this->createBatchProcessor($exporter),
KnownValues::VALUE_SIMPLE => $this->createSimpleProcessor($exporter),
Values::VALUE_NOOP, Values::VALUE_NONE => NoopLogRecordProcessor::getInstance(),
default => throw new InvalidArgumentException('Unknown processor: ' . $name),
};
}
private function createBatchProcessor(LogRecordExporterInterface $exporter): LogRecordProcessorInterface
{
$provider = new LogRecordProcessorBatch();
// Create configuration array from environment variables
$config = [
'schedule_delay' => Configuration::getInt(Variables::OTEL_BLRP_SCHEDULE_DELAY, 5000),
'export_timeout' => Configuration::getInt(Variables::OTEL_BLRP_EXPORT_TIMEOUT, 30000),
'max_queue_size' => Configuration::getInt(Variables::OTEL_BLRP_MAX_QUEUE_SIZE, 2048),
'max_export_batch_size' => Configuration::getInt(Variables::OTEL_BLRP_MAX_EXPORT_BATCH_SIZE, 512),
'exporter' => new LogRecordExporterComponentPlugin($exporter),
];
return $provider->createPlugin($config, $this->context);
}
private function createSimpleProcessor(LogRecordExporterInterface $exporter): LogRecordProcessorInterface
{
$provider = new LogRecordProcessorSimple();
$config = [
'exporter' => new LogRecordExporterComponentPlugin($exporter),
];
return $provider->createPlugin($config, $this->context);
}
}
/**
* Simple ComponentPlugin wrapper for existing LogRecordExporter instances
*/
class LogRecordExporterComponentPlugin
{
public function __construct(private readonly LogRecordExporterInterface $exporter) {}
public function create(Context $context): LogRecordExporterInterface
{
return $this->exporter;
}
}