From d50cf17edeb12e6eb4da22575f82ba7579d4a3be Mon Sep 17 00:00:00 2001 From: zhang_yao Date: Tue, 24 Dec 2024 21:16:39 +0800 Subject: [PATCH] [KYUUBI #6615] Make Jetty sending server version in response configurable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # :mag: Description ## Issue References ๐Ÿ”— This pull request fixes #6615 ## Describe Your Solution ๐Ÿ”ง Add a config item that controls whether Jetty should send its version in response. This is an additional patch which enables/disables sending Jetty version for prometheus reporter. Sending Jetty version could be disabled by calling HttpConfiguration::setSendServerVersion(false) ## Types of changes :bookmark: - [x] Bugfix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Test Plan ๐Ÿงช Compiled and tested manually. #### Behavior Without This Pull Request :coffin: #### Behavior With This Pull Request :tada: #### Related Unit Tests --- # Checklist ๐Ÿ“ - [ ] This patch was not authored or co-authored using [Generative Tooling](https://www.apache.org/legal/generative-tooling.html) **Be nice. Be informative.** Closes #6685 from paul8263/KYUUBI-6615-patch. Closes #6615 0638a5116 [zhang_yao] [KYUUBI #6615] Make Jetty sending server version in response configurable Authored-by: zhang_yao Signed-off-by: Cheng Pan --- .../metrics/PrometheusReporterService.scala | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/kyuubi-metrics/src/main/scala/org/apache/kyuubi/metrics/PrometheusReporterService.scala b/kyuubi-metrics/src/main/scala/org/apache/kyuubi/metrics/PrometheusReporterService.scala index ab014caf14e..e62e2190906 100644 --- a/kyuubi-metrics/src/main/scala/org/apache/kyuubi/metrics/PrometheusReporterService.scala +++ b/kyuubi-metrics/src/main/scala/org/apache/kyuubi/metrics/PrometheusReporterService.scala @@ -21,11 +21,12 @@ import com.codahale.metrics.MetricRegistry import io.prometheus.client.CollectorRegistry import io.prometheus.client.dropwizard.DropwizardExports import io.prometheus.client.exporter.MetricsServlet -import org.eclipse.jetty.server.Server +import org.eclipse.jetty.server.{HttpConfiguration, HttpConnectionFactory, Server, ServerConnector} import org.eclipse.jetty.servlet.{ServletContextHandler, ServletHolder} import org.apache.kyuubi.KyuubiException import org.apache.kyuubi.config.KyuubiConf +import org.apache.kyuubi.config.KyuubiConf.FRONTEND_JETTY_SEND_VERSION_ENABLED import org.apache.kyuubi.service.AbstractService class PrometheusReporterService(registry: MetricRegistry) @@ -35,12 +36,21 @@ class PrometheusReporterService(registry: MetricRegistry) // VisibleForTesting private[metrics] var httpServer: Server = _ + private[metrics] var httpServerConnector: ServerConnector = _ @volatile protected var isStarted = false override def initialize(conf: KyuubiConf): Unit = { val port = conf.get(MetricsConf.METRICS_PROMETHEUS_PORT) val contextPath = conf.get(MetricsConf.METRICS_PROMETHEUS_PATH) - httpServer = new Server(port) + val jettyVersionEnabled = conf.get(FRONTEND_JETTY_SEND_VERSION_ENABLED) + + val httpConf = new HttpConfiguration() + httpConf.setSendServerVersion(jettyVersionEnabled) + httpServer = new Server() + httpServerConnector = new ServerConnector(httpServer, new HttpConnectionFactory(httpConf)) + httpServerConnector.setPort(port) + httpServer.addConnector(httpServerConnector) + val context = new ServletContextHandler context.setContextPath("/") httpServer.setHandler(context) @@ -56,6 +66,7 @@ class PrometheusReporterService(registry: MetricRegistry) if (!isStarted) { try { httpServer.start() + httpServerConnector.start() info(s"Prometheus metrics HTTP server has started at ${httpServer.getURI}.") } catch { case rethrow: Exception => @@ -78,12 +89,14 @@ class PrometheusReporterService(registry: MetricRegistry) private def stopHttpServer(): Unit = { if (httpServer != null) { try { + httpServerConnector.stop() httpServer.stop() info("Prometheus metrics HTTP server has stopped.") } catch { case err: Exception => error("Cannot safely stop prometheus metrics HTTP server", err) } finally { httpServer = null + httpServerConnector = null } } }