function TraceContextPropagator::extractImpl
1 call to TraceContextPropagator::extractImpl()
- TraceContextPropagator::extract in vendor/
open-telemetry/ api/ Trace/ Propagation/ TraceContextPropagator.php - Extracts specific values from the provided carrier into the provided {via an {
File
-
vendor/
open-telemetry/ api/ Trace/ Propagation/ TraceContextPropagator.php, line 97
Class
- TraceContextPropagator
- TraceContext is a propagator that supports the W3C Trace Context format (https://www.w3.org/TR/trace-context/)
Namespace
OpenTelemetry\API\Trace\PropagationCode
private static function extractImpl($carrier, PropagationGetterInterface $getter) : SpanContextInterface {
$traceparent = $getter->get($carrier, self::TRACEPARENT);
if ($traceparent === null) {
return SpanContext::getInvalid();
}
// traceParent = {version}-{trace-id}-{parent-id}-{trace-flags}
$pieces = explode('-', $traceparent);
// If the header does not have at least 4 pieces, it is invalid -- restart the trace.
if (count($pieces) < 4) {
return SpanContext::getInvalid();
}
[
$version,
$traceId,
$spanId,
$traceFlags,
] = $pieces;
/**
* Return invalid if:
* - Version is invalid (not 2 char hex or 'ff')
* - Trace version, trace ID, span ID or trace flag are invalid
*/
if (!TraceContextValidator::isValidTraceVersion($version) || !SpanContextValidator::isValidTraceId($traceId) || !SpanContextValidator::isValidSpanId($spanId) || !TraceContextValidator::isValidTraceFlag($traceFlags)) {
return SpanContext::getInvalid();
}
// Return invalid if the trace version is not a future version but still has > 4 pieces.
$versionIsFuture = hexdec($version) > hexdec(self::VERSION);
if (count($pieces) > 4 && !$versionIsFuture) {
return SpanContext::getInvalid();
}
// Only the sampled flag is extracted from the traceFlags (00000001)
$convertedTraceFlags = hexdec($traceFlags);
$isSampled = ($convertedTraceFlags & TraceFlags::SAMPLED) === TraceFlags::SAMPLED;
// Tracestate = 'Vendor1=Value1,...,VendorN=ValueN'
$rawTracestate = $getter->get($carrier, self::TRACESTATE);
if ($rawTracestate !== null) {
$tracestate = new TraceState($rawTracestate);
return SpanContext::createFromRemoteParent($traceId, $spanId, $isSampled ? TraceFlags::SAMPLED : TraceFlags::DEFAULT, $tracestate);
}
// Only traceparent header is extracted. No tracestate.
return SpanContext::createFromRemoteParent($traceId, $spanId, $isSampled ? TraceFlags::SAMPLED : TraceFlags::DEFAULT);
}