|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2018 OpenCensus Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace OpenCensus\Trace\Exporter\Stackdriver; |
| 19 | + |
| 20 | +use Google\Cloud\Trace\Annotation; |
| 21 | +use Google\Cloud\Trace\Link; |
| 22 | +use Google\Cloud\Trace\MessageEvent; |
| 23 | +use Google\Cloud\Trace\Span; |
| 24 | +use Google\Cloud\Trace\Status; |
| 25 | +use OpenCensus\Trace\Annotation as OCAnnotation; |
| 26 | +use OpenCensus\Trace\Link as OCLink; |
| 27 | +use OpenCensus\Trace\MessageEvent as OCMessageEvent; |
| 28 | +use OpenCensus\Trace\Span as OCSpan; |
| 29 | +use OpenCensus\Trace\Status as OCStatus; |
| 30 | +use OpenCensus\Trace\SpanData; |
| 31 | + |
| 32 | +/** |
| 33 | + * This class handles converting from the OpenCensus data model into its |
| 34 | + * Stackdriver Trace representation. |
| 35 | + */ |
| 36 | +class SpanConverter |
| 37 | +{ |
| 38 | + const ATTRIBUTE_MAP = [ |
| 39 | + OCSpan::ATTRIBUTE_HOST => '/http/host', |
| 40 | + OCSpan::ATTRIBUTE_PORT => '/http/port', |
| 41 | + OCSpan::ATTRIBUTE_METHOD => '/http/method', |
| 42 | + OCSpan::ATTRIBUTE_PATH => '/http/url', |
| 43 | + OCSpan::ATTRIBUTE_USER_AGENT => '/http/user_agent', |
| 44 | + OCSpan::ATTRIBUTE_STATUS_CODE => '/http/status_code' |
| 45 | + ]; |
| 46 | + const LINK_TYPE_MAP = [ |
| 47 | + OCLink::TYPE_UNSPECIFIED => Link::TYPE_UNSPECIFIED, |
| 48 | + OCLink::TYPE_CHILD_LINKED_SPAN => Link::TYPE_CHILD_LINKED_SPAN, |
| 49 | + OCLink::TYPE_PARENT_LINKED_SPAN => Link::TYPE_PARENT_LINKED_SPAN |
| 50 | + ]; |
| 51 | + const MESSAGE_TYPE_MAP = [ |
| 52 | + OCMessageEvent::TYPE_UNSPECIFIED => MessageEvent::TYPE_UNSPECIFIED, |
| 53 | + OCMessageEvent::TYPE_SENT => MessageEvent::TYPE_SENT, |
| 54 | + OCMessageEvent::TYPE_RECEIVED => MessageEvent::TYPE_RECEIVED |
| 55 | + ]; |
| 56 | + |
| 57 | + /** |
| 58 | + * Convert an OpenCensus SpanData to its Stackdriver Trace representation. |
| 59 | + * |
| 60 | + * @access private |
| 61 | + * |
| 62 | + * @param SpanData $span The span to convert. |
| 63 | + * @return Span |
| 64 | + */ |
| 65 | + public static function convertSpan(SpanData $span) |
| 66 | + { |
| 67 | + $spanOptions = [ |
| 68 | + 'name' => $span->name(), |
| 69 | + 'startTime' => $span->startTime(), |
| 70 | + 'endTime' => $span->endTime(), |
| 71 | + 'spanId' => $span->spanId(), |
| 72 | + 'attributes' => self::convertAttributes($span->attributes()), |
| 73 | + 'stackTrace' => $span->stackTrace(), |
| 74 | + 'links' => self::convertLinks($span->links()), |
| 75 | + 'timeEvents' => self::convertTimeEvents($span->timeEvents()), |
| 76 | + 'status' => self::convertStatus($span->status()) |
| 77 | + ]; |
| 78 | + if ($span->parentSpanId()) { |
| 79 | + $spanOptions['parentSpanId'] = $span->parentSpanId(); |
| 80 | + } |
| 81 | + return new Span($span->traceId(), $spanOptions); |
| 82 | + } |
| 83 | + |
| 84 | + private static function convertAttributes(array $attributes) |
| 85 | + { |
| 86 | + $newAttributes = []; |
| 87 | + foreach ($attributes as $key => $value) { |
| 88 | + if (array_key_exists($key, self::ATTRIBUTE_MAP)) { |
| 89 | + $newAttributes[self::ATTRIBUTE_MAP[$key]] = $value; |
| 90 | + } else { |
| 91 | + $newAttributes[$key] = $value; |
| 92 | + } |
| 93 | + } |
| 94 | + return $newAttributes; |
| 95 | + } |
| 96 | + |
| 97 | + private static function convertLinks(array $links) |
| 98 | + { |
| 99 | + return array_map(function (OCLink $link) { |
| 100 | + return new Link($link->traceId(), $link->spanId(), [ |
| 101 | + 'type' => self::LINK_TYPE_MAP[$link->type()], |
| 102 | + 'attributes' => $link->attributes() |
| 103 | + ]); |
| 104 | + }, $links); |
| 105 | + } |
| 106 | + |
| 107 | + private static function convertTimeEvents(array $events) |
| 108 | + { |
| 109 | + $newEvents = []; |
| 110 | + foreach ($events as $event) { |
| 111 | + if ($event instanceof OCAnnotation) { |
| 112 | + $newEvents[] = self::convertAnnotation($event); |
| 113 | + } elseif ($event instanceof OCMessageEvent) { |
| 114 | + $newEvents[] = self::convertMessageEvent($event); |
| 115 | + } |
| 116 | + } |
| 117 | + return $newEvents; |
| 118 | + } |
| 119 | + |
| 120 | + private static function convertAnnotation(OCAnnotation $annotation) |
| 121 | + { |
| 122 | + return new Annotation($annotation->description(), [ |
| 123 | + 'attributes' => $annotation->attributes() |
| 124 | + ]); |
| 125 | + } |
| 126 | + |
| 127 | + private static function convertMessageEvent(OCMessageEvent $messageEvent) |
| 128 | + { |
| 129 | + return new MessageEvent($messageEvent->id(), [ |
| 130 | + 'type' => self::MESSAGE_TYPE_MAP[$messageEvent->type()], |
| 131 | + 'uncompressedSizeBytes' => $messageEvent->uncompressedSize(), |
| 132 | + 'compressedSizeBytes' => $messageEvent->compressedSize() |
| 133 | + ]); |
| 134 | + } |
| 135 | + |
| 136 | + private static function convertStatus(OCStatus $status = null) |
| 137 | + { |
| 138 | + if ($status) { |
| 139 | + return new Status($status->code(), $status->message()); |
| 140 | + } else { |
| 141 | + return null; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments