cosmetic fixes

This commit is contained in:
Alexander Kuzmenkov 2020-08-28 22:02:50 +03:00
parent f87db0fbe3
commit 063cc386a3
4 changed files with 25 additions and 10 deletions

View File

@ -70,7 +70,6 @@ void ClientInfo::write(WriteBuffer & out, const UInt64 server_protocol_revision)
writeBinary(opentelemetry_parent_span_id, out); writeBinary(opentelemetry_parent_span_id, out);
writeBinary(opentelemetry_tracestate, out); writeBinary(opentelemetry_tracestate, out);
writeBinary(opentelemetry_trace_flags, out); writeBinary(opentelemetry_trace_flags, out);
std::cerr << fmt::format("wrote {:x}, {}, {}\n", opentelemetry_trace_id, opentelemetry_span_id, opentelemetry_parent_span_id) << StackTrace().toString() << std::endl;
} }
} }
@ -155,28 +154,44 @@ bool ClientInfo::setOpenTelemetryTraceparent(const std::string & traceparent,
uint64_t trace_parent = 0; uint64_t trace_parent = 0;
uint8_t trace_flags = 0; uint8_t trace_flags = 0;
// Version 00, which is the only one we can parse, is fixed width. Use this
// fact for an additional sanity check.
const int expected_length = 2 + 1 + 32 + 1 + 16 + 1 + 2;
if (traceparent.length() != expected_length)
{
error = fmt::format("unexpected length {}, expected {}",
traceparent.length(), expected_length);
return false;
}
// clang-tidy doesn't like sscanf:
// error: 'sscanf' used to convert a string to an unsigned integer value,
// but function will not report conversion errors; consider using 'strtoul'
// instead [cert-err34-c,-warnings-as-errors]
// There is no other ready solution, and hand-rolling a more complicated
// parser for an HTTP header in C++ sounds like RCE.
// NOLINTNEXTLINE(cert-err34-c)
int result = sscanf(&traceparent[0], int result = sscanf(&traceparent[0],
"%2" SCNx8 "-%16" SCNx64 "%16" SCNx64 "-%16" SCNx64 "-%2" SCNx8, "%2" SCNx8 "-%16" SCNx64 "%16" SCNx64 "-%16" SCNx64 "-%2" SCNx8,
&version, &trace_id_high, &trace_id_low, &trace_parent, &trace_flags); &version, &trace_id_high, &trace_id_low, &trace_parent, &trace_flags);
if (result == EOF) if (result == EOF)
{ {
error = "Failed to parse traceparent header (EOF)"; error = "EOF";
return false; return false;
} }
// We read uint128 as two uint64, so 5 parts and not 4.
if (result != 5) if (result != 5)
{ {
error = fmt::format("Failed to parse traceparent header" error = fmt::format("could only read {} parts instead of the expected 5",
"(could only read {} parts instead of the expected 5)",
result); result);
return false; return false;
} }
if (version != 0) if (version != 0)
{ {
error = fmt::format("Unexpected version {} of traceparent header:" error = fmt::format("unexpected version {}, expected 00", version);
"expected 00", version);
return false; return false;
} }

View File

@ -58,7 +58,7 @@ public:
String initial_user; String initial_user;
String initial_query_id; String initial_query_id;
Poco::Net::SocketAddress initial_address; Poco::Net::SocketAddress initial_address;
// OpenTelemetry things // OpenTelemetry things
__uint128_t opentelemetry_trace_id = 0; __uint128_t opentelemetry_trace_id = 0;
// Span ID is not strictly the client info, but convenient to keep here. // Span ID is not strictly the client info, but convenient to keep here.

View File

@ -1092,7 +1092,7 @@ void Context::setCurrentQueryId(const String & query_id)
random.words.a = thread_local_rng(); //-V656 random.words.a = thread_local_rng(); //-V656
random.words.b = thread_local_rng(); //-V656 random.words.b = thread_local_rng(); //-V656
fmt::print(stderr, "traceid {}, ==0 {}\n", client_info.opentelemetry_trace_id, client_info.opentelemetry_trace_id == 0); fmt::print(stderr, "traceid {}, ==0 {}\n", client_info.opentelemetry_trace_id, client_info.opentelemetry_trace_id == 0);
if (client_info.opentelemetry_trace_id == 0) if (client_info.opentelemetry_trace_id == 0)
{ {
@ -1120,7 +1120,7 @@ void Context::setCurrentQueryId(const String & query_id)
QueryUUID(const char * bytes, Poco::UUID::Version version) QueryUUID(const char * bytes, Poco::UUID::Version version)
: Poco::UUID(bytes, version) {} : Poco::UUID(bytes, version) {}
}; };
query_id_to_set = QueryUUID(random.bytes, Poco::UUID::UUID_RANDOM).toString(); query_id_to_set = QueryUUID(random.bytes, Poco::UUID::UUID_RANDOM).toString();
} }

View File

@ -147,7 +147,7 @@ static void logQuery(const String & query, const Context & context, bool interna
(current_user != "default" ? ", user: " + context.getClientInfo().current_user : ""), (current_user != "default" ? ", user: " + context.getClientInfo().current_user : ""),
(!initial_query_id.empty() && current_query_id != initial_query_id ? ", initial_query_id: " + initial_query_id : std::string()), (!initial_query_id.empty() && current_query_id != initial_query_id ? ", initial_query_id: " + initial_query_id : std::string()),
joinLines(query)); joinLines(query));
LOG_TRACE(&Poco::Logger::get("executeQuery"), LOG_TRACE(&Poco::Logger::get("executeQuery"),
"OpenTelemetry trace id {:x}, span id {}, parent span id {}", "OpenTelemetry trace id {:x}, span id {}, parent span id {}",
context.getClientInfo().opentelemetry_trace_id, context.getClientInfo().opentelemetry_span_id, context.getClientInfo().opentelemetry_trace_id, context.getClientInfo().opentelemetry_span_id,