|
| 1 | +// Copyright 2019, 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/b3.h" |
| 16 | + |
| 17 | +#include <cstdint> |
| 18 | +#include <cstring> |
| 19 | +#include <string> |
| 20 | + |
| 21 | +#include "absl/strings/ascii.h" |
| 22 | +#include "absl/strings/escaping.h" |
| 23 | +#include "opencensus/trace/span_context.h" |
| 24 | +#include "opencensus/trace/span_id.h" |
| 25 | +#include "opencensus/trace/trace_id.h" |
| 26 | +#include "opencensus/trace/trace_options.h" |
| 27 | + |
| 28 | +namespace opencensus { |
| 29 | +namespace trace { |
| 30 | +namespace propagation { |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +// Returns true if the string only contains valid hex digits. |
| 35 | +bool IsHexDigits(absl::string_view s) { |
| 36 | + for (int i = 0; i < s.length(); ++i) { |
| 37 | + if (!absl::ascii_isxdigit(s[i])) return false; |
| 38 | + } |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +} // namespace |
| 43 | + |
| 44 | +SpanContext FromB3Headers(absl::string_view b3_trace_id, |
| 45 | + absl::string_view b3_span_id, |
| 46 | + absl::string_view b3_sampled, |
| 47 | + absl::string_view b3_flags) { |
| 48 | + static SpanContext invalid; |
| 49 | + uint8_t sampled; |
| 50 | + |
| 51 | + if (b3_sampled == "1") { |
| 52 | + sampled = 1; |
| 53 | + } else if (b3_sampled == "0" || b3_sampled.empty()) { |
| 54 | + sampled = 0; |
| 55 | + } else { |
| 56 | + return invalid; |
| 57 | + } |
| 58 | + |
| 59 | + if (b3_flags == "1") { |
| 60 | + sampled = 1; |
| 61 | + } else if (!b3_flags.empty()) { |
| 62 | + return invalid; |
| 63 | + } |
| 64 | + |
| 65 | + if (b3_trace_id.length() != 32 && b3_trace_id.length() != 16) return invalid; |
| 66 | + if (b3_span_id.length() != 16) return invalid; |
| 67 | + |
| 68 | + if (!IsHexDigits(b3_trace_id)) { |
| 69 | + return invalid; |
| 70 | + } |
| 71 | + if (!IsHexDigits(b3_span_id)) { |
| 72 | + return invalid; |
| 73 | + } |
| 74 | + |
| 75 | + std::string trace_id_binary = absl::HexStringToBytes(b3_trace_id); |
| 76 | + std::string span_id_binary = absl::HexStringToBytes(b3_span_id); |
| 77 | + |
| 78 | + // Extend 64-bit trace_id to 128-bit. |
| 79 | + uint8_t extended_trace_id[16]; |
| 80 | + if (b3_trace_id.length() == 16) { |
| 81 | + memset(extended_trace_id, 0, 8); |
| 82 | + memcpy(extended_trace_id + 8, trace_id_binary.data(), 8); |
| 83 | + } |
| 84 | + |
| 85 | + return SpanContext( |
| 86 | + TraceId((b3_trace_id.length() == 16) |
| 87 | + ? extended_trace_id |
| 88 | + : reinterpret_cast<const uint8_t*>(trace_id_binary.data())), |
| 89 | + SpanId(reinterpret_cast<const uint8_t*>(span_id_binary.data())), |
| 90 | + TraceOptions(&sampled)); |
| 91 | +} |
| 92 | + |
| 93 | +std::string ToB3TraceIdHeader(const SpanContext& ctx) { |
| 94 | + return ctx.trace_id().ToHex(); |
| 95 | +} |
| 96 | + |
| 97 | +std::string ToB3SpanIdHeader(const SpanContext& ctx) { |
| 98 | + return ctx.span_id().ToHex(); |
| 99 | +} |
| 100 | + |
| 101 | +std::string ToB3SampledHeader(const SpanContext& ctx) { |
| 102 | + return ctx.trace_options().IsSampled() ? "1" : "0"; |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace propagation |
| 106 | +} // namespace trace |
| 107 | +} // namespace opencensus |
0 commit comments