You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2021/07/25 15:38:35 UTC

[incubator-tuweni] branch main updated: Add OpenTelemetry tracing to JSON-RPC

This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/main by this push:
     new 210a91f  Add OpenTelemetry tracing to JSON-RPC
     new cd8a9f3  Merge pull request #318 from atoulme/add_tracing_jsonrpc
210a91f is described below

commit 210a91fd92e206cc6feeeb9a91a54ce407bd9ebb
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Sat Jul 24 23:35:47 2021 -0700

    Add OpenTelemetry tracing to JSON-RPC
---
 dependency-versions.gradle                         |  5 +++--
 jsonrpc-app/build.gradle                           |  2 ++
 .../org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt    | 25 ++++++++++++----------
 .../org/apache/tuweni/jsonrpc/JSONRPCClient.kt     |  7 ++++--
 .../org/apache/tuweni/jsonrpc/JSONRPCServer.kt     |  3 ++-
 .../org/apache/tuweni/metrics/MetricsService.kt    |  9 +++++++-
 6 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/dependency-versions.gradle b/dependency-versions.gradle
index 2aee862..3875d70 100644
--- a/dependency-versions.gradle
+++ b/dependency-versions.gradle
@@ -59,10 +59,11 @@ dependencyManagement {
 
     dependencySet(group: 'io.vertx', version: '4.1.2') {
       entry 'vertx-core'
-      entry 'vertx-web'
-      entry 'vertx-web-client'
       entry 'vertx-lang-kotlin'
       entry 'vertx-lang-kotlin-coroutines'
+      entry 'vertx-opentelemetry'
+      entry 'vertx-web'
+      entry 'vertx-web-client'
     }
 
     dependency('javax.servlet:javax.servlet-api:4.0.1')
diff --git a/jsonrpc-app/build.gradle b/jsonrpc-app/build.gradle
index b84f129..9c1037f 100644
--- a/jsonrpc-app/build.gradle
+++ b/jsonrpc-app/build.gradle
@@ -35,6 +35,8 @@ dependencies {
   implementation 'io.vertx:vertx-core'
   implementation 'io.vertx:vertx-lang-kotlin-coroutines'
   implementation 'io.vertx:vertx-lang-kotlin'
+  implementation 'io.vertx:vertx-opentelemetry'
+  implementation 'io.opentelemetry:opentelemetry-sdk'
   implementation 'io.opentelemetry:opentelemetry-sdk-metrics'
   implementation 'javax.servlet:javax.servlet-api'
   implementation 'org.bouncycastle:bcprov-jdk15on'
diff --git a/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt b/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt
index f7db85f..0c8debe 100644
--- a/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt
+++ b/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt
@@ -17,6 +17,8 @@
 package org.apache.tuweni.jsonrpc.app
 
 import io.vertx.core.Vertx
+import io.vertx.core.VertxOptions
+import io.vertx.tracing.opentelemetry.OpenTelemetryOptions
 import kotlinx.coroutines.asCoroutineDispatcher
 import kotlinx.coroutines.runBlocking
 import org.apache.tuweni.eth.JSONRPCRequest
@@ -41,7 +43,7 @@ object JSONRPCApp {
   fun main(args: Array<String>) {
     val configFile = Paths.get(if (args.isNotEmpty()) args[0] else "config.toml")
     Security.addProvider(BouncyCastleProvider())
-    val vertx = Vertx.vertx()
+
     val config = JSONRPCConfig(configFile)
     if (config.config.hasErrors()) {
       for (error in config.config.errors()) {
@@ -49,24 +51,25 @@ object JSONRPCApp {
       }
       System.exit(1)
     }
-    val app = JSONRPCApplication(vertx, config)
+    val metricsService = MetricsService(
+      "json-rpc",
+      port = config.metricsPort(),
+      networkInterface = config.metricsNetworkInterface(),
+      enableGrpcPush = config.metricsGrpcPushEnabled(),
+      enablePrometheus = config.metricsPrometheusEnabled()
+    )
+    val vertx = Vertx.vertx(VertxOptions().setTracingOptions(OpenTelemetryOptions(metricsService.openTelemetry)))
+    val app = JSONRPCApplication(vertx, config, metricsService)
     app.run()
   }
 }
 
 class JSONRPCApplication(
   val vertx: Vertx,
-  val config: JSONRPCConfig
+  val config: JSONRPCConfig,
+  val metricsService: MetricsService
 ) {
 
-  private val metricsService = MetricsService(
-    "json-rpc",
-    port = config.metricsPort(),
-    networkInterface = config.metricsNetworkInterface(),
-    enableGrpcPush = config.metricsGrpcPushEnabled(),
-    enablePrometheus = config.metricsPrometheusEnabled()
-  )
-
   fun run() {
     // TODO allow more options such as allowlist of certificates, enforce client authentication.
     val trustOptions = VertxTrustOptions.recordClientFingerprints(config.clientFingerprintsFile())
diff --git a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt
index 8d116ad..0440271 100644
--- a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt
+++ b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt
@@ -19,7 +19,9 @@ package org.apache.tuweni.jsonrpc
 import com.fasterxml.jackson.databind.ObjectMapper
 import io.vertx.core.Vertx
 import io.vertx.core.buffer.Buffer
+import io.vertx.core.tracing.TracingPolicy
 import io.vertx.ext.web.client.WebClient
+import io.vertx.ext.web.client.WebClientOptions
 import kotlinx.coroutines.CompletableDeferred
 import org.apache.tuweni.eth.Address
 import org.apache.tuweni.eth.Transaction
@@ -34,10 +36,11 @@ val mapper = ObjectMapper()
 class JSONRPCClient(
   vertx: Vertx,
   val serverPort: Int,
-  val serverHost: String
+  val serverHost: String,
+  val userAgent: String = "Apache Tuweni JSON-RPC Client",
 ) : Closeable {
 
-  val client = WebClient.create(vertx)
+  val client = WebClient.create(vertx, WebClientOptions().setUserAgent(userAgent).setTryUseCompression(true).setTracingPolicy(TracingPolicy.ALWAYS) as WebClientOptions)
 
   /**
    * Sends a signed transaction to the Ethereum network.
diff --git a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCServer.kt b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCServer.kt
index d9e466f..5c6ffbe 100644
--- a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCServer.kt
+++ b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCServer.kt
@@ -28,6 +28,7 @@ import io.vertx.core.http.HttpServer
 import io.vertx.core.http.HttpServerOptions
 import io.vertx.core.json.JsonObject
 import io.vertx.core.net.TrustOptions
+import io.vertx.core.tracing.TracingPolicy
 import io.vertx.ext.auth.AuthProvider
 import io.vertx.ext.auth.User
 import io.vertx.ext.auth.authorization.Authorization
@@ -88,7 +89,7 @@ class JSONRPCServer(
   }
 
   fun start() = async {
-    val serverOptions = HttpServerOptions().setPort(port).setHost(networkInterface).setSsl(ssl)
+    val serverOptions = HttpServerOptions().setPort(port).setHost(networkInterface).setSsl(ssl).setTracingPolicy(TracingPolicy.ALWAYS)
     trustOptions?.let {
       serverOptions.setTrustOptions(it)
     }
diff --git a/metrics/src/main/kotlin/org/apache/tuweni/metrics/MetricsService.kt b/metrics/src/main/kotlin/org/apache/tuweni/metrics/MetricsService.kt
index 727f4ca..1fde892 100644
--- a/metrics/src/main/kotlin/org/apache/tuweni/metrics/MetricsService.kt
+++ b/metrics/src/main/kotlin/org/apache/tuweni/metrics/MetricsService.kt
@@ -31,7 +31,14 @@ import io.prometheus.client.CollectorRegistry
 import io.prometheus.client.exporter.HTTPServer
 import java.net.InetSocketAddress
 
-class MetricsService(jobName: String, reportingIntervalMillis: Long = 5000, port: Int = 9090, networkInterface: String = "0.0.0.0", enablePrometheus: Boolean = true, enableGrpcPush: Boolean = true) {
+class MetricsService(
+  jobName: String,
+  reportingIntervalMillis: Long = 5000,
+  port: Int = 9090,
+  networkInterface: String = "0.0.0.0",
+  enablePrometheus: Boolean = true,
+  enableGrpcPush: Boolean = true,
+) {
 
   private val server: HTTPServer?
   val meterSdkProvider: SdkMeterProvider

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org