From cdd7ad020e165fe680703b6d3319b908b682fb7a Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 20 Oct 2023 17:12:29 +0300 Subject: jaeger-client -> opentelemetry --- .../sdk/Logs/Exporter/ConsoleExporter.php | 106 ++++++++ .../sdk/Logs/Exporter/ConsoleExporterFactory.php | 19 ++ .../sdk/Logs/Exporter/InMemoryExporter.php | 48 ++++ .../sdk/Logs/Exporter/InMemoryExporterFactory.php | 16 ++ .../sdk/Logs/Exporter/NoopExporter.php | 28 +++ .../open-telemetry/sdk/Logs/Exporter/_register.php | 6 + vendor/open-telemetry/sdk/Logs/ExporterFactory.php | 29 +++ .../sdk/Logs/LogRecordExporterFactoryInterface.php | 10 + .../sdk/Logs/LogRecordExporterInterface.php | 18 ++ vendor/open-telemetry/sdk/Logs/LogRecordLimits.php | 29 +++ .../sdk/Logs/LogRecordLimitsBuilder.php | 58 +++++ .../sdk/Logs/LogRecordProcessorFactory.php | 62 +++++ .../sdk/Logs/LogRecordProcessorInterface.php | 15 ++ vendor/open-telemetry/sdk/Logs/Logger.php | 37 +++ vendor/open-telemetry/sdk/Logs/LoggerProvider.php | 56 +++++ .../sdk/Logs/LoggerProviderBuilder.php | 55 +++++ .../sdk/Logs/LoggerProviderFactory.php | 24 ++ .../sdk/Logs/LoggerProviderInterface.php | 13 + .../open-telemetry/sdk/Logs/LoggerSharedState.php | 60 +++++ .../open-telemetry/sdk/Logs/NoopLoggerProvider.php | 33 +++ .../sdk/Logs/Processor/BatchLogRecordProcessor.php | 273 +++++++++++++++++++++ .../sdk/Logs/Processor/MultiLogRecordProcessor.php | 62 +++++ .../sdk/Logs/Processor/NoopLogRecordProcessor.php | 37 +++ .../Logs/Processor/SimpleLogRecordProcessor.php | 38 +++ .../sdk/Logs/PsrSeverityMapperInterface.php | 50 ++++ .../open-telemetry/sdk/Logs/ReadWriteLogRecord.php | 9 + .../open-telemetry/sdk/Logs/ReadableLogRecord.php | 103 ++++++++ .../sdk/Logs/SimplePsrFileLogger.php | 83 +++++++ 28 files changed, 1377 insertions(+) create mode 100644 vendor/open-telemetry/sdk/Logs/Exporter/ConsoleExporter.php create mode 100644 vendor/open-telemetry/sdk/Logs/Exporter/ConsoleExporterFactory.php create mode 100644 vendor/open-telemetry/sdk/Logs/Exporter/InMemoryExporter.php create mode 100644 vendor/open-telemetry/sdk/Logs/Exporter/InMemoryExporterFactory.php create mode 100644 vendor/open-telemetry/sdk/Logs/Exporter/NoopExporter.php create mode 100644 vendor/open-telemetry/sdk/Logs/Exporter/_register.php create mode 100644 vendor/open-telemetry/sdk/Logs/ExporterFactory.php create mode 100644 vendor/open-telemetry/sdk/Logs/LogRecordExporterFactoryInterface.php create mode 100644 vendor/open-telemetry/sdk/Logs/LogRecordExporterInterface.php create mode 100644 vendor/open-telemetry/sdk/Logs/LogRecordLimits.php create mode 100644 vendor/open-telemetry/sdk/Logs/LogRecordLimitsBuilder.php create mode 100644 vendor/open-telemetry/sdk/Logs/LogRecordProcessorFactory.php create mode 100644 vendor/open-telemetry/sdk/Logs/LogRecordProcessorInterface.php create mode 100644 vendor/open-telemetry/sdk/Logs/Logger.php create mode 100644 vendor/open-telemetry/sdk/Logs/LoggerProvider.php create mode 100644 vendor/open-telemetry/sdk/Logs/LoggerProviderBuilder.php create mode 100644 vendor/open-telemetry/sdk/Logs/LoggerProviderFactory.php create mode 100644 vendor/open-telemetry/sdk/Logs/LoggerProviderInterface.php create mode 100644 vendor/open-telemetry/sdk/Logs/LoggerSharedState.php create mode 100644 vendor/open-telemetry/sdk/Logs/NoopLoggerProvider.php create mode 100644 vendor/open-telemetry/sdk/Logs/Processor/BatchLogRecordProcessor.php create mode 100644 vendor/open-telemetry/sdk/Logs/Processor/MultiLogRecordProcessor.php create mode 100644 vendor/open-telemetry/sdk/Logs/Processor/NoopLogRecordProcessor.php create mode 100644 vendor/open-telemetry/sdk/Logs/Processor/SimpleLogRecordProcessor.php create mode 100644 vendor/open-telemetry/sdk/Logs/PsrSeverityMapperInterface.php create mode 100644 vendor/open-telemetry/sdk/Logs/ReadWriteLogRecord.php create mode 100644 vendor/open-telemetry/sdk/Logs/ReadableLogRecord.php create mode 100644 vendor/open-telemetry/sdk/Logs/SimplePsrFileLogger.php (limited to 'vendor/open-telemetry/sdk/Logs') diff --git a/vendor/open-telemetry/sdk/Logs/Exporter/ConsoleExporter.php b/vendor/open-telemetry/sdk/Logs/Exporter/ConsoleExporter.php new file mode 100644 index 000000000..e34fa308c --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/Exporter/ConsoleExporter.php @@ -0,0 +1,106 @@ +transport = $transport; + } + + /** + * @param iterable $batch + */ + public function export(iterable $batch, ?CancellationInterface $cancellation = null): FutureInterface + { + $resource = null; + $scopes = []; + foreach ($batch as $record) { + if (!$resource) { + $resource = $this->convertResource($record->getResource()); + } + $key = $this->scopeKey($record->getInstrumentationScope()); + if (!array_key_exists($key, $scopes)) { + $scopes[$key] = $this->convertInstrumentationScope($record->getInstrumentationScope()); + } + $scopes[$key]['logs'][] = $this->convertLogRecord($record); + } + $output = [ + 'resource' => $resource, + 'scopes' => array_values($scopes), + ]; + $this->transport->send(json_encode($output, JSON_PRETTY_PRINT)); + + return new CompletedFuture(true); + } + + public function forceFlush(?CancellationInterface $cancellation = null): bool + { + return true; + } + + public function shutdown(?CancellationInterface $cancellation = null): bool + { + return true; + } + private function convertLogRecord(ReadableLogRecord $record): array + { + $spanContext = $record->getSpanContext(); + + return [ + 'timestamp' => $record->getTimestamp(), + 'observed_timestamp' => $record->getObservedTimestamp(), + 'severity_number' => $record->getSeverityNumber(), + 'severity_text' => $record->getSeverityText(), + 'body' => $record->getBody(), + 'trace_id' => $spanContext !== null ? $spanContext->getTraceId() : '', + 'span_id' => $spanContext !== null ? $spanContext->getSpanId() : '', + 'trace_flags' => $spanContext !== null ? $spanContext->getTraceFlags() : null, + 'attributes' => $record->getAttributes()->toArray(), + 'dropped_attributes_count' => $record->getAttributes()->getDroppedAttributesCount(), + ]; + } + + private function convertResource(ResourceInfo $resource): array + { + return [ + 'attributes' => $resource->getAttributes()->toArray(), + 'dropped_attributes_count' => $resource->getAttributes()->getDroppedAttributesCount(), + ]; + } + + private function scopeKey(InstrumentationScopeInterface $scope): string + { + return serialize([$scope->getName(), $scope->getVersion(), $scope->getSchemaUrl(), $scope->getAttributes()]); + } + + private function convertInstrumentationScope(InstrumentationScopeInterface $scope): array + { + return [ + 'name' => $scope->getName(), + 'version' => $scope->getVersion(), + 'attributes' => $scope->getAttributes()->toArray(), + 'dropped_attributes_count' => $scope->getAttributes()->getDroppedAttributesCount(), + 'schema_url' => $scope->getSchemaUrl(), + 'logs' => [], + ]; + } +} diff --git a/vendor/open-telemetry/sdk/Logs/Exporter/ConsoleExporterFactory.php b/vendor/open-telemetry/sdk/Logs/Exporter/ConsoleExporterFactory.php new file mode 100644 index 000000000..a959540a0 --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/Exporter/ConsoleExporterFactory.php @@ -0,0 +1,19 @@ +create('php://stdout', 'application/json'); + + return new ConsoleExporter($transport); + } +} diff --git a/vendor/open-telemetry/sdk/Logs/Exporter/InMemoryExporter.php b/vendor/open-telemetry/sdk/Logs/Exporter/InMemoryExporter.php new file mode 100644 index 000000000..dca0531f3 --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/Exporter/InMemoryExporter.php @@ -0,0 +1,48 @@ +storage = $storage ?? new ArrayObject(); + } + + /** + * @inheritDoc + */ + public function export(iterable $batch, ?CancellationInterface $cancellation = null): FutureInterface + { + foreach ($batch as $record) { + $this->storage[] = $record; + } + + return new CompletedFuture(true); + } + + public function forceFlush(?CancellationInterface $cancellation = null): bool + { + return true; + } + + public function shutdown(?CancellationInterface $cancellation = null): bool + { + return true; + } + + public function getStorage(): ArrayObject + { + return $this->storage; + } +} diff --git a/vendor/open-telemetry/sdk/Logs/Exporter/InMemoryExporterFactory.php b/vendor/open-telemetry/sdk/Logs/Exporter/InMemoryExporterFactory.php new file mode 100644 index 000000000..6f24defe0 --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/Exporter/InMemoryExporterFactory.php @@ -0,0 +1,16 @@ +create(); + } +} diff --git a/vendor/open-telemetry/sdk/Logs/LogRecordExporterFactoryInterface.php b/vendor/open-telemetry/sdk/Logs/LogRecordExporterFactoryInterface.php new file mode 100644 index 000000000..523bec1ba --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/LogRecordExporterFactoryInterface.php @@ -0,0 +1,10 @@ + $batch + */ + public function export(iterable $batch, ?CancellationInterface $cancellation = null): FutureInterface; + public function forceFlush(?CancellationInterface $cancellation = null): bool; + public function shutdown(?CancellationInterface $cancellation = null): bool; +} diff --git a/vendor/open-telemetry/sdk/Logs/LogRecordLimits.php b/vendor/open-telemetry/sdk/Logs/LogRecordLimits.php new file mode 100644 index 000000000..9f71e62ee --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/LogRecordLimits.php @@ -0,0 +1,29 @@ +attributesFactory = $attributesFactory; + } + + public function getAttributeFactory(): AttributesFactoryInterface + { + return $this->attributesFactory; + } +} diff --git a/vendor/open-telemetry/sdk/Logs/LogRecordLimitsBuilder.php b/vendor/open-telemetry/sdk/Logs/LogRecordLimitsBuilder.php new file mode 100644 index 000000000..3aa5217ef --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/LogRecordLimitsBuilder.php @@ -0,0 +1,58 @@ +attributeCountLimit = $attributeCountLimit; + + return $this; + } + + /** + * @param int $attributeValueLengthLimit Maximum allowed attribute value length + */ + public function setAttributeValueLengthLimit(int $attributeValueLengthLimit): LogRecordLimitsBuilder + { + $this->attributeValueLengthLimit = $attributeValueLengthLimit; + + return $this; + } + + /** + * @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#attribute-limits + */ + public function build(): LogRecordLimits + { + $attributeCountLimit = $this->attributeCountLimit + ?: Configuration::getInt(Variables::OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT); + $attributeValueLengthLimit = $this->attributeValueLengthLimit + ?: Configuration::getInt(Variables::OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT); + + if ($attributeValueLengthLimit === PHP_INT_MAX) { + $attributeValueLengthLimit = null; + } + + $attributesFactory = Attributes::factory($attributeCountLimit, $attributeValueLengthLimit); + + return new LogRecordLimits($attributesFactory); + } +} diff --git a/vendor/open-telemetry/sdk/Logs/LogRecordProcessorFactory.php b/vendor/open-telemetry/sdk/Logs/LogRecordProcessorFactory.php new file mode 100644 index 000000000..dec463735 --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/LogRecordProcessorFactory.php @@ -0,0 +1,62 @@ +createProcessor($name, $exporter, $meterProvider); + } + + switch (count($processors)) { + case 0: + return NoopLogRecordProcessor::getInstance(); + case 1: + return $processors[0]; + default: + return new MultiLogRecordProcessor($processors); + } + } + + private function createProcessor(string $name, LogRecordExporterInterface $exporter, ?MeterProviderInterface $meterProvider = null): LogRecordProcessorInterface + { + switch ($name) { + case KnownValues::VALUE_BATCH: + return new BatchLogRecordProcessor( + $exporter, + ClockFactory::getDefault(), + Configuration::getInt(Variables::OTEL_BLRP_MAX_QUEUE_SIZE), + Configuration::getInt(Variables::OTEL_BLRP_SCHEDULE_DELAY), + Configuration::getInt(Variables::OTEL_BLRP_EXPORT_TIMEOUT), + Configuration::getInt(Variables::OTEL_BLRP_MAX_EXPORT_BATCH_SIZE), + true, + $meterProvider, + ); + case KnownValues::VALUE_SIMPLE: + return new SimpleLogRecordProcessor($exporter); + case Values::VALUE_NOOP: + case Values::VALUE_NONE: + return NoopLogRecordProcessor::getInstance(); + default: + throw new InvalidArgumentException('Unknown processor: ' . $name); + } + } +} diff --git a/vendor/open-telemetry/sdk/Logs/LogRecordProcessorInterface.php b/vendor/open-telemetry/sdk/Logs/LogRecordProcessorInterface.php new file mode 100644 index 000000000..1977d48fd --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/LogRecordProcessorInterface.php @@ -0,0 +1,15 @@ +loggerSharedState = $loggerSharedState; + $this->scope = $scope; + } + + public function emit(LogRecord $logRecord): void + { + $readWriteLogRecord = new ReadWriteLogRecord($this->scope, $this->loggerSharedState, $logRecord); + // @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/sdk.md#onemit + $this->loggerSharedState->getProcessor()->onEmit( + $readWriteLogRecord, + $readWriteLogRecord->getContext(), + ); + } +} diff --git a/vendor/open-telemetry/sdk/Logs/LoggerProvider.php b/vendor/open-telemetry/sdk/Logs/LoggerProvider.php new file mode 100644 index 000000000..f0a8266c1 --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/LoggerProvider.php @@ -0,0 +1,56 @@ +loggerSharedState = new LoggerSharedState( + $resource ?? ResourceInfoFactory::defaultResource(), + (new LogRecordLimitsBuilder())->build(), + $processor + ); + $this->instrumentationScopeFactory = $instrumentationScopeFactory; + } + + /** + * @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/sdk.md#logger-creation + */ + public function getLogger(string $name, ?string $version = null, ?string $schemaUrl = null, iterable $attributes = []): LoggerInterface + { + if ($this->loggerSharedState->hasShutdown()) { + return NoopLogger::getInstance(); + } + $scope = $this->instrumentationScopeFactory->create($name, $version, $schemaUrl, $attributes); + + return new Logger($this->loggerSharedState, $scope); + } + + public function shutdown(CancellationInterface $cancellation = null): bool + { + return $this->loggerSharedState->shutdown($cancellation); + } + + public function forceFlush(CancellationInterface $cancellation = null): bool + { + return $this->loggerSharedState->forceFlush($cancellation); + } + + public static function builder(): LoggerProviderBuilder + { + return new LoggerProviderBuilder(); + } +} diff --git a/vendor/open-telemetry/sdk/Logs/LoggerProviderBuilder.php b/vendor/open-telemetry/sdk/Logs/LoggerProviderBuilder.php new file mode 100644 index 000000000..37c56245c --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/LoggerProviderBuilder.php @@ -0,0 +1,55 @@ + + */ + private array $processors = []; + private ?ResourceInfo $resource = null; + + public function addLogRecordProcessor(LogRecordProcessorInterface $processor): self + { + $this->processors[] = $processor; + + return $this; + } + + public function setResource(ResourceInfo $resource): self + { + $this->resource = $resource; + + return $this; + } + + public function build(): LoggerProviderInterface + { + return new LoggerProvider( + $this->buildProcessor(), + new InstrumentationScopeFactory(Attributes::factory()), + $this->resource + ); + } + + private function buildProcessor(): LogRecordProcessorInterface + { + switch (count($this->processors)) { + case 0: + return NoopLogRecordProcessor::getInstance(); + case 1: + return $this->processors[0]; + default: + return new MultiLogRecordProcessor($this->processors); + } + } +} diff --git a/vendor/open-telemetry/sdk/Logs/LoggerProviderFactory.php b/vendor/open-telemetry/sdk/Logs/LoggerProviderFactory.php new file mode 100644 index 000000000..3d0e965fd --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/LoggerProviderFactory.php @@ -0,0 +1,24 @@ +create(); + $processor = (new LogRecordProcessorFactory())->create($exporter, $meterProvider); + $instrumentationScopeFactory = new InstrumentationScopeFactory((new LogRecordLimitsBuilder())->build()->getAttributeFactory()); + + return new LoggerProvider($processor, $instrumentationScopeFactory); + } +} diff --git a/vendor/open-telemetry/sdk/Logs/LoggerProviderInterface.php b/vendor/open-telemetry/sdk/Logs/LoggerProviderInterface.php new file mode 100644 index 000000000..5debb13cc --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/LoggerProviderInterface.php @@ -0,0 +1,13 @@ +resource = $resource; + $this->limits = $limits; + $this->processor = $processor; + } + public function hasShutdown(): bool + { + return null !== $this->shutdownResult; + } + + public function getResource(): ResourceInfo + { + return $this->resource; + } + + public function getProcessor(): LogRecordProcessorInterface + { + return $this->processor; + } + + public function getLogRecordLimits(): LogRecordLimits + { + return $this->limits; + } + + public function shutdown(?CancellationInterface $cancellation = null): bool + { + if ($this->shutdownResult !== null) { + return $this->shutdownResult; + } + $this->shutdownResult = $this->processor->shutdown($cancellation); + + return $this->shutdownResult; + } + + public function forceFlush(?CancellationInterface $cancellation = null): bool + { + return $this->processor->forceFlush($cancellation); + } +} diff --git a/vendor/open-telemetry/sdk/Logs/NoopLoggerProvider.php b/vendor/open-telemetry/sdk/Logs/NoopLoggerProvider.php new file mode 100644 index 000000000..819e02ee5 --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/NoopLoggerProvider.php @@ -0,0 +1,33 @@ + 'batching']; + private const ATTRIBUTES_QUEUED = self::ATTRIBUTES_PROCESSOR + ['state' => 'queued']; + private const ATTRIBUTES_PENDING = self::ATTRIBUTES_PROCESSOR + ['state' => 'pending']; + private const ATTRIBUTES_PROCESSED = self::ATTRIBUTES_PROCESSOR + ['state' => 'processed']; + private const ATTRIBUTES_DROPPED = self::ATTRIBUTES_PROCESSOR + ['state' => 'dropped']; + private const ATTRIBUTES_FREE = self::ATTRIBUTES_PROCESSOR + ['state' => 'free']; + + private LogRecordExporterInterface $exporter; + private ClockInterface $clock; + private int $maxQueueSize; + private int $scheduledDelayNanos; + private int $maxExportBatchSize; + private bool $autoFlush; + private ContextInterface $exportContext; + + private ?int $nextScheduledRun = null; + private bool $running = false; + private int $dropped = 0; + private int $processed = 0; + private int $batchId = 0; + private int $queueSize = 0; + /** @var list */ + private array $batch = []; + /** @var SplQueue> */ + private SplQueue $queue; + /** @var SplQueue */ + private SplQueue $flush; + + private bool $closed = false; + + public function __construct( + LogRecordExporterInterface $exporter, + ClockInterface $clock, + int $maxQueueSize = self::DEFAULT_MAX_QUEUE_SIZE, + int $scheduledDelayMillis = self::DEFAULT_SCHEDULE_DELAY, + int $exportTimeoutMillis = self::DEFAULT_EXPORT_TIMEOUT, + int $maxExportBatchSize = self::DEFAULT_MAX_EXPORT_BATCH_SIZE, + bool $autoFlush = true, + ?MeterProviderInterface $meterProvider = null + ) { + if ($maxQueueSize <= 0) { + throw new InvalidArgumentException(sprintf('Maximum queue size (%d) must be greater than zero', $maxQueueSize)); + } + if ($scheduledDelayMillis <= 0) { + throw new InvalidArgumentException(sprintf('Scheduled delay (%d) must be greater than zero', $scheduledDelayMillis)); + } + if ($exportTimeoutMillis <= 0) { + throw new InvalidArgumentException(sprintf('Export timeout (%d) must be greater than zero', $exportTimeoutMillis)); + } + if ($maxExportBatchSize <= 0) { + throw new InvalidArgumentException(sprintf('Maximum export batch size (%d) must be greater than zero', $maxExportBatchSize)); + } + if ($maxExportBatchSize > $maxQueueSize) { + throw new InvalidArgumentException(sprintf('Maximum export batch size (%d) must be less than or equal to maximum queue size (%d)', $maxExportBatchSize, $maxQueueSize)); + } + + $this->exporter = $exporter; + $this->clock = $clock; + $this->maxQueueSize = $maxQueueSize; + $this->scheduledDelayNanos = $scheduledDelayMillis * 1_000_000; + $this->maxExportBatchSize = $maxExportBatchSize; + $this->autoFlush = $autoFlush; + + $this->exportContext = Context::getCurrent(); + $this->queue = new SplQueue(); + $this->flush = new SplQueue(); + + if ($meterProvider === null) { + return; + } + + $meter = $meterProvider->getMeter('io.opentelemetry.sdk'); + $meter + ->createObservableUpDownCounter( + 'otel.logs.log_processor.logs', + '{logs}', + 'The number of log records received by the processor', + ) + ->observe(function (ObserverInterface $observer): void { + $queued = $this->queue->count() * $this->maxExportBatchSize + count($this->batch); + $pending = $this->queueSize - $queued; + $processed = $this->processed; + $dropped = $this->dropped; + + $observer->observe($queued, self::ATTRIBUTES_QUEUED); + $observer->observe($pending, self::ATTRIBUTES_PENDING); + $observer->observe($processed, self::ATTRIBUTES_PROCESSED); + $observer->observe($dropped, self::ATTRIBUTES_DROPPED); + }); + $meter + ->createObservableUpDownCounter( + 'otel.logs.log_processor.queue.limit', + '{logs}', + 'The queue size limit', + ) + ->observe(function (ObserverInterface $observer): void { + $observer->observe($this->maxQueueSize, self::ATTRIBUTES_PROCESSOR); + }); + $meter + ->createObservableUpDownCounter( + 'otel.logs.log_processor.queue.usage', + '{logs}', + 'The current queue usage', + ) + ->observe(function (ObserverInterface $observer): void { + $queued = $this->queue->count() * $this->maxExportBatchSize + count($this->batch); + $pending = $this->queueSize - $queued; + $free = $this->maxQueueSize - $this->queueSize; + + $observer->observe($queued, self::ATTRIBUTES_QUEUED); + $observer->observe($pending, self::ATTRIBUTES_PENDING); + $observer->observe($free, self::ATTRIBUTES_FREE); + }); + } + + public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void + { + if ($this->closed) { + return; + } + + if ($this->queueSize === $this->maxQueueSize) { + $this->dropped++; + + return; + } + + $this->queueSize++; + $this->batch[] = $record; + $this->nextScheduledRun ??= $this->clock->now() + $this->scheduledDelayNanos; + + if (count($this->batch) === $this->maxExportBatchSize) { + $this->enqueueBatch(); + } + if ($this->autoFlush) { + $this->flush(); + } + } + + public function forceFlush(?CancellationInterface $cancellation = null): bool + { + if ($this->closed) { + return false; + } + + return $this->flush(__FUNCTION__, $cancellation); + } + + public function shutdown(?CancellationInterface $cancellation = null): bool + { + if ($this->closed) { + return false; + } + + $this->closed = true; + + return $this->flush(__FUNCTION__, $cancellation); + } + + private function flush(?string $flushMethod = null, ?CancellationInterface $cancellation = null): bool + { + if ($flushMethod !== null) { + $flushId = $this->batchId + $this->queue->count() + (int) (bool) $this->batch; + $this->flush->enqueue([$flushId, $flushMethod, $cancellation, !$this->running, Context::getCurrent()]); + } + + if ($this->running) { + return false; + } + + $success = true; + $exception = null; + $this->running = true; + + try { + for (;;) { + while (!$this->flush->isEmpty() && $this->flush->bottom()[0] <= $this->batchId) { + [, $flushMethod, $cancellation, $propagateResult, $context] = $this->flush->dequeue(); + $scope = $context->activate(); + + try { + $result = $this->exporter->$flushMethod($cancellation); + if ($propagateResult) { + $success = $result; + } + } catch (Throwable $e) { + if ($propagateResult) { + $exception = $e; + } else { + self::logError(sprintf('Unhandled %s error', $flushMethod), ['exception' => $e]); + } + } finally { + $scope->detach(); + } + } + + if (!$this->shouldFlush()) { + break; + } + + if ($this->queue->isEmpty()) { + $this->enqueueBatch(); + } + $batchSize = count($this->queue->bottom()); + $this->batchId++; + $scope = $this->exportContext->activate(); + + try { + $this->exporter->export($this->queue->dequeue())->await(); + } catch (Throwable $e) { + self::logError('Unhandled export error', ['exception' => $e]); + } finally { + $this->processed += $batchSize; + $this->queueSize -= $batchSize; + $scope->detach(); + } + } + } finally { + $this->running = false; + } + + if ($exception !== null) { + throw $exception; + } + + return $success; + } + + private function shouldFlush(): bool + { + return !$this->flush->isEmpty() + || $this->autoFlush && !$this->queue->isEmpty() + || $this->autoFlush && $this->nextScheduledRun !== null && $this->clock->now() > $this->nextScheduledRun; + } + + private function enqueueBatch(): void + { + assert($this->batch !== []); + + $this->queue->enqueue($this->batch); + $this->batch = []; + $this->nextScheduledRun = null; + } +} diff --git a/vendor/open-telemetry/sdk/Logs/Processor/MultiLogRecordProcessor.php b/vendor/open-telemetry/sdk/Logs/Processor/MultiLogRecordProcessor.php new file mode 100644 index 000000000..753a75df8 --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/Processor/MultiLogRecordProcessor.php @@ -0,0 +1,62 @@ +processors[] = $processor; + } + } + + public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void + { + foreach ($this->processors as $processor) { + $processor->onEmit($record, $context); + } + } + + /** + * Returns `true` if all processors shut down successfully, else `false` + * Subsequent calls to `shutdown` are a no-op. + */ + public function shutdown(?CancellationInterface $cancellation = null): bool + { + $result = true; + foreach ($this->processors as $processor) { + if (!$processor->shutdown($cancellation)) { + $result = false; + } + } + + return $result; + } + + /** + * Returns `true` if all processors flush successfully, else `false`. + */ + public function forceFlush(?CancellationInterface $cancellation = null): bool + { + $result = true; + foreach ($this->processors as $processor) { + if (!$processor->forceFlush($cancellation)) { + $result = false; + } + } + + return $result; + } +} diff --git a/vendor/open-telemetry/sdk/Logs/Processor/NoopLogRecordProcessor.php b/vendor/open-telemetry/sdk/Logs/Processor/NoopLogRecordProcessor.php new file mode 100644 index 000000000..7028052e1 --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/Processor/NoopLogRecordProcessor.php @@ -0,0 +1,37 @@ +exporter = $exporter; + } + + /** + * @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/sdk.md#onemit + */ + public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void + { + $this->exporter->export([$record]); + } + + public function shutdown(?CancellationInterface $cancellation = null): bool + { + return $this->exporter->shutdown($cancellation); + } + + public function forceFlush(?CancellationInterface $cancellation = null): bool + { + return $this->exporter->forceFlush($cancellation); + } +} diff --git a/vendor/open-telemetry/sdk/Logs/PsrSeverityMapperInterface.php b/vendor/open-telemetry/sdk/Logs/PsrSeverityMapperInterface.php new file mode 100644 index 000000000..3bb288c56 --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/PsrSeverityMapperInterface.php @@ -0,0 +1,50 @@ + 7, + // Interesting events. Examples: User logs in, SQL logs. + PsrLogLevel::INFO => 6, + // Normal but significant events. + PsrLogLevel::NOTICE => 5, + // Exceptional occurrences that are not errors. Examples: Use of deprecated APIs, poor use of an API, + // undesirable things that are not necessarily wrong. + PsrLogLevel::WARNING => 4, + // Runtime errors that do not require immediate action but should typically be logged and monitored. + PsrLogLevel::ERROR => 3, + // Critical conditions. Example: Application component unavailable, unexpected exception. + PsrLogLevel::CRITICAL => 2, + // Action must be taken immediately. Example: Entire website down, database unavailable, etc. + // This should trigger the alerts and wake you up. + PsrLogLevel::ALERT => 1, + // Emergency: system is unusable. + PsrLogLevel::EMERGENCY => 0, + ]; + + /** + * Mappig of OpenTelemetry SeverityNumber to PsrLogLevel. + * @see: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/specification/logs/data-model.md#field-severitynumber + */ + public const SEVERITY_NUMBER = [ + PsrLogLevel::DEBUG => 5, + PsrLogLevel::INFO => 9, + PsrLogLevel::NOTICE => 10, + PsrLogLevel::WARNING => 13, + PsrLogLevel::ERROR => 17, + PsrLogLevel::CRITICAL => 18, + PsrLogLevel::ALERT => 21, + PsrLogLevel::EMERGENCY => 22, + ]; +} diff --git a/vendor/open-telemetry/sdk/Logs/ReadWriteLogRecord.php b/vendor/open-telemetry/sdk/Logs/ReadWriteLogRecord.php new file mode 100644 index 000000000..9bb4b1564 --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/ReadWriteLogRecord.php @@ -0,0 +1,9 @@ +scope = $scope; + $this->loggerSharedState = $loggerSharedState; + + parent::__construct($logRecord->body); + $this->timestamp = $logRecord->timestamp; + $this->observedTimestamp = $logRecord->observedTimestamp + ?? (int) (microtime(true) * LogRecord::NANOS_PER_SECOND); + $this->context = $logRecord->context; + $context = $this->context ?? Context::getCurrent(); + $this->spanContext = Span::fromContext($context)->getContext(); + $this->severityNumber = $logRecord->severityNumber; + $this->severityText = $logRecord->severityText; + + //convert attributes now so that excess data is not sent to processors + $this->convertedAttributes = $this->loggerSharedState + ->getLogRecordLimits() + ->getAttributeFactory() + ->builder($logRecord->attributes, new LogRecordAttributeValidator()) + ->build(); + } + + public function getInstrumentationScope(): InstrumentationScopeInterface + { + return $this->scope; + } + + public function getResource(): ResourceInfo + { + return $this->loggerSharedState->getResource(); + } + + public function getTimestamp(): ?int + { + return $this->timestamp; + } + + public function getObservedTimestamp(): ?int + { + return $this->observedTimestamp; + } + + public function getContext(): ?ContextInterface + { + return $this->context; + } + + public function getSpanContext(): ?SpanContextInterface + { + return $this->spanContext; + } + + public function getSeverityNumber(): ?int + { + return $this->severityNumber; + } + + public function getSeverityText(): ?string + { + return $this->severityText; + } + + /** + * @return mixed|null + */ + public function getBody() + { + return $this->body; + } + + public function getAttributes(): AttributesInterface + { + return $this->convertedAttributes; + } +} diff --git a/vendor/open-telemetry/sdk/Logs/SimplePsrFileLogger.php b/vendor/open-telemetry/sdk/Logs/SimplePsrFileLogger.php new file mode 100644 index 000000000..9d9d55de6 --- /dev/null +++ b/vendor/open-telemetry/sdk/Logs/SimplePsrFileLogger.php @@ -0,0 +1,83 @@ +filename = $filename; + $this->loggerName = $loggerName; + } + + /** + * @psalm-suppress MoreSpecificImplementedParamType + */ + public function log($level, $message, array $context = []): void + { + $level = strtolower($level); + + if (!in_array($level, self::getLogLevels(), true)) { + throw new InvalidArgumentException( + sprintf('Invalid Log level: "%s"', $level) + ); + } + + file_put_contents($this->filename, $this->formatLog((string) $level, (string) $message, $context), FILE_APPEND); + } + + /** + * @param string $level + * @param string $message + * @param array $context + * @return string + */ + private function formatLog(string $level, string $message, array $context = []): string + { + try { + $encodedContext = json_encode($context, JSON_THROW_ON_ERROR); + } catch (Throwable $t) { + $encodedContext = sprintf('(Could not encode context: %s)', $t->getMessage()); + } + + return sprintf( + '[%s] %s %s: %s %s%s', + date(DATE_RFC3339_EXTENDED), + $this->loggerName, + $level, + $message, + $encodedContext, + PHP_EOL + ); + } + + /** + * @return array + */ + private static function getLogLevels(): array + { + return self::$logLevels ?? self::$logLevels = (new ReflectionClass(LogLevel::class))->getConstants(); + } +} -- cgit v1.2.3