@@ -11,13 +11,16 @@ import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
11
11
import io.ktor.http.ContentType
12
12
import io.ktor.http.HttpStatusCode
13
13
import io.ktor.server.application.ApplicationCall
14
+ import io.ktor.server.request.httpMethod
14
15
import io.ktor.server.response.respondBytes
15
16
import io.ktor.server.response.respondText
16
17
import io.ktor.server.routing.Route
17
18
import io.ktor.server.routing.Routing
18
19
import io.ktor.server.routing.get
19
20
import io.ktor.server.routing.head
20
21
import io.ktor.server.routing.route
22
+ import io.micrometer.core.instrument.Tag
23
+ import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
21
24
import kotlin.time.Duration.Companion.hours
22
25
23
26
private val logger = logger { }
@@ -26,27 +29,35 @@ typealias ArtifactResult = Result<Map<String, Artifact>>
26
29
27
30
private val bindingsCache = Cache .Builder <ActionCoords , ArtifactResult >().expireAfterWrite(1 .hours).build()
28
31
29
- fun Routing.artifactRoutes () {
32
+ fun Routing.artifactRoutes (prometheusRegistry : PrometheusMeterRegistry ) {
30
33
route(" {owner}/{name}/{version}/{file}" ) {
31
- artifact(refresh = false )
34
+ artifact(prometheusRegistry, refresh = false )
32
35
}
33
36
34
37
route(" /refresh/{owner}/{name}/{version}/{file}" ) {
35
- artifact(refresh = true )
38
+ artifact(prometheusRegistry, refresh = true )
36
39
}
37
40
}
38
41
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)
42
48
}
43
49
44
- private fun Route.headArtifact (refresh : Boolean ) {
50
+ private fun Route.headArtifact (
51
+ prometheusRegistry : PrometheusMeterRegistry ,
52
+ refresh : Boolean ,
53
+ ) {
45
54
head {
46
55
val bindingArtifacts = call.toBindingArtifacts(refresh) ? : return @head call.respondNotFound()
47
56
48
57
val file = call.parameters[" file" ] ? : return @head call.respondNotFound()
49
58
59
+ incrementArtifactCounter(prometheusRegistry, call)
60
+
50
61
if (file in bindingArtifacts) {
51
62
call.respondText(" Exists" , status = HttpStatusCode .OK )
52
63
} else {
@@ -55,14 +66,19 @@ private fun Route.headArtifact(refresh: Boolean) {
55
66
}
56
67
}
57
68
58
- private fun Route.getArtifact (refresh : Boolean ) {
69
+ private fun Route.getArtifact (
70
+ prometheusRegistry : PrometheusMeterRegistry ,
71
+ refresh : Boolean ,
72
+ ) {
59
73
get {
60
74
val bindingArtifacts = call.toBindingArtifacts(refresh) ? : return @get call.respondNotFound()
61
75
62
76
if (refresh && ! deliverOnRefreshRoute) return @get call.respondText(" OK" )
63
77
64
78
val file = call.parameters[" file" ] ? : return @get call.respondNotFound()
65
79
80
+ incrementArtifactCounter(prometheusRegistry, call)
81
+
66
82
val artifact = bindingArtifacts[file] ? : return @get call.respondNotFound()
67
83
68
84
when (artifact) {
@@ -85,3 +101,33 @@ private suspend fun ApplicationCall.toBindingArtifacts(refresh: Boolean): Map<St
85
101
bindingsCache.get(actionCoords) { runCatching { actionCoords.buildVersionArtifacts()!! } }.getOrNull()
86
102
}
87
103
}
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
+ }
0 commit comments