Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
turboFei committed Dec 25, 2024
1 parent c3046d4 commit 6a6a511
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
21 changes: 11 additions & 10 deletions docs/configuration/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,16 +399,17 @@ You can configure the Kyuubi properties in `$KYUUBI_HOME/conf/kyuubi-defaults.co

### Metrics

| Key | Default | Meaning | Type | Since |
|---------------------------------|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|-------|
| kyuubi.metrics.console.interval | PT5S | How often should report metrics to console | duration | 1.2.0 |
| kyuubi.metrics.enabled | true | Set to true to enable kyuubi metrics system | boolean | 1.2.0 |
| kyuubi.metrics.json.interval | PT5S | How often should report metrics to JSON file | duration | 1.2.0 |
| kyuubi.metrics.json.location | metrics | Where the JSON metrics file located | string | 1.2.0 |
| kyuubi.metrics.prometheus.path | /metrics | URI context path of prometheus metrics HTTP server | string | 1.2.0 |
| kyuubi.metrics.prometheus.port | 10019 | Prometheus metrics HTTP server port | int | 1.2.0 |
| kyuubi.metrics.reporters | PROMETHEUS | A comma-separated list for all metrics reporters<ul> <li>CONSOLE - ConsoleReporter which outputs measurements to CONSOLE periodically.</li> <li>JMX - JmxReporter which listens for new metrics and exposes them as MBeans.</li> <li>JSON - JsonReporter which outputs measurements to json file periodically.</li> <li>PROMETHEUS - PrometheusReporter which exposes metrics in Prometheus format.</li> <li>SLF4J - Slf4jReporter which outputs measurements to system log periodically.</li></ul> | set | 1.2.0 |
| kyuubi.metrics.slf4j.interval | PT5S | How often should report metrics to SLF4J logger | duration | 1.2.0 |
| Key | Default | Meaning | Type | Since |
|---------------------------------------------------|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|--------|
| kyuubi.metrics.console.interval | PT5S | How often should report metrics to console | duration | 1.2.0 |
| kyuubi.metrics.enabled | true | Set to true to enable kyuubi metrics system | boolean | 1.2.0 |
| kyuubi.metrics.json.interval | PT5S | How often should report metrics to JSON file | duration | 1.2.0 |
| kyuubi.metrics.json.location | metrics | Where the JSON metrics file located | string | 1.2.0 |
| kyuubi.metrics.prometheus.labels.instance.enabled | false | Whether to add instance label to prometheus metrics | boolean | 1.10.2 |
| kyuubi.metrics.prometheus.path | /metrics | URI context path of prometheus metrics HTTP server | string | 1.2.0 |
| kyuubi.metrics.prometheus.port | 10019 | Prometheus metrics HTTP server port | int | 1.2.0 |
| kyuubi.metrics.reporters | PROMETHEUS | A comma-separated list for all metrics reporters<ul> <li>CONSOLE - ConsoleReporter which outputs measurements to CONSOLE periodically.</li> <li>JMX - JmxReporter which listens for new metrics and exposes them as MBeans.</li> <li>JSON - JsonReporter which outputs measurements to json file periodically.</li> <li>PROMETHEUS - PrometheusReporter which exposes metrics in Prometheus format.</li> <li>SLF4J - Slf4jReporter which outputs measurements to system log periodically.</li></ul> | set | 1.2.0 |
| kyuubi.metrics.slf4j.interval | PT5S | How often should report metrics to SLF4J logger | duration | 1.2.0 |

### Operation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ object MetricsConf {
.checkValue(path => path.startsWith("/"), "Context path must start with '/'")
.createWithDefault("/metrics")

val METRICS_PROMETHEUS_METRICS_INSTANCE_ENABLED: ConfigEntry[Boolean] =
buildConf("kyuubi.metrics.prometheus.metrics.instance.enabled")
val METRICS_PROMETHEUS_LABELS_INSTANCE_ENABLED: ConfigEntry[Boolean] =
buildConf("kyuubi.metrics.prometheus.labels.instance.enabled")
.doc("Whether to add instance label to prometheus metrics")
.version("1.10.2")
.booleanConf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ class PrometheusReporterService(registry: MetricRegistry)
httpServer.setHandler(context)

new DropwizardExports(registry).register(bridgeRegistry)
if (conf.get(MetricsConf.METRICS_PROMETHEUS_METRICS_INSTANCE_ENABLED)) {
val instance = s"${JavaUtils.findLocalInetAddress.getCanonicalHostName}:$port"
if (conf.get(MetricsConf.METRICS_PROMETHEUS_LABELS_INSTANCE_ENABLED)) {
val instanceLabel =
Map("instance" -> s"${JavaUtils.findLocalInetAddress.getCanonicalHostName}:$port")
context.addServlet(
new ServletHolder(createPrometheusServletWithInstance(instance)),
new ServletHolder(createPrometheusServletWithLabels(instanceLabel)),
contextPath)
} else {
val metricsServlet = new MetricsServlet(bridgeRegistry)
Expand Down Expand Up @@ -112,13 +113,13 @@ class PrometheusReporterService(registry: MetricRegistry)
}
}

private def createPrometheusServletWithInstance(instance: String): HttpServlet = {
private def createPrometheusServletWithLabels(labels: Map[String, String]): HttpServlet = {
new HttpServlet {
override def doGet(request: HttpServletRequest, response: HttpServletResponse): Unit = {
try {
response.setContentType("text/plain;charset=utf-8")
response.setStatus(HttpServletResponse.SC_OK)
response.getWriter.print(getMetricsSnapshot(instance))
response.getWriter.print(getMetricsSnapshot(labels))
} catch {
case e: IllegalArgumentException =>
response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage)
Expand All @@ -135,17 +136,22 @@ class PrometheusReporterService(registry: MetricRegistry)
}
}

private def getMetricsSnapshot(instance: String): String = {
private def getMetricsSnapshot(labels: Map[String, String]): String = {
val metricsSnapshotWriter = new java.io.StringWriter
val contentType = TextFormat.chooseContentType(null)
TextFormat.writeFormat(contentType, metricsSnapshotWriter, bridgeRegistry.metricFamilySamples())
val labelStr = labelString(labels)
metricsSnapshotWriter.toString.split("\n").map { line =>
if (line.startsWith("#")) {
line
} else {
val Array(metrics, value) = line.split("\\s+", 2)
s"""$metrics{instance=\"$instance\"} $value"""
s"""$metrics${labelStr} $value"""
}
}.mkString("\n")
}

private def labelString(labels: Map[String, String]): String = {
labels.map { case (k, v) => s"""$k="$v"""" }.toArray.sorted.mkString("{", ",", "}")
}
}

0 comments on commit 6a6a511

Please sign in to comment.