Use native Map

This commit is contained in:
Yatsishin Ilya 2022-07-04 14:07:29 +00:00
parent 202008e80b
commit b4e62e06e7
4 changed files with 21 additions and 55 deletions

View File

@ -12,6 +12,7 @@
#include <Common/hex.h>
#include <Common/CurrentThread.h>
#include "Core/Field.h"
namespace DB
@ -64,13 +65,7 @@ void OpenTelemetrySpanLogElement::appendToBlock(MutableColumns & columns) const
// The user might add some ints values, and we will have Int Field, and the
// insert will fail because the column requires Strings. Convert the fields
// here, because it's hard to remember to convert them in all other places.
Map map(attribute_names.size());
for (size_t attr_idx = 0; attr_idx < map.size(); ++attr_idx)
{
map[attr_idx] = Tuple{attribute_names[attr_idx], toString(attribute_values[attr_idx])};
}
columns[i++]->insert(map);
columns[i++]->insert(attributes);
}
@ -158,9 +153,7 @@ void OpenTelemetrySpanHolder::addAttribute(const std::string& name, UInt64 value
if (trace_id == UUID())
return;
this->attribute_names.push_back(name);
this->attribute_values.push_back(std::to_string(value));
assert(this->attribute_names.size() == this->attribute_values.size());
this->attributes.push_back(Tuple{name, toString(value)});
}
void OpenTelemetrySpanHolder::addAttribute(const std::string& name, const std::string& value)
@ -168,9 +161,7 @@ void OpenTelemetrySpanHolder::addAttribute(const std::string& name, const std::s
if (trace_id == UUID())
return;
this->attribute_names.push_back(name);
this->attribute_values.push_back(value);
assert(this->attribute_names.size() == this->attribute_values.size());
this->attributes.push_back(Tuple{name, value});
}
void OpenTelemetrySpanHolder::addAttribute(const Exception & e)
@ -178,9 +169,7 @@ void OpenTelemetrySpanHolder::addAttribute(const Exception & e)
if (trace_id == UUID())
return;
this->attribute_names.push_back("clickhouse.exception");
this->attribute_values.push_back(getExceptionMessage(e, false));
assert(this->attribute_names.size() == this->attribute_values.size());
this->attributes.push_back(Tuple{"clickhouse.exception", getExceptionMessage(e, false)});
}
void OpenTelemetrySpanHolder::addAttribute(std::exception_ptr e)
@ -188,9 +177,7 @@ void OpenTelemetrySpanHolder::addAttribute(std::exception_ptr e)
if (trace_id == UUID() || e == nullptr)
return;
this->attribute_names.push_back("clickhouse.exception");
this->attribute_values.push_back(getExceptionMessage(e, false));
assert(this->attribute_names.size() == this->attribute_values.size());
this->attributes.push_back(Tuple{"clickhouse.exception", getExceptionMessage(e, false)});
}
bool OpenTelemetryTraceContext::parseTraceparentHeader(const std::string & traceparent,

View File

@ -15,8 +15,7 @@ struct OpenTelemetrySpan
std::string operation_name;
UInt64 start_time_us;
UInt64 finish_time_us;
Array attribute_names;
Array attribute_values;
Map attributes;
// I don't understand how Links work, namely, which direction should they
// point to, and how they are related with parent_span_id, so no Links for now.
};

View File

@ -384,8 +384,7 @@ void ThreadStatus::detachQuery(bool exit_if_already_detached, bool thread_exits)
span.finish_time_us =
std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
span.attribute_names.push_back("clickhouse.thread_id");
span.attribute_values.push_back(thread_id);
span.attributes.push_back(Tuple{"clickhouse.thread_id", toString(thread_id)});
opentelemetry_span_log->add(span);
}

View File

@ -301,28 +301,16 @@ static void onExceptionBeforeStart(const String & query_for_logging, ContextPtr
span.operation_name = "query";
span.start_time_us = current_time_us;
span.finish_time_us = time_in_microseconds(std::chrono::system_clock::now());
/// Keep values synchronized to type enum in QueryLogElement::createBlock.
span.attribute_names.push_back("clickhouse.query_status");
span.attribute_values.push_back("ExceptionBeforeStart");
span.attribute_names.push_back("db.statement");
span.attribute_values.push_back(elem.query);
span.attribute_names.push_back("clickhouse.query_id");
span.attribute_values.push_back(elem.client_info.current_query_id);
span.attribute_names.push_back("clickhouse.exception");
span.attribute_values.push_back(elem.exception);
span.attribute_names.push_back("clickhouse.exception_code");
span.attribute_values.push_back(elem.exception_code);
span.attributes.reserve(7);
span.attributes.push_back(Tuple{"clickhouse.query_status", "ExceptionBeforeStart"});
span.attributes.push_back(Tuple{"db.statement", "elem.query"});
span.attributes.push_back(Tuple{"clickhouse.query_id", "elem.client_info.current_query_id"});
span.attributes.push_back(Tuple{"clickhouse.exception", "elem.exception"});
span.attributes.push_back(Tuple{"clickhouse.exception_code", "elem.exception_code"});
span.attributes.push_back(Tuple{"clickhouse.query_status", "ExceptionBeforeStart"});
if (!context->query_trace_context.tracestate.empty())
{
span.attribute_names.push_back("clickhouse.tracestate");
span.attribute_values.push_back(
context->query_trace_context.tracestate);
span.attributes.push_back(Tuple{"clickhouse.tracestate", context->query_trace_context.tracestate});
}
opentelemetry_span_log->add(span);
@ -956,20 +944,13 @@ static std::tuple<ASTPtr, BlockIO> executeQueryImpl(
span.start_time_us = elem.query_start_time_microseconds;
span.finish_time_us = time_in_microseconds(finish_time);
/// Keep values synchronized to type enum in QueryLogElement::createBlock.
span.attribute_names.push_back("clickhouse.query_status");
span.attribute_values.push_back("QueryFinish");
span.attribute_names.push_back("db.statement");
span.attribute_values.push_back(elem.query);
span.attribute_names.push_back("clickhouse.query_id");
span.attribute_values.push_back(elem.client_info.current_query_id);
span.attributes.reserve(4);
span.attributes.push_back(Tuple{"clickhouse.query_status", "QueryFinish"});
span.attributes.push_back(Tuple{"db.statement", elem.query});
span.attributes.push_back(Tuple{"clickhouse.query_id", elem.client_info.current_query_id});
if (!context->query_trace_context.tracestate.empty())
{
span.attribute_names.push_back("clickhouse.tracestate");
span.attribute_values.push_back(
context->query_trace_context.tracestate);
span.attributes.push_back(Tuple{"clickhouse.tracestate", context->query_trace_context.tracestate});
}
opentelemetry_span_log->add(span);