clickhouse-java icon indicating copy to clipboard operation
clickhouse-java copied to clipboard

[client, jdbc] Auto-generate Query ID if absent

Open chernser opened this issue 1 month ago • 2 comments

Description

Client and JDBC driver should generate Query ID if it is not set.

chernser avatar Nov 04 '25 06:11 chernser

The main issue here is that API do not provide a good way to access queryId after execution. Also generating query may be useful while tracing - so we need to know it before sending the request.

chernser avatar Nov 17 '25 21:11 chernser

InsertResponse and QueryResponse classes have #getQueryId() method. However not every operation returns it. In case of failure it should be propagated to the logging.

Client does retries. Should queryId be generated each time?

chernser avatar Dec 01 '25 19:12 chernser

Hey! I just came across this issue as I was wondering why the queryId is not set as an attribute by the ClickHouse OTel Java Instrumentation. Is the instrumentation maintained by the same people as this repo?

Either way, if this is desired behaviour I would be willing to contribute.

patschl avatar Dec 18 '25 11:12 patschl

Good day, @patschl ! The query ID is generated on server is missing in the request settings. This issue is more about adding query ID whenever it is useful while troubleshooting. For example, add MDC context so logger can use it. Or add it to error messages we produce. I have a PR for that.

chernser avatar Dec 18 '25 15:12 chernser

Thanks for the fast response @chernser .

I guess this would make the instrumentation quite inconsistent if the query ID is not yet generated on the client side, but would anything else be a reason not to add this to the instrumentation?

So I should rather open a ticket/ask over at otel java instrumentation?

patschl avatar Dec 18 '25 17:12 patschl

If query reached server then we will get query id in header and will provide in response object. Else query id doesn't make sense because it is not present on server. In this case good client exception should be thrown. However some API do not return response object and it is a problem.

You may use this issue to add requirements.

And sorry, I think, I did not get this part of the question :

but would anything else be a reason not to add this to the instrumentation?

Would you please tell more about what instrumentation are you looking for?

Thanks!

chernser avatar Dec 18 '25 17:12 chernser

Sorry if my earlier explanation wasn’t clear enough.

I was talking about the OpenTelemetry Java instrumentation for clickhouse, which already exists here. It currently only captures basic attributes like database name, connection URL and the statement, but nothing clickhouse specific (like query ID).

I might have been wrong in the assumption that this was instrumentation was driven by the maintainers of this repository.

patschl avatar Dec 19 '25 09:12 patschl

no worries, @patschl !

Thank you for pointing out - I was not aware of this. I will review what the project does and see if I can help here. Here is a separate issue https://github.com/ClickHouse/clickhouse-java/issues/2700

chernser avatar Dec 19 '25 15:12 chernser

Nice. I am also willing to contribute if you can't find the time.

Sorry, one last question; I looked at the open PR for this issue. Do you plan to provide an implementation for the queryIdGenerator when using the JDBC Driver instead of the client?

patschl avatar Dec 20 '25 11:12 patschl