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 +++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 vendor/open-telemetry/sdk/Logs/Exporter/ConsoleExporter.php (limited to 'vendor/open-telemetry/sdk/Logs/Exporter/ConsoleExporter.php') 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' => [], + ]; + } +} -- cgit v1.2.3