This is an automated email from the ASF dual-hosted git repository.
frankchen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/druid.git
The following commit(s) were added to refs/heads/master by this push:
new 13af85cb8d0 Add user agent to headers (#18505)
13af85cb8d0 is described below
commit 13af85cb8d0ca1b523bf3f0c33cdacd6448054a5
Author: Gabriel Chang <[email protected]>
AuthorDate: Sat Sep 13 16:56:59 2025 +0800
Add user agent to headers (#18505)
* Add user agent to headers
* Fix checkstyle
* Revert some format changes
---
.../apache/druid/guice/http/HttpClientModule.java | 29 +++++++++++++++++++---
.../druid/guice/http/JettyHttpClientModule.java | 15 +++++++++++
.../java/org/apache/druid/server/DruidNode.java | 14 +++++++++++
.../org/apache/druid/server/DruidNodeTest.java | 1 +
4 files changed, 56 insertions(+), 3 deletions(-)
diff --git
a/server/src/main/java/org/apache/druid/guice/http/HttpClientModule.java
b/server/src/main/java/org/apache/druid/guice/http/HttpClientModule.java
index 2aaf30a469f..8a9f39e0127 100644
--- a/server/src/main/java/org/apache/druid/guice/http/HttpClientModule.java
+++ b/server/src/main/java/org/apache/druid/guice/http/HttpClientModule.java
@@ -21,6 +21,7 @@ package org.apache.druid.guice.http;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
+import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.Binder;
import com.google.inject.Binding;
import com.google.inject.Inject;
@@ -30,11 +31,18 @@ import org.apache.druid.guice.LazySingleton;
import org.apache.druid.guice.annotations.EscalatedClient;
import org.apache.druid.guice.annotations.EscalatedGlobal;
import org.apache.druid.guice.annotations.Global;
+import org.apache.druid.guice.annotations.Self;
import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.http.client.AbstractHttpClient;
import org.apache.druid.java.util.http.client.HttpClient;
import org.apache.druid.java.util.http.client.HttpClientConfig;
import org.apache.druid.java.util.http.client.HttpClientInit;
+import org.apache.druid.java.util.http.client.Request;
+import org.apache.druid.java.util.http.client.response.HttpResponseHandler;
+import org.apache.druid.server.DruidNode;
import org.apache.druid.server.security.Escalator;
+import org.jboss.netty.handler.codec.http.HttpHeaders;
+import org.joda.time.Duration;
import javax.net.ssl.SSLContext;
import java.lang.annotation.Annotation;
@@ -87,6 +95,7 @@ public class HttpClientModule implements Module
private final boolean isEscalated;
private final boolean eagerByDefault;
private Escalator escalator;
+ private DruidNode node;
public HttpClientProvider(Class<? extends Annotation> annotationClazz,
boolean isEscalated, boolean eagerByDefault)
{
@@ -96,9 +105,10 @@ public class HttpClientModule implements Module
}
@Inject
- public void inject(Escalator escalator)
+ public void inject(Escalator escalator, @Self DruidNode node)
{
this.escalator = escalator;
+ this.node = node;
}
@Override
@@ -127,11 +137,24 @@ public class HttpClientModule implements Module
builder.build(),
getLifecycleProvider().get()
);
+ HttpClient clientWithUserAgent = new AbstractHttpClient()
+ {
+ @Override
+ public <Intermediate, Final> ListenableFuture<Final> go(
+ Request request,
+ HttpResponseHandler<Intermediate, Final> handler,
+ Duration readTimeout
+ )
+ {
+ request.setHeader(HttpHeaders.Names.USER_AGENT,
StringUtils.format("%s/%s", node.getServiceName(), node.getVersion()));
+ return client.go(request, handler, readTimeout);
+ }
+ };
if (isEscalated) {
- return escalator.createEscalatedClient(client);
+ return escalator.createEscalatedClient(clientWithUserAgent);
} else {
- return client;
+ return clientWithUserAgent;
}
}
}
diff --git
a/server/src/main/java/org/apache/druid/guice/http/JettyHttpClientModule.java
b/server/src/main/java/org/apache/druid/guice/http/JettyHttpClientModule.java
index c4eefa02704..4ef5192e28e 100644
---
a/server/src/main/java/org/apache/druid/guice/http/JettyHttpClientModule.java
+++
b/server/src/main/java/org/apache/druid/guice/http/JettyHttpClientModule.java
@@ -22,16 +22,23 @@ package org.apache.druid.guice.http;
import com.google.common.base.Preconditions;
import com.google.inject.Binder;
import com.google.inject.Binding;
+import com.google.inject.Inject;
import com.google.inject.Module;
import org.apache.druid.guice.JsonConfigProvider;
import org.apache.druid.guice.LazySingleton;
import org.apache.druid.guice.annotations.Global;
+import org.apache.druid.guice.annotations.Self;
+import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.java.util.common.lifecycle.Lifecycle;
+import org.apache.druid.server.DruidNode;
import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
+import org.jboss.netty.handler.codec.http.HttpHeaders;
import javax.net.ssl.SSLContext;
+
import java.lang.annotation.Annotation;
/**
@@ -65,6 +72,10 @@ public class JettyHttpClientModule implements Module
public static class HttpClientProvider extends
AbstractHttpClientProvider<HttpClient>
{
+ @Inject
+ @Self
+ private DruidNode node;
+
public HttpClientProvider(Class<? extends Annotation> annotation)
{
super(annotation);
@@ -93,6 +104,10 @@ public class JettyHttpClientModule implements Module
final QueuedThreadPool pool = new
QueuedThreadPool(config.getNumMaxThreads());
pool.setName(JettyHttpClientModule.class.getSimpleName() +
"-threadPool-" + pool.hashCode());
httpClient.setExecutor(pool);
+ httpClient.setUserAgentField(new HttpField(
+ HttpHeaders.Names.USER_AGENT,
+ StringUtils.format("%s/%s", node.getServiceName(), node.getVersion())
+ ));
final Lifecycle lifecycle = getLifecycleProvider().get();
diff --git a/server/src/main/java/org/apache/druid/server/DruidNode.java
b/server/src/main/java/org/apache/druid/server/DruidNode.java
index 1c077d0cd73..225f054c73e 100644
--- a/server/src/main/java/org/apache/druid/server/DruidNode.java
+++ b/server/src/main/java/org/apache/druid/server/DruidNode.java
@@ -25,12 +25,14 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.google.common.net.HostAndPort;
import com.google.inject.name.Named;
+import org.apache.druid.common.guava.GuavaUtils;
import org.apache.druid.common.utils.SocketUtil;
import org.apache.druid.java.util.common.IAE;
import org.apache.druid.java.util.common.ISE;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
+
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
@@ -80,6 +82,13 @@ public class DruidNode
@JsonProperty
private boolean enableTlsPort = false;
+ @JsonProperty
+ @NotNull
+ private final String version = GuavaUtils.firstNonNull(
+ DruidNode.class.getPackage().getImplementationVersion(),
+ "unknown"
+ );
+
public DruidNode(
String serviceName,
String host,
@@ -236,6 +245,11 @@ public class DruidNode
return tlsPort;
}
+ public String getVersion()
+ {
+ return version;
+ }
+
public DruidNode withService(String service)
{
return new DruidNode(service, host, bindOnHost, plaintextPort, tlsPort,
enablePlaintextPort, enableTlsPort);
diff --git a/server/src/test/java/org/apache/druid/server/DruidNodeTest.java
b/server/src/test/java/org/apache/druid/server/DruidNodeTest.java
index 158d58b3b53..f0fde0117ac 100644
--- a/server/src/test/java/org/apache/druid/server/DruidNodeTest.java
+++ b/server/src/test/java/org/apache/druid/server/DruidNodeTest.java
@@ -53,6 +53,7 @@ public class DruidNodeTest
// Hosts which report only ipv6 will have getDefaultHost() report
something like fe80::6e40:8ff:fe93:9230
// but getHostAndPort() reports [fe80::6e40:8ff:fe93:9230]
Assert.assertEquals(HostAndPort.fromString(DruidNode.getDefaultHost()).toString(),
node.getHostAndPort());
+ Assert.assertEquals("unknown", node.getVersion()); // unknown because not
compiled with version
node = new DruidNode(service, "2001:db8:85a3::8a2e:370:7334", false, -1,
null, true, false);
Assert.assertEquals("2001:db8:85a3::8a2e:370:7334", node.getHost());
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]