Coverage Report

Created: 2023-10-10 06:39

/proc/self/cwd/opencensus/trace/internal/trace_context.cc
Line
Count
Source
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
689
bool IsLowercaseHexDigits(absl::string_view s) {
34
12.0k
  for (int i = 0; i < s.length(); ++i) {
35
11.3k
    if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) {
36
19
      return false;
37
19
    }
38
11.3k
  }
39
670
  return true;
40
689
}
41
42
// Returns true if the converted string was valid hex.
43
689
bool FromHex(absl::string_view hex, std::string* bin) {
44
689
  if (!IsLowercaseHexDigits(hex)) return false;
45
670
  *bin = absl::HexStringToBytes(hex);
46
670
  return true;
47
689
}
48
49
}  // namespace
50
51
323
SpanContext FromTraceParentHeader(absl::string_view header) {
52
323
  constexpr int kDelimiterLen = 1;
53
323
  constexpr char kDelimiter = '-';
54
323
  constexpr int kVersionLen = 1;
55
323
  constexpr int kTraceIdLen = 16;
56
323
  constexpr int kSpanIdLen = 8;
57
323
  constexpr int kTraceOptionsLen = 1;
58
323
  constexpr int kVersionLenHex = 2 * kVersionLen;
59
323
  constexpr int kTraceIdLenHex = 2 * kTraceIdLen;
60
323
  constexpr int kSpanIdLenHex = 2 * kSpanIdLen;
61
323
  constexpr int kTraceOptionsLenHex = 2 * kTraceOptionsLen;
62
323
  constexpr int kTotalLenInHexDigits = kVersionLenHex + kTraceIdLenHex +
63
323
                                       kSpanIdLenHex + kTraceOptionsLenHex +
64
323
                                       3 * kDelimiterLen;
65
323
  constexpr int kVersionOfs = 0;
66
323
  constexpr int kTraceIdOfs = kVersionOfs + kVersionLenHex + kDelimiterLen;
67
323
  constexpr int kSpanIdOfs = kTraceIdOfs + kTraceIdLenHex + kDelimiterLen;
68
323
  constexpr int kOptionsOfs = kSpanIdOfs + kSpanIdLenHex + kDelimiterLen;
69
323
  static_assert(kOptionsOfs + kTraceOptionsLenHex == kTotalLenInHexDigits,
70
323
                "bad offsets");
71
323
  static SpanContext invalid;
72
323
  if (header.size() != kTotalLenInHexDigits || header[kVersionOfs] != '0' ||
73
323
      header[kVersionOfs + 1] != '0' ||
74
323
      header[kTraceIdOfs - kDelimiterLen] != kDelimiter ||
75
323
      header[kSpanIdOfs - kDelimiterLen] != kDelimiter ||
76
323
      header[kOptionsOfs - kDelimiterLen] != kDelimiter) {
77
82
    return invalid;  // Invalid length, version or format.
78
82
  }
79
241
  std::string trace_id_bin, span_id_bin, options_bin;
80
241
  if (!FromHex(header.substr(kTraceIdOfs, kTraceIdLenHex), &trace_id_bin) ||
81
241
      !FromHex(header.substr(kSpanIdOfs, kSpanIdLenHex), &span_id_bin) ||
82
241
      !FromHex(header.substr(kOptionsOfs, kTraceOptionsLenHex), &options_bin)) {
83
19
    return invalid;  // Invalid hex.
84
19
  }
85
222
  return SpanContext(
86
222
      TraceId(reinterpret_cast<const uint8_t*>(trace_id_bin.data())),
87
222
      SpanId(reinterpret_cast<const uint8_t*>(span_id_bin.data())),
88
222
      TraceOptions(reinterpret_cast<const uint8_t*>(options_bin.data())));
89
241
}
90
91
219
std::string ToTraceParentHeader(const SpanContext& ctx) {
92
219
  return absl::StrCat("00-", ctx.ToString());
93
219
}
94
95
}  // namespace propagation
96
}  // namespace trace
97
}  // namespace opencensus