Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1167,10 +1167,10 @@ public Builder setClientName(String clientName) {
* Sets the client name and the client metadata, which identify the application to the server. See
* {@link #setClientName(String)} for the client name. The client adds the metadata to the comment group of the
* {@code User-Agent} header, in the iteration order of {@code metadata}, and then its own {@code lv},
* {@code os} and {@code hc} (HTTP client) entries if the metadata does not set these keys. For example, the
* client name {@code my-app/1.0} and the metadata {@code fv=flink/2.0.0} and {@code api=table} give
* {@code my-app/1.0 clickhouse-java-v2/<version> (fv:flink/2.0.0; api:table; lv:java/<java.version>;
* os:<os.name>; hc:ahc_<Apache HttpClient version>)}.
* {@code os}, {@code arch} (CPU architecture) and {@code hc} (HTTP client) entries if the metadata does not set
* these keys. For example, the client name {@code my-app/1.0} and the metadata {@code fv=flink/2.0.0} and
* {@code api=table} give {@code my-app/1.0 clickhouse-java-v2/<version> (fv:flink/2.0.0; api:table;
* lv:java/<java.version>; os:<os.name>; arch:<os.arch>; hc:ahc_<Apache HttpClient version>)}.
* <p>
* Keys and values are trimmed, and entries with a {@code null} or empty key or value are ignored. Keys and
* values must be printable ASCII, a key must not contain spaces or any of {@code :;(),=}, and a value must not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
* Builds the identification of the client and of the application that uses it. The identification is the value of
* the {@code User-Agent} header, in the standard ClickHouse client format:
* {@code <application> clickhouse-java-v2/<version> (<key>:<value>; ...; lv:java/<java.version>; os:<os.name>;
* hc:ahc_<Apache HttpClient version>)}.
* arch:<os.arch>; hc:ahc_<Apache HttpClient version>)}.
* <p>
* The application part is the client name ({@link ClientConfigProperties#CLIENT_NAME}), or the custom
* {@code User-Agent} header if the application set one. The key/value pairs are the client metadata
* ({@link ClientConfigProperties#CLIENT_ID_METADATA}). The {@code hc} entry is the HTTP client that sends the requests:
* {@code ahc_} (Apache HttpClient) and its version.
* ({@link ClientConfigProperties#CLIENT_ID_METADATA}). The {@code arch} entry is the CPU architecture that the JVM
* reports. The {@code hc} entry is the HTTP client that sends the requests: {@code ahc_} (Apache HttpClient) and its
* version.
* <p>
* The client metadata is validated when {@link Client.Builder#build()} parses the configuration. {@link Client}
* creates one instance and updates it when the client name changes. The instance composes the value for the client
Expand All @@ -35,6 +36,8 @@ public final class WorkloadIdentitySupport {

private static final String METADATA_KEY_OS = "os";

private static final String METADATA_KEY_ARCHITECTURE = "arch";

private static final String METADATA_KEY_HTTP_CLIENT = "hc";

/**
Expand Down Expand Up @@ -147,8 +150,8 @@ private static boolean isClientMetadataText(String str, String forbiddenChars) {

/**
* Composes the client part: {@code clickhouse-java-v2/<version> (<key>:<value>; ...)}. The key/value pairs are the
* client metadata, if it is set, then {@code lv:java/<java.version>}, {@code os:<os.name>} and
* {@code hc:ahc_<Apache HttpClient version>} if the client metadata does not set these keys (ignoring case). The
* client metadata, if it is set, then {@code lv:java/<java.version>}, {@code os:<os.name>}, {@code arch:<os.arch>}
* and {@code hc:ahc_<Apache HttpClient version>} if the client metadata does not set these keys (ignoring case). The
* client sends requests over HTTP with Apache HttpClient ({@code ahc}).
*/
private static String composeClientPart(Map<String, Object> configuration) {
Expand All @@ -157,6 +160,7 @@ private static String composeClientPart(Map<String, Object> configuration) {
ClientUtils.putIfAbsentIgnoreCase(metadata, METADATA_KEY_LANGUAGE_VERSION,
"java/" + System.getProperty("java.version"));
ClientUtils.putIfAbsentIgnoreCase(metadata, METADATA_KEY_OS, System.getProperty("os.name"));
ClientUtils.putIfAbsentIgnoreCase(metadata, METADATA_KEY_ARCHITECTURE, System.getProperty("os.arch"));
ClientUtils.putIfAbsentIgnoreCase(metadata, METADATA_KEY_HTTP_CLIENT, "ahc_" + Versions.APACHE_HTTP_CLIENT);

StringJoiner comment = new StringJoiner("; ", Client.CLIENT_USER_AGENT + Client.clientVersion + " (", ")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1271,9 +1271,9 @@ public void testUserAgentHasCompleteProductName(String clientName, Pattern userA
@DataProvider(name = "testUserAgentHasCompleteProductName_dataProvider")
public static Object[][] testUserAgentHasCompleteProductName_dataProvider() {
return new Object[][] {
{ "", Pattern.compile("clickhouse-java-v2/[^ ]+ \\(lv:java/[^;]+; os:[^;()]+; hc:ahc_[\\d.]+\\)") },
{ "test-client/1.0", Pattern.compile("test-client/1.0 clickhouse-java-v2/[^ ]+ \\(lv:java/[^;]+; os:[^;()]+; hc:ahc_[\\d.]+\\)")},
{ "test-client/", Pattern.compile("test-client/ clickhouse-java-v2/[^ ]+ \\(lv:java/[^;]+; os:[^;()]+; hc:ahc_[\\d.]+\\)")}};
{ "", Pattern.compile("clickhouse-java-v2/[^ ]+ \\(lv:java/[^;]+; os:[^;()]+; arch:[^;()]+; hc:ahc_[\\d.]+\\)") },
{ "test-client/1.0", Pattern.compile("test-client/1.0 clickhouse-java-v2/[^ ]+ \\(lv:java/[^;]+; os:[^;()]+; arch:[^;()]+; hc:ahc_[\\d.]+\\)")},
{ "test-client/", Pattern.compile("test-client/ clickhouse-java-v2/[^ ]+ \\(lv:java/[^;]+; os:[^;()]+; arch:[^;()]+; hc:ahc_[\\d.]+\\)")}};
}

@Test(dataProvider = "testClientNameDataProvider")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static Object[][] userAgentData() {
Map<String, String> osMetadata = new LinkedHashMap<>();
osMetadata.put("os", "Android 14");
osMetadata.put("api", "table");
osMetadata.put("arch", "arm64-v8a");

Map<String, String> withIgnoredEntries = new LinkedHashMap<>();
withIgnoredEntries.put("api", "");
Expand Down Expand Up @@ -103,7 +104,8 @@ public static Object[][] userAgentData() {
{(Consumer<Client.Builder>) b -> b.setClientName(" my-app/1.0 "),
userAgent("my-app/1.0", null)},
{(Consumer<Client.Builder>) b -> b.setClientName("my-app/1.0", osMetadata),
"my-app/1.0 clickhouse-java-v2/" + Client.clientVersion + " (os:Android 14; api:table; lv:java/"
"my-app/1.0 clickhouse-java-v2/" + Client.clientVersion
+ " (os:Android 14; api:table; arch:arm64-v8a; lv:java/"
+ System.getProperty("java.version") + "; hc:ahc_" + Versions.APACHE_HTTP_CLIENT
+ ")"},
};
Expand Down Expand Up @@ -165,6 +167,7 @@ private static String userAgent(String application, String metadata) {
+ " (" + (metadata == null ? "" : metadata + "; ")
+ "lv:java/" + System.getProperty("java.version")
+ "; os:" + System.getProperty("os.name")
+ "; arch:" + System.getProperty("os.arch")
+ "; hc:ahc_" + Versions.APACHE_HTTP_CLIENT + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ private static String userAgentHeader(HttpAPIClientHelper helper, String clientN

private static String clientPart() {
return "clickhouse-java-v2/" + Client.clientVersion + " (lv:java/" + System.getProperty("java.version")
+ "; os:" + System.getProperty("os.name") + "; hc:ahc_" + Versions.APACHE_HTTP_CLIENT + ")";
+ "; os:" + System.getProperty("os.name") + "; arch:" + System.getProperty("os.arch") + "; hc:ahc_"
+ Versions.APACHE_HTTP_CLIENT + ")";
}

private static String headerValue(HttpPost req, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class WorkloadIdentitySupportTest {

private static final String OS = "os:" + System.getProperty("os.name");

private static final String ARCHITECTURE = "arch:" + System.getProperty("os.arch");

private static final String HTTP_CLIENT = "hc:ahc_" + Versions.APACHE_HTTP_CLIENT;

@Test(dataProvider = "requestUserAgents")
Expand Down Expand Up @@ -102,12 +104,15 @@ public void testClientMetadataReplacesClientEntries(String metadata, String expe
@DataProvider(name = "clientEntriesMetadata")
public static Object[][] clientEntriesMetadata() {
return new Object[][]{
{"os=Android 14", "os:Android 14; " + LANGUAGE_VERSION + "; " + HTTP_CLIENT},
{"api=table,lv=kotlin/2.0", "api:table; lv:kotlin/2.0; " + OS + "; " + HTTP_CLIENT},
{"hc=okhttp_4.12", "hc:okhttp_4.12; " + LANGUAGE_VERSION + "; " + OS},
{"OS=Android 14,LV=kotlin/2.0", "OS:Android 14; LV:kotlin/2.0; " + HTTP_CLIENT},
{"os=Android 14,hc=okhttp_4.12,api=table,lv=kotlin/2.0",
"os:Android 14; hc:okhttp_4.12; api:table; lv:kotlin/2.0"},
{"os=Android 14", "os:Android 14; " + LANGUAGE_VERSION + "; " + ARCHITECTURE + "; " + HTTP_CLIENT},
{"api=table,lv=kotlin/2.0",
"api:table; lv:kotlin/2.0; " + OS + "; " + ARCHITECTURE + "; " + HTTP_CLIENT},
{"hc=okhttp_4.12", "hc:okhttp_4.12; " + LANGUAGE_VERSION + "; " + OS + "; " + ARCHITECTURE},
{"arch=arm64-v8a", "arch:arm64-v8a; " + LANGUAGE_VERSION + "; " + OS + "; " + HTTP_CLIENT},
{"OS=Android 14,LV=kotlin/2.0,ARCH=arm64-v8a",
"OS:Android 14; LV:kotlin/2.0; ARCH:arm64-v8a; " + HTTP_CLIENT},
{"os=Android 14,hc=okhttp_4.12,api=table,arch=arm64-v8a,lv=kotlin/2.0",
"os:Android 14; hc:okhttp_4.12; api:table; arch:arm64-v8a; lv:kotlin/2.0"},
};
}

Expand Down Expand Up @@ -146,6 +151,7 @@ private static String userAgent(String application) {

private static String userAgent(String application, String metadata) {
return (application == null ? "" : application + " ") + "clickhouse-java-v2/" + Client.clientVersion + " ("
+ (metadata == null ? "" : metadata + "; ") + LANGUAGE_VERSION + "; " + OS + "; " + HTTP_CLIENT + ")";
+ (metadata == null ? "" : metadata + "; ") + LANGUAGE_VERSION + "; " + OS + "; " + ARCHITECTURE + "; "
+ HTTP_CLIENT + ")";
}
}
8 changes: 4 additions & 4 deletions docs/clickhouse-docs/client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ Configuration is defined during client creation. See `com.clickhouse.client.api.
| `allowBinaryReaderToReuseBuffers(boolean reuse)` | `reuse` - flag to enable/disable | If enabled, reader will use pre-allocated buffers to do numbers transcoding. Reduces GC pressure for numeric data. | - | - |
| `columnToMethodMatchingStrategy(ColumnToMethodMatchingStrategy strategy)` | `strategy` - matching strategy implementation | Sets custom strategy to be used for matching DTO class fields and DB columns when registering DTO. | `none` | `none` |
| `setClientName(String clientName)` | `clientName` - application name string | Sets the client name, which identifies the application. Will be passed as the first part of the `User-Agent` header. Use it instead of setting the `User-Agent` header. Does not change the client metadata. | - | `client_name` |
| `setClientName(String clientName, Map<String, String> metadata)` | `clientName` - application name string<br/>`metadata` - key/value pairs | Sets the client name and the client metadata. The metadata is added to the comment group of the `User-Agent` header, in the iteration order of the map, then the `lv`, `os` and `hc` entries if the map does not set these keys. Keys and values must be printable ASCII, a key must not contain spaces or any of `:;(),=`, and a value must not contain any of `;()`. As a property, the value is written as `key1=value1,key2=value2` (escape a `,` or `\` in a value with `\`). Client-level only: not read from operation settings. | - | `client_id_metadata` |
| `setClientName(String clientName, Map<String, String> metadata)` | `clientName` - application name string<br/>`metadata` - key/value pairs | Sets the client name and the client metadata. The metadata is added to the comment group of the `User-Agent` header, in the iteration order of the map, then the `lv`, `os`, `arch` and `hc` entries if the map does not set these keys. Keys and values must be printable ASCII, a key must not contain spaces or any of `:;(),=`, and a value must not contain any of `;()`. As a property, the value is written as `key1=value1,key2=value2` (escape a `,` or `\` in a value with `\`). Client-level only: not read from operation settings. | - | `client_id_metadata` |
| `registerClientMetrics(Object registry, String name)` | `registry` - Micrometer registry instance<br/>`name` - metrics group name | Registers sensors with Micrometer (https://micrometer.io/) registry instance. | - | - |
| `setServerVersion(String version)` | `version` - server version string | Sets server version to avoid version detection. | - | `server_version` |
| `typeHintMapping(Map typeHintMapping)` | `typeHintMapping` - map of type hints | Sets type hint mapping for ClickHouse types. For example, to make multidimensional arrays be present as Java containers. | - | `type_hint_mapping` |
Expand All @@ -278,8 +278,8 @@ Configuration is defined during client creation. See `com.clickhouse.client.api.
There are two fields in a query log that identify application originated a request: `client_name` and `http_user_agent`. Native TCP protocol uses
`client_name` to identify application. HTTP protocol uses `http_user_agent` to identify application. Client builder has method `setClientName` correct values
for both protocols.
The field `http_user_agent` is set according to the standard ClickHouse client format: `<application>/<version> clickhouse-java-v2/<version> (<key>:<value>; ...; lv:java/<java version>; os:<operating system>; hc:ahc_<Apache HttpClient version>)`.
What is set by `setClientName` method comes first. Key/value pairs set by `setClientName(String, Map)` are added to the comment group, then `lv`, `os` and `hc` if the pairs do not set these keys.
The field `http_user_agent` is set according to the standard ClickHouse client format: `<application>/<version> clickhouse-java-v2/<version> (<key>:<value>; ...; lv:java/<java version>; os:<operating system>; arch:<CPU architecture>; hc:ahc_<Apache HttpClient version>)`.
What is set by `setClientName` method comes first. Key/value pairs set by `setClientName(String, Map)` are added to the comment group, then `lv`, `os`, `arch` and `hc` if the pairs do not set these keys.

For example:
```java showLineNumbers
Expand All @@ -301,7 +301,7 @@ public Client createClientWithMetadata() {
```
will result in the following `http_user_agent` value:
```
my-app-01/1.0 clickhouse-java-v2/0.12.0 (fv:flink/2.0.0; api:table; lv:java/17.0.17; os:Linux; hc:ahc_5.6.4)
my-app-01/1.0 clickhouse-java-v2/0.12.0 (fv:flink/2.0.0; api:table; lv:java/17.0.17; os:Linux; arch:amd64; hc:ahc_5.6.4)
```

Use `setClientName` to identify the application. Do not set the `User-Agent` header for this: a custom `User-Agent` header replaces
Expand Down
4 changes: 2 additions & 2 deletions docs/clickhouse-docs/jdbc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ conn.setClientInfo(com.clickhouse.jdbc.ClientInfoProperties.APPLICATION_NAME.get

Both ways will result in the following `http_user_agent` value in the query log:
```
my-app-01/1.0 jdbc-v2/0.12.0 clickhouse-java-v2/0.12.0 (lv:java/17.0.17; os:Linux; hc:ahc_5.6.4)
my-app-01/1.0 jdbc-v2/0.12.0 clickhouse-java-v2/0.12.0 (lv:java/17.0.17; os:Linux; arch:amd64; hc:ahc_5.6.4)
```

To add key/value pairs to the comment group of the header, set the `client_id_metadata` property. For example,
`client_id_metadata=fv=flink/2.0.0,api=table` gives `(fv:flink/2.0.0; api:table; lv:java/17.0.17; os:Linux; hc:ahc_5.6.4)`.
`client_id_metadata=fv=flink/2.0.0,api=table` gives `(fv:flink/2.0.0; api:table; lv:java/17.0.17; os:Linux; arch:amd64; hc:ahc_5.6.4)`.

**Note:** We recommend using `app_name/version` format for `client_name` property because it helps to identify the application in the query log.
Use the client name to identify the application. Do not set a custom `User-Agent` HTTP header for this: it replaces the application part of the header, including `jdbc-v2/<version>`.
Expand Down
Loading
Loading