Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Playground] Change options and add metrics #1509

Merged
merged 4 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions modules/fbs-runner/checker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ dependencies {
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.14.2'
implementation 'mysql:mysql-connector-java:8.0.32'
implementation 'org.postgresql:postgresql:42.5.3'
implementation platform("io.opentelemetry:opentelemetry-bom:1.31.0")
implementation platform('io.opentelemetry:opentelemetry-bom-alpha:1.31.0-alpha')
implementation("io.opentelemetry:opentelemetry-api");
implementation("io.opentelemetry:opentelemetry-sdk");
implementation("io.opentelemetry:opentelemetry-sdk-metrics");
implementation("io.opentelemetry:opentelemetry-exporter-logging");
implementation('io.opentelemetry:opentelemetry-exporter-prometheus')
testImplementation 'junit:junit:4.13.2'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.util.{Failure, Success, Try}

class PsqlOperationsService(override val dbName: String, override val username: String, override val queryTimeout: Int)
extends DBOperationsService(dbName, username, queryTimeout) {
Expand Down Expand Up @@ -152,4 +153,19 @@

client.queryFuture(s"$tables $constrains $views $routines $triggers")
}

override def queryFutureWithTimeout(client: JDBCClient, sql: String): Future[ResultSet] = {
client.getConnectionFuture().flatMap(con => {
con.queryFuture(s"SET statement_timeout = ${queryTimeout*1000};").flatMap(_ => {
con.queryFuture(sql) transform {

Check warning on line 160 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/services/db/PsqlOperationsService.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/services/db/PsqlOperationsService.scala#L158-L160

Added lines #L158 - L160 were not covered by tests
case Success(result) =>
con.close()
Try(result)

Check warning on line 163 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/services/db/PsqlOperationsService.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/services/db/PsqlOperationsService.scala#L162-L163

Added lines #L162 - L163 were not covered by tests
case Failure(exception) =>
con.close()
Failure(throw exception)

Check warning on line 166 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/services/db/PsqlOperationsService.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/services/db/PsqlOperationsService.scala#L165-L166

Added lines #L165 - L166 were not covered by tests
}
})
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.thm.ii.fbs.util

import io.opentelemetry.exporter.prometheus.PrometheusHttpServer
import io.opentelemetry.sdk.OpenTelemetrySdk
import io.opentelemetry.sdk.metrics.SdkMeterProvider
import io.opentelemetry.sdk.resources.Resource

object Metrics {
private val prometheusHttpServer = PrometheusHttpServer.builder().build()
val openTelemetry: OpenTelemetrySdk = initOpenTelemetry()

Check warning on line 10 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/util/Metrics.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/util/Metrics.scala#L8-L10

Added lines #L8 - L10 were not covered by tests

private def initOpenTelemetry(): OpenTelemetrySdk = {
// Include required service.name resource attribute on all spans and metrics
val resource = Resource.getDefault.merge(Resource.builder.put("service-name", "fbs-runner").build)
val openTelemetrySdk = OpenTelemetrySdk.builder.setMeterProvider(
SdkMeterProvider.builder.setResource(resource).registerMetricReader(prometheusHttpServer).build

Check warning on line 16 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/util/Metrics.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/util/Metrics.scala#L14-L16

Added lines #L14 - L16 were not covered by tests
).buildAndRegisterGlobal
openTelemetrySdk

Check warning on line 18 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/util/Metrics.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/util/Metrics.scala#L18

Added line #L18 was not covered by tests
}
}

Check warning on line 20 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/util/Metrics.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/util/Metrics.scala#L20

Added line #L20 was not covered by tests
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import de.thm.ii.fbs.services.runner.SQLPlaygroundService
import de.thm.ii.fbs.types._
import de.thm.ii.fbs.util.DBTypes.PSQL_CONFIG_KEY
import de.thm.ii.fbs.util.PlaygroundDBConnections
import de.thm.ii.fbs.util.{Metrics, PlaygroundDBConnections}
import de.thm.ii.fbs.verticles.HttpVerticle
import de.thm.ii.fbs.verticles.runner.SqlPlaygroundVerticle.RUN_ADDRESS
import io.vertx.core.json.JsonObject
import io.vertx.lang.scala.{ScalaLogger, ScalaVerticle}
import io.vertx.scala.core.eventbus.Message
import io.vertx.scala.ext.jdbc.JDBCClient
import io.vertx.scala.ext.sql.ResultSet

import java.sql.{SQLException, SQLTimeoutException}
import java.util.Date
import scala.concurrent.Future
import scala.util.{Failure, Success}

Expand All @@ -31,6 +33,10 @@
class SqlPlaygroundVerticle extends ScalaVerticle {
private val logger = ScalaLogger.getLogger(this.getClass.getName)
private var sqlPools = Map[String, SqlPoolWithConfig]()
private val meter = Metrics.openTelemetry.meterBuilder("de.thm.mni.ii.fbs.verticles.runner.playground").build()
private val processingCounter = meter.upDownCounterBuilder("processingCount").setDescription("Processing Requests").build()
private val processingTimeCounter = meter.histogramBuilder("processingTime").ofLongs().setDescription("Time for processing").setUnit("ms").build()
private val errorCounter = meter.counterBuilder("errorCount").setDescription("Error Count").build()

Check warning on line 39 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala#L36-L39

Added lines #L36 - L39 were not covered by tests

/**
* start SqlRunnerVerticle
Expand All @@ -43,9 +49,9 @@
.put("user", config.getString("SQL_PLAYGROUND_PSQL_SERVER_USERNAME", "root"))
.put("password", config.getString("SQL_PLAYGROUND_PSQL_SERVER_PASSWORD", ""))
.put("url", config.getString("SQL_PLAYGROUND_PSQL_SERVER_URL", "jdbc:postgresql://localhost:5432"))
.put("max_pool_size", config.getInteger("SQL_PLAYGROUND_INSTANCES", 15))
.put("max_pool_size", config.getInteger("SQL_PLAYGROUND_INSTANCES", 256))

Check warning on line 52 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala#L52

Added line #L52 was not covered by tests
.put("driver_class", "org.postgresql.Driver")
.put("max_idle_time", config.getInteger("SQL_MAX_IDLE_TIME", 10))
.put("max_idle_time", config.getInteger("SQL_MAX_IDLE_TIME", 60))

Check warning on line 54 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala#L54

Added line #L54 was not covered by tests
.put("dataSourceName", psqlDataSource)
val psqlPool = JDBCClient.createShared(vertx, psqlConfig, psqlDataSource)
sqlPools += (PSQL_CONFIG_KEY -> SqlPoolWithConfig(psqlPool, psqlConfig))
Expand All @@ -56,16 +62,34 @@
private def startSqlPlayground(msg: Message[JsonObject]): Future[Unit] = Future {
val runArgs = msg.body().mapTo(classOf[SqlPlaygroundRunArgs])

processingCounter.add(1)
val startTime = new Date().getTime
val end = (failure: Boolean) => {
val endTime = new Date().getTime
processingTimeCounter.record(endTime - startTime)
processingCounter.add(-1)

Check warning on line 70 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala#L65-L70

Added lines #L65 - L70 were not covered by tests
if (failure) errorCounter.add(1)
}

try {
logger.info(s"SqlPlayground received execution ${runArgs.executionId}")

val con = getConnection(runArgs)

if (con.isDefined) {
executeQueries(runArgs, con.get)
executeQueries(runArgs, con.get) onComplete {

Check warning on line 80 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala#L80

Added line #L80 was not covered by tests
case Success(_) =>
end(false)

Check warning on line 82 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala#L82

Added line #L82 was not covered by tests
case Failure(_) =>
end(true)

Check warning on line 84 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala#L84

Added line #L84 was not covered by tests
}
} else {
end(false)

Check warning on line 87 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala#L87

Added line #L87 was not covered by tests
}
} catch {
case e: Throwable => handleError(runArgs, e)
case e: Throwable =>
end(true)
handleError(runArgs, e)

Check warning on line 92 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala#L91-L92

Added lines #L91 - L92 were not covered by tests
}
}

Expand All @@ -80,10 +104,10 @@
}
}

private def executeQueries(runArgs: SqlPlaygroundRunArgs, con: PlaygroundDBConnections): Unit = {
val sqlPlayground = new SQLPlaygroundService(runArgs, con, config.getInteger("SQL_QUERY_TIMEOUT_S", 10))
private def executeQueries(runArgs: SqlPlaygroundRunArgs, con: PlaygroundDBConnections): Future[(ResultSet, ResultSet)] = {
val sqlPlayground = new SQLPlaygroundService(runArgs, con, config.getInteger("SQL_QUERY_TIMEOUT_S", 1))

Check warning on line 108 in modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala

View check run for this annotation

Codecov / codecov/patch

modules/fbs-runner/checker/src/main/scala/de/thm/ii/fbs/verticles/runner/SqlPlaygroundVerticle.scala#L107-L108

Added lines #L107 - L108 were not covered by tests

sqlPlayground.executeStatement().onComplete({
sqlPlayground.executeStatement() andThen {
case Success(value) =>
try {
logger.info(s"Execution-${runArgs.executionId} Finished")
Expand All @@ -99,7 +123,7 @@
handleError(runArgs, ex, getSQLErrorMsg(runArgs, ex))
case Failure(ex) =>
handleError(runArgs, ex)
})
}
}

private def handleError(runArgs: SqlPlaygroundRunArgs, e: Throwable, msg: String = "Die Ausführung des Statements ist fehlgeschlagen."): Unit = {
Expand Down