From b4e62e06e7f2b493f800f9a022f9308214261c93 Mon Sep 17 00:00:00 2001 From: Yatsishin Ilya <2159081+qoega@users.noreply.github.com> Date: Mon, 4 Jul 2022 14:07:29 +0000 Subject: [PATCH] Use native Map --- src/Interpreters/OpenTelemetrySpanLog.cpp | 25 +++---------- src/Interpreters/OpenTelemetrySpanLog.h | 3 +- src/Interpreters/ThreadStatusExt.cpp | 3 +- src/Interpreters/executeQuery.cpp | 45 +++++++---------------- 4 files changed, 21 insertions(+), 55 deletions(-) diff --git a/src/Interpreters/OpenTelemetrySpanLog.cpp b/src/Interpreters/OpenTelemetrySpanLog.cpp index e1199fa03b1..79000aab375 100644 --- a/src/Interpreters/OpenTelemetrySpanLog.cpp +++ b/src/Interpreters/OpenTelemetrySpanLog.cpp @@ -12,6 +12,7 @@ #include #include +#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, diff --git a/src/Interpreters/OpenTelemetrySpanLog.h b/src/Interpreters/OpenTelemetrySpanLog.h index 677a283bb56..34f4765c8c4 100644 --- a/src/Interpreters/OpenTelemetrySpanLog.h +++ b/src/Interpreters/OpenTelemetrySpanLog.h @@ -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. }; diff --git a/src/Interpreters/ThreadStatusExt.cpp b/src/Interpreters/ThreadStatusExt.cpp index 42db91f47c0..53d7fd0457a 100644 --- a/src/Interpreters/ThreadStatusExt.cpp +++ b/src/Interpreters/ThreadStatusExt.cpp @@ -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::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); } diff --git a/src/Interpreters/executeQuery.cpp b/src/Interpreters/executeQuery.cpp index 4b328f0466e..33ceef4ad7c 100644 --- a/src/Interpreters/executeQuery.cpp +++ b/src/Interpreters/executeQuery.cpp @@ -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 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);