feat: Encode traceID and spanID as hex string (#34807)

Issue: https://github.com/zilliztech/knowhere/pull/714

Signed-off-by: Cai Yudong <yudong.cai@zilliz.com>
pull/35304/head
Cai Yudong 2024-08-06 15:20:16 +08:00 committed by GitHub
parent 91df03afe8
commit 3c9a47c8db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 94 additions and 72 deletions

View File

@ -16,7 +16,6 @@
#include <iomanip>
#include <iostream>
#include <vector>
#include <utility>
#include "opentelemetry/exporters/jaeger/jaeger_exporter_factory.h"
@ -164,23 +163,43 @@ EmptySpanID(const TraceContext* ctx) {
return isEmptyID(ctx->spanID, trace::SpanId::kSize);
}
std::vector<uint8_t>
GetTraceIDAsVector(const TraceContext* ctx) {
std::string
BytesToHexStr(const uint8_t* data, const size_t len) {
std::stringstream ss;
for (size_t i = 0; i < len; i++) {
ss << std::hex << std::setw(2) << std::setfill('0')
<< static_cast<int>(data[i]);
}
return ss.str();
}
std::string
GetIDFromHexStr(const std::string& hexStr) {
std::stringstream ss;
for (size_t i = 0; i < hexStr.length(); i += 2) {
std::string byteStr = hexStr.substr(i, 2);
char byte = static_cast<char>(std::stoi(byteStr, nullptr, 16));
ss << byte;
}
return ss.str();
}
std::string
GetTraceIDAsHexStr(const TraceContext* ctx) {
if (ctx != nullptr && !EmptyTraceID(ctx)) {
return std::vector<uint8_t>(
ctx->traceID, ctx->traceID + opentelemetry::trace::TraceId::kSize);
return BytesToHexStr(ctx->traceID,
opentelemetry::trace::TraceId::kSize);
} else {
return {};
return std::string();
}
}
std::vector<uint8_t>
GetSpanIDAsVector(const TraceContext* ctx) {
std::string
GetSpanIDAsHexStr(const TraceContext* ctx) {
if (ctx != nullptr && !EmptySpanID(ctx)) {
return std::vector<uint8_t>(
ctx->spanID, ctx->spanID + opentelemetry::trace::SpanId::kSize);
return BytesToHexStr(ctx->spanID, opentelemetry::trace::SpanId::kSize);
} else {
return {};
return std::string();
}
}

View File

@ -62,11 +62,17 @@ EmptyTraceID(const TraceContext* ctx);
bool
EmptySpanID(const TraceContext* ctx);
std::vector<uint8_t>
GetTraceIDAsVector(const TraceContext* ctx);
std::string
BytesToHexStr(const uint8_t* data, const size_t len);
std::vector<uint8_t>
GetSpanIDAsVector(const TraceContext* ctx);
std::string
GetIDFromHexStr(const std::string& hexStr);
std::string
GetTraceIDAsHexStr(const TraceContext* ctx);
std::string
GetSpanIDAsHexStr(const TraceContext* ctx);
struct AutoSpan {
explicit AutoSpan(const std::string& name,

View File

@ -126,9 +126,9 @@ class VectorIndex : public IndexBase {
if (search_info.trace_ctx_.traceID != nullptr &&
search_info.trace_ctx_.spanID != nullptr) {
search_cfg[knowhere::meta::TRACE_ID] =
tracer::GetTraceIDAsVector(&search_info.trace_ctx_);
tracer::GetTraceIDAsHexStr(&search_info.trace_ctx_);
search_cfg[knowhere::meta::SPAN_ID] =
tracer::GetSpanIDAsVector(&search_info.trace_ctx_);
tracer::GetSpanIDAsHexStr(&search_info.trace_ctx_);
search_cfg[knowhere::meta::TRACE_FLAGS] =
search_info.trace_ctx_.traceFlags;
}

View File

@ -52,9 +52,9 @@ PrepareBFSearchParams(const SearchInfo& search_info) {
if (search_info.trace_ctx_.traceID != nullptr &&
search_info.trace_ctx_.spanID != nullptr) {
search_cfg[knowhere::meta::TRACE_ID] =
tracer::GetTraceIDAsVector(&search_info.trace_ctx_);
tracer::GetTraceIDAsHexStr(&search_info.trace_ctx_);
search_cfg[knowhere::meta::SPAN_ID] =
tracer::GetSpanIDAsVector(&search_info.trace_ctx_);
tracer::GetSpanIDAsHexStr(&search_info.trace_ctx_);
search_cfg[knowhere::meta::TRACE_FLAGS] =
search_info.trace_ctx_.traceFlags;
}

View File

@ -13,7 +13,7 @@
# Update KNOWHERE_VERSION for the first occurrence
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES "")
set( KNOWHERE_VERSION 685bbf31 )
set( KNOWHERE_VERSION c91e59b2 )
set( GIT_REPOSITORY "https://github.com/zilliztech/knowhere.git")
message(STATUS "Knowhere repo: ${GIT_REPOSITORY}")
message(STATUS "Knowhere version: ${KNOWHERE_VERSION}")

View File

@ -16,7 +16,6 @@
#include "common/Tracer.h"
#include "common/EasyAssert.h"
#include "common/Tracer.h"
#include "knowhere/comp/index_param.h"
#include "knowhere/config.h"
@ -47,74 +46,72 @@ TEST(Tracer, Span) {
config->nodeID = 1;
initTelemetry(*config);
const auto trace_id_vec = std::vector<uint8_t>({0x01,
0x23,
0x45,
0x67,
0x89,
0xab,
0xcd,
0xef,
0xfe,
0xdc,
0xba,
0x98,
0x76,
0x54,
0x32,
0x10});
const auto span_id_vec =
std::vector<uint8_t>({0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef});
auto ctx = std::make_shared<TraceContext>();
ctx->traceID = trace_id_vec.data();
ctx->spanID = span_id_vec.data();
ctx->traceID = new uint8_t[16]{0x01,
0x23,
0x45,
0x67,
0x89,
0xab,
0xcd,
0xef,
0xfe,
0xdc,
0xba,
0x98,
0x76,
0x54,
0x32,
0x10};
ctx->spanID =
new uint8_t[8]{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
ctx->traceFlags = 1;
auto span = StartSpan("test", ctx.get());
ASSERT_TRUE(span->GetContext().trace_id() ==
trace::TraceId({ctx->traceID, 16}));
delete[] ctx->traceID;
delete[] ctx->spanID;
}
TEST(Tracer, Config) {
const auto trace_id_vec = std::vector<uint8_t>({0x01,
0x23,
0x45,
0x67,
0x89,
0xab,
0xcd,
0xef,
0xfe,
0xdc,
0xba,
0x98,
0x76,
0x54,
0x32,
0x10});
const auto span_id_vec =
std::vector<uint8_t>({0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef});
TEST(Tracer, Hex) {
auto ctx = std::make_shared<TraceContext>();
ctx->traceID = trace_id_vec.data();
ctx->spanID = span_id_vec.data();
ctx->traceID = new uint8_t[16]{0x01,
0x23,
0x45,
0x67,
0x89,
0xab,
0xcd,
0xef,
0xfe,
0xdc,
0xba,
0x98,
0x76,
0x54,
0x32,
0x10};
ctx->spanID =
new uint8_t[8]{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
ctx->traceFlags = 1;
knowhere::Json search_cfg = {};
// save trace context into search conf
search_cfg[knowhere::meta::TRACE_ID] =
tracer::GetTraceIDAsVector(ctx.get());
search_cfg[knowhere::meta::SPAN_ID] = tracer::GetSpanIDAsVector(ctx.get());
tracer::GetTraceIDAsHexStr(ctx.get());
search_cfg[knowhere::meta::SPAN_ID] = tracer::GetSpanIDAsHexStr(ctx.get());
search_cfg[knowhere::meta::TRACE_FLAGS] = ctx->traceFlags;
std::cout << "search config: " << search_cfg.dump() << std::endl;
auto trace_id_cfg =
search_cfg[knowhere::meta::TRACE_ID].get<std::vector<uint8_t>>();
auto span_id_cfg =
search_cfg[knowhere::meta::SPAN_ID].get<std::vector<uint8_t>>();
auto trace_id_str = GetIDFromHexStr(search_cfg[knowhere::meta::TRACE_ID]);
auto span_id_str = GetIDFromHexStr(search_cfg[knowhere::meta::SPAN_ID]);
ASSERT_TRUE(memcmp(ctx->traceID, trace_id_cfg.data(), 16) == 0);
ASSERT_TRUE(memcmp(ctx->spanID, span_id_cfg.data(), 8) == 0);
ASSERT_TRUE(strncmp((char*)ctx->traceID, trace_id_str.c_str(), 16) == 0);
ASSERT_TRUE(strncmp((char*)ctx->spanID, span_id_str.c_str(), 8) == 0);
delete[] ctx->traceID;
delete[] ctx->spanID;
}