-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Prometheus REST endpoint (moved from JVB). (#175)
* Add Prometheus REST endpoint (moved from JVB). * Add Prometheus REST endpoint. * Add tests. * Update pom.xml. This commit relates to PR #1915 in jitsi-videobridge. Signed-off-by: Alexandre Fonseca <4717855+alexcsf@users.noreply.github.com> * fix: Add jvmTarget, jersey-hk2 and args to pom. * fix: Add jvmTarget, jersey-hk2 and args to pom. Signed-off-by: Alexandre Fonseca <4717855+alexcsf@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
278 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
jicoco-metrics/src/main/java/org/jitsi/rest/prometheus/Prometheus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright @ 2022 - present 8x8, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jitsi.rest.prometheus; | ||
|
||
import io.prometheus.client.exporter.common.*; | ||
import jakarta.ws.rs.*; | ||
import jakarta.ws.rs.core.*; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jitsi.metrics.*; | ||
|
||
/** | ||
* A REST endpoint exposing JVB stats for Prometheus. | ||
* Any scraper supporting Prometheus' text-based formats ({@code text/plain; version=0.0.4} or OpenMetrics) | ||
* is compatible with this {@code /metrics} endpoint.<br> | ||
* JSON is provided when the client performs a request with the 'Accept' header set to {@code application/json}.<br> | ||
* The response defaults to {@code text/plain; version=0.0.4} formatted output. | ||
* | ||
* @see <a href="https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md"> | ||
* Prometheus' exposition formats</a> | ||
*/ | ||
@Path("/metrics") | ||
public class Prometheus | ||
{ | ||
@NotNull | ||
private final MetricsContainer metricsContainer; | ||
|
||
public Prometheus(@NotNull MetricsContainer metricsContainer) | ||
{ | ||
this.metricsContainer = metricsContainer; | ||
} | ||
|
||
@GET | ||
@Produces(TextFormat.CONTENT_TYPE_004) | ||
public String getPrometheusPlainText() | ||
{ | ||
return metricsContainer.getPrometheusMetrics(TextFormat.CONTENT_TYPE_004); | ||
} | ||
|
||
@GET | ||
@Produces(TextFormat.CONTENT_TYPE_OPENMETRICS_100) | ||
public String getPrometheusOpenMetrics() | ||
{ | ||
return metricsContainer.getPrometheusMetrics(TextFormat.CONTENT_TYPE_OPENMETRICS_100); | ||
} | ||
|
||
@GET | ||
@Produces({MediaType.APPLICATION_JSON, "*/*;q=0"}) | ||
public String getJsonString() | ||
{ | ||
return metricsContainer.getJsonString(); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
jicoco-metrics/src/test/kotlin/org/jitsi/rest/prometheus/PrometheusTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.jitsi.rest.prometheus | ||
|
||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.types.shouldBeInstanceOf | ||
import io.prometheus.client.CollectorRegistry | ||
import io.prometheus.client.exporter.common.TextFormat | ||
import jakarta.ws.rs.core.Application | ||
import jakarta.ws.rs.core.MediaType | ||
import jakarta.ws.rs.core.Response | ||
import org.eclipse.jetty.http.HttpStatus | ||
import org.glassfish.jersey.server.ResourceConfig | ||
import org.glassfish.jersey.test.JerseyTest | ||
import org.glassfish.jersey.test.TestProperties | ||
import org.jitsi.metrics.MetricsContainer | ||
import org.json.simple.JSONObject | ||
import org.json.simple.parser.JSONParser | ||
import org.junit.Test | ||
|
||
class PrometheusTest : JerseyTest() { | ||
private lateinit var metricsContainer: MetricsContainer | ||
private val baseUrl = "/metrics" | ||
private val mediaType004 = MediaType("text", "plain", mapOf("charset" to "utf-8", "version" to "0.0.4")) | ||
private val mediaTypeOpenMetrics = | ||
MediaType("application", "openmetrics-text", mapOf("charset" to "utf-8", "version" to "1.0.0")) | ||
|
||
override fun configure(): Application { | ||
metricsContainer = MetricsContainer(CollectorRegistry()).apply { | ||
checkForNameConflicts = false | ||
registerLongGauge("gauge", "A gauge", 50) | ||
registerBooleanMetric("boolean", "A boolean", true) | ||
} | ||
|
||
enable(TestProperties.LOG_TRAFFIC) | ||
enable(TestProperties.DUMP_ENTITY) | ||
return object : ResourceConfig() { | ||
init { | ||
register(Prometheus(metricsContainer)) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun testGetJson() { | ||
val resp = target(baseUrl).request(MediaType.APPLICATION_JSON_TYPE).get() | ||
resp.status shouldBe HttpStatus.OK_200 | ||
resp.mediaType shouldBe MediaType.APPLICATION_JSON_TYPE | ||
with(MetricsContainer(CollectorRegistry())) { | ||
registerLongGauge("gauge", "A gauge", 50) | ||
registerBooleanMetric("boolean", "A boolean", true) | ||
resp.getResultAsJson() shouldBe mapOf("gauge" to 50, "boolean" to true) | ||
} | ||
} | ||
|
||
@Test | ||
fun testGetPrometheus004() { | ||
val resp = target(baseUrl).request(TextFormat.CONTENT_TYPE_004).get() | ||
resp.status shouldBe HttpStatus.OK_200 | ||
resp.mediaType shouldBe mediaType004 | ||
with(MetricsContainer(CollectorRegistry())) { | ||
registerLongGauge("gauge", "A gauge", 50) | ||
registerBooleanMetric("boolean", "A boolean", true) | ||
val expected = getPrometheusMetrics(TextFormat.CONTENT_TYPE_004).split("\n").sorted() | ||
resp.getSortedLines() shouldBe expected | ||
} | ||
} | ||
|
||
@Test | ||
fun testGetPrometheusOpenMetrics() { | ||
val resp = target(baseUrl).request(TextFormat.CONTENT_TYPE_OPENMETRICS_100).get() | ||
resp.status shouldBe HttpStatus.OK_200 | ||
resp.mediaType shouldBe mediaTypeOpenMetrics | ||
with(MetricsContainer(CollectorRegistry())) { | ||
registerLongGauge("gauge", "A gauge", 50) | ||
registerBooleanMetric("boolean", "A boolean", true) | ||
val expected = getPrometheusMetrics(TextFormat.CONTENT_TYPE_OPENMETRICS_100).split("\n").sorted() | ||
resp.getSortedLines() shouldBe expected | ||
} | ||
} | ||
|
||
private fun Response.getResultAsJson(): JSONObject { | ||
val obj = JSONParser().parse(readEntity(String::class.java)) | ||
obj.shouldBeInstanceOf<JSONObject>() | ||
return obj | ||
} | ||
|
||
private fun Response.getSortedLines(): List<String> { | ||
return readEntity(String::class.java).split("\n").sorted() | ||
} | ||
} |