Start tracing at HTTP entry

This commit is contained in:
Frank Chen 2022-07-07 17:41:27 +08:00
parent 2e8c530bed
commit 0a44a44feb

View File

@ -533,20 +533,7 @@ void HTTPHandler::processQuery(
session->makeSessionContext(session_id, session_timeout, session_check == "1"); session->makeSessionContext(session_id, session_timeout, session_check == "1");
} }
// Parse the OpenTelemetry traceparent header. auto context = session->makeQueryContext(session->getClientInfo());
ClientInfo client_info = session->getClientInfo();
if (request.has("traceparent"))
{
std::string opentelemetry_traceparent = request.get("traceparent");
std::string error;
if (!client_info.client_trace_context.parseTraceparentHeader(opentelemetry_traceparent, error))
{
LOG_DEBUG(log, "Failed to parse OpenTelemetry traceparent header '{}': {}", opentelemetry_traceparent, error);
}
client_info.client_trace_context.tracestate = request.get("tracestate", "");
}
auto context = session->makeQueryContext(std::move(client_info));
/// The client can pass a HTTP header indicating supported compression method (gzip or deflate). /// The client can pass a HTTP header indicating supported compression method (gzip or deflate).
String http_response_compression_methods = request.get("Accept-Encoding", ""); String http_response_compression_methods = request.get("Accept-Encoding", "");
@ -944,6 +931,7 @@ void HTTPHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse
/// In case of exception, send stack trace to client. /// In case of exception, send stack trace to client.
bool with_stacktrace = false; bool with_stacktrace = false;
OpenTelemetryThreadTraceContextScopePtr thread_trace_context;
try try
{ {
if (request.getMethod() == HTTPServerRequest::HTTP_OPTIONS) if (request.getMethod() == HTTPServerRequest::HTTP_OPTIONS)
@ -951,6 +939,26 @@ void HTTPHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse
processOptionsRequest(response, server.config()); processOptionsRequest(response, server.config());
return; return;
} }
// Parse the OpenTelemetry traceparent header.
ClientInfo& client_info = session->getClientInfo();
if (request.has("traceparent"))
{
std::string opentelemetry_traceparent = request.get("traceparent");
std::string error;
if (!client_info.client_trace_context.parseTraceparentHeader(opentelemetry_traceparent, error))
{
LOG_DEBUG(log, "Failed to parse OpenTelemetry traceparent header '{}': {}", opentelemetry_traceparent, error);
}
client_info.client_trace_context.tracestate = request.get("tracestate", "");
}
thread_trace_context = request_context->startTracing("HTTPHandler::handleRequest()");
if (thread_trace_context)
{
thread_trace_context->root_span.addAttribute("clickhouse.uri", request.getURI());
}
response.setContentType("text/plain; charset=UTF-8"); response.setContentType("text/plain; charset=UTF-8");
response.set("X-ClickHouse-Server-Display-Name", server_display_name); response.set("X-ClickHouse-Server-Display-Name", server_display_name);
/// For keep-alive to work. /// For keep-alive to work.
@ -1001,6 +1009,11 @@ void HTTPHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse
trySendExceptionToClient(exception_message, exception_code, request, response, used_output); trySendExceptionToClient(exception_message, exception_code, request, response, used_output);
} }
if (thread_trace_context)
{
thread_trace_context->root_span.addAttribute("clickhouse.http_status", response.getStatus());
}
used_output.finalize(); used_output.finalize();
} }