Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(otel): support abseil <= 20210324 #12993

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions google/cloud/internal/trace_propagator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <opentelemetry/context/propagation/composite_propagator.h>
#include <opentelemetry/trace/context.h>
#include <opentelemetry/trace/propagation/http_trace_context.h>
#include <cstdlib>

namespace google {
namespace cloud {
Expand Down Expand Up @@ -60,13 +61,15 @@ class CloudTraceContext
using opentelemetry::trace::TraceId;
std::array<char, 2 * TraceId::kSize> trace_id;
span_context.trace_id().ToLowerBase16(trace_id);
std::array<char, 2 * SpanId::kSize> span_id;
span_context.span_id().ToLowerBase16(span_id);
std::uint64_t span_id_dec;
if (!absl::SimpleHexAtoi(absl::string_view{span_id.data(), span_id.size()},
&span_id_dec)) {
return;
}
// We would prefer to use `absl::SimpleHexAtoi`, but it is not available in
// the oldest version of Abseil we support. So we use `std::strtoull`, which
// requires the input to be null-terminated.
std::array<char, 2 * SpanId::kSize + 1> span_id;
span_context.span_id().ToLowerBase16({span_id.data(), span_id.size() - 1});
span_id[2 * SpanId::kSize] = '\0';
char* end = nullptr;
std::uint64_t span_id_dec = std::strtoull(span_id.data(), &end, 16);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not handle errors, just like the implementation with absl::SimpleHexAtoi() did?

Suggested change
std::uint64_t span_id_dec = std::strtoull(span_id.data(), &end, 16);
auto const span_id_dec = std::strtoull(span_id.data(), &end, 16);
if (end != span_id.data() + span_id.size()) return;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (end - span_id.data() != 2 * SpanId::kSize) return;
carrier.Set(
"x-cloud-trace-context",
absl::StrCat(absl::string_view{trace_id.data(), trace_id.size()}, "/",
Expand Down