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

fix: add traces to updates with generared keys #112

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import scala.annotation.nowarn
@nowarn
private[doobie] case class TracedStatement(
p: PreparedStatement,
queryString: String
queryString: String,
c: Any*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does c stand for? Could you please use a more descriptive name?

I see that we didn't set a good example with p for preparedStatement on line 16, but we should do better in the future.

) extends PreparedStatement {
def executeQuery(): ResultSet = p.executeQuery()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object TracedTransactor {

def runTraced[A](f: TracedOp[A]): TracedOp[A] =
Kleisli {
case TracedStatement(p, sql) =>
case TracedStatement(p, sql, _*) =>
Trace[F].span(config.fullyQualifiedSpanName(formatQuery(extractQueryNameOrSql(sql))))(
Trace[F].put("span.type" -> "db") >> f(p)
)
Expand All @@ -93,12 +93,24 @@ object TracedTransactor {

override val executeQuery: TracedOp[ResultSet] =
runTraced(super.executeQuery)

}

override lazy val ConnectionInterpreter: ConnectionInterpreter =
new ConnectionInterpreter {
override def prepareStatement(a: String): Kleisli[F, Connection, PreparedStatement] =
super.prepareStatement(a).map(TracedStatement(_, a): PreparedStatement)
override def prepareStatement( a: String, b: Array[String]): Kleisli[F, Connection, PreparedStatement] =
super.prepareStatement(a, b).map(TracedStatement(_, a, b): PreparedStatement)
override def prepareStatement(a: String, b: Array[Int]): Kleisli[F, Connection, PreparedStatement] =
super.prepareStatement(a, b).map(TracedStatement(_, a, b): PreparedStatement)
override def prepareStatement(a: String, b: Int) =
super.prepareStatement(a, b).map(TracedStatement(_, a, b): PreparedStatement)
override def prepareStatement(a: String, b: Int, c: Int) =
super.prepareStatement(a, b, c).map(TracedStatement(_, a, b, c): PreparedStatement)
override def prepareStatement(a: String, b: Int, c: Int, d: Int) =
super.prepareStatement(a, b, c, d).map(TracedStatement(_, a, b, c, d): PreparedStatement)
Comment on lines +103 to +112
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the variadic parameter is not used anywhere, why is it needed to be set?


override def getTypeMap: Nothing =
super.getTypeMap.asInstanceOf // See: https://github.com/tpolecat/doobie/blob/v1.0.0-RC4/modules/core/src/test/scala/doobie/util/StrategySuite.scala#L47
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class TracedTransactorTest extends CatsEffectSuite {

database.test("Trace updates") { db =>
val create = sql"CREATE TABLE a (id INT, name VARCHAR)".update.run
val insert = sql"INSERT INTO a VALUES (${2: Int}, ${"abc": String})".update.run
val insert =
sql"INSERT INTO a VALUES (${2: Int}, ${"abc": String})".update.withUniqueGeneratedKeys[String]("name")
assertIO(
run((create >> insert).transact(db)).map(_.last),
SpanData("test-db:db.execute:INSERT INTO a VALUES (?, ?)", Map("span.type" -> "db"))
Expand Down