|
| 1 | +// Copyright 2018, OpenCensus Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "opencensus/trace/propagation/trace_context.h" |
| 16 | + |
| 17 | +#include "opencensus/trace/span_context.h" |
| 18 | +#include "opencensus/trace/span_id.h" |
| 19 | +#include "opencensus/trace/trace_id.h" |
| 20 | +#include "opencensus/trace/trace_options.h" |
| 21 | + |
| 22 | +#include "absl/strings/ascii.h" |
| 23 | +#include "absl/strings/escaping.h" |
| 24 | +#include "absl/strings/str_cat.h" |
| 25 | + |
| 26 | +namespace opencensus { |
| 27 | +namespace trace { |
| 28 | +namespace propagation { |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +// Returns true if the string only contains valid lowercase hex digits. |
| 33 | +bool IsLowercaseHexDigits(absl::string_view s) { |
| 34 | + for (int i = 0; i < s.length(); ++i) { |
| 35 | + if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + } |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +// Returns true if the converted string was valid hex. |
| 43 | +bool FromHex(absl::string_view hex, std::string* bin) { |
| 44 | + if (!IsLowercaseHexDigits(hex)) return false; |
| 45 | + *bin = absl::HexStringToBytes(hex); |
| 46 | + return true; |
| 47 | +} |
| 48 | + |
| 49 | +} // namespace |
| 50 | + |
| 51 | +SpanContext FromTraceParentHeader(absl::string_view header) { |
| 52 | + constexpr int kDelimiterLen = 1; |
| 53 | + constexpr char kDelimiter = '-'; |
| 54 | + constexpr int kVersionLen = 1; |
| 55 | + constexpr int kTraceIdLen = 16; |
| 56 | + constexpr int kSpanIdLen = 8; |
| 57 | + constexpr int kTraceOptionsLen = 1; |
| 58 | + constexpr int kVersionLenHex = 2 * kVersionLen; |
| 59 | + constexpr int kTraceIdLenHex = 2 * kTraceIdLen; |
| 60 | + constexpr int kSpanIdLenHex = 2 * kSpanIdLen; |
| 61 | + constexpr int kTraceOptionsLenHex = 2 * kTraceOptionsLen; |
| 62 | + constexpr int kTotalLenInHexDigits = kVersionLenHex + kTraceIdLenHex + |
| 63 | + kSpanIdLenHex + kTraceOptionsLenHex + |
| 64 | + 3 * kDelimiterLen; |
| 65 | + constexpr int kVersionOfs = 0; |
| 66 | + constexpr int kTraceIdOfs = kVersionOfs + kVersionLenHex + kDelimiterLen; |
| 67 | + constexpr int kSpanIdOfs = kTraceIdOfs + kTraceIdLenHex + kDelimiterLen; |
| 68 | + constexpr int kOptionsOfs = kSpanIdOfs + kSpanIdLenHex + kDelimiterLen; |
| 69 | + static_assert(kOptionsOfs + kTraceOptionsLenHex == kTotalLenInHexDigits, |
| 70 | + "bad offsets"); |
| 71 | + static SpanContext invalid; |
| 72 | + if (header.size() != kTotalLenInHexDigits || header[kVersionOfs] != '0' || |
| 73 | + header[kVersionOfs + 1] != '0' || |
| 74 | + header[kTraceIdOfs - kDelimiterLen] != kDelimiter || |
| 75 | + header[kSpanIdOfs - kDelimiterLen] != kDelimiter || |
| 76 | + header[kOptionsOfs - kDelimiterLen] != kDelimiter) { |
| 77 | + return invalid; // Invalid length, version or format. |
| 78 | + } |
| 79 | + std::string trace_id_bin, span_id_bin, options_bin; |
| 80 | + if (!FromHex(header.substr(kTraceIdOfs, kTraceIdLenHex), &trace_id_bin) || |
| 81 | + !FromHex(header.substr(kSpanIdOfs, kSpanIdLenHex), &span_id_bin) || |
| 82 | + !FromHex(header.substr(kOptionsOfs, kTraceOptionsLenHex), &options_bin)) { |
| 83 | + return invalid; // Invalid hex. |
| 84 | + } |
| 85 | + return SpanContext( |
| 86 | + TraceId(reinterpret_cast<const uint8_t*>(trace_id_bin.data())), |
| 87 | + SpanId(reinterpret_cast<const uint8_t*>(span_id_bin.data())), |
| 88 | + TraceOptions(reinterpret_cast<const uint8_t*>(options_bin.data()))); |
| 89 | +} |
| 90 | + |
| 91 | +std::string ToTraceParentHeader(const SpanContext& ctx) { |
| 92 | + return absl::StrCat("00-", ctx.ToString()); |
| 93 | +} |
| 94 | + |
| 95 | +} // namespace propagation |
| 96 | +} // namespace trace |
| 97 | +} // namespace opencensus |
0 commit comments