Skip to content

Commit 2daafe1

Browse files
authored
refactor(server): allow not passing Prometheus registry to most routes (#1927)
To improve testability, for the purpose of testing the fix in #1924
1 parent 19c5826 commit 2daafe1

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutes.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ private val bindingsCache =
4242
.recordStats()
4343
.asLoadingCache<ActionCoords, ArtifactResult> { runCatching { it.buildVersionArtifacts()!! } }
4444

45-
fun Routing.artifactRoutes(prometheusRegistry: PrometheusMeterRegistry) {
46-
CaffeineCacheMetrics.monitor(prometheusRegistry, bindingsCache.underlying(), "bindings_cache")
45+
fun Routing.artifactRoutes(prometheusRegistry: PrometheusMeterRegistry? = null) {
46+
prometheusRegistry?.let {
47+
CaffeineCacheMetrics.monitor(it, bindingsCache.underlying(), "bindings_cache")
48+
}
4749

4850
route("{owner}/{name}/{version}/{file}") {
4951
artifact(prometheusRegistry, refresh = false)
@@ -55,15 +57,15 @@ fun Routing.artifactRoutes(prometheusRegistry: PrometheusMeterRegistry) {
5557
}
5658

5759
private fun Route.artifact(
58-
prometheusRegistry: PrometheusMeterRegistry,
60+
prometheusRegistry: PrometheusMeterRegistry?,
5961
refresh: Boolean = false,
6062
) {
6163
headArtifact(prometheusRegistry, refresh)
6264
getArtifact(prometheusRegistry, refresh)
6365
}
6466

6567
private fun Route.headArtifact(
66-
prometheusRegistry: PrometheusMeterRegistry,
68+
prometheusRegistry: PrometheusMeterRegistry?,
6769
refresh: Boolean,
6870
) {
6971
head {
@@ -77,12 +79,12 @@ private fun Route.headArtifact(
7779
call.respondNotFound()
7880
}
7981

80-
incrementArtifactCounter(prometheusRegistry, call)
82+
prometheusRegistry?.incrementArtifactCounter(call)
8183
}
8284
}
8385

8486
private fun Route.getArtifact(
85-
prometheusRegistry: PrometheusMeterRegistry,
87+
prometheusRegistry: PrometheusMeterRegistry?,
8688
refresh: Boolean,
8789
) {
8890
get {
@@ -99,7 +101,7 @@ private fun Route.getArtifact(
99101
is JarArtifact -> call.respondBytes(artifact.data(), ContentType.parse("application/java-archive"))
100102
}
101103

102-
incrementArtifactCounter(prometheusRegistry, call)
104+
prometheusRegistry?.incrementArtifactCounter(call)
103105
}
104106
}
105107

@@ -119,10 +121,7 @@ private suspend fun ApplicationCall.toBindingArtifacts(refresh: Boolean): Map<St
119121
return bindingsCache.get(actionCoords).getOrNull()
120122
}
121123

122-
private fun incrementArtifactCounter(
123-
prometheusRegistry: PrometheusMeterRegistry,
124-
call: ApplicationCall,
125-
) {
124+
private fun PrometheusMeterRegistry.incrementArtifactCounter(call: ApplicationCall) {
126125
val owner = call.parameters["owner"] ?: "unknown"
127126
val name = call.parameters["name"] ?: "unknown"
128127
val version = call.parameters["version"] ?: "unknown"
@@ -135,7 +134,7 @@ private fun incrementArtifactCounter(
135134
?.toString() ?: "unknown"
136135

137136
val counter =
138-
prometheusRegistry.counter(
137+
this.counter(
139138
"artifact_requests_total",
140139
listOf(
141140
Tag.of("owner", owner),

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/MetadataRoutes.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ private val metadataCache =
3232
}
3333
}
3434

35-
fun Routing.metadataRoutes(prometheusRegistry: PrometheusMeterRegistry) {
36-
CaffeineCacheMetrics.monitor(prometheusRegistry, metadataCache.underlying(), "metadata_cache")
35+
fun Routing.metadataRoutes(prometheusRegistry: PrometheusMeterRegistry? = null) {
36+
prometheusRegistry?.let {
37+
CaffeineCacheMetrics.monitor(it, metadataCache.underlying(), "metadata_cache")
38+
}
3739

3840
route("{owner}/{name}/{file}") {
3941
metadata()

0 commit comments

Comments
 (0)