Skip to content

Commit 973a546

Browse files
authored
feat(server): replace OpenTelemetry with Micrometer (#1823)
To test locally, follow these steps after installing docker and docker compose: 1. Execute `./gradlew jit-binding-server:publishImageToLocalRegistry` 2. Create `docker-compose.yml` with the following content: ``` services: github-workflows-kt: image: github-workflows-kt-jit-binding-server ports: - "8080:8080" environment: - GITHUB_TOKEN networks: prometheus: grafana: image: grafana/grafana:11.5.0 ports: - "3000:3000" volumes: - ./datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml networks: prometheus: prometheus: image: prom/prometheus:v3.1.0 configs: - source: prometheus target: /etc/prometheus/prometheus.yml ports: - "9090:9090" command: - "--config.file=/etc/prometheus/prometheus.yml" - "--web.enable-remote-write-receiver" networks: prometheus: networks: prometheus: configs: prometheus: file: prometheus.yml ``` 3. Create `.env` on the same folder as docker-compose: ``` GITHUB_TOKEN=XXX ``` where XXX is a generated github token with access to public repositories 4. Create `prometheus.yml` on the same folder as docker-compose: ``` global: scrape_interval: 5s scrape_configs: - job_name: 'ktor-app' metrics_path: '/metrics' static_configs: - targets: ['github-workflows-kt:8080'] ``` 5. Create `datasources.yaml` on the same folder as docker-compose: ``` apiVersion: 1 datasources: - name: Prometheus type: prometheus url: http://prometheus:9090/ access: proxy isDefault: true ``` 6. Execute `docker compose up -d` - Workflows should be accessible on port 8080 - Prometheus should be accessible on port 9090 - Grafana should be accessible on port 3000
1 parent e4349d1 commit 973a546

File tree

5 files changed

+29
-165
lines changed

5 files changed

+29
-165
lines changed

jit-binding-server/build.gradle.kts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ dependencies {
1616
implementation("io.ktor:ktor-serialization-kotlinx-json")
1717
implementation("io.ktor:ktor-server-call-logging")
1818
implementation("io.ktor:ktor-server-call-id")
19-
implementation("io.opentelemetry.instrumentation:opentelemetry-ktor-3.0:2.10.0-alpha")
20-
implementation("io.opentelemetry:opentelemetry-sdk:1.47.0")
21-
implementation("io.opentelemetry:opentelemetry-exporter-otlp:1.47.0")
22-
implementation("io.opentelemetry:opentelemetry-exporter-logging:1.47.0")
19+
implementation("io.ktor:ktor-server-metrics-micrometer")
20+
implementation("io.micrometer:micrometer-registry-prometheus:1.14.4")
21+
2322
implementation("io.github.reactivecircus.cache4k:cache4k:0.14.0")
2423
implementation("io.github.oshai:kotlin-logging:7.0.4")
2524
implementation(platform("org.apache.logging.log4j:log4j-bom:2.24.3"))

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ package io.github.typesafegithub.workflows.jitbindingserver
33
import io.ktor.server.response.respondText
44
import io.ktor.server.routing.Routing
55
import io.ktor.server.routing.get
6+
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
7+
8+
fun Routing.internalRoutes(prometheusRegistry: PrometheusMeterRegistry) {
9+
get("/metrics") {
10+
call.respondText(prometheusRegistry.scrape())
11+
}
612

7-
fun Routing.internalRoutes() {
813
get("/status") {
914
call.respondText("OK")
1015
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@ import io.ktor.server.engine.embeddedServer
77
import io.ktor.server.netty.Netty
88
import io.ktor.server.response.respondText
99
import io.ktor.server.routing.routing
10+
import io.micrometer.prometheusmetrics.PrometheusConfig
11+
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
12+
import java.time.Duration
1013

14+
private val prometheusRegistry =
15+
PrometheusMeterRegistry(
16+
object : PrometheusConfig {
17+
override fun get(key: String): String? = null
18+
19+
override fun prefix(): String = "github-actions-binding-server"
20+
21+
override fun step() = Duration.ofSeconds(10)
22+
},
23+
)
1124
private val logger =
1225
System
1326
/*
@@ -25,10 +38,10 @@ fun main() {
2538
logger.error(throwable) { "Uncaught exception in thread $thread" }
2639
}
2740
embeddedServer(Netty, port = 8080) {
28-
installPlugins()
41+
installPlugins(prometheusRegistry)
2942

3043
routing {
31-
internalRoutes()
44+
internalRoutes(prometheusRegistry)
3245

3346
artifactRoutes()
3447
metadataRoutes()

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

Lines changed: 0 additions & 154 deletions
This file was deleted.

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package io.github.typesafegithub.workflows.jitbindingserver
33
import io.ktor.http.HttpHeaders
44
import io.ktor.server.application.Application
55
import io.ktor.server.application.install
6+
import io.ktor.server.metrics.micrometer.MicrometerMetrics
67
import io.ktor.server.plugins.callid.CallId
78
import io.ktor.server.plugins.callid.callIdMdc
89
import io.ktor.server.plugins.callid.generate
910
import io.ktor.server.plugins.calllogging.CallLogging
10-
import io.opentelemetry.instrumentation.ktor.v3_0.server.KtorServerTracing
11+
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
1112

12-
fun Application.installPlugins() {
13+
fun Application.installPlugins(prometheusRegistry: PrometheusMeterRegistry) {
1314
install(CallId) {
1415
generate(15, "abcdefghijklmnopqrstuvwxyz0123456789")
1516
replyToHeader(HttpHeaders.XRequestId)
@@ -19,7 +20,7 @@ fun Application.installPlugins() {
1920
callIdMdc("request-id")
2021
}
2122

22-
install(KtorServerTracing) {
23-
setOpenTelemetry(buildOpenTelemetryConfig("github-actions-bindings"))
23+
install(MicrometerMetrics) {
24+
registry = prometheusRegistry
2425
}
2526
}

0 commit comments

Comments
 (0)