/proc/self/cwd/opencensus/trace/internal/b3.cc
Line | Count | Source (jump to first uncovered line) |
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 | 13 | bool IsHexDigits(absl::string_view s) { |
36 | 160 | for (int i = 0; i < s.length(); ++i) { |
37 | 156 | if (!absl::ascii_isxdigit(s[i])) return false; |
38 | 156 | } |
39 | 4 | return true; |
40 | 13 | } |
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 | 545 | absl::string_view b3_flags) { |
48 | 545 | static SpanContext invalid; |
49 | 545 | uint8_t sampled; |
50 | | |
51 | 545 | if (b3_sampled == "1") { |
52 | 329 | sampled = 1; |
53 | 329 | } else if (b3_sampled == "0" || b3_sampled.empty()) { |
54 | 2 | sampled = 0; |
55 | 214 | } else { |
56 | 214 | return invalid; |
57 | 214 | } |
58 | | |
59 | 331 | if (b3_flags == "1") { |
60 | 2 | sampled = 1; |
61 | 329 | } else if (!b3_flags.empty()) { |
62 | 109 | return invalid; |
63 | 109 | } |
64 | | |
65 | 222 | if (b3_trace_id.length() != 32 && b3_trace_id.length() != 16) return invalid; |
66 | 11 | if (b3_span_id.length() != 16) return invalid; |
67 | | |
68 | 11 | if (!IsHexDigits(b3_trace_id)) { |
69 | 9 | return invalid; |
70 | 9 | } |
71 | 2 | if (!IsHexDigits(b3_span_id)) { |
72 | 0 | return invalid; |
73 | 0 | } |
74 | | |
75 | 2 | std::string trace_id_binary = absl::HexStringToBytes(b3_trace_id); |
76 | 2 | std::string span_id_binary = absl::HexStringToBytes(b3_span_id); |
77 | | |
78 | 2 | uint8_t extended_trace_id[16]; |
79 | | |
80 | | // trace_id_ptr must point to a 128-bit trace_id. |
81 | 2 | const uint8_t* trace_id_ptr; |
82 | 2 | if (trace_id_binary.length() == 16) { |
83 | 1 | trace_id_ptr = reinterpret_cast<const uint8_t*>(trace_id_binary.data()); |
84 | 1 | } else if (trace_id_binary.length() == 8) { |
85 | | // Extend 64-bit trace_id to 128-bit using the buffer. |
86 | 1 | memset(extended_trace_id, 0, 8); |
87 | 1 | memcpy(extended_trace_id + 8, trace_id_binary.data(), 8); |
88 | 1 | trace_id_ptr = extended_trace_id; |
89 | 1 | } else { |
90 | 0 | return invalid; |
91 | 0 | } |
92 | | |
93 | 2 | return SpanContext( |
94 | 2 | TraceId(trace_id_ptr), |
95 | 2 | SpanId(reinterpret_cast<const uint8_t*>(span_id_binary.data())), |
96 | 2 | TraceOptions(&sampled)); |
97 | 2 | } |
98 | | |
99 | 0 | std::string ToB3TraceIdHeader(const SpanContext& ctx) { |
100 | 0 | return ctx.trace_id().ToHex(); |
101 | 0 | } |
102 | | |
103 | 0 | std::string ToB3SpanIdHeader(const SpanContext& ctx) { |
104 | 0 | return ctx.span_id().ToHex(); |
105 | 0 | } |
106 | | |
107 | 0 | std::string ToB3SampledHeader(const SpanContext& ctx) { |
108 | 0 | return ctx.trace_options().IsSampled() ? "1" : "0"; |
109 | 0 | } |
110 | | |
111 | | } // namespace propagation |
112 | | } // namespace trace |
113 | | } // namespace opencensus |