|
| 1 | +#include <clickhouse/base/string_utils.h> |
1 | 2 | #include <clickhouse/client.h> |
2 | 3 |
|
3 | 4 | #include "readonly_client_test.h" |
@@ -43,6 +44,30 @@ class ClientCase : public testing::TestWithParam<ClientOptions> { |
43 | 44 | return "SELECT " + column_name + " FROM " + table_name; |
44 | 45 | } |
45 | 46 |
|
| 47 | + void FlushLogs() { |
| 48 | + try { |
| 49 | + client_->Execute("SYSTEM FLUSH LOGS"); |
| 50 | + } catch (const std::exception & e) { |
| 51 | + std::cerr << "Got error while flushing logs: " << e.what() << std::endl; |
| 52 | + const auto wait_for_flush = []() { |
| 53 | + // Insufficient privileges, the only safe way is to wait long enough for system |
| 54 | + // to flush the logs automaticaly. Usualy it takes 7.5 seconds, so just in case, |
| 55 | + // wait 3 times that to ensure that all previously executed queries are in the logs now. |
| 56 | + const auto wait_duration = std::chrono::seconds(23); |
| 57 | + std::cerr << "Now we wait " << wait_duration << "..." << std::endl; |
| 58 | + std::this_thread::sleep_for(wait_duration); |
| 59 | + }; |
| 60 | + // DB::Exception: clickhouse_cpp_cicd: Not enough privileges. To execute this query it's necessary to have grant SYSTEM FLUSH LOGS ON |
| 61 | + if (std::string(e.what()).find("To execute this query it's necessary to have grant SYSTEM FLUSH LOGS ON") != std::string::npos) { |
| 62 | + wait_for_flush(); |
| 63 | + } |
| 64 | + // DB::Exception: clickhouse_cpp_cicd: Cannot execute query in readonly mode |
| 65 | + if (std::string(e.what()).find("Cannot execute query in readonly mode") != std::string::npos) { |
| 66 | + wait_for_flush(); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
46 | 71 | std::unique_ptr<Client> client_; |
47 | 72 | const std::string table_name = "test_clickhouse_cpp_test_ut_table"; |
48 | 73 | const std::string column_name = "test_column"; |
@@ -850,19 +875,7 @@ TEST_P(ClientCase, Query_ID) { |
850 | 875 | client_->SelectCancelable("SELECT 'b', count(*) FROM " + table_name, query_id, [](const Block &) { return true; }); |
851 | 876 | client_->Execute(Query("TRUNCATE TABLE " + table_name, query_id)); |
852 | 877 |
|
853 | | - try { |
854 | | - client_->Execute("SYSTEM FLUSH LOGS"); |
855 | | - } catch (const std::exception & e) { |
856 | | - // DB::Exception: clickhouse_cpp_cicd: Not enough privileges. To execute this query it's necessary to have grant SYSTEM FLUSH LOGS ON |
857 | | - if (std::string(e.what()).find("To execute this query it's necessary to have grant SYSTEM FLUSH LOGS ON") != std::string::npos) { |
858 | | - // Insufficient privileges, the only safe way is to wait long enough for system |
859 | | - // to flush the logs automaticaly. Usualy it takes 7.5 seconds, so just in case, |
860 | | - // wait 3 times that to ensure that all previously executed queries are in the logs now. |
861 | | - const auto wait_duration = std::chrono::seconds(23); |
862 | | - std::cerr << "Got error while flushing logs, now we wait " << wait_duration << "..." << std::endl; |
863 | | - std::this_thread::sleep_for(wait_duration); |
864 | | - } |
865 | | - } |
| 878 | + FlushLogs(); |
866 | 879 |
|
867 | 880 | size_t total_count = 0; |
868 | 881 | client_->Select("SELECT type, query_kind, query_id, query " |
@@ -1115,6 +1128,31 @@ TEST_P(ClientCase, ServerLogs) { |
1115 | 1128 | } |
1116 | 1129 |
|
1117 | 1130 |
|
| 1131 | +TEST_P(ClientCase, TracingContext) { |
| 1132 | + Block block; |
| 1133 | + createTableWithOneColumn<ColumnString>(block); |
| 1134 | + |
| 1135 | + Query query("INSERT INTO " + table_name + " (*) VALUES (\'Foo\'), (\'Bar\')" ); |
| 1136 | + open_telemetry::TracingContext tracing_context; |
| 1137 | + std::srand(std::time(0)); |
| 1138 | + tracing_context.trace_id = {std::rand(), std::rand()}; |
| 1139 | + query.SetTracingContext(tracing_context); |
| 1140 | + client_->Execute(query); |
| 1141 | + |
| 1142 | + FlushLogs(); |
| 1143 | + |
| 1144 | + size_t received_rows = 0; |
| 1145 | + client_->Select("SELECT trace_id, toString(trace_id), operation_name " |
| 1146 | + "FROM system.opentelemetry_span_log " |
| 1147 | + "WHERE trace_id = toUUID(\'" + ToString(tracing_context.trace_id) + "\');", |
| 1148 | + [&](const Block& block) { |
| 1149 | + // std::cerr << PrettyPrintBlock{block} << std::endl; |
| 1150 | + received_rows += block.GetRowCount(); |
| 1151 | + }); |
| 1152 | + |
| 1153 | + EXPECT_GT(received_rows, 0u); |
| 1154 | +} |
| 1155 | + |
1118 | 1156 | const auto LocalHostEndpoint = ClientOptions() |
1119 | 1157 | .SetHost( getEnvOrDefault("CLICKHOUSE_HOST", "localhost")) |
1120 | 1158 | .SetPort( getEnvOrDefault<size_t>("CLICKHOUSE_PORT", "9000")) |
|
0 commit comments