|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenTelemetry\Symfony\OtelSdkBundle\DataCollector; |
| 6 | + |
| 7 | +use OpenTelemetry\API\Trace\TracerInterface; |
| 8 | +use OpenTelemetry\SDK\Trace\SpanDataInterface; |
| 9 | +use OpenTelemetry\SDK\Trace\SpanExporterInterface; |
| 10 | +use OpenTelemetry\SDK\Trace\TracerProviderInterface; |
| 11 | +use Symfony\Component\HttpFoundation\Request; |
| 12 | +use Symfony\Component\HttpFoundation\Response; |
| 13 | +use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
| 14 | +use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface; |
| 15 | + |
| 16 | +class OtelDataCollector extends DataCollector implements LateDataCollectorInterface |
| 17 | +{ |
| 18 | + private ?TracerInterface $tracer = null; |
| 19 | + private ?TracerProviderInterface $tracerProvider = null; |
| 20 | + public array $collectedSpans = []; |
| 21 | + |
| 22 | + public function reset(): void |
| 23 | + { |
| 24 | + $this->data = []; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * {@inheritDoc} |
| 29 | + */ |
| 30 | + public function collect(Request $request, Response $response, \Throwable $exception = null): void |
| 31 | + { |
| 32 | + // Everything is collected during the request, and formatted on kernel terminate. |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * {@inheritDoc} |
| 37 | + */ |
| 38 | + public function getName(): string |
| 39 | + { |
| 40 | + return 'otel'; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @return array|\Symfony\Component\VarDumper\Cloner\Data |
| 45 | + */ |
| 46 | + public function getData() |
| 47 | + { |
| 48 | + return $this->data; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritDoc} |
| 53 | + */ |
| 54 | + public function lateCollect(): void |
| 55 | + { |
| 56 | + $this->loadDataFromTracerSharedState(); |
| 57 | + $this->data['spans'] = $this->orderedSpans(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param class-string $class |
| 62 | + */ |
| 63 | + public function getClassLocation(string $class): array |
| 64 | + { |
| 65 | + $reflection = new \ReflectionClass($class); |
| 66 | + |
| 67 | + return [ |
| 68 | + 'class' => $reflection->getShortName(), |
| 69 | + 'file' => $reflection->getFileName(), |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + public function setTracer(TracerInterface $tracer): void |
| 74 | + { |
| 75 | + $this->tracer = $tracer; |
| 76 | + } |
| 77 | + |
| 78 | + public function setTracerProvider(TracerProviderInterface $tracerProvider): void |
| 79 | + { |
| 80 | + $this->tracerProvider = $tracerProvider; |
| 81 | + } |
| 82 | + |
| 83 | + public function setExporterData(SpanExporterInterface $exporter): void |
| 84 | + { |
| 85 | + $this->data['exporter'] = $this->getClassLocation(get_class($exporter)); |
| 86 | + //Add directory to exporter class because all exporter are named "Exporter" |
| 87 | + $this->data['exporter']['class'] = str_replace('.php', '', implode('/', array_slice(explode('/', $this->data['exporter']['file']), -2, 2, true))); |
| 88 | + } |
| 89 | + |
| 90 | + private function loadDataFromTracerSharedState(): void |
| 91 | + { |
| 92 | + $objectWithSharedState = $this->tracer ?? $this->tracerProvider ?? null; |
| 93 | + if ($objectWithSharedState === null) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + $reflectedTracer = new \ReflectionClass($objectWithSharedState); |
| 98 | + $tss = $reflectedTracer->getProperty('tracerSharedState'); |
| 99 | + $tss->setAccessible(true); |
| 100 | + $this->data['id_generator'] = $this->getClassLocation(get_class($tss->getValue($objectWithSharedState)->getIdGenerator())); |
| 101 | + $this->data['sampler'] = $this->getClassLocation(get_class($tss->getValue($objectWithSharedState)->getSampler())); |
| 102 | + $this->data['span_processor'] = $this->getClassLocation(get_class($tss->getValue($objectWithSharedState)->getSpanProcessor())); |
| 103 | + $this->data['resource_info_attributes'] = $this->cloneVar($tss->getValue($objectWithSharedState)->getResource()->getAttributes()); |
| 104 | + $this->data['span_limits'] = $this->cloneVar($tss->getValue($objectWithSharedState)->getSpanLimits()); |
| 105 | + } |
| 106 | + |
| 107 | + private function orderedSpans(): array |
| 108 | + { |
| 109 | + $spanData = []; |
| 110 | + $rootSpanId = null; |
| 111 | + /** @var \OpenTelemetry\SDK\Trace\Span $span */ |
| 112 | + foreach ($this->collectedSpans as $span) { |
| 113 | + //probably find better way to identify root span |
| 114 | + if (false === $span->getParentContext()->isValid()) { |
| 115 | + $spanData['root']['data'] = $this->spanDataToArray($span->toSpanData()); |
| 116 | + $spanData['root']['children'] = []; |
| 117 | + $rootSpanId = $span->getContext()->getSpanId(); |
| 118 | + } |
| 119 | + |
| 120 | + if ($rootSpanId === $span->getParentContext()->getSpanId()) { |
| 121 | + $spanData['root']['children'][] = $this->spanDataToArray($span->toSpanData()); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return $spanData; |
| 126 | + } |
| 127 | + |
| 128 | + private function spanDataToArray(SpanDataInterface $spanData): array |
| 129 | + { |
| 130 | + return [ |
| 131 | + 'spanId' => $spanData->getSpanId(), |
| 132 | + 'traceId' => $spanData->getTraceId(), |
| 133 | + 'name' => $spanData->getName(), |
| 134 | + 'kind' => $spanData->getKind(), |
| 135 | + 'attributes' => $spanData->getAttributes()->toArray(), |
| 136 | + 'status' => $spanData->getStatus()->getCode(), |
| 137 | + 'parentSpanId' => $spanData->getParentSpanId(), |
| 138 | + 'links' => $spanData->getLinks(), |
| 139 | + 'events' => $spanData->getEvents(), |
| 140 | + ]; |
| 141 | + } |
| 142 | +} |
0 commit comments