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 --- .../Trace/SpanExporter/FriendlySpanConverter.php | 173 +++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 vendor/open-telemetry/sdk/Trace/SpanExporter/FriendlySpanConverter.php (limited to 'vendor/open-telemetry/sdk/Trace/SpanExporter/FriendlySpanConverter.php') diff --git a/vendor/open-telemetry/sdk/Trace/SpanExporter/FriendlySpanConverter.php b/vendor/open-telemetry/sdk/Trace/SpanExporter/FriendlySpanConverter.php new file mode 100644 index 000000000..1f8178e10 --- /dev/null +++ b/vendor/open-telemetry/sdk/Trace/SpanExporter/FriendlySpanConverter.php @@ -0,0 +1,173 @@ +convertSpan($span); + } + + return $aggregate; + } + + /** + * friendlySpan does the heavy lifting converting a span into an array + * + * @param SpanDataInterface $span + * @return array + */ + private function convertSpan(SpanDataInterface $span): array + { + return [ + self::NAME_ATTR => $span->getName(), + self::CONTEXT_ATTR => $this->convertContext($span->getContext()), + self::RESOURCE_ATTR => $this->convertResource($span->getResource()), + self::PARENT_SPAN_ATTR => $this->covertParentContext($span->getParentContext()), + self::KIND_ATTR => $this->convertKind($span->getKind()), + self::START_ATTR => $span->getStartEpochNanos(), + self::END_ATTR => $span->getEndEpochNanos(), + self::ATTRIBUTES_ATTR => $this->convertAttributes($span->getAttributes()), + self::STATUS_ATTR => $this->covertStatus($span->getStatus()), + self::EVENTS_ATTR => $this->convertEvents($span->getEvents()), + self::LINKS_ATTR => $this->convertLinks($span->getLinks()), + ]; + } + + /** + * @param SpanContextInterface $context + * @return array + */ + private function convertContext(SpanContextInterface $context): array + { + return [ + self::TRACE_ID_ATTR => $context->getTraceId(), + self::SPAN_ID_ATTR => $context->getSpanId(), + self::TRACE_STATE_ATTR => (string) $context->getTraceState(), + ]; + } + + /** + * @param ResourceInfo $resource + * @return array + */ + private function convertResource(ResourceInfo $resource): array + { + return $resource->getAttributes()->toArray(); + } + + /** + * @param SpanContextInterface $context + * @return string + */ + private function covertParentContext(SpanContextInterface $context): string + { + return $context->isValid() ? $context->getSpanId() : ''; + } + + /** + * Translates SpanKind from its integer representation to a more human friendly string. + * + * @param int $kind + * @return string + */ + private function convertKind(int $kind): string + { + return array_flip( + (new ReflectionClass(SpanKind::class)) + ->getConstants() + )[$kind]; + } + + /** + * @param \OpenTelemetry\SDK\Common\Attribute\AttributesInterface $attributes + * @return array + */ + private function convertAttributes(AttributesInterface $attributes): array + { + return $attributes->toArray(); + } + + /** + * @param StatusDataInterface $status + * @return array + */ + private function covertStatus(StatusDataInterface $status): array + { + return [ + self::CODE_ATTR => $status->getCode(), + self::DESCRIPTION_ATTR => $status->getDescription(), + ]; + } + + /** + * @param array $events + * @return array + */ + private function convertEvents(array $events): array + { + $result = []; + + foreach ($events as $event) { + $result[] = [ + self::NAME_ATTR => $event->getName(), + self::TIMESTAMP_ATTR => $event->getEpochNanos(), + self::ATTRIBUTES_ATTR => $this->convertAttributes($event->getAttributes()), + ]; + } + + return $result; + } + + /** + * @param array $links + * @return array + */ + private function convertLinks(array $links): array + { + $result = []; + + foreach ($links as $link) { + $result[] = [ + self::CONTEXT_ATTR => $this->convertContext($link->getSpanContext()), + self::ATTRIBUTES_ATTR => $this->convertAttributes($link->getAttributes()), + ]; + } + + return $result; + } +} -- cgit v1.2.3