Add optional OpenTelemetry Netty server instrumentation

- Update lys.version to 2026.04.14

- Add optional compileOnly dependency on opentelemetry-netty-4.1 in rbcs-server

- Add runtime guard to only activate instrumentation when OTel classes are on classpath

- Insert OTel combined handler after HttpServerCodec in the Netty pipeline

- Add requires-static JPMS directives for optional module support
This commit is contained in:
OpenCode
2026-04-28 11:51:19 +00:00
parent 5d190d81ab
commit ffe84fd331
14 changed files with 125 additions and 2 deletions

View File

@@ -4,7 +4,7 @@ org.gradle.caching=true
rbcs.version = 0.5.0
lys.version = 2026.03.26
lys.version = 2026.04.28
gitea.maven.url = https://gitea.woggioni.net/api/packages/woggioni/maven
docker.registry.url=gitea.woggioni.net

View File

@@ -16,6 +16,7 @@ import java.util.stream.Collectors;
@Value
public class Configuration {
boolean enableTelemetry;
String host;
int port;
boolean proxyProtocolEnabled;
@@ -150,6 +151,7 @@ public class Configuration {
}
public static Configuration of(
boolean enableTelemetry,
String host,
int port,
boolean proxyProtocolEnabled,
@@ -166,6 +168,7 @@ public class Configuration {
Tls tls
) {
return new Configuration(
enableTelemetry,
host,
port,
proxyProtocolEnabled,

View File

@@ -13,6 +13,11 @@ dependencies {
implementation catalog.netty.buffer
implementation catalog.netty.transport
implementation catalog.netty.codec.haproxy
compileOnly catalog.opentelemetry.netty['4']['1']
compileOnly catalog.opentelemetry.sdk.extension.autoconfigure
compileOnly catalog.opentelemetry.logback.appender['1']['0']
compileOnly catalog.opentelemetry.extension.trace.propagators
compileOnly catalog.logback.classic
api project(':rbcs-common')
api project(':rbcs-api')

View File

@@ -17,6 +17,11 @@ module net.woggioni.rbcs.server {
requires io.netty.common;
requires io.netty.codec;
requires io.netty.codec.haproxy;
requires static io.opentelemetry.api;
requires static io.opentelemetry.instrumentation.netty_4_1;
requires static io.opentelemetry.sdk.autoconfigure;
requires static io.opentelemetry.instrumentation.logback_appender_1_0;
requires static io.opentelemetry.extension.trace.propagation;
requires org.slf4j;
exports net.woggioni.rbcs.server;

View File

@@ -68,6 +68,8 @@ import net.woggioni.rbcs.common.Xml
import net.woggioni.rbcs.common.createLogger
import net.woggioni.rbcs.common.debug
import net.woggioni.rbcs.common.info
import net.woggioni.rbcs.server.otel.OtelIntegration
import net.woggioni.rbcs.server.otel.OtelSdkIntegration
import net.woggioni.rbcs.server.auth.AbstractNettyHttpAuthenticator
import net.woggioni.rbcs.server.auth.Authorizer
import net.woggioni.rbcs.server.auth.RoleAuthorizer
@@ -431,6 +433,7 @@ class RemoteBuildCacheServer(private val cfg: Configuration) {
maxChunkSize = cfg.connection.chunkSize
}
pipeline.addLast(HttpServerCodec(httpDecoderConfig))
OtelIntegration.createHandler(cfg.isEnableTelemetry)?.let { pipeline.addLast(it) }
pipeline.addLast(ReadTriggerDuplexHandler.NAME, ReadTriggerDuplexHandler())
pipeline.addLast(MaxRequestSizeHandler.NAME, MaxRequestSizeHandler(cfg.connection.maxRequestSize))
pipeline.addLast(HttpChunkContentCompressor(1024))
@@ -525,6 +528,7 @@ class RemoteBuildCacheServer(private val cfg: Configuration) {
}
fun run(): ServerHandle {
OtelSdkIntegration.initialize(cfg.isEnableTelemetry)
// Create the multithreaded event loops for the server
val bossGroup = MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory())
val channelFactory = ChannelFactory<SocketChannel> { NioSocketChannel() }

View File

@@ -46,6 +46,8 @@ object Parser {
var groups = emptyMap<String, Group>()
var tls: Tls? = null
val serverPath = root.renderAttribute("path")
var enableTelemetry = root.renderAttribute("enable-telemetry")
?.let(String::toBoolean) ?: false
var incomingConnectionsBacklogSize = 1024
var authentication: Authentication? = null
for (child in root.asIterable()) {
@@ -233,6 +235,7 @@ object Parser {
}
}
return Configuration.of(
enableTelemetry,
host,
port,
proxyProtocolEnabled,

View File

@@ -29,6 +29,7 @@ object Serializer {
?.let { serverPath ->
attr("path", serverPath)
}
attr("enable-telemetry", conf.isEnableTelemetry.toString())
node("bind") {
attr("host", conf.host)
attr("port", conf.port.toString())

View File

@@ -0,0 +1,32 @@
package net.woggioni.rbcs.server.otel
import io.netty.channel.ChannelHandler
import io.opentelemetry.api.GlobalOpenTelemetry
import io.opentelemetry.instrumentation.netty.v4_1.NettyServerTelemetry
import net.woggioni.rbcs.common.createLogger
import net.woggioni.rbcs.common.warn
object OtelIntegration {
private val log = createLogger<OtelIntegration>()
val isAvailable: Boolean by lazy {
runCatching {
Class.forName("io.opentelemetry.api.OpenTelemetry")
}.fold(
onSuccess = { true },
onFailure = {
log.warn { "OpenTelemetry classes not on classpath, instrumentation disabled" }
false
},
)
}
fun createHandler(enabled: Boolean): ChannelHandler? {
return if (enabled && isAvailable) createHandlerInternal() else null
}
private fun createHandlerInternal(): ChannelHandler {
return NettyServerTelemetry.create(GlobalOpenTelemetry.get()).createCombinedHandler()
}
}

View File

@@ -0,0 +1,58 @@
package net.woggioni.rbcs.server.otel
import net.woggioni.rbcs.common.createLogger
import net.woggioni.rbcs.common.info
object OtelSdkIntegration {
private val log = createLogger<OtelSdkIntegration>()
private val isAvailable: Boolean by lazy {
runCatching {
Class.forName("io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk")
}.fold(
onSuccess = { true },
onFailure = {
log.info { "OpenTelemetry SDK autoconfigure not on classpath" }
false
},
)
}
private val appenderAvailable: Boolean by lazy {
runCatching {
Class.forName("io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender")
}.fold(
onSuccess = { true },
onFailure = {
log.info { "OpenTelemetry logback appender not on classpath" }
false
},
)
}
fun initialize(enabled: Boolean) {
if (!enabled || !isAvailable) return
log.info { "Initializing OpenTelemetry SDK with auto-configuration" }
val sdk = io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal()
.build()
.openTelemetrySdk
if (appenderAvailable) {
runCatching {
val clazz = Class.forName("io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender")
clazz.getMethod("install", Class.forName("io.opentelemetry.api.OpenTelemetry"))
.invoke(null, sdk)
log.info { "OpenTelemetry logback appender installed" }
}.onFailure { ex ->
val msg = ex.localizedMessage ?: ex.javaClass.name
log.info { "Failed to install OpenTelemetry logback appender: $msg" }
}
}
log.info { "OpenTelemetry SDK initialized successfully" }
}
}

View File

@@ -2,7 +2,8 @@
<rbcs:server
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rbcs="urn:net.woggioni.rbcs.server"
xs:schemaLocation="urn:net.woggioni.rbcs.server jpms://net.woggioni.rbcs.server/net/woggioni/rbcs/server/schema/rbcs-server.xsd">
xs:schemaLocation="urn:net.woggioni.rbcs.server jpms://net.woggioni.rbcs.server/net/woggioni/rbcs/server/schema/rbcs-server.xsd"
enable-telemetry="false">
<bind host="127.0.0.1" port="8080" incoming-connections-backlog-size="1024"/>
<cache xs:type="rbcs:fileSystemCacheType" path="${sys:java.io.tmpdir}/rbcs" max-age="P7D"/>
</rbcs:server>

View File

@@ -59,6 +59,14 @@
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="enable-telemetry" type="xs:boolean" use="optional" default="false">
<xs:annotation>
<xs:documentation>
Enable OpenTelemetry distributed tracing for the server.
Even when enabled, telemetry only activates if OpenTelemetry classes are present on the classpath.
</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:complexType name="bindType">

View File

@@ -32,6 +32,7 @@ abstract class AbstractBasicAuthServerTest : AbstractServerTest() {
override fun setUp() {
this.cacheDir = testDir.resolve("cache")
cfg = Configuration.of(
false,
"127.0.0.1",
getFreePort(),
false,

View File

@@ -138,6 +138,7 @@ abstract class AbstractTlsServerTest : AbstractServerTest() {
this.cacheDir = testDir.resolve("cache")
createKeyStoreAndTrustStore()
cfg = Configuration(
false,
"127.0.0.1",
getFreePort(),
false,

View File

@@ -32,6 +32,7 @@ class NoAuthServerTest : AbstractServerTest() {
override fun setUp() {
this.cacheDir = testDir.resolve("cache")
cfg = Configuration(
false,
"127.0.0.1",
getFreePort(),
false,