Skip to content

Commit 5dff6e9

Browse files
committed
Feat: Send userAgent with client identity
This patch teaches session negotiation to send a user agent of the form "com.bloomberg.bmq(javaXX.YY.ZZ):AA.BB.CC", where "XX.YY.ZZ" is the version of Java this SDK is running on, and "AA.BB.CC" is the SDK version. It additionally sets the user agent to in the `PlainConsumerIT` and `PlainProducerIT` integration test clients to different strings. These clients are not simple users of this SDK, and construct the sockets they communicate on directly. It is thus useful to distinguish them on the broker from normal users of the SDK. Signed-off-by: Patrick M. Niedzielski <pniedzielski@bloomberg.net>
1 parent e468717 commit 5dff6e9

File tree

7 files changed

+35
-8
lines changed

7 files changed

+35
-8
lines changed

bmq-sdk/src/main/java/com/bloomberg/bmq/impl/TcpBrokerConnection.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Bloomberg Finance L.P.
2+
* Copyright 2022-2025 Bloomberg Finance L.P.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -373,9 +373,16 @@ private NegotiationMessageChoice createNegoMsg() {
373373
clientIdentity.setClusterName("");
374374
clientIdentity.setClusterNodeId(-1);
375375
clientIdentity.setSdkLanguage(ClientLanguage.E_JAVA);
376+
clientIdentity.setUserAgent(constructUserAgent());
376377
return negoMsgChoice;
377378
}
378379

380+
private String constructUserAgent() {
381+
String javaVersion = "java" + SystemUtil.getJavaVersionString();
382+
String sdkVersion = VersionUtil.getSdkVersion();
383+
return "com.bloomberg.bmq(" + javaVersion + "):" + sdkVersion;
384+
}
385+
379386
private WriteStatus negotiate() throws IOException {
380387
if (negotiationMsg == null) {
381388
negotiationMsg = createNegoMsg();

bmq-sdk/src/main/java/com/bloomberg/bmq/impl/infr/msg/ClientIdentity.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Bloomberg Finance L.P.
2+
* Copyright 2022-2025 Bloomberg Finance L.P.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ public class ClientIdentity {
2727
private String clusterName;
2828
private Integer clusterNodeId;
2929
private ClientLanguage sdkLanguage;
30+
private String userAgent;
3031

3132
public ClientIdentity() {
3233
init();
@@ -48,6 +49,7 @@ private void init() {
4849
clusterName = "";
4950
clusterNodeId = -1;
5051
sdkLanguage = ClientLanguage.E_UNKNOWN;
52+
userAgent = "";
5153
}
5254

5355
public Integer protocolVersion() {
@@ -138,6 +140,14 @@ public void setSdkLanguage(ClientLanguage value) {
138140
sdkLanguage = value;
139141
}
140142

143+
public String userAgent() {
144+
return userAgent;
145+
}
146+
147+
public void setUserAgent(String value) {
148+
userAgent = value;
149+
}
150+
141151
public Object createNewInstance() {
142152
return new ClientIdentity();
143153
}

bmq-sdk/src/main/java/com/bloomberg/bmq/impl/infr/util/SystemUtil.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Bloomberg Finance L.P.
2+
* Copyright 2022-2025 Bloomberg Finance L.P.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ public static JavaVersion getJavaVersion() {
4848
JavaVersion result = JavaVersion.JAVA_UNSUPPORTED;
4949

5050
try {
51-
String version = System.getProperty("java.version");
51+
String version = getJavaVersionString();
5252

5353
result =
5454
Arrays.stream(JavaVersion.values())
@@ -63,6 +63,10 @@ public static JavaVersion getJavaVersion() {
6363
return result;
6464
}
6565

66+
public static String getJavaVersionString() {
67+
return System.getProperty("java.version");
68+
}
69+
6670
public static int getProcessId() {
6771
int pid = 0;
6872
String vmname = null;

bmq-sdk/src/test/java/com/bloomberg/bmq/impl/infr/codec/JsonDecoderUtilTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Bloomberg Finance L.P.
2+
* Copyright 2022-2025 Bloomberg Finance L.P.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -84,6 +84,7 @@ void testDecodeClientIdentity() throws IOException {
8484
assertEquals("", ci.features());
8585
assertEquals("", ci.clusterName());
8686
assertEquals("E_JAVA", ci.sdkLanguage().toString());
87+
assertEquals("user_agent", ci.userAgent());
8788
}
8889

8990
@Test
@@ -128,6 +129,7 @@ void testDecodeBrokerResponse() throws IOException {
128129
assertEquals("", ci.features());
129130
assertEquals("cluster_name", ci.clusterName());
130131
assertEquals("E_JAVA", ci.sdkLanguage().toString());
132+
assertEquals("user_agent", ci.userAgent());
131133
}
132134

133135
@Test

bmq-sdk/src/test/java/com/bloomberg/bmq/impl/infr/proto/SchemaEventImplBuilderTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Bloomberg Finance L.P.
2+
* Copyright 2022-2025 Bloomberg Finance L.P.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,6 +61,7 @@ void testBuildClientIdentityJson() throws IOException {
6161
clientIdentity.setClusterName("");
6262
clientIdentity.setClusterNodeId(-1);
6363
clientIdentity.setSdkLanguage(ClientLanguage.E_JAVA);
64+
clientIdentity.setUserAgent("user_agent");
6465

6566
ByteBuffer[] message;
6667
SchemaEventBuilder schemaBuilder = new SchemaEventBuilder();
@@ -93,6 +94,7 @@ void testBuildBrokerResponseJson() throws IOException {
9394
clientIdentity.setClusterNodeId(-1);
9495
clientIdentity.setClusterName("cluster_name");
9596
clientIdentity.setSdkLanguage(ClientLanguage.E_JAVA);
97+
clientIdentity.setUserAgent("user_agent");
9698

9799
ByteBuffer[] message;
98100
SchemaEventBuilder schemaBuilder = new SchemaEventBuilder();

bmq-sdk/src/test/java/com/bloomberg/bmq/it/PlainConsumerIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Bloomberg Finance L.P.
2+
* Copyright 2022-2025 Bloomberg Finance L.P.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -115,6 +115,7 @@ public static ByteBuffer[] getLastMessage(int port, Uri uri)
115115
clientIdentity.setClusterName("");
116116
clientIdentity.setClusterNodeId(-1);
117117
clientIdentity.setSdkLanguage(ClientLanguage.E_JAVA);
118+
clientIdentity.setUserAgent("com.bloomberg.bmq.it.PlainConsumerIT");
118119

119120
SchemaEventBuilder schemaBuilder = new SchemaEventBuilder();
120121

bmq-sdk/src/test/java/com/bloomberg/bmq/it/PlainProducerIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Bloomberg Finance L.P.
2+
* Copyright 2022-2025 Bloomberg Finance L.P.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -117,6 +117,7 @@ public static void sendMessage(
117117
clientIdentity.setClusterName("");
118118
clientIdentity.setClusterNodeId(-1);
119119
clientIdentity.setSdkLanguage(ClientLanguage.E_JAVA);
120+
clientIdentity.setUserAgent("com.bloomberg.bmq.it.PlainProducerIT");
120121

121122
SchemaEventBuilder schemaBuilder = new SchemaEventBuilder();
122123

0 commit comments

Comments
 (0)