Skip to content

Commit 31ef2bd

Browse files
authored
feat(server): add metrics to Artifact Route (#1824)
1 parent b435b63 commit 31ef2bd

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

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

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
1111
import io.ktor.http.ContentType
1212
import io.ktor.http.HttpStatusCode
1313
import io.ktor.server.application.ApplicationCall
14+
import io.ktor.server.request.httpMethod
1415
import io.ktor.server.response.respondBytes
1516
import io.ktor.server.response.respondText
1617
import io.ktor.server.routing.Route
1718
import io.ktor.server.routing.Routing
1819
import io.ktor.server.routing.get
1920
import io.ktor.server.routing.head
2021
import io.ktor.server.routing.route
22+
import io.micrometer.core.instrument.Tag
23+
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
2124
import kotlin.time.Duration.Companion.hours
2225

2326
private val logger = logger { }
@@ -26,27 +29,35 @@ typealias ArtifactResult = Result<Map<String, Artifact>>
2629

2730
private val bindingsCache = Cache.Builder<ActionCoords, ArtifactResult>().expireAfterWrite(1.hours).build()
2831

29-
fun Routing.artifactRoutes() {
32+
fun Routing.artifactRoutes(prometheusRegistry: PrometheusMeterRegistry) {
3033
route("{owner}/{name}/{version}/{file}") {
31-
artifact(refresh = false)
34+
artifact(prometheusRegistry, refresh = false)
3235
}
3336

3437
route("/refresh/{owner}/{name}/{version}/{file}") {
35-
artifact(refresh = true)
38+
artifact(prometheusRegistry, refresh = true)
3639
}
3740
}
3841

39-
private fun Route.artifact(refresh: Boolean = false) {
40-
headArtifact(refresh)
41-
getArtifact(refresh)
42+
private fun Route.artifact(
43+
prometheusRegistry: PrometheusMeterRegistry,
44+
refresh: Boolean = false,
45+
) {
46+
headArtifact(prometheusRegistry, refresh)
47+
getArtifact(prometheusRegistry, refresh)
4248
}
4349

44-
private fun Route.headArtifact(refresh: Boolean) {
50+
private fun Route.headArtifact(
51+
prometheusRegistry: PrometheusMeterRegistry,
52+
refresh: Boolean,
53+
) {
4554
head {
4655
val bindingArtifacts = call.toBindingArtifacts(refresh) ?: return@head call.respondNotFound()
4756

4857
val file = call.parameters["file"] ?: return@head call.respondNotFound()
4958

59+
incrementArtifactCounter(prometheusRegistry, call)
60+
5061
if (file in bindingArtifacts) {
5162
call.respondText("Exists", status = HttpStatusCode.OK)
5263
} else {
@@ -55,14 +66,19 @@ private fun Route.headArtifact(refresh: Boolean) {
5566
}
5667
}
5768

58-
private fun Route.getArtifact(refresh: Boolean) {
69+
private fun Route.getArtifact(
70+
prometheusRegistry: PrometheusMeterRegistry,
71+
refresh: Boolean,
72+
) {
5973
get {
6074
val bindingArtifacts = call.toBindingArtifacts(refresh) ?: return@get call.respondNotFound()
6175

6276
if (refresh && !deliverOnRefreshRoute) return@get call.respondText("OK")
6377

6478
val file = call.parameters["file"] ?: return@get call.respondNotFound()
6579

80+
incrementArtifactCounter(prometheusRegistry, call)
81+
6682
val artifact = bindingArtifacts[file] ?: return@get call.respondNotFound()
6783

6884
when (artifact) {
@@ -85,3 +101,33 @@ private suspend fun ApplicationCall.toBindingArtifacts(refresh: Boolean): Map<St
85101
bindingsCache.get(actionCoords) { runCatching { actionCoords.buildVersionArtifacts()!! } }.getOrNull()
86102
}
87103
}
104+
105+
private fun incrementArtifactCounter(
106+
prometheusRegistry: PrometheusMeterRegistry,
107+
call: ApplicationCall,
108+
) {
109+
val owner = call.parameters["owner"] ?: "unknown"
110+
val name = call.parameters["name"] ?: "unknown"
111+
val version = call.parameters["version"] ?: "unknown"
112+
val file = call.parameters["file"] ?: "unknown"
113+
val method = call.request.httpMethod.value
114+
val status =
115+
call.response
116+
.status()
117+
?.value
118+
?.toString() ?: "unknown"
119+
120+
val counter =
121+
prometheusRegistry.counter(
122+
"artifact_requests_total",
123+
listOf(
124+
Tag.of("owner", owner),
125+
Tag.of("name", name),
126+
Tag.of("version", version),
127+
Tag.of("file", file),
128+
Tag.of("method", method),
129+
Tag.of("status", status),
130+
),
131+
)
132+
counter.increment()
133+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fun main() {
4343
routing {
4444
internalRoutes(prometheusRegistry)
4545

46-
artifactRoutes()
46+
artifactRoutes(prometheusRegistry)
4747
metadataRoutes()
4848
}
4949
}.start(wait = true)

0 commit comments

Comments
 (0)