|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 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\Propagator; |
| 19 | + |
| 20 | +use OpenCensus\Trace\TraceContext; |
| 21 | + |
| 22 | +/** |
| 23 | + * This propagator uses HTTP headers to propagate TraceContext over HTTP. |
| 24 | + * There are two possible headers `X-Cloud-Trace-Context` and `Trace-Context`. |
| 25 | + * This class handles both formats. |
| 26 | + * |
| 27 | + * The current format of the header is <trace-id>[/<span-id>][;o=<options>]. |
| 28 | + * The options are a bitmask of options. Currently the only option is the |
| 29 | + * least significant bit which signals whether the request was traced or not |
| 30 | + * (1 = traced, 0 = not traced). |
| 31 | + */ |
| 32 | +class HttpHeaderPropagator implements PropagatorInterface |
| 33 | +{ |
| 34 | + const HTTP_HEADERS = [ |
| 35 | + 'HTTP_X_CLOUD_TRACE_CONTEXT', |
| 36 | + 'HTTP_TRACE_CONTEXT' |
| 37 | + ]; |
| 38 | + const CONTEXT_HEADER_FORMAT = '/([0-9a-f]{32})(?:\/(\d+))?(?:;o=(\d+))?/'; |
| 39 | + |
| 40 | + /** |
| 41 | + * Generate a TraceContext object from the all the HTTP headers |
| 42 | + * |
| 43 | + * @param array $headers |
| 44 | + * @return TraceContext |
| 45 | + */ |
| 46 | + public function parse($headers) |
| 47 | + { |
| 48 | + foreach (self::HTTP_HEADERS as $header) { |
| 49 | + if (array_key_exists($header, $headers)) { |
| 50 | + return self::deserialize($headers[$header]); |
| 51 | + } |
| 52 | + } |
| 53 | + return new TraceContext(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Generate a TraceContext object from the Trace Context header |
| 58 | + * |
| 59 | + * @param string $header |
| 60 | + * @return TraceContext |
| 61 | + */ |
| 62 | + public function deserialize($header) |
| 63 | + { |
| 64 | + if (preg_match(self::CONTEXT_HEADER_FORMAT, $header, $matches)) { |
| 65 | + return new TraceContext( |
| 66 | + $matches[1], |
| 67 | + array_key_exists(2, $matches) ? $matches[2] : null, |
| 68 | + array_key_exists(3, $matches) ? $matches[3] == '1' : null, |
| 69 | + true |
| 70 | + ); |
| 71 | + } |
| 72 | + return new TraceContext(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Convert a TraceContext to header string |
| 77 | + * |
| 78 | + * @param TraceContext $context |
| 79 | + * @return string |
| 80 | + */ |
| 81 | + public function serialize(TraceContext $context) |
| 82 | + { |
| 83 | + $ret = '' . $context->traceId(); |
| 84 | + if ($context->spanId()) { |
| 85 | + $ret .= '/' . $context->spanId(); |
| 86 | + } |
| 87 | + $ret .= ';o=' . ($context->enabled() ? '1' : '0'); |
| 88 | + return $ret; |
| 89 | + } |
| 90 | +} |
0 commit comments