Skip to content

Commit

Permalink
resolved aditya's questions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianlin-Zhao committed Aug 20, 2020
1 parent fabc345 commit 0a8021a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
24 changes: 11 additions & 13 deletions api/include/opentelemetry/trace/propagation/http_trace_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class HttpTraceContext : public HTTPTextFormat<T>
uint8_t buf[kTraceIdBytes / 2];
for (int i = 0; i < kTraceIdBytes; i++)
{
int tmp = CharToInt(*trc_id);
int tmp = HexToInt(*trc_id);
if (tmp < 0)
{
for (int j = 0; j < kTraceIdBytes / 2; j++)
Expand Down Expand Up @@ -127,7 +127,7 @@ class HttpTraceContext : public HTTPTextFormat<T>
uint8_t buf[kSpanIdBytes / 2];
for (int i = 0; i < kSpanIdBytes; i++)
{
int tmp = CharToInt(spn_id[i]);
int tmp = HexToInt(spn_id[i]);
if (tmp < 0)
{
for (int j = 0; j < kSpanIdBytes / 2; j++)
Expand All @@ -150,8 +150,8 @@ class HttpTraceContext : public HTTPTextFormat<T>

static TraceFlags GenerateTraceFlagsFromString(nostd::string_view trace_flags)
{
int tmp1 = CharToInt(trace_flags[0]);
int tmp2 = CharToInt(trace_flags[1]);
int tmp1 = HexToInt(trace_flags[0]);
int tmp2 = HexToInt(trace_flags[1]);
if (tmp1 < 0 || tmp2 < 0)
return TraceFlags(0); // check for invalid char
uint8_t buf = tmp1 * 16 + tmp2;
Expand All @@ -161,7 +161,7 @@ class HttpTraceContext : public HTTPTextFormat<T>
private:
// Converts a single character to a corresponding integer (e.g. '1' to 1), return -1
// if the character is not a valid number in hex.
static uint8_t CharToInt(char c)
static uint8_t HexToInt(char c)
{
if (c >= '0' && c <= '9')
{
Expand Down Expand Up @@ -241,20 +241,18 @@ class HttpTraceContext : public HTTPTextFormat<T>
nostd::string_view trace_flags = trace_parent.substr(
kHeaderElementLengths[0] + kHeaderElementLengths[1] + kHeaderElementLengths[2] + 3);

if (version == "ff")
{
return SpanContext(false, false);
}

if (trace_id == "00000000000000000000000000000000" || span_id == "0000000000000000")
if (version == "ff" &&
(trace_id == "00000000000000000000000000000000" || span_id == "0000000000000000"))
{
return SpanContext(false, false);
}

// validate ids
if (!IsValidHex(version) || !IsValidHex(trace_id) || !IsValidHex(span_id) ||
!IsValidHex(trace_flags))
{
return SpanContext(false, false);
}

TraceId trace_id_obj = GenerateTraceIdFromString(trace_id);
SpanId span_id_obj = GenerateSpanIdFromString(span_id);
Expand All @@ -269,8 +267,8 @@ class HttpTraceContext : public HTTPTextFormat<T>
{
return SpanContext(false, false);
}
SpanContext context_from_parent_header = ExtractContextFromTraceParent(trace_parent);
return context_from_parent_header;

return ExtractContextFromTraceParent(trace_parent);
}
};
} // namespace propagation
Expand Down
6 changes: 3 additions & 3 deletions api/test/trace/propagation/http_text_format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ static void Setter(std::map<std::string, std::string> &carrier,
carrier[std::string(trace_type)] = std::string(trace_description);
}

static trace::propagation::HttpTraceContext<std::map<std::string, std::string>> format =
trace::propagation::HttpTraceContext<std::map<std::string, std::string>>();

using MapHttpTraceContext =
trace::propagation::HttpTraceContext<std::map<std::string, std::string>>;

static MapHttpTraceContext format = MapHttpTraceContext();

TEST(HTTPTextFormatTest, TraceIdBufferGeneration)
{
constexpr uint8_t buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
Expand Down

0 comments on commit 0a8021a

Please sign in to comment.