|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenTelemetry\Extension\Propagator\Jaeger; |
| 6 | + |
| 7 | +use OpenTelemetry\API\Trace\Span; |
| 8 | +use OpenTelemetry\API\Trace\SpanContext; |
| 9 | +use OpenTelemetry\API\Trace\SpanContextInterface; |
| 10 | +use OpenTelemetry\API\Trace\SpanContextValidator; |
| 11 | +use OpenTelemetry\API\Trace\TraceFlags; |
| 12 | +use OpenTelemetry\Context\Context; |
| 13 | +use OpenTelemetry\Context\ContextInterface; |
| 14 | +use OpenTelemetry\Context\Propagation\ArrayAccessGetterSetter; |
| 15 | +use OpenTelemetry\Context\Propagation\PropagationGetterInterface; |
| 16 | +use OpenTelemetry\Context\Propagation\PropagationSetterInterface; |
| 17 | +use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * JaegerPropagator is a propagator that supports the specification for the header |
| 21 | + * "uber-trace-id" used for trace context propagation across service boundaries. |
| 22 | + * (https://www.jaegertracing.io/docs/1.52/client-libraries/#propagation-format) |
| 23 | + */ |
| 24 | +class JaegerPropagator implements TextMapPropagatorInterface |
| 25 | +{ |
| 26 | + private const UBER_TRACE_ID_HEADER = 'uber-trace-id'; |
| 27 | + |
| 28 | + private const IS_NOT_SAMPLED = 0; |
| 29 | + private const IS_SAMPLED = 1; |
| 30 | + private const IS_DEBUG = 2; |
| 31 | + private const DEFAULT_PARENT_SPAN_ID = 0; |
| 32 | + |
| 33 | + private const FIELDS = [ |
| 34 | + self::UBER_TRACE_ID_HEADER, |
| 35 | + ]; |
| 36 | + |
| 37 | + private static ?TextMapPropagatorInterface $instance = null; |
| 38 | + |
| 39 | + public static function getInstance(): TextMapPropagatorInterface |
| 40 | + { |
| 41 | + if (self::$instance === null) { |
| 42 | + self::$instance = new JaegerPropagator(); |
| 43 | + } |
| 44 | + |
| 45 | + return self::$instance; |
| 46 | + } |
| 47 | + |
| 48 | + public function fields(): array |
| 49 | + { |
| 50 | + return self::FIELDS; |
| 51 | + } |
| 52 | + |
| 53 | + /** {@inheritdoc} */ |
| 54 | + public function inject(&$carrier, PropagationSetterInterface $setter = null, ContextInterface $context = null): void |
| 55 | + { |
| 56 | + $setter ??= ArrayAccessGetterSetter::getInstance(); |
| 57 | + $context ??= Context::getCurrent(); |
| 58 | + $spanContext = Span::fromContext($context)->getContext(); |
| 59 | + |
| 60 | + if (!$spanContext->isValid()) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + $flag = $this->getFlag($spanContext, $context); |
| 65 | + |
| 66 | + $uberTraceId = sprintf( |
| 67 | + '%s:%s:%d:%d', |
| 68 | + $spanContext->getTraceId(), |
| 69 | + $spanContext->getSpanId(), |
| 70 | + self::DEFAULT_PARENT_SPAN_ID, |
| 71 | + $flag |
| 72 | + ); |
| 73 | + |
| 74 | + $setter->set($carrier, self::UBER_TRACE_ID_HEADER, $uberTraceId); |
| 75 | + } |
| 76 | + |
| 77 | + /** {@inheritdoc} */ |
| 78 | + public function extract($carrier, PropagationGetterInterface $getter = null, ContextInterface $context = null): ContextInterface |
| 79 | + { |
| 80 | + $getter ??= ArrayAccessGetterSetter::getInstance(); |
| 81 | + $context ??= Context::getCurrent(); |
| 82 | + |
| 83 | + $spanContext = self::extractImpl($carrier, $getter, $context); |
| 84 | + if (!$spanContext->isValid()) { |
| 85 | + return $context; |
| 86 | + } |
| 87 | + |
| 88 | + return $context->withContextValue(Span::wrap($spanContext)); |
| 89 | + } |
| 90 | + |
| 91 | + private function getFlag(SpanContextInterface $spanContext, ContextInterface $context): int |
| 92 | + { |
| 93 | + if ($spanContext->isSampled()) { |
| 94 | + if ($context->get(JaegerDebugFlagContextKey::instance())) { |
| 95 | + return self::IS_DEBUG | self::IS_SAMPLED; |
| 96 | + } |
| 97 | + |
| 98 | + return self::IS_SAMPLED; |
| 99 | + } |
| 100 | + |
| 101 | + return self::IS_NOT_SAMPLED; |
| 102 | + } |
| 103 | + |
| 104 | + private static function extractImpl($carrier, PropagationGetterInterface $getter, ContextInterface &$context): SpanContextInterface |
| 105 | + { |
| 106 | + $headerValue = $getter->get($carrier, self::UBER_TRACE_ID_HEADER); |
| 107 | + |
| 108 | + if ($headerValue === null) { |
| 109 | + return SpanContext::getInvalid(); |
| 110 | + } |
| 111 | + |
| 112 | + $pieces = explode(':', $headerValue); |
| 113 | + |
| 114 | + if (count($pieces) != 4) { |
| 115 | + return SpanContext::getInvalid(); |
| 116 | + } |
| 117 | + |
| 118 | + [$traceId, $spanId, $parentSpanId, $traceFlags] = $pieces; |
| 119 | + |
| 120 | + $traceId = str_pad($traceId, SpanContextValidator::TRACE_LENGTH, '0', STR_PAD_LEFT); |
| 121 | + $spanId = str_pad($spanId, SpanContextValidator::SPAN_LENGTH, '0', STR_PAD_LEFT); |
| 122 | + |
| 123 | + if (!SpanContextValidator::isValidTraceId($traceId) || !SpanContextValidator::isValidSpanId($spanId)) { |
| 124 | + return SpanContext::getInvalid(); |
| 125 | + } |
| 126 | + |
| 127 | + if ((int) $traceFlags & self::IS_DEBUG) { |
| 128 | + $context = $context->with(JaegerDebugFlagContextKey::instance(), true); |
| 129 | + } |
| 130 | + |
| 131 | + $isSampled = ((int) $traceFlags) & 1; |
| 132 | + |
| 133 | + return SpanContext::createFromRemoteParent( |
| 134 | + $traceId, |
| 135 | + $spanId, |
| 136 | + $isSampled ? TraceFlags::SAMPLED : TraceFlags::DEFAULT |
| 137 | + ); |
| 138 | + } |
| 139 | +} |
0 commit comments