forked from typelevel/doobie
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
394 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version = 3.7.14 | ||
version = 3.7.17 | ||
runner.dialect = scala213source3 | ||
|
||
align.preset = none | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
modules/zio/src/it/scala/zio/internal/metrics/MetricRegistryExposed.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
modules/zio/src/it/scala/zoobie/sqlcommenter/SQLCommenterIntegrationSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package zoobie.sqlcommenter | ||
|
||
import doobie.syntax.string.* | ||
import io.opentelemetry.api.common.AttributeKey | ||
import io.opentelemetry.api.common.Attributes | ||
import io.opentelemetry.api.trace.Span | ||
import io.opentelemetry.api.trace.SpanContext | ||
import io.opentelemetry.api.trace.StatusCode | ||
import io.opentelemetry.api.trace.TraceFlags | ||
import io.opentelemetry.api.trace.TraceState | ||
import zio.Chunk | ||
import zio.ZIO | ||
import zio.durationInt | ||
import zio.test.TestAspect | ||
import zio.test.ZIOSpecDefault | ||
import zio.test.assertCompletes | ||
import zoobie.ConnectionPoolConfig | ||
import zoobie.Transactor | ||
import zoobie.postgres.PostgreSQLConnectionConfig | ||
import zoobie.postgres.pool | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
object SQLCommenterIntegrationSpec extends ZIOSpecDefault { | ||
|
||
override val spec = test("SQLCommenterIntegrationSpec") { | ||
val spanContext = new SpanContext { | ||
override val getTraceId = "3b120af54ca6f7efacddf3e538dd4988" | ||
override val getSpanId = "7cdf802020b41208" | ||
override val getTraceFlags = TraceFlags.getSampled | ||
override val getTraceState = TraceState.builder().put("key", "value").build() | ||
override val isRemote = false | ||
} | ||
val span = new Span { | ||
override def setAttribute[T](key: AttributeKey[T], value: T) = ??? | ||
override def addEvent(name: String, attributes: Attributes) = ??? | ||
override def addEvent(name: String, attributes: Attributes, timestamp: Long, unit: TimeUnit) = ??? | ||
override def setStatus(statusCode: StatusCode, description: String) = ??? | ||
override def recordException(exception: Throwable, additionalAttributes: Attributes) = ??? | ||
override def updateName(name: String) = ??? | ||
override def end(): Unit = ??? | ||
override def end(timestamp: Long, unit: TimeUnit): Unit = ??? | ||
override def isRecording = ??? | ||
override def getSpanContext = spanContext | ||
} | ||
|
||
for { | ||
p <- pool(connectionConfig, config) | ||
interpreter = TraceInterpreter.create(Transactor.kleisliInterpreter, ZIO.succeed(Some(span))) | ||
transactor = Transactor(p.get, interpreter.ConnectionInterpreter, Transactor.strategies.transactional) | ||
_ <- transactor.run(fr"SELECT 1".query[Int].unique) | ||
} yield { | ||
assertCompletes | ||
} | ||
} | ||
|
||
override val aspects = super.aspects ++ Chunk( | ||
TestAspect.timed, | ||
TestAspect.timeout(90.seconds), | ||
TestAspect.withLiveClock, | ||
) | ||
|
||
private lazy val connectionConfig = PostgreSQLConnectionConfig( | ||
host = "localhost", | ||
database = "world", | ||
username = "postgres", | ||
password = "password", | ||
applicationName = "doobie", | ||
) | ||
|
||
private lazy val config = ConnectionPoolConfig( | ||
name = "zoobie-postgres-it", | ||
size = 5, | ||
queueSize = 1_000, | ||
maxConnectionLifetime = 30.seconds, | ||
validationTimeout = 2.seconds, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
modules/zio/src/main/scala/zoobie/sqlcommenter/SQLCommenter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package zoobie.sqlcommenter | ||
|
||
import java.net.URLEncoder | ||
import java.nio.charset.StandardCharsets | ||
import scala.collection.immutable.SortedMap | ||
import scala.jdk.CollectionConverters.* | ||
|
||
// https://google.github.io/sqlcommenter/spec/ | ||
final case class SQLCommenter( | ||
controller: Option[String], | ||
action: Option[String], | ||
framework: Option[String], | ||
trace: Option[SQLCommenter.Trace], | ||
) { | ||
import SQLCommenter.serializeKeyValues | ||
|
||
def format: String = { | ||
val traceState = trace.flatMap(_.state).map { state => | ||
state | ||
.filter { case (k, _) => k.nonEmpty } | ||
.map { case (k, v) => s"$k=$v" } | ||
.mkString(",") | ||
} | ||
val m = SortedMap( | ||
"controller" -> controller, | ||
"action" -> action, | ||
"framework" -> framework, | ||
"traceparent" -> trace.map(_.parent), | ||
"tracestate" -> traceState, | ||
).collect { case (k, Some(v)) => (k, v) } | ||
serializeKeyValues(m) | ||
} | ||
|
||
} | ||
object SQLCommenter { | ||
|
||
final case class Trace( | ||
traceId: String, | ||
spanId: String, | ||
options: Byte, | ||
state: Option[Map[String, String]], | ||
) { | ||
def parent = String.format("00-%s-%s-%02X", traceId, spanId, options) | ||
} | ||
object Trace { | ||
|
||
def fromOpenTelemetryContext(spanContext: io.opentelemetry.api.trace.SpanContext) = { | ||
Option(spanContext).filter(_.isValid).map { ctx => | ||
val traceId = ctx.getTraceId | ||
val spanId = ctx.getSpanId | ||
val options = ctx.getTraceFlags | ||
|
||
val state = Option(ctx.getTraceState).filter(!_.isEmpty).map { state => | ||
state.asMap().asScala.toMap | ||
} | ||
|
||
Trace(traceId = traceId, spanId = spanId, options.asByte, state) | ||
} | ||
} | ||
} | ||
|
||
private[sqlcommenter] val serializeKey = | ||
urlEncode andThen escapeMetaCharacters | ||
private[sqlcommenter] val serializeValue = | ||
urlEncode andThen escapeMetaCharacters andThen sqlEscape | ||
|
||
private[sqlcommenter] def serializeKeyValue(k: String, v: String) = s"${serializeKey(k)}=${serializeValue(v)}" | ||
|
||
private[sqlcommenter] def serializeKeyValues(m: Map[String, String]) = { | ||
if (m.isEmpty) "" | ||
else m.toList.sorted.map(serializeKeyValue.tupled).mkString(",") | ||
} | ||
|
||
@SuppressWarnings(Array("org.wartremover.warts.Null")) | ||
private def urlEncode(s: String) = { | ||
URLEncoder.encode(s, StandardCharsets.UTF_8) | ||
.replaceAll("%27", "'") | ||
.replaceAll("\\+", "%20") | ||
} | ||
private def escapeMetaCharacters(s: String) = s.replaceAll("'", "\\\\'") | ||
private def sqlEscape(s: String) = s"'$s'" | ||
|
||
def affix(state: SQLCommenter, sql: String): String = { | ||
val commentStr = state.format | ||
if (commentStr.isEmpty) sql else sql.concat(s"\n/*${commentStr}*/") | ||
} | ||
} |
Oops, something went wrong.