Skip to content

Commit

Permalink
Merge pull request #857 from rossabaker/otel4s-0.2
Browse files Browse the repository at this point in the history
otel4s-0.2.1
  • Loading branch information
mpilquist committed May 8, 2023
2 parents be9e8a6 + 92f689a commit 2a9b469
Show file tree
Hide file tree
Showing 47 changed files with 300 additions and 241 deletions.
19 changes: 10 additions & 9 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ ThisBuild / mimaBinaryIssueFilters ++= List(

// This is used in a couple places
lazy val fs2Version = "3.7.0-RC5"
lazy val natchezVersion = "0.3.1"
lazy val openTelemetryVersion = "1.25.0"
lazy val otel4sVersion = "0.2.1"

// Global Settings
lazy val commonSettings = Seq(
Expand Down Expand Up @@ -88,7 +89,7 @@ lazy val commonSettings = Seq(
),

// Coverage Exclusions
coverageExcludedPackages := "ffstest.*;tests.*;example.*;natchez.http4s.*",
coverageExcludedPackages := "ffstest.*;tests.*;example.*",

// uncomment in case of emergency
// scalacOptions ++= { if (scalaVersion.value.startsWith("3.")) Seq("-source:3.0-migration") else Nil },
Expand Down Expand Up @@ -116,7 +117,7 @@ lazy val core = crossProject(JVMPlatform, JSPlatform, NativePlatform)
"org.scodec" %%% "scodec-bits" % "1.1.37",
"org.scodec" %%% "scodec-core" % (if (tlIsScala3.value) "2.2.1" else "1.11.10"),
"org.scodec" %%% "scodec-cats" % "1.2.0",
"org.tpolecat" %%% "natchez-core" % natchezVersion,
"org.typelevel" %%% "otel4s-core-trace" % otel4sVersion,
"org.tpolecat" %%% "sourcepos" % "1.1.0",
"org.typelevel" %%% "twiddles-core" % "0.6.0",
) ++ Seq(
Expand Down Expand Up @@ -201,9 +202,12 @@ lazy val example = project
.settings(commonSettings)
.settings(
libraryDependencies ++= Seq(
"org.tpolecat" %%% "natchez-honeycomb" % natchezVersion,
"org.tpolecat" %%% "natchez-jaeger" % natchezVersion,
)
"org.typelevel" %% "otel4s-java" % otel4sVersion,
"io.opentelemetry" % "opentelemetry-exporter-otlp" % openTelemetryVersion % Runtime,
"io.opentelemetry" % "opentelemetry-sdk-extension-autoconfigure" % s"${openTelemetryVersion}-alpha" % Runtime,
),
run / fork := true,
javaOptions += "-Dotel.java.global-autoconfigure.enabled=true"
// ) ++ Seq(
// "org.http4s" %%% "http4s-dsl" % "0.21.22",
// "org.http4s" %%% "http4s-blaze-server" % "0.21.22",
Expand All @@ -229,9 +233,6 @@ lazy val docs = project
.settings(commonSettings)
.settings(
mdocIn := (Compile / sourceDirectory).value / "laika",
libraryDependencies ++= Seq(
"org.tpolecat" %%% "natchez-jaeger" % natchezVersion,
),
laikaConfig := {
import laika.rewrite.link._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package skunk.net.protocol

import cats.MonadThrow
import cats.syntax.all._
import natchez.Trace
import org.typelevel.otel4s.trace.Tracer
import skunk.net.MessageSocket
import skunk.net.message._
import skunk.exception.{
Expand All @@ -16,12 +16,12 @@ import skunk.exception.{

private[protocol] trait StartupCompanionPlatform { this: Startup.type =>

private[protocol] def authenticationSASL[F[_]: MonadThrow: MessageSocket: Trace](
private[protocol] def authenticationSASL[F[_]: MonadThrow: MessageSocket: Tracer](
sm: StartupMessage,
password: Option[String],
mechanisms: List[String]
): F[Unit] =
Trace[F].span("authenticationSASL") {
Tracer[F].span("authenticationSASL").surround {
if (mechanisms.contains(Scram.SaslMechanism)) {
for {
pw <- requirePassword[F](sm, password)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.ongres.scram.common.stringprep.StringPreparations

import cats.MonadError
import cats.syntax.all._
import natchez.Trace
import org.typelevel.otel4s.trace.Tracer
import scala.util.control.NonFatal
import skunk.net.MessageSocket
import skunk.net.message._
Expand All @@ -20,14 +20,14 @@ import skunk.exception.{

private[protocol] trait StartupCompanionPlatform { this: Startup.type =>

private[protocol] def authenticationSASL[F[_]: MessageSocket: Trace](
private[protocol] def authenticationSASL[F[_]: MessageSocket: Tracer](
sm: StartupMessage,
password: Option[String],
mechanisms: List[String]
)(
implicit ev: MonadError[F, Throwable]
): F[Unit] =
Trace[F].span("authenticationSASL") {
Tracer[F].span("authenticationSASL").surround {
for {
client <- {
try ScramClient.
Expand Down
28 changes: 14 additions & 14 deletions modules/core/shared/src/main/scala/Session.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import fs2.concurrent.Signal
import fs2.io.net.{ Network, SocketGroup, SocketOption }
import fs2.Pipe
import fs2.Stream
import natchez.Trace
import org.typelevel.otel4s.trace.Tracer
import skunk.codec.all.bool
import skunk.data._
import skunk.net.Protocol
Expand Down Expand Up @@ -401,7 +401,7 @@ object Session {
* @param queryCache Size of the cache for query checking
* @group Constructors
*/
def pooled[F[_]: Temporal: Trace: Network: Console](
def pooled[F[_]: Temporal: Tracer: Network: Console](
host: String,
port: Int = 5432,
user: String,
Expand All @@ -418,11 +418,11 @@ object Session {
parseCache: Int = 1024,
readTimeout: Duration = Duration.Inf,
): Resource[F, Resource[F, Session[F]]] = {
pooledF[F](host, port, user, database, password, max, debug, strategy, ssl, parameters, socketOptions, commandCache, queryCache, parseCache, readTimeout).map(_.apply(Trace[F]))
pooledF[F](host, port, user, database, password, max, debug, strategy, ssl, parameters, socketOptions, commandCache, queryCache, parseCache, readTimeout).map(_.apply(Tracer[F]))
}

/**
* Resource yielding a function from Trace to `SessionPool` managing up to `max` concurrent `Session`s. Typically you
* Resource yielding a function from Tracer to `SessionPool` managing up to `max` concurrent `Session`s. Typically you
* will `use` this resource once on application startup and pass the resulting
* `Resource[F, Session[F]]` to the rest of your program.
*
Expand Down Expand Up @@ -458,9 +458,9 @@ object Session {
queryCache: Int = 1024,
parseCache: Int = 1024,
readTimeout: Duration = Duration.Inf,
): Resource[F, Trace[F] => Resource[F, Session[F]]] = {
): Resource[F, Tracer[F] => Resource[F, Session[F]]] = {

def session(socketGroup: SocketGroup[F], sslOp: Option[SSLNegotiation.Options[F]], cache: Describe.Cache[F])(implicit T: Trace[F]): Resource[F, Session[F]] =
def session(socketGroup: SocketGroup[F], sslOp: Option[SSLNegotiation.Options[F]], cache: Describe.Cache[F])(implicit T: Tracer[F]): Resource[F, Session[F]] =
for {
pc <- Resource.eval(Parse.Cache.empty[F](parseCache))
s <- fromSocketGroup[F](socketGroup, host, port, user, database, password, debug, strategy, socketOptions, sslOp, parameters, cache, pc, readTimeout)
Expand All @@ -471,7 +471,7 @@ object Session {
for {
dc <- Resource.eval(Describe.Cache.empty[F](commandCache, queryCache))
sslOp <- ssl.toSSLNegotiationOptions(if (debug) logger.some else none)
pool <- Pool.ofF({implicit T: Trace[F] => session(Network[F], sslOp, dc)}, max)(Recyclers.full)
pool <- Pool.ofF({implicit T: Tracer[F] => session(Network[F], sslOp, dc)}, max)(Recyclers.full)
} yield pool
}

Expand All @@ -481,7 +481,7 @@ object Session {
* single-session pool. This method is shorthand for `Session.pooled(..., max = 1, ...).flatten`.
* @see pooled
*/
def single[F[_]: Temporal: Trace: Network: Console](
def single[F[_]: Temporal: Tracer: Network: Console](
host: String,
port: Int = 5432,
user: String,
Expand All @@ -496,10 +496,10 @@ object Session {
parseCache: Int = 1024,
readTimeout: Duration = Duration.Inf,
): Resource[F, Session[F]] =
singleF[F](host, port, user, database, password, debug, strategy, ssl, parameters, commandCache, queryCache, parseCache, readTimeout).apply(Trace[F])
singleF[F](host, port, user, database, password, debug, strategy, ssl, parameters, commandCache, queryCache, parseCache, readTimeout).apply(Tracer[F])

/**
* Resource yielding logically unpooled sessions given a Trace. This can be convenient for demonstrations and
* Resource yielding logically unpooled sessions given a Tracer. This can be convenient for demonstrations and
* programs that only need a single session. In reality each session is managed by its own
* single-session pool.
* @see pooledF
Expand All @@ -518,8 +518,8 @@ object Session {
queryCache: Int = 1024,
parseCache: Int = 1024,
readTimeout: Duration = Duration.Inf,
): Trace[F] => Resource[F, Session[F]] =
Kleisli((_: Trace[F]) => pooledF(
): Tracer[F] => Resource[F, Session[F]] =
Kleisli((_: Tracer[F]) => pooledF(
host = host,
port = port,
user = user,
Expand All @@ -535,10 +535,10 @@ object Session {
parseCache = parseCache,
readTimeout = readTimeout
)).flatMap(f =>
Kleisli { implicit T: Trace[F] => f(T) }
Kleisli { implicit T: Tracer[F] => f(T) }
).run

def fromSocketGroup[F[_]: Temporal: Trace: Console](
def fromSocketGroup[F[_]: Temporal: Tracer: Console](
socketGroup: SocketGroup[F],
host: String,
port: Int = 5432,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package skunk.exception

import cats.syntax.all._
import natchez.TraceValue
import org.typelevel.otel4s.Attribute
import skunk.SqlState
import skunk.data.Type
import skunk.util.Origin
Expand Down Expand Up @@ -33,26 +33,26 @@ class PostgresErrorException (
argumentsOrigin = argumentsOrigin,
) {

override def fields: Map[String, TraceValue] = {
var map = super.fields

map += "error.postgres.message" -> message
map += "error.postgres.severity" -> severity
map += "error.postgres.code" -> code

internalPosition.foreach(a => map += "error.postgres.internalPosition" -> a)
internalQuery .foreach(a => map += "error.postgres.internalQuery" -> a)
where .foreach(a => map += "error.postgres.where" -> a)
schemaName .foreach(a => map += "error.postgres.schemaName" -> a)
tableName .foreach(a => map += "error.postgres.tableName" -> a)
columnName .foreach(a => map += "error.postgres.columnName" -> a)
dataTypeName .foreach(a => map += "error.postgres.dataTypeName" -> a)
constraintName .foreach(a => map += "error.postgres.constraintName" -> a)
fileName .foreach(a => map += "error.postgres.fileName" -> a)
line .foreach(a => map += "error.postgres.line" -> a)
routine .foreach(a => map += "error.postgres.routine" -> a)

map
override def fields: List[Attribute[_]] = {
val builder = List.newBuilder[Attribute[_]]

builder += Attribute("error.postgres.message" , message)
builder += Attribute("error.postgres.severity" , severity)
builder += Attribute("error.postgres.code" , code)

internalPosition.foreach(a => builder += Attribute("error.postgres.internalPosition" , a.toLong))
internalQuery .foreach(a => builder += Attribute("error.postgres.internalQuery" , a.toLong))
where .foreach(a => builder += Attribute("error.postgres.where" , a))
schemaName .foreach(a => builder += Attribute("error.postgres.schemaName" , a))
tableName .foreach(a => builder += Attribute("error.postgres.tableName" , a))
columnName .foreach(a => builder += Attribute("error.postgres.columnName" , a))
dataTypeName .foreach(a => builder += Attribute("error.postgres.dataTypeName" , a))
constraintName .foreach(a => builder += Attribute("error.postgres.constraintName" , a))
fileName .foreach(a => builder += Attribute("error.postgres.fileName" , a))
line .foreach(a => builder += Attribute("error.postgres.line" , a.toLong))
routine .foreach(a => builder += Attribute("error.postgres.routine" , a))

builder.result()
}

/**
Expand Down
41 changes: 20 additions & 21 deletions modules/core/shared/src/main/scala/exception/SkunkException.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
package skunk.exception

import cats.syntax.all._
import org.typelevel.otel4s.Attribute
import skunk.data.Type
import skunk.Query
import skunk.util.{ CallSite, Origin, Pretty }
import natchez.Fields
import natchez.TraceValue

class SkunkException protected[skunk](
val sql: Option[String],
Expand All @@ -22,41 +21,41 @@ class SkunkException protected[skunk](
val sqlOrigin: Option[Origin] = None,
val argumentsOrigin: Option[Origin] = None,
val callSite: Option[CallSite] = None
) extends Exception(message) with Fields {
) extends Exception(message) {

override def fields: Map[String, TraceValue] = {
def fields: List[Attribute[_]] = {

var map: Map[String, TraceValue] = Map.empty
val builder = List.newBuilder[Attribute[_]]

map += "error.message" -> message
builder += Attribute("error.message", message)

sql .foreach(a => map += "error.sql" -> a)
position.foreach(a => map += "error.position" -> a)
detail .foreach(a => map += "error.detail" -> a)
hint .foreach(a => map += "error.hint" -> a)
sql .foreach(a => builder += Attribute("error.sql" , a))
position.foreach(a => builder += Attribute("error.position" , a.toLong))
detail .foreach(a => builder += Attribute("error.detail" , a))
hint .foreach(a => builder += Attribute("error.hint" , a))

(arguments.zipWithIndex).foreach { case ((typ, os), n) =>
map += s"error.argument.${n + 1}.type" -> typ.name
map += s"error.argument.${n + 1}.value" -> os.getOrElse[String]("NULL")
builder += Attribute(s"error.argument.${n + 1}.type" , typ.name)
builder += Attribute(s"error.argument.${n + 1}.value" , os.getOrElse[String]("NULL"))
}

sqlOrigin.foreach { o =>
map += "error.sqlOrigin.file" -> o.file
map += "error.sqlOrigin.line" -> o.line
builder += Attribute("error.sqlOrigin.file" , o.file)
builder += Attribute("error.sqlOrigin.line" , o.line.toLong)
}

argumentsOrigin.foreach { o =>
map += "error.argumentsOrigin.file" -> o.file
map += "error.argumentsOrigin.line" -> o.line
builder += Attribute("error.argumentsOrigin.file" , o.file)
builder += Attribute("error.argumentsOrigin.line" , o.line.toLong)
}

callSite.foreach { cs =>
map += "error.callSite.origin.file" -> cs.origin.file
map += "error.callSite.origin.line" -> cs.origin.line
map += "error.callSite.origin.method" -> cs.methodName
builder += Attribute("error.callSite.origin.file" , cs.origin.file)
builder += Attribute("error.callSite.origin.line" , cs.origin.line.toLong)
builder += Attribute("error.callSite.origin.method" , cs.methodName)
}

map
builder.result()
}

protected def title: String =
Expand Down Expand Up @@ -148,4 +147,4 @@ object SkunkException {
argumentsOrigin = argsOrigin
)

}
}
6 changes: 3 additions & 3 deletions modules/core/shared/src/main/scala/net/Protocol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import skunk.{ Command, Query, Statement, ~, Void }
import skunk.data._
import skunk.util.{ Namer, Origin }
import skunk.util.Typer
import natchez.Trace
import org.typelevel.otel4s.trace.Tracer
import fs2.io.net.{ SocketGroup, SocketOption }
import skunk.net.protocol.Describe
import scala.concurrent.duration.Duration
Expand Down Expand Up @@ -196,7 +196,7 @@ object Protocol {
* @param host Postgres server host
* @param port Postgres port, default 5432
*/
def apply[F[_]: Temporal: Trace: Console](
def apply[F[_]: Temporal: Tracer: Console](
host: String,
port: Int,
debug: Boolean,
Expand All @@ -213,7 +213,7 @@ object Protocol {
p <- Resource.eval(fromMessageSocket(bms, nam, describeCache, parseCache))
} yield p

def fromMessageSocket[F[_]: Concurrent: Trace](
def fromMessageSocket[F[_]: Concurrent: Tracer](
bms: BufferedMessageSocket[F],
nam: Namer[F],
dc: Describe.Cache[F],
Expand Down
Loading

0 comments on commit 2a9b469

Please sign in to comment.