From 2ae43bb4e8a8890629f36a0bbc3d5a0229f463ab Mon Sep 17 00:00:00 2001 From: Frank Chen Date: Mon, 19 Sep 2022 11:11:27 +0800 Subject: [PATCH] Add test case Signed-off-by: Frank Chen --- src/Common/OpenTelemetryTraceContext.cpp | 12 +-- .../02423_ddl_for_opentelemetry.reference | 8 ++ .../02423_ddl_for_opentelemetry.sh | 92 +++++++++++++++++++ 3 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 tests/queries/0_stateless/02423_ddl_for_opentelemetry.reference create mode 100755 tests/queries/0_stateless/02423_ddl_for_opentelemetry.sh diff --git a/src/Common/OpenTelemetryTraceContext.cpp b/src/Common/OpenTelemetryTraceContext.cpp index 314118201bf..0a64900db9b 100644 --- a/src/Common/OpenTelemetryTraceContext.cpp +++ b/src/Common/OpenTelemetryTraceContext.cpp @@ -232,11 +232,11 @@ void TracingContext::deserialize(ReadBuffer & buf) { buf >> "tracing: " >> this->trace_id - >> " " + >> "\n" >> this->span_id - >> " " + >> "\n" >> this->tracestate - >> " " + >> "\n" >> this->trace_flags >> "\n"; } @@ -246,11 +246,11 @@ void TracingContext::serialize(WriteBuffer & buf) const { buf << "tracing: " << this->trace_id - << " " + << "\n" << this->span_id - << " " + << "\n" << this->tracestate - << " " + << "\n" << this->trace_flags << "\n"; } diff --git a/tests/queries/0_stateless/02423_ddl_for_opentelemetry.reference b/tests/queries/0_stateless/02423_ddl_for_opentelemetry.reference new file mode 100644 index 00000000000..19b2fe09a20 --- /dev/null +++ b/tests/queries/0_stateless/02423_ddl_for_opentelemetry.reference @@ -0,0 +1,8 @@ +1 +1 +2 +===case 2==== +1 +1 +exception_code=60 +exception_code=60 diff --git a/tests/queries/0_stateless/02423_ddl_for_opentelemetry.sh b/tests/queries/0_stateless/02423_ddl_for_opentelemetry.sh new file mode 100755 index 00000000000..272eaf4e345 --- /dev/null +++ b/tests/queries/0_stateless/02423_ddl_for_opentelemetry.sh @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +# Tags: distributed + +CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CURDIR"/../shell_config.sh + +# This function takes following arguments: +# $1 - OpenTelemetry Trace Id +# $2 - Query +# $3 - Query Settings +# $4 - Output device, default is stdout +function execute_query() +{ + if [ -n "${4}" ]; then + output=$4 + else + output="/dev/stdout" + fi + + echo $2 | ${CLICKHOUSE_CURL} \ + -X POST \ + -H "traceparent: 00-$1-5150000000000515-01" \ + -H "tracestate: a\nb cd" \ + "${CLICKHOUSE_URL}?${3}" \ + --data @- \ + > $output +} + +# This function takes 3 argument: +# $1 - OpenTelemetry Trace Id +# $2 - Fields +# $3 - operation_name pattern +function check_span() +{ +${CLICKHOUSE_CLIENT} -nq " + SYSTEM FLUSH LOGS; + + SELECT ${2} + FROM system.opentelemetry_span_log + WHERE finish_date >= yesterday() + AND lower(hex(trace_id)) = '${1}' + AND operation_name like '${3}' + ;" +} + +# +# Set up +# +${CLICKHOUSE_CLIENT} -q " +DROP TABLE IF EXISTS ddl_test_for_opentelemetry; +" + +# +# Case 1, a normal case +# +trace_id=$(${CLICKHOUSE_CLIENT} -q "select lower(hex(generateUUIDv4()))"); +execute_query $trace_id "CREATE TABLE ddl_test_for_opentelemetry ON CLUSTER test_shard_localhost (id UInt64) Engine=MergeTree ORDER BY id" "distributed_ddl_output_mode=none" + +check_span $trace_id "count()" "HTTPHandler" +check_span $trace_id "count()" "%DDLWorker::processTask%" + +# There should be two 'query' spans, +# one is for the HTTPHandler, the other is for the DDL executing in DDLWorker +check_span $trace_id "count()" "query" + +# Echo a separator so that the reference file is more clear for reading +echo "===case 2====" + +# +# Case 2, an exceptional case, DROP a non-exist table +# +trace_id=$(${CLICKHOUSE_CLIENT} -q "select lower(hex(generateUUIDv4()))"); + +# Since this query is supposed to fail, we redirect the error message to /dev/null to discard the error message so that it won't pollute the reference file. +# The exception will be checked in the span log +execute_query $trace_id "DROP TABLE ddl_test_for_opentelemetry_non_exist ON CLUSTER test_shard_localhost" "distributed_ddl_output_mode=none" "/dev/null" + +check_span $trace_id "count()" "HTTPHandler" +check_span $trace_id "count()" "%DDLWorker::processTask%" + +# There should be two 'query' spans, +# one is for the HTTPHandler, the other is for the DDL executing in DDLWorker. +# Both of these two spans contain exception +check_span $trace_id "concat('exception_code=', attribute['clickhouse.exception_code'])" "query" + +# +# Tear down +# +${CLICKHOUSE_CLIENT} -q " +DROP TABLE IF EXISTS ddl_test_for_opentelemetry; +" \ No newline at end of file